设置 MySQL 时出错:Table 'mysql.plugin' 不存在

Error setting up MySQL: Table 'mysql.plugin' doesn't exist

我有一个 MySQL 5.7 实例 运行ning 在 Ubuntu 16 上没有问题,但是当我尝试安装任何其他包或尝试通过 apt-get 更新现有包时,我收到以下错误:

# apt-get upgrade

Setting up mysql-server-5.7 (5.7.20-0ubuntu0.16.04.1) ...
Checking if update is needed.
Checking server version.
Running queries to upgrade MySQL server.
mysql_upgrade: [ERROR] 1146: Table 'mysql.plugin' doesn't exist
mysql_upgrade failed with exit status 5
 dpkg: error processing package mysql-server-5.7 (--configure):
  subprocess installed post-installation script returned error exit status 1
No apport report written because the error message indicates its a followup error from a previous failure.
dpkg: dependency problems prevent configuration of mysql-server:
mysql-server depends on mysql-server-5.7; however:
Package mysql-server-5.7 is not configured yet.

dpkg: error processing package mysql-server (--configure):
 dependency problems - leaving unconfigured
Errors were encountered while processing:
 mysql-server-5.7
 mysql-server

并且如果我 运行 a mysqlcheck,看起来有些系统表存在但其他的缺失:

mysql.columns_priv                                 OK
mysql.db                                           OK
mysql.engine_cost
Error    : Table 'mysql.engine_cost' doesn't exist
status   : Operation failed
mysql.event                                        OK
mysql.func                                         OK
mysql.general_log                                  OK
mysql.gtid_executed
Error    : Table 'mysql.gtid_executed' doesn't exist
status   : Operation failed
mysql.help_category
Error    : Table 'mysql.help_category' doesn't exist
status   : Operation failed
mysql.help_keyword
Error    : Table 'mysql.help_keyword' doesn't exist
status   : Operation failed
mysql.help_relation
Error    : Table 'mysql.help_relation' doesn't exist
status   : Operation failed
mysql.help_topic
Error    : Table 'mysql.help_topic' doesn't exist
status   : Operation failed
mysql.innodb_index_stats
Error    : Table 'mysql.innodb_index_stats' doesn't exist
status   : Operation failed
mysql.innodb_table_stats
Error    : Table 'mysql.innodb_table_stats' doesn't exist
status   : Operation failed
mysql.ndb_binlog_index                             OK
mysql.plugin
Error    : Table 'mysql.plugin' doesn't exist
status   : Operation failed
mysql.proc                                         OK
mysql.procs_priv                                   OK
mysql.proxies_priv                                 OK
mysql.server_cost
Error    : Table 'mysql.server_cost' doesn't exist
status   : Operation failed
mysql.servers
Error    : Table 'mysql.servers' doesn't exist
status   : Operation failed
mysql.slave_master_info
Error    : Table 'mysql.slave_master_info' doesn't exist
status   : Operation failed
mysql.slave_relay_log_info
Error    : Table 'mysql.slave_relay_log_info' doesn't exist
status   : Operation failed
mysql.slave_worker_info
Error    : Table 'mysql.slave_worker_info' doesn't exist
status   : Operation failed
mysql.slow_log                                     OK
mysql.tables_priv                                  OK
mysql.time_zone
Error    : Table 'mysql.time_zone' doesn't exist
status   : Operation failed
mysql.time_zone_leap_second
Error    : Table 'mysql.time_zone_leap_second' doesn't exist
status   : Operation failed
mysql.time_zone_name
Error    : Table 'mysql.time_zone_name' doesn't exist
status   : Operation failed
mysql.time_zone_transition
Error    : Table 'mysql.time_zone_transition' doesn't exist
status   : Operation failed
mysql.time_zone_transition_type
Error    : Table 'mysql.time_zone_transition_type' doesn't exist
status   : Operation failed
mysql.user                                         OK
sys.sys_config
Error    : Table 'sys.sys_config' doesn't exist
status   : Operation failed

但是,数据库是正确的 运行ning,如果我手动检查 SQL,看起来 表存在。

mysql> SHOW TABLES;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| engine_cost               |
| event                     |
| func                      |
| general_log               |
| gtid_executed             |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| server_cost               |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+

我使用 innodb_file_per_table=1 设置,/var/lib/mysql 目录的所有者正确设置为 mysql 用户。

我想在不转储现有数据的情况下解决这个问题,但我运行没主意了。

最后唯一解决问题的方法是清除并重新安装 mysql 库,备份数据库并在所有过程后恢复它:

# Backup database
time mysqldump -u root -p database > /var/backups/restore_backup.sql

# Backup config and data
mv /var/lib/mysql /tmp/backups/mysql-lib.bak
cp /etc/mysql/my.cnf /tmp/backups/my.cnf.bak

# Purge library
sudo apt-get remove --purge mysql-server mysql-client mysql-common
sudo apt-get autoremove
sudo apt-get autoclean

# Reinstall
sudo apt-get update
sudo apt-get install mysql-server

# Restore config
cp /tmp/backups/my.cnf.bak /etc/mysql/my.cnf
sudo service mysql restart

# Create database
mysql -u root -p
mysql> CREATE DATABASE database;

# Restore backup
time mysql -u root -p database < /var/backups/restore_backup.sql

我今天遇到了同样的问题,我设法在没有备份-重新安装-还原步骤的情况下解决了它。 (虽然我有备份,每个人都应该!)

就我而言,/var/lib/mysql/mysql/ 目录中有一个 plugin.ibd 文件,其他丢失的 table 也有文件,但可能由于某种原因它们不完整。

我做了以下事情:

  1. mysql.plugin table 的文件移动到另一个目录,例如:mv /var/lib/mysql/mysql/plugin.* /other/place/
  2. 运行 apt-get upgrade -y 看看会发生什么。升级脚本正确地重新创建了 mysql.plugin table 并抱怨了另一个 table.
  3. 我用升级脚本告诉我的 table 名称重复了上面的步骤,在用这种方式重新创建 19 table 秒后,升级成功完成。

我以 root 身份执行的所有步骤:

cd /var/lib/mysql/mysql
mkdir temp
mv engine_cost.*               temp/
mv gtid_executed.*             temp/
mv help_category.*             temp/
mv help_keyword.*              temp/
mv help_relation.*             temp/
mv help_topic.*                temp/
mv innodb_index_stats.*        temp/
mv innodb_table_stats.*        temp/
mv plugin.*                    temp/
mv server_cost.*               temp/
mv servers.*                   temp/
mv slave_master_info.*         temp/
mv slave_relay_log_info.*      temp/
mv slave_worker_info.*         temp/
mv time_zone.*                 temp/
mv time_zone_leap_second.*     temp/
mv time_zone_name.*            temp/
mv time_zone_transition.*      temp/
mv time_zone_transition_type.* temp/
apt-get upgrade -y

希望对大家有所帮助。

这适用于 CentOS 7.9.2009 w/WHM

我不必重新安装 MySQL。我能够自动重新创建 bad/missing tables.

作为 root 或 sudo

cd /var/lib/mysql/mysql

为临时存储创建目录(始终备份!)

mkdir /root/temp-mysql-mysql-bak

将每个错误的文件 table 移动到您的临时目录

mv TABLE_NAME_HERE.* /root/temp-mysql-mysql-bak/

例子

mv engine_cost.* /root/temp-mysql-mysql-bak/
mv time_zone.* /root/temp-mysql-mysql-bak/
mv plugin.* /root/temp-mysql-mysql-bak/

升级

yum upgrade -y

MySQL升级

mysql_upgrade --upgrade-system-tables --force

重启

systemctl mysql restart

尝试 运行:

bash-4.2# mysql_install_db --user=mysql --ldata=/var/lib/mysql/
Installing MariaDB/MySQL system tables in '/var/lib/mysql/' ...
OK

这应该在数据目录中安装系统表。然后重启mysql/mariadb服务。