背景
最近读 MySQL 的官方文档发现 8.0.16 版本引入的一个新功能 ---- 参数检查。以前要是想知道 my.cnf 配置文件,写的有没有问题;一个可行的方案就用它来启动一个 MySQL 服务,如果能正常地起来,说明至少没有致命的错误。现在看来不需要搞这么复杂了。
新版本的 mysqld 程序,包含了一个参数检查的功能。
用法
用法上和启动 MySQL 服务差不多,只是要明确地告诉 mysqld 这次只做参数检查,不要启动服务。使用时像下面这样调用就行
mysqld --defaults-file=/etc/my-3309.cnf --validate-config也就是说我们只需要多指定一个 --validate-config 选项就行,如果配置文件有问题,程序会直接把错误打印出来。
如果对配置的要求比较高,比如说一些不再推荐的参数也要发现的话可以再加一个选项,像下面这样
mysqld --defaults-file=/etc/my-3309.cnf --validate-config --log-error-verbosity=2
2022-12-08T19:36:55.126560+08:00 0 [Warning] [MY-011068] [Server] The syntax 'slave_preserve_commit_order' is deprecated and will be removed in a future release. Please use replica_preserve_commit_order instead.
2022-12-08T19:36:55.126565+08:00 0 [Warning] [MY-011069] [Server] The syntax '--replica-parallel-type' is deprecated and will be removed in a future release.现在好了,一些将要废弃的配置项也能看到,不用我们真的去启动服务分析日志。
亮点
如果它就这么点能力还不会让我眼前一亮;主要是我的开发环境比较复杂,不同的 MySQL 开发版本同时存在。
ps -ef | grep mysqld
mysql33+ 789 1 0 Dec03 ? 00:12:02 /usr/local/mysql-8.0.29-linux-glibc2.12-x86_64/bin/mysqld --defaults-file=/etc/my-3306.cnf
mysql33+ 790 1 0 Dec03 ? 00:19:32 /usr/local/mysql-8.0.30-linux-glibc2.12-x86_64/bin/mysqld --defaults-file=/etc/my-3308.cnf
mysql33+ 791 1 0 Dec03 ? 00:17:17 /usr/local/mysql-8.0.31-linux-glibc2.12-x86_64/bin/mysqld --defaults-file=/etc/my-3309.cnf
root 186358 186181 0 19:42 pts/0 00:00:00 grep --color=auto mysqld可以看到 8.0.29 , 8.0.30, 8.0.31 三个版本同时在我的机器上跑着,有一次我无意间用一个 8.0.31 的 mysqld 程序去检查一个 8.0.30 版本的配置文件,它还真给我检查出了一些毛病。只能默默在心里说牛逼!!!
# 这里的 /etc/my-3308.cnf 是一份 8.0.30 的配置文件
cd /usr/local/mysql-8.0.31-linux-glibc2.12-x86_64/
bin/mysqld --defaults-file=/etc/my-3308.cnf --validate-config
2022-12-08T19:46:50.519715+08:00 0 [ERROR] [MY-010340] [Server] Error message file '/usr/local/mysql-8.0.30-linux-glibc2.12-x86_64/share/english/errmsg.sys' had only 5683 error messages, but it should contain at least 5708 error messages. Check that the above file is the right version for this program!可以看到它通过配置文件找到了 basedir ,并且默默的帮我检查了 basedir 下的文件对不对,这也真的够仔细了。