mysql_upgrade

本质上为一个封装了mysqlcheck命令的脚本,流程如下:

1 mysqlcheck --no-defaults --databases --fix-db-names --fix-table-names mysql
2 mysqlcheck --no-defaults --check-upgrade --databases --auto-repair mysql
3 mysql < fix_priv_tables
4 mysqlcheck --no-defaults --all-databases --skip-database=mysql --fix-db-names --fix-table-names
5 mysqlcheck --no-defaults --check-upgrade --all-databases --skip-database=mysql --auto-repair

执行完毕后在数据目录生成mysql_upgrade_info文件,记录检查过的表,下次再调用时可以跳过; 

执行步骤:

1)检查所有数据表同当前binary的兼容性,若不兼容则尝试修复,修复失败则必须手工执行;

手工修复:mysqldump重新加载或者null alternation(alter table t engine=innodb);

如果仅仅是修改表的collation,则可调用ALTER TABLE xxx CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_mysql500_ci;

2)升级mysql系统表;

调用mysql_upgrade后,需要重启mysql才能让系统表更新生效;

如果单机运行了多个mysql实例,则指定连接参数

shell>mysql_upgrade --protocol=tcp -P 3306 [other_options]复制
shell>mysql_upgrade --protocol=tcp -P 3307 [other_options]

输出结果

Table upgrade required.

Please do "REPAIR TABLE `tbl_name`" or dump/reload to fix it!

mysqlcheck

https://www.modb.pro/db/41010

该工具可以在服务运行状态下执行
检查特定的表:
如果应用中提示某个表坏了,使用下面的命令来检查。

$ mysqlcheck -c newmandela order -uroot -p
Enter password:
newmandela.order OK
newmandela 是库名, order是表名,还需要输入用户名和密码

检查一个库中的所有表:

$ mysqlcheck -c newmandela -uroot -p
Enter password:
newmandela.account OK
newmandela.alarm OK
newmandela.alarm_settings OK
newmandela.auth_group OK
newmandela.auth_group_permissions OK
newmandela.auth_permission OK

检查所有库中的所有表:
全部的库和表都检查一遍了。

$mysqlcheck -c --all-databases -uroot -p
Enter password:
apmonitor.acinfo OK
apmonitor.apdailysts OK
apmonitor.apinfo OK
apmonitor.apmonthsts OK
apmonitor.apscanlog OK
apmonitor.auth_group OK

如果只想检查某几个库呢? 可以使用 --databases 参数:

$ mysqlcheck -c --databases newmandela radius -uroot -p
Enter password:
newmandela.account OK
newmandela.alarm OK
newmandela.alarm_settings OK
newmandela.auth_group OK

使用 mysqlcheck 分析表:

$ mysqlcheck -a radius payment_transactionrecord -uroot -p
Enter password:
radius.payment_transactionrecord Table is already up to date
上面的命令 用来分析 radius 库的 payment_transactionrecord表, -a 表示 analyze

使用 mysqlcheck 优化表:

mysqlcheck -o radius payment_transactionrecord -uroot -p
Enter password:
radius.payment_transactionrecord OK
-o 代表 optimize ,这里是优化 radius 库的 payment_transactionrecord 表

使用 mysqlcheck 修复表:

mysqlcheck -r radius payment_transactionrecord -uroot -p
Enter password:
radius.payment_transactionrecord OK-r 代表 repair ,这里是 修复 radius 库的 payment_transactionrecord 表

检查,优化,修复表组合命令:

mysqlcheck -uroot -p --auto-repair -o newmandela
Enter password:
newmandela.account
note : Table does not support optimize, doing recreate + analyze instead
status : OK
newmandela.alarm
note : Table does not support optimize, doing recreate + analyze instead
status : OK
newmandela.alarm_settings
note : Table does not support optimize, doing recreate + analyze instead
status : OK