Error database import from localhost to server - mysql

I want to upload database from localhost to server and I get 1067 error.
Localhost Mysql version - 5.7
Server - MariaDB, v5.5.68
Code part
CREATE TABLE `wp_momopay_payments` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`order_id` varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT '',
`amount` int(10) unsigned DEFAULT '0',
`phone_number` varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT '',
`created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`payment_id` varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT '',
`failed_reason` varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT '',
`salt` varchar(128) COLLATE utf8mb4_unicode_ci DEFAULT '',
`status` varchar(36) COLLATE utf8mb4_unicode_ci DEFAULT 'pending',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Error message
ERROR 1067 (42000) at line 1721: Invalid default value for 'created'
program 'mysql' finished with non-zero exit code: 1

Mariadb 5.5 only allows TIMESTAMP, not DATETIME, columns to have the CURRENT_TIMESTAMP default value. So your table definition uses a feature it lacks.
Mariadb 5.5 hit end-of-life on 11-April-2020, well over two years ago.

Related

Why is sql not letting me create this table?

I'm trying to migrate a db
from: MySQL Distrib 5.5.60-MariaDB, for Linux (x86_64)
to: MySQL 5.5.4, UNIX
I tried importing the db as a zip package and it started throwing errors so now I'm trying to re-create each table one at a time on phpMyAdmin.
The query below is throwing a #1064 Syntax error, and I'm having trouble figuring out the issue.
MySQL Said:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(2) NOT NULL DEFAULT CURRENT_TIMESTAMP(2) ON UPDATE CURRENT_TIMESTAMP(2),
`st' at line 6
I'm looking at line 6, trying to find any reserved words, missing data, typos, and or obsolete commands but no luck.
CREATE TABLE `tblmoto_auth_policies` (
`policy_id` int(11) NOT NULL AUTO_INCREMENT,
`policy_name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`policy_desc` text COLLATE utf8_unicode_ci NOT NULL,
`policy_url` text COLLATE utf8_unicode_ci NOT NULL,
`date_added` timestamp(2) NOT NULL DEFAULT CURRENT_TIMESTAMP(2) ON UPDATE CURRENT_TIMESTAMP,
`status` smallint(2) NOT NULL DEFAULT '1',
PRIMARY KEY (`policy_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
I could use some help.
Thanks in advance.
CURRENT_TIMESTAMP is not good
Try this:
CREATE TABLE `tblmoto_auth_policies` (
`policy_id` int(11) NOT NULL AUTO_INCREMENT,
`policy_name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`policy_desc` text COLLATE utf8_unicode_ci NOT NULL,
`policy_url` text COLLATE utf8_unicode_ci NOT NULL,
`date_added` timestamp(2) NOT NULL DEFAULT CURRENT_TIMESTAMP(2) ON UPDATE CURRENT_TIMESTAMP(2),
`status` smallint(2) NOT NULL DEFAULT '1',
PRIMARY KEY (`policy_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
This works in SQL Fiddle:
CREATE TABLE `tblmoto_auth_policies` (
`policy_id` int(11) NOT NULL AUTO_INCREMENT,
`policy_name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`policy_desc` text COLLATE utf8_unicode_ci NOT NULL,
`policy_url` text COLLATE utf8_unicode_ci NOT NULL,
`date_added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`status` smallint(2) NOT NULL DEFAULT '1',
PRIMARY KEY (`policy_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
i.e., remove the precision (the (2)) from the definition of the date_added column.
TIMESTAMP(2) is valid syntax, but not in combination with the DEFAULT CURRENT_TIMESTAMP nor ON UPDATE CURRENT_TIMESTAMP auto-initializers.

ERROR 1067 (42000) Invalid default value '0000-00-00 00:00:00'

Importing an SQL file via SSH on Ubuntu server using SQL Ver 14.14 Distrib 5.7.20, for Linux (x86_64) using EditLine wrapper.
The line that the error points to is this line:
CREATE TABLE `wp_users` (
The line that has user_registered is this line:
`user_registered` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
Not sure what is wrong.
The whole CREATE TABLE command as requested:
CREATE TABLE `wp_users` (
`ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user_login` varchar(60) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_pass` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_nicename` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_email` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_url` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_registered` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`user_activation_key` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_status` int(11) NOT NULL DEFAULT '0',
`display_name` varchar(250) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
PRIMARY KEY (`ID`),
KEY `user_login_key` (`user_login`),
KEY `user_nicename` (`user_nicename`),
KEY `user_email` (`user_email`)
) ENGINE=MyISAM AUTO_INCREMENT=53 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ;
I guess you're using MySQL 5.7 (or higher). There the default value for SQL mode is among others NO_ZERO_IN_DATE,NO_ZERO_DATE.
You can check this with
SHOW GLOBAL VARIABLES LIKE 'sql_mode';
/* or...*/
SHOW SESSION VARIABLES LIKE 'sql_mode';
Either you remove above mentioned values from the sql mode, or you just make your column nullable and store NULL instead of 0000-00-00 00:00:00. I'd prefer the latter. It doesn't waste storage and is clear in its meaning.

Error when importing to server

SQL query:
CREATE TABLE `wp_layerslider` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`author` int(10) NOT NULL DEFAULT '0',
`name` varchar(100) COLLATE utf8mb4_unicode_520_ci DEFAULT '',
`slug` varchar(100) COLLATE utf8mb4_unicode_520_ci DEFAULT '',
`data` mediumtext COLLATE utf8mb4_unicode_520_ci NOT NULL,
`date_c` int(10) NOT NULL,
`date_m` int(10) NOT NULL,
`schedule_start` int(10) NOT NULL DEFAULT '0',
`schedule_end` int(10) NOT NULL DEFAULT '0',
`flag_hidden` tinyint(1) NOT NULL DEFAULT '0',
`flag_deleted` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
MySQL said: Documentation
1273 - Unknown collation: 'utf8mb4_unicode_520_ci'
When attempting to import a database. I'm very new to all of this, hoping someone can assist in simple terms for me
could be you are using an oldeversion of mysql db .. and the collate you need is not avialable .. you could use try alter the table or change the collate
ALTER TABLE `wp_layerslider` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci

What could be wrong here

DROP TABLE IF EXISTS 'ci_sessions';
CREATE TABLE 'ci_sessions' (
'session_id' varchar(40) COLLATE utf8_bin NOT NULL DEFAULT '0',
'ip_address' varchar(16) COLLATE utf8_bin NOT NULL DEFAULT '0',
'user_agent' varchar(120) COLLATE utf8_bin DEFAULT NULL,
'last_activity' int(10) unsigned NOT NULL DEFAULT '0',
'user_data' text COLLATE utf8_bin NOT NULL, PRIMARY KEY ('session_id'),
KEY 'last_activity_idx' ('last_activity') )
ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
I've tried to run this code on phpMyAdmin and it says
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near ''ci_sessions'' at line 1
It's the same error in all your table and column names. You use ' while you should use backticks:
`
So this should work:
DROP TABLE IF EXISTS `ci_sessions`;
CREATE TABLE `ci_sessions` (
`session_id` varchar(40) COLLATE utf8_bin NOT NULL DEFAULT '0',
`ip_address` varchar(16) COLLATE utf8_bin NOT NULL DEFAULT '0',
`user_agent` varchar(120) COLLATE utf8_bin DEFAULT NULL,
`last_activity` int(10) unsigned NOT NULL DEFAULT '0',
`user_data` text COLLATE utf8_bin NOT NULL, PRIMARY KEY (`session_id`),
KEY `last_activity_idx` (`last_activity`) )
ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
As a sidenote - you use NOT NULL and then set the default value to the string 0. quite often. That indicates bad practice, or at least more work for later queries. A column named last_activity for example indicates that null values should be allowed...
Second sidenote - last_activity would typically be a DATETIME column, not an int(10)

Zero timestamp value not accepted when using MySQL .NET connector

We have a C# tool to create a (MySQL) schema on a developer machine based on the (MySQL) schema used in production. This tool runs SHOW CREATE TABLE queries on the source connection, grab the resulting table creation scripts and run them locally.
I'm using MySQL 5.7.7 Community Server locally.
One of the creation script looks like this:
CREATE TABLE `agency` (
`id` int(11) unsigned NOT NULL,
`name` varchar(512) COLLATE utf8_unicode_ci NOT NULL,
`url` varchar(512) CHARACTER SET ascii NOT NULL,
`location_path` varchar(10) CHARACTER SET ascii NOT NULL,
`email_pattern` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`generic_email` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
`address` text COLLATE utf8_unicode_ci,
`zip_code` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`city` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`region` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`country` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`phone` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`inserted_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
When I run this query in MySQL Workbench, the table is successfully created.
However, when this query is executed on the destination connection by our C# tool, the following error occurs: Failure: Invalid default value for 'inserted_at'.
My MySQL Workbench is configured with the following SQL_MODE: TRADITIONAL,ALLOW_INVALID_DATES.
My MySQL Server instance is configured with the following sql-mode: STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Here are the connection strings used by our C# tool:
Local connection string:
Server=localhost;Database=XXX;Uid=XXX;Pwd=XXX;AllowZeroDateTime=true
Source connection string:
Server=X.X.X.X;Database=XXX;Uid=XXX;Pwd=XXX;Use Compression=true;AllowZeroDateTime=true
Any idea appreciated!
It looks like removing STRICT_TRANS_TABLES from sql-mode in my.ini fixes the problem.
Is this the correct fix?