分析一例 mysqldump bug

数据库关系型数据库技术服务知识库
问题描述

客户在使用火山引擎 RDS for MySQL(5.7.32) 过程中,希望将实例中全量数据导入到本地测试环境中(5.7.19),导入完成之后,发现 sys schema 无法使用,报错如下:

mysql> select * from session;
ERROR 1356 (HY000): View 'sys.session' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them

mysql> select * from processlist;
ERROR 1356 (HY000): View 'sys.processlist' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them

检查 RDS,发现 RDS 工作正常,并未出现 sys schema 损坏的情况。

问题复现

客户的命令中使用了 --all-databases 参数,备份命令如下:

mysqldump -h 111.62.xx.xx  -urudonx -p$password --all-databases --set-gtid-purged=OFF --master-data=2 --single-transaction --routines > all_databaes.sql

使用具有 super user 将备份文件导入到自建的测试环境中:

mysql> source /root/all_databases.sql

待导入完成之后,问题可以复现,进行如下检查,发现没有任何存储过程和函数:

mysql> SHOW FUNCTION STATUS WHERE Db = 'sys';
Empty set (0.01 sec)

mysql> SHOW PROCEDURE STATUS WHERE Db = 'sys';
Empty set (0.00 sec)

mysql> SELECT COUNT(*) FROM mysql.proc;
+----------+
| COUNT(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)

mysql> select count(*) from information_schema.ROUTINES where ROUTINE_SCHEMA='sys';
+----------+
| count(*) |
+----------+
|       0  |
+----------+
1 row in set (0.00 sec)

检查备份文件后发现:

  1. DROP TABLE IF EXISTS proc;
  2. 重建 mysql.proc
  3. 锁表进行备份,备份完成之后解锁,但是在此过程中未发现 mysqldump 备份任何 sys schema 中 routines:
LOCK TABLES `proc` WRITE;
/*!40000 ALTER TABLE `proc` DISABLE KEYS */;
/*!40000 ALTER TABLE `proc` ENABLE KEYS */;
UNLOCK TABLES;
问题分析

当 mysqldump 使用 --all-databases 参数时,不会备份 mysql,performance_schema 以及 sys schema,官方建议我们使用 --database sys 对 sys schema 进行备份,不过问题在于当使用了 --all-databases时,mysqldump 清空了 mysql.proc,以致于 sys 无法正常使用。 以下摘自官方文档[1]:

mysqldump does not dump the INFORMATION_SCHEMA, performance_schema, or sys schema by default. To dump any of these, name them explicitly on the command line. You can also name them with the --databases option

在社区中,也有此 bug 的相关报告[2],其中提到:

It does not matter that mysqldump does not includes the sys schema, but the problem is that mysqldump would affect the existing sys schema.

解决方案

根据参考文档[1][2],有如下解决方案:

1. 使用 mysql_upgrade 进行修复

使用 mysql_upgrade 进行修复 sys schema,可以从输出中看到 Found 0 sys functions, but expected 22. Re-installing the sys schema。


(base) [root@ip-10-0-0-22 ~]# mysql_upgrade -S /tmp/mysql3306.sock --upgrade-system-tables --force
Checking server version.
Running queries to upgrade MySQL server.
Checking system database.
mysql.columns_priv                                 OK
mysql.db                                           OK
mysql.engine_cost                                  OK
mysql.event                                        OK
mysql.func                                         OK
mysql.general_log                                  OK
mysql.gtid_executed                                OK
mysql.help_category                                OK
mysql.help_keyword                                 OK
mysql.help_relation                                OK
mysql.help_topic                                   OK
mysql.innodb_index_stats                           OK
mysql.innodb_table_stats                           OK
mysql.ndb_binlog_index                             OK
mysql.plugin                                       OK
mysql.proc                                         OK
mysql.procs_priv                                   OK
mysql.proxies_priv                                 OK
mysql.server_cost                                  OK
mysql.servers                                      OK
mysql.slave_master_info                            OK
mysql.slave_relay_log_info                         OK
mysql.slave_worker_info                            OK
mysql.slow_log                                     OK
mysql.tables_priv                                  OK
mysql.time_zone                                    OK
mysql.time_zone_leap_second                        OK
mysql.time_zone_name                               OK
mysql.time_zone_transition                         OK
mysql.time_zone_transition_type                    OK
mysql.user                                         OK
The sys schema is already up to date (version 1.5.1).
Found 0 sys functions, but expected 22. Re-installing the sys schema.
Upgrading the sys schema.
Checking databases.
sys.sys_config                                     OK
Upgrade process completed successfully.
Checking if update is needed.

2. 使用 --databases 进行备份

使用 --databases 参数进行备份:

(base) [root@ip-10-0-0-22 ~]# mysqldump -h 111.62.xx.xx  -urudonx -p$password --databases rudonx sys mysql --set-gtid-purged=OFF --master-data=2 --single-transaction --routines > all_databases_new.sql
```
`
备份内容如下:
![图片](https://lf3-volc-editor.volccdn.com/obj/volcfe/sop-public/upload_3246e1eabd2d917d537a1d798370a058.png)
# 参考文档
[1][https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html](https://dev.mysql.com/doc/refman/5.7/en/mysqldump.html)
[2][https://bugs.mysql.com/bug.php?id=83259](https://bugs.mysql.com/bug.php?id=83259)
**如果您有其他问题,欢迎您联系火山引擎**[技术支持服务](https://console.volcengine.com/ticket/createTicketV2/)

38
0
0
0
相关产品
评论
未登录
看完啦,登录分享一下感受吧~
暂无评论