全局变量的修改持久化
在8之前的版本中,对于全局变量的修改,其只会影响其内存值,而不会持久化到配置文件中。数据库重启,又会恢复成修改前的值。从8开始,可通过SET PERSIST命令将全局变量的修改持久化到配置文件中。
修改持久化
mysql> show variables like '%max_connections%'; +------------------------+-------+ | Variable_name | Value | +------------------------+-------+ | max_connections | 151 | | mysqlx_max_connections | 100 | +------------------------+-------+ 2 rows in set (0.03 sec) mysql> set persist max_connections=300; Query OK, 0 rows affected (0.00 sec) mysql> show variables like '%max_connections%'; +------------------------+-------+ | Variable_name | Value | +------------------------+-------+ | max_connections | 300 | | mysqlx_max_connections | 100 | +------------------------+-------+ 2 rows in set (0.00 sec)
修改后的保存位置
在数据库启动时,会首先读取其它配置文件,最后才读取mysqld-auto.cnf文件。不建议手动修改该文件,其有可能导致数据库在启动过程中因解析错误而失败。如果出现这种情况,可手动删除mysqld-auto.cnf文件或将persisted_globals_load变量设置为off来避免该文件的加载。
scutech@scutech:~$ cat /mysql/data/mysqld-auto.cnf { "Version" : 1 , "mysql_server" : { "max_connections" : { "Value" : "300" , "Metadata" : { "Timestamp" : 1583910814250180 , "User" : "root" , "Host" : "localhost" } } } } mysql> select * from performance_schema.persisted_variables; +-----------------+----------------+ | VARIABLE_NAME | VARIABLE_VALUE | +-----------------+----------------+ | max_connections | 300 | +-----------------+----------------+ 1 row in set (0.01 sec)
持久化为默认值
还可以通过下述方式将全局变量持久化为默认值。注意,是默认值,而不是修改前的值。
mysql> set persist max_connections=default; Query OK, 0 rows affected (0.00 sec) mysql> select * from performance_schema.persisted_variables; +-----------------+----------------+ | VARIABLE_NAME | VARIABLE_VALUE | +-----------------+----------------+ | max_connections | 151 | +-----------------+----------------+ 1 row in set (0.00 sec) mysql> show variables like '%max_connections%'; +------------------------+-------+ | Variable_name | Value | +------------------------+-------+ | max_connections | 151 | | mysqlx_max_connections | 100 | +------------------------+-------+ 2 rows in set (0.01 sec) scutech@scutech:~$ cat /mysql/data/mysqld-auto.cnf { "Version" : 1 , "mysql_server" : { "max_connections" : { "Value" : "151" , "Metadata" : { "Timestamp" : 1583911527762399 , "User" : "root" , "Host" : "localhost" } } } }scutech@scutech:~$ scutech@scutech:~$
这个命令同“set global max_connections=default”类似,都会将变量的值设置为默认值,只不过前者还会将默认值持久化到配置文件中。
清空持久化变量
对于已经持久化了变量,可通过reset persist命令清除掉,注意,其只是清空mysqld-auto.cnf和performance_schema.persisted_variables中的内容,对于已经修改了的变量的值,不会产生任何影响。
mysql> reset persist; Query OK, 0 rows affected (0.00 sec) mysql> select * from performance_schema.variables_info where variable_source like 'PERSISTED'\G Empty set (0.04 sec) cat /mysql/data/mysqld-auto.cnf { "Version" : 1 , "mysql_server" : { } }scutech@scutech:~$
修改只读变量
但是对于read only 的参数,修改参数后需要重启才能生效
mysql> set persist innodb_log_file_size=2073741824; ERROR 1238 (HY000): Variable 'innodb_log_file_size' is a read only variable mysql> set persist_only innodb_log_file_size=2073741824; Query OK, 0 rows affected (0.00 sec) mysql> show variables like 'innodb_log_file_size'; +----------------------+-----------+ | Variable_name | Value | +----------------------+-----------+ | innodb_log_file_size | 104857600 | +----------------------+-----------+ 1 row in set (0.02 sec) mysql> restart ; Query OK, 0 rows affected (0.00 sec) mysql> show variables like 'innodb_log_file_size'; No connection. Trying to reconnect... Connection id: 8 Current database: *** NONE *** +----------------------+------------+ | Variable_name | Value | +----------------------+------------+ | innodb_log_file_size | 2073034752 | +----------------------+------------+ 1 row in set (0.02 sec) mysql> select * from performance_schema.variables_info where variable_source like 'PERSISTED'\G *************************** 1. row *************************** VARIABLE_NAME: innodb_log_file_size VARIABLE_SOURCE: PERSISTED VARIABLE_PATH: /mysql/data/mysqld-auto.cnf MIN_VALUE: 4194304 MAX_VALUE: 18446744073709551615 SET_TIME: 2020-03-11 15:55:20.334556 SET_USER: root SET_HOST: localhost 1 row in set (0.01 sec) mysql> system cat /mysql/data/mysqld-auto.cnf { "Version" : 1 , "mysql_server" : { "mysql_server_static_options" : { "innodb_log_file_size" : { "Value" : "2073741824" , "Metadata" : { "Timestamp" : 1583913320334556 , "User" : "root" , "Host" : "localhost" } } } } }mysql> mysql> system ls -lh /mysql/data/ib_logfile* -rw-r----- 1 mysql mysql 2.0G Mar 11 15:56 /mysql/data/ib_logfile0 -rw-r----- 1 mysql mysql 2.0G Mar 11 15:56 /mysql/data/ib_logfile1