MySQL 8 新特性:全局参数持久化!

时间:2022-01-28 04:53:35

MySQL 8 新特性:全局参数持久化!

前言

自从 2018 年发布第一版 MySQL 8.0.11 正式版至今,MySQL 版本已经更新迭代到 8.0.26,相对于稳定的 5.7 版本来说,8.0 在性能上的提升是毋庸置疑的!

随着越来越多的企业开始使用 MySQL 8.0 版本,对于 DBA 来说是一个挑战,也是一个机遇!????

本文主要讨论下 MySQL 8.0 版本的新特性:全局参数持久化

全局参数持久化

MySQL 8.0 版本支持在线修改全局参数并持久化,通过加上 PERSIST 关键字,可以将修改的参数持久化到新的配置文件(mysqld-auto.cnf)中,重启 MySQL 时,可以从该配置文件获取到最新的配置参数!

[WL#8688]:https://dev.mysql.com/worklog/task/?id=8688

启用这个功能,使用特定的语法 SET PERSIST 来设定任意可动态修改的全局变量!

  • SET PERSIST语句可以修改内存中变量的值,并且将修改后的值写?数据?录中的 mysqld-auto.cnf 中。
  • SET PERSIST_ONLY语句不会修改内存中变量的值,只是将修改后的值写?数据?录中的 mysqld-auto.cnf 中。

以 max_connections 参数为例:

  1. mysql>select*fromperformance_schema.persisted_variables;
  2. Emptyset(0.00sec)
  3.  
  4. mysql>showvariableslike'%max_connections%';
  5. +------------------------+-------+
  6. |Variable_name|Value|
  7. +------------------------+-------+
  8. |max_connections|151|
  9. |mysqlx_max_connections|100|
  10. +------------------------+-------+
  11. 2rowsinset(0.00sec)
  12.  
  13. mysql>setpersistmax_connections=300;
  14. QueryOK,0rowsaffected(0.00sec)
  15.  
  16. mysql>select*fromperformance_schema.persisted_variables;
  17. +-----------------+----------------+
  18. |VARIABLE_NAME|VARIABLE_VALUE|
  19. +-----------------+----------------+
  20. |max_connections|300|
  21. +-----------------+----------------+
  22. 1rowinset(0.00sec)

系统会在数据目录下生成一个包含 json 格式的 mysqld-auto.cnf 的文件,格式化后如下所示,当 my.cnf 和mysqld-auto.cnf 同时存在时,后者具有更高优先级。

  1. {
  2. "Version":1,
  3. "mysql_server":{
  4. "max_connections":{
  5. "Value":"300",
  6. "Metadata":{
  7. "Timestamp":1632575065787609,
  8. "User":"root",
  9. "Host":"localhost"
  10. }
  11. }
  12. }
  13. }

注意: 即使你通过 SET PERSIST 修改配置的值并没有任何变化,也会写入到 mysqld-auto.cnf 文件中。但你可以通过设置成 DEFAULT 值的方式来恢复初始默认值!

如果想要恢复 max_connections 参数为初始默认值,只需要执行:

  1. mysql>setpersistmax_connections=DEFAULT;
  2. QueryOK,0rowsaffected(0.00sec)
  3.  
  4. mysql>select*fromperformance_schema.persisted_variables;
  5. +-----------------+----------------+
  6. |VARIABLE_NAME|VARIABLE_VALUE|
  7. +-----------------+----------------+
  8. |max_connections|151|
  9. +-----------------+----------------+
  10. 1rowinset(0.00sec)

如果想要移除所有的全局持久化参数,则只需执行:

  1. mysql>RESETPERSIST;
  2. QueryOK,0rowsaffected(0.00sec)
  3.  
  4. mysql>select*fromperformance_schema.persisted_variables;
  5. Emptyset(0.00sec)

当然,删除 mysqld-auto.cnf 文件后,重启 MySQL 也可!

原文链接:https://mp.weixin.qq.com/s/5rBDcyYN6w13105piWaT6g