导入 WordPress 数据库 - #1075 - table 定义不正确;只能有一个自动列,并且必须将其定义为键

Importing WordPress Database - #1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key

我正在尝试使用 phpMyAdmin 将 WordPress 数据库从 Plesk 移动到 cPanel,但在导入时出现以下错误:

SQL query:

Table structure for table `wp_commentmeta`

CREATE TABLE IF NOT EXISTS  `wp_commentmeta` (

 `meta_id` BIGINT( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT ,
 `comment_id` BIGINT( 20 ) UNSIGNED NOT NULL DEFAULT  '0',
 `meta_key` VARCHAR( 255 ) DEFAULT NULL ,
 `meta_value` LONGTEXT
) ENGINE = MYISAM AUTO_INCREMENT =236 DEFAULT CHARSET = utf8;

MySQL said: Documentation

#1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key

我像往常一样使用快速选项导出数据库,然后进行正常导入。

sql 导出的相关部分是:

--
-- Table structure for table `wp_commentmeta`
--

CREATE TABLE IF NOT EXISTS `wp_commentmeta` (
  `meta_id` bigint(20) unsigned NOT NULL auto_increment,
  `comment_id` bigint(20) unsigned NOT NULL default '0',
  `meta_key` varchar(255) default NULL,
  `meta_value` longtext
) ENGINE=MyISAM AUTO_INCREMENT=236 DEFAULT CHARSET=utf8;

所以我尝试了 Google

中提到的解决方案
CREATE TABLE IF NOT EXISTS `wp_commentmeta` (
  `meta_id` bigint(20) unsigned NOT NULL PRIMARY KEY auto_increment,
  `comment_id` bigint(20) unsigned NOT NULL default '0',
  `meta_key` varchar(255) default NULL,
  `meta_value` longtext
) ENGINE=MyISAM AUTO_INCREMENT=236 DEFAULT CHARSET=utf8;

这次我得到了这个错误:

SQL query:

CREATE TABLE IF NOT EXISTS  `wp_comments` (

 `comment_ID` BIGINT( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT ,
 `comment_post_ID` BIGINT( 20 ) UNSIGNED NOT NULL DEFAULT  '0',
 `comment_author` TINYTEXT NOT NULL ,
 `comment_author_email` VARCHAR( 100 ) NOT NULL DEFAULT  '',
 `comment_author_url` VARCHAR( 200 ) NOT NULL DEFAULT  '',
 `comment_author_IP` VARCHAR( 100 ) NOT NULL DEFAULT  '',
 `comment_date` DATETIME NOT NULL DEFAULT  '0000-00-00 00:00:00',
 `comment_date_gmt` DATETIME NOT NULL DEFAULT  '0000-00-00 00:00:00',
 `comment_content` TEXT NOT NULL ,
 `comment_karma` INT( 11 ) NOT NULL DEFAULT  '0',
 `comment_approved` VARCHAR( 20 ) NOT NULL DEFAULT  '1',
 `comment_agent` VARCHAR( 255 ) NOT NULL DEFAULT  '',
 `comment_type` VARCHAR( 20 ) NOT NULL DEFAULT  '',
 `comment_parent` BIGINT( 20 ) UNSIGNED NOT NULL DEFAULT  '0',
 `user_id` BIGINT( 20 ) UNSIGNED NOT NULL DEFAULT  '0'
) ENGINE = MYISAM AUTO_INCREMENT =226 DEFAULT CHARSET = utf8;

MySQL said: Documentation

#1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key 

wp_comments 的 CREATE 部分如下。

DROP TABLE IF EXISTS `wp_comments`;
CREATE TABLE IF NOT EXISTS `wp_comments` (
  `comment_ID` bigint(20) unsigned NOT NULL auto_increment,
  `comment_post_ID` bigint(20) unsigned NOT NULL default '0',
  `comment_author` tinytext NOT NULL,
  `comment_author_email` varchar(100) NOT NULL default '',
  `comment_author_url` varchar(200) NOT NULL default '',
  `comment_author_IP` varchar(100) NOT NULL default '',
  `comment_date` datetime NOT NULL default '0000-00-00 00:00:00',
  `comment_date_gmt` datetime NOT NULL default '0000-00-00 00:00:00',
  `comment_content` text NOT NULL,
  `comment_karma` int(11) NOT NULL default '0',
  `comment_approved` varchar(20) NOT NULL default '1',
  `comment_agent` varchar(255) NOT NULL default '',
  `comment_type` varchar(20) NOT NULL default '',
  `comment_parent` bigint(20) unsigned NOT NULL default '0',
  `user_id` bigint(20) unsigned NOT NULL default '0'
) ENGINE=MyISAM AUTO_INCREMENT=226 DEFAULT CHARSET=utf8;

转储底部是以下 auto_increment 信息。

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `wp_commentmeta`
--
ALTER TABLE `wp_commentmeta`
AUTO_INCREMENT=236;
--
-- AUTO_INCREMENT for table `wp_comments`
--
ALTER TABLE `wp_comments`
AUTO_INCREMENT=226;
--
-- AUTO_INCREMENT for table `wp_event_list`
--
ALTER TABLE `wp_event_list`
AUTO_INCREMENT=9;
--
-- AUTO_INCREMENT for table `wp_layerslider`
--
ALTER TABLE `wp_layerslider`
AUTO_INCREMENT=6;
--
-- AUTO_INCREMENT for table `wp_options`
--
ALTER TABLE `wp_options`
AUTO_INCREMENT=497473;
--
-- AUTO_INCREMENT for table `wp_postmeta`
--
ALTER TABLE `wp_postmeta`
AUTO_INCREMENT=18312;
--
-- AUTO_INCREMENT for table `wp_posts`
--
ALTER TABLE `wp_posts`
AUTO_INCREMENT=2083;
--
-- AUTO_INCREMENT for table `wp_terms`
--
ALTER TABLE `wp_terms`
AUTO_INCREMENT=136;
--
-- AUTO_INCREMENT for table `wp_term_taxonomy`
--
ALTER TABLE `wp_term_taxonomy`
AUTO_INCREMENT=137;
--
-- AUTO_INCREMENT for table `wp_usermeta`
--
ALTER TABLE `wp_usermeta`
AUTO_INCREMENT=1527;
--
-- AUTO_INCREMENT for table `wp_users`
--
ALTER TABLE `wp_users`
AUTO_INCREMENT=43;
--
-- AUTO_INCREMENT for table `wp_woocommerce_attribute_taxonomies`
--
ALTER TABLE `wp_woocommerce_attribute_taxonomies`
AUTO_INCREMENT=5;
--
-- AUTO_INCREMENT for table `wp_woocommerce_order_itemmeta`
--
ALTER TABLE `wp_woocommerce_order_itemmeta`
AUTO_INCREMENT=1869;
--
-- AUTO_INCREMENT for table `wp_woocommerce_order_items`
--
ALTER TABLE `wp_woocommerce_order_items`
AUTO_INCREMENT=294;
--
-- AUTO_INCREMENT for table `wp_woocommerce_tax_rates`
--
ALTER TABLE `wp_woocommerce_tax_rates`
AUTO_INCREMENT=4;
--
-- AUTO_INCREMENT for table `wp_woocommerce_termmeta`
--
ALTER TABLE `wp_woocommerce_termmeta`
AUTO_INCREMENT=116;

这就是我真正陷入困境的地方,因为我已经迅速而突然地达到了我的知识极限并且不想让事情变得更糟。我习惯于在 create 中的 alter table 部分中查看信息,但不知道 id 我应该将其复制到 create 部分还是什么。

有人可以提供一些关于为什么会发生这种情况的提示。

谢谢。

可能您正在使用两个不同版本的 phpmyadmin,一个在 plesk 中,另一个在您的 cpanel 系统中?

您可以尝试 'Adminer',这是一个强大的 phpmyadmin 替代方案,它仅基于一个文件!

从这里下载:http://www.adminer.org/en/

将 adminer.php 复制到要从中导出数据的服务器和要导入 sql 数据的服务器。

转到您的 website/adminer.php 并使用您拥有的凭据登录到您的数据库。导出和导入类似于 phpmyadmin,但优点是您使用的是一种通用版本的管理软件,可确保导入和导出 运行 正常。

对于每个 Wordpress table,以这种方式添加其密钥(参见倒数第二行):

CREATE TABLE IF NOT EXISTS `wp_commentmeta` (
  `meta_id` bigint(20) unsigned NOT NULL auto_increment,
  `comment_id` bigint(20) unsigned NOT NULL default '0',
  `meta_key` varchar(255) default NULL,
  `meta_value` longtext,
  key (meta_id) -- add this line (remember to add the comma in the previous line)
) ENGINE=MyISAM AUTO_INCREMENT=236 DEFAULT CHARSET=utf8;

WordPress tables:

wp_commentmeta
wp_comments
wp_links
wp_options
wp_postmeta
wp_posts
wp_terms
wp_term_relationships
wp_term_taxonomy
wp_usermeta
wp_users

这个问题由 phpMyAdmin (PMA) 和 "fixed" 记录,主要是说您不能将当前版本与 MySQL 5.0 一起使用。

Table export with auto_increment, primary key creates invalid statements > Problems due to missing enforcement of the minimum supported MySQL version

发现我的服务器是 运行 PMA 4.3 和 MYSQL 5.0.95,而我的本地服务器是 MYSQL 5.5。我不知道为什么现在这是个问题,因为旧的 PMA 会 import/export 像 mysqldump 一样漂亮,我猜他们出于性能原因更改并简化了语法,这是合法的。

如果您像我一样,您从 MySQL 5.5(托管服务器)导出了 tables 并尝试导入到 MySQL 5.6(XAMPP Mac) 然后你得到了可怕的 1075 错误。在网上查了好几个小时才发现跟自增和主键有关。不是数据库程序员,此信息(由上面的 liquified 在链接中提供)无助于解决问题,因为您基本上被告知:"Hey, don't do that"。嗯,先生。 PMA 错误,已经解决了,那么我该如何解决?

以下是对我有用的方法:

您导出的 SQL 在底部附近有一堆语句 "ALTER" 您在顶部创建的所有 table。您需要做的就是复制到上面的 CREATE 语句中。

因此,在底部,您的 ALTER wp_commentmeta 看起来像这样:

ALTER TABLE `wp_commentmeta`
  ADD PRIMARY KEY  (`meta_id`),
  ADD KEY `comment_id` (`comment_id`),
  ADD KEY `meta_key` (`meta_key`);

在顶部,CREATE 看起来像这样:

CREATE TABLE IF NOT EXISTS `wp_commentmeta` (
  `meta_id` bigint(20) unsigned NOT NULL auto_increment,
  `comment_id` bigint(20) unsigned NOT NULL default '0',
  `meta_key` varchar(255) default NULL,
  `meta_value` longtext
) TYPE=MyISAM AUTO_INCREMENT=67;

解决方案是删除底部的 ALTER 并将这些语句放入 CREATE,如下所示(在 'longtext' 后添加逗号):

CREATE TABLE IF NOT EXISTS `wp_commentmeta` (
  `meta_id` bigint(20) unsigned NOT NULL auto_increment,
  `comment_id` bigint(20) unsigned NOT NULL default '0',
  `meta_key` varchar(255) default NULL,
  `meta_value` longtext,
PRIMARY KEY (`meta_id`),
  KEY `comment_id` (`comment_id`),
  KEY `meta_key` (`meta_key`)
) TYPE=MyISAM AUTO_INCREMENT=67;

现在,如果您使用它,您将因语法错误而收到 1064 错误。一个人可以休息一下吗?您仍然需要为这个新版本更改 MyISAM 内容:

TYPE=MyISAM AUTO_INCREMENT=67;

改为

ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=67;

最后,您的最终 CREATE 声明将如下所示,并且您不需要在 SQL:

底部添加任何 ALTER table 语句
CREATE TABLE IF NOT EXISTS `wp_commentmeta` (
  `meta_id` bigint(20) unsigned NOT NULL auto_increment,
  `comment_id` bigint(20) unsigned NOT NULL default '0',
  `meta_key` varchar(255) default NULL,
  `meta_value` longtext,
  PRIMARY KEY (`meta_id`),
  KEY `comment_id` (`comment_id`),
  KEY `meta_key` (`meta_key`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=50 ;

是的,如果您打算将其导入新数据库,则必须手动编辑 SQL。如果你有很多 tables and/or 网站受此 'bug' 影响,这将需要一些时间,所以喝杯咖啡,不管有用什么,修复它并继续你的生活.

现在,如果您仍然遇到错误,请检查您的语法,确保在从 ALTER table 复制时删除 'ADD'。去掉 ';'并正确使用逗号。如果您设法导入了部分数据库,一些 tables,但在语法上遇到困难,转储所有 tables 并在完成修复后再次尝试导入。我遇到了 1062:重复的主键,因为我设法导入了一些 table,而其他的失败了。当我再次尝试导入时,已经为 table 设置了主键。

所有这些令人头疼的事情都是因为新 PMA/MySQL 中的性能 'enhancements'。骗子!

我从另一个 phpMyAdmin 导出时遇到同样的问题,文件 mysql 导出不包含主键,然后在导出时我选择了方法 "Custom - display all possible options",然后我检查了"IF NOT EXISTS (less efficient as indexes will be generated during table creation)"。然后导出的文件在文件中包含主键。我的问题解决了。我希望这对你有帮助。