WordPress数据库丢失自动增量

时间:2016-05-04 作者:cw29

将Wordpress数据库回滚到备份版本后,似乎所有表都丢失了主键列上的auto\\u增量。我在另一篇文章中读到,这可能与InnoDB在内存中存储auto\\u增量值有关。我以前回滚并迁移过数据库,但没有出现此类问题。以前有人遇到过类似的问题吗?提前感谢您的帮助。

5 个回复
SO网友:Tim

我有一个类似的问题,我解决了它,因为这在谷歌上很受欢迎,因为我正在寻找它,它可能会帮助其他人。

我使用数据库迁移服务将几个Wordpress数据库从AWS RDS MySQL迁移到EC2实例上运行的MySQL。我不知道的是,它不会复制索引、键、自动增量,或者其他任何基本内容。当然,最好的方法是使用mysqldump转储数据库并手动导入,但一次Wordpress安装有重大更改,我不想重做。相反,我手动重新创建了auto\\u增量值和索引。

我已经记录了fixed Wordpress auto increment here on my website, 这是一份对我有用的东西。我可能会做进一步的更改,我会更新网站,但我可能不记得更新这个问题。

ALTER TABLE wp_termmeta MODIFY COLUMN meta_id bigint(20) unsigned NOT NULL auto_increment;
ALTER TABLE wp_terms MODIFY COLUMN term_id bigint(20) unsigned NOT NULL auto_increment;
ALTER TABLE wp_term_taxonomy MODIFY COLUMN term_taxonomy_id bigint(20) unsigned NOT NULL auto_increment;
ALTER TABLE wp_commentmeta MODIFY COLUMN meta_id bigint(20) unsigned NOT NULL auto_increment;
ALTER TABLE wp_comments MODIFY COLUMN comment_ID bigint(20) unsigned NOT NULL auto_increment;
ALTER TABLE wp_links MODIFY COLUMN link_id bigint(20) unsigned NOT NULL auto_increment;
ALTER TABLE wp_options MODIFY COLUMN option_id bigint(20) unsigned NOT NULL auto_increment;
ALTER TABLE wp_postmeta MODIFY COLUMN meta_id bigint(20) unsigned NOT NULL auto_increment;
ALTER TABLE wp_users MODIFY COLUMN ID bigint(20) unsigned NOT NULL auto_increment;
ALTER TABLE wp_posts MODIFY COLUMN ID bigint(20) unsigned NOT NULL auto_increment;
ALTER TABLE wp_usermeta MODIFY COLUMN umeta_id bigint(20) unsigned NOT NULL auto_increment;

CREATE INDEX term_id on wp_termmeta (term_id);
CREATE INDEX meta_key on wp_termmeta (meta_key(191));
CREATE INDEX slug on wp_terms (slug(191));
CREATE INDEX name on wp_terms (name(191));
CREATE UNIQUE INDEX term_id_taxonomy on wp_term_taxonomy (term_id, taxonomy);
CREATE INDEX taxonomy on wp_term_taxonomy (taxonomy );
CREATE INDEX comment_id on wp_commentmeta (comment_id);
CREATE INDEX meta_key on wp_commentmeta (meta_key(191));
CREATE INDEX comment_post_ID on wp_comments (comment_post_ID);
CREATE INDEX comment_approved_date_gmt on wp_comments (comment_approved,comment_date_gmt);
CREATE INDEX comment_date_gmt on wp_comments (comment_date_gmt);
CREATE INDEX comment_parent on wp_comments (comment_parent);
CREATE INDEX comment_author_email on wp_comments (comment_author_email(10));
CREATE INDEX link_visible on wp_links (link_visible);
CREATE UNIQUE INDEX option_name on wp_options (option_name);
CREATE INDEX post_id on wp_postmeta (post_id);
CREATE INDEX meta_key on wp_postmeta (meta_key);
CREATE INDEX post_name on wp_posts (post_name(191));
CREATE INDEX type_status_date on wp_posts (post_type,post_status,post_date,ID);
CREATE INDEX post_parent on wp_posts (post_parent);
CREATE INDEX post_author on wp_posts (post_author);
CREATE INDEX user_login_key on wp_users (user_login);
CREATE INDEX user_nicename on wp_users (user_nicename);
CREATE INDEX user_email on wp_users (user_email);
CREATE INDEX user_id on wp_usermeta (user_id);
CREATE INDEX meta_key on wp_usermeta (meta_key(191));

ALTER TABLE wp_terms AUTO_INCREMENT = 10000;
ALTER TABLE wp_term_taxonomy AUTO_INCREMENT = 10000;
ALTER TABLE wp_commentmeta AUTO_INCREMENT = 10000;
ALTER TABLE wp_comments AUTO_INCREMENT = 10000;
ALTER TABLE wp_links AUTO_INCREMENT = 10000;
ALTER TABLE wp_options AUTO_INCREMENT = 10000;
ALTER TABLE wp_postmeta AUTO_INCREMENT = 10000;
ALTER TABLE wp_users AUTO_INCREMENT = 10000;
ALTER TABLE wp_posts AUTO_INCREMENT = 10000;
ALTER TABLE wp_usermeta AUTO_INCREMENT = 10000;
注释

您应该检查表格,并确保将auto\\u increment设置为对该表格有意义的值

SO网友:Ben Erwin

为什么会这样?以下是我的问题:

如果使用phpadmin导出数据库并在重新导入时出错,则添加主键的代码不会运行,因为它位于SQL文件的末尾,而不是创建时。

在我弄明白这一点之前,我更新了phpmyadmin 5 beta,它导入了带有密钥的文件,尽管我仍然有错误。

第一课是,不要让您的导入崩溃,即使您的表在那里。我的崩溃在以wp\\u w开头的表上,所以它出现在用户和我的自动增量之后。

如果查看SQL导出的底部,您将找到用于添加主键和自动递增的alter表。

You don\'t need to specify the auto increment 它自动知道下一个增量是什么样的:

ALTER TABLE wp_posts CHANGE ID ID  BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT;
如果发生这种情况后您有管理活动,那么您的键字段中有零,这将不允许您设置主键,如果没有主键,则无法自动递增。因此,您需要对每个表运行删除脚本,如下所示:

DELETE FROM wp_posts  WHERE ID=0;
这是一组完整的更新如果表中有这些更新,它将抛出并出错。

DELETE FROM wp_termmeta  WHERE meta_id=0;
DELETE FROM wp_terms  WHERE term_id=0;
DELETE FROM wp_term_taxonomy  WHERE term_taxonomy_id=0;
DELETE FROM wp_commentmeta  WHERE meta_id=0;
DELETE FROM wp_comments  WHERE comment_ID=0;
DELETE FROM wp_links  WHERE link_id=0;
DELETE FROM wp_options  WHERE option_id=0;
DELETE FROM wp_postmeta  WHERE meta_id=0;
DELETE FROM wp_users  WHERE ID=0;
DELETE FROM wp_posts  WHERE ID=0;
DELETE FROM wp_usermeta  WHERE umeta_id=0;

ALTER TABLE  wp_termmeta ADD PRIMARY KEY(meta_id);
ALTER TABLE  wp_terms ADD PRIMARY KEY(term_id);
ALTER TABLE  wp_term_taxonomy ADD PRIMARY KEY(term_taxonomy_id);
ALTER TABLE  wp_commentmeta ADD PRIMARY KEY(meta_id);
ALTER TABLE  wp_comments ADD PRIMARY KEY(comment_ID);
ALTER TABLE  wp_links ADD PRIMARY KEY(link_id);
ALTER TABLE  wp_options ADD PRIMARY KEY(option_id);
ALTER TABLE  wp_postmeta ADD PRIMARY KEY(meta_id);
ALTER TABLE  wp_users ADD PRIMARY KEY(ID);
ALTER TABLE  wp_posts ADD PRIMARY KEY(ID);
ALTER TABLE  wp_usermeta ADD PRIMARY KEY(umeta_id);

ALTER TABLE wp_termmeta CHANGE meta_id meta_id  BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT;
ALTER TABLE wp_terms CHANGE term_id term_id  BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT;
ALTER TABLE wp_term_taxonomy CHANGE term_taxonomy_id term_taxonomy_id  BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT;
ALTER TABLE wp_commentmeta CHANGE meta_id meta_id  BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT;
ALTER TABLE wp_comments CHANGE comment_ID comment_ID  BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT;
ALTER TABLE wp_links CHANGE link_id link_id  BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT;
ALTER TABLE wp_options CHANGE option_id option_id  BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT;
ALTER TABLE wp_postmeta CHANGE meta_id meta_id  BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT;
ALTER TABLE wp_users CHANGE ID ID  BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT;
ALTER TABLE wp_posts CHANGE ID ID  BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT;
ALTER TABLE wp_usermeta CHANGE umeta_id umeta_id  BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT;

SO网友:N00b

为什么会这样?很难确定,因为有很多变量需要考虑:导出或导入时出错,MySQL版本等等。

这是一个非常具体的MySQL数据库问题,与WordPress本身没有太大关系。获取问题的特定非推测性答案why, 我建议你进去问问SODBA 提供了大量有关备份过程的详细信息。

<小时>Solution: ALTER TABLE table_name AUTO_INCREMENT = increment_number

此设置AUTO_INCREMENT 手动切换到所选号码increment_number 价值must be 至少有一个数字比该表主键的当前最大数字高,该数字是自动递增的。另外,不要忘记更改table_name


示例:ALTER TABLE wp_posts AUTO_INCREMENT = 2043 <- ID列中的最大数字+1

额外说明:

您需要对每个打乱自动递增的表重复此操作,可能有一种方法可以一次更改所有表,但我不是SQL专家(correct me if there is)ALTER TABLE 原因是整个表格的构建更多信息:herehere

SO网友:Ramon Fincken

我为此写了一个更新。

我使用内置的WP核心模式来确保所有的WP核心表都存在(即使在将来发布5.1.1或更高版本时)。它将删除损坏的行并重新添加键和主键。可以在此处查看免费脚本(以及更多说明):https://wpindexfixer.tools.managedwphosting.nl/

也无需猜测自动增量值。

SO网友:Paresh Barad

我忘了从最后一个MySQL文件导入索引,所以我获取了相同的问题,很难一个接一个地启动自动增量查询,所以创建了脚本,它会获取一个动态表并检查主键如果找到脚本和主键,那么它将动态应用于自动增量。

Take db connection variable from your wp-config.php and save in youWordPressss root and run by url.

// Database configration
$host = \'localhost\';
$dbuser   = \'dbuser\';
$dbpassword   = \'dbpassword\';
$dbname         = \'database\';

// connect to DB
$conn = new mysqli($host, $dbuser, $dbpassword);
try {
    $connection = new PDO("mysql:host=$host;dbname=$dbname", $dbuser, $dbpassword, array(PDO::MYSQL_ATTR_INIT_COMMAND => \'SET sql_mode="NO_ZERO_DATE"\'));
    $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully";
} catch (PDOException $e) {
    exit("Connection failed: " . $e->getMessage());
}

// get all tables from DB
$stmt = $connection->prepare(\'SHOW TABLES\');
$stmt->execute();
$table_names = array();
foreach ($stmt->fetchAll() as $row) {
    $table_names[] = $row[0];
}

// for all tables
foreach ($table_names as $table_name) {

    // get the name of primary key
    $stmt = $connection->prepare("show keys from $table_name where Key_name = \'PRIMARY\'");
    $stmt->execute();
    $key_name = $stmt->fetch()[\'Column_name\'];

    // get the type of primary key
    $stmt = $connection->prepare("show fields from $table_name where Field = \'$key_name\'");
    $stmt->execute();
    $key_type = $stmt->fetch()[\'Type\'];

    // primary already exist then going to add auto increment
    if ($key_name) {

        try {
            // if auto_increment was missing there might be a row with key=0 . compute the next available primary key
            $sql = "select (ifnull( max($key_name), 0)+1) as next_id from $table_name";
            $stmt = $connection->prepare($sql);
            $stmt->execute();
            $next_id = $stmt->fetch()[\'next_id\'];

            // give a sane primary key to a row that has key = 0 if it exists
            $sql = "update $table_name set $key_name = $next_id where $key_name = 0";
            $stmt = $connection->prepare($sql);
            $stmt->execute();

            // set auto_increment to the primary key
            $sql = "alter table $table_name modify column $key_name $key_type auto_increment";
            $stmt = $connection->prepare($sql);
            $stmt->execute();

        } catch (PDOException $e) {
            echo $e->getMessage() . \'\\n\';
        }
    } else {
        echo "primary key not found in table $table_name.\\n";
    }
}
$connection = null;