Current user: root@localhost
SSL: Not in use
Server version: 8.0.18 MySQL Community Server - GPL
Protocol version: 10
Connection: Localhost via UNIX socket

查看当前连接数
root@localhost [(none)]> show variables like '%max_connections%';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| max_connections | 1500 |
| mysqlx_max_connections | 100 |
+------------------------+-------+
2 rows in set (0.00 sec)
复制
可以看到,max_connections当前值是1500;
使用8.0之前的方法进行修改
root@localhost [(none)]> set global max_connections=200;
Query OK, 0 rows affected (0.00 sec)
复制
查看修改后的值:
root@localhost [(none)]> show variables like '%max_connections%';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| max_connections | 200 |
| mysqlx_max_connections | 100 |
+------------------------+-------+
2 rows in set (0.00 sec)
复制
同时查看my.cnf文件
root@localhost [(none)]> system cat mysql/my3306/my.cnf |grep max_connections
max_connections = 1500
root@localhost [(none)]>
复制
配置文件是没有调整过的;这个符合我们常规运维的操作,如果要持久化操作,就必须手工对配置文件进行修改;
我们使用8.0之后提供的持久化方法进行修改
root@localhost [(none)]> set persist max_connections=200;
Query OK, 0 rows affected (0.00 sec)
root@localhost [(none)]>
root@localhost [(none)]> show variables like '%max_connections%';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| max_connections | 200 |
| mysqlx_max_connections | 100 |
+------------------------+-------+
2 rows in set (0.00 sec)
复制
# 参数已经被修改了,我们看下配置文件是否被持久化了.
root@localhost [(none)]> system cat mysql/my3306/my.cnf |grep max_connections
max_connections = 1500
root@localhost [(none)]>
复制
怎么还是1500?数据库没有做持久化么?
不要着急,我们看下官网的介绍:
SET can also be used to persist certain global system variables to the mysqld-auto.cnf file in the data directory, to affect server operation for subsequent startups.
https://dev.mysql.com/doc/refman/8.0/en/persisted-system-variables.html
原来持久化系统参数操作会在data目录下生成mysqld-auto.cnf文件,用来保存持久化的变量值。我们来看下有没有这个文件:
root@localhost [(none)]> system ls -al mysqldata/my3307/data/*cnf*
-rw-r----- 1 mysql mysql 56 Feb 2 18:16 mysqldata/my3307/data/auto.cnf
-rw-r----- 1 mysql mysql 171 Feb 3 09:20 mysqldata/my3307/data/mysqld-auto.cnf
root@localhost [(none)]>
复制
是有mysqld-auto.cnf这个文件的,我们看下这个文件的内容:
root@localhost [(none)]> system cat mysqldata/my3307/data/mysqld-auto.cnf
{ "Version" : 1 , "mysql_server" : { "max_connections" : { "Value" : "200" , "Metadata" : { "Timestamp" : 1612315259975427 , "User" : "root" , "Host" : "localhost" } } } }
root@localhost [(none)]>
复制
这个文件里面记载了max_connections变量,而Value正是我们修改的200.
那我们现在来重启下数据库,看看持久化的操作是不是可以生效的;
关闭:
root@localhost [(none)]> shutdown;
Query OK, 0 rows affected (0.00 sec)
root@localhost [(none)]>
复制
重启:
[mysql@Tdongkf base8018]$ nohup bin/mysqld_safe --defaults-file=/mysql/my3307/my.cnf &
[1] 119536
[mysql@Tdongkf base8018]$ nohup: ignoring input and appending output to ‘nohup.out’
复制
再次查看我们设置的全局变量:
root@localhost [(none)]> show variables like '%max_connections%';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| max_connections | 200 |
| mysqlx_max_connections | 100 |
+------------------------+-------+
2 rows in set (0.01 sec)
复制
我们发现,max_connections变量已经是修改后的200了,说明MySQL数据库已经将我们之前设置的参数持久化到配置文件中了,只不过这个配置文件是MySQL为我们重新生成了一个,在data目录下的mysqld-auto.cnf文件。
配置参数的生效是先读取默认指定的参数,最后再读取持久化文件中的参数,并且会覆盖默认参数文件中的参数,达到调整参数持久化的目的;
如果我们只是想持久化配置文件,而不是在线调整数据库参数,希望数据库在下次重启之后参数再生效,该如何操作呢?
MySQL为我们提供了PERSIST_ONLY方法:
To persist a global system variable to the mysqld-auto.cnf file without setting the global variable runtime value, precede the variable name by the PERSIST_ONLY keyword or the @@PERSIST_ONLY.
https://dev.mysql.com/doc/refman/8.0/en/persisted-system-variables.html
我们使用PERSIST_ONLY方式来设置参数:
root@localhost [(none)]> set PERSIST_ONLY max_connections=500;
Query OK, 0 rows affected (0.00 sec)
复制
我们来检查下数据库参数:
root@localhost [(none)]> show variables like '%max_connections%';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| max_connections | 200 |
| mysqlx_max_connections | 100 |
+------------------------+-------+
2 rows in set (0.00 sec)
复制
还是200,数据库变量并没有改变,我们看下持久化文件:
root@localhost [(none)]> system cat /mysqldata/my3307/data/mysqld-auto.cnf
{ "Version" : 1 , "mysql_server" : { "max_connections" : { "Value" : "500" , "Metadata" : { "Timestamp" : 1612316314365922 , "User" : "root" , "Host" : "localhost" } } } }
复制
已经有max_connections的Value=500的参数了。根据之前的测试,这个文件会在下次重启之后自动加载,即数据库参数在重启之后会变为max_connections=500;
如果我们想取消持久化的参数,恢复为原先的值怎么办呢?
MySQL数据库为我们提供了reset persist方法;
RESET PERSIST removes persisted settings from mysqld-auto.cnf.
https://dev.mysql.com/doc/refman/8.0/en/persisted-system-variables.html
也很简单,直接执行:
root@localhost [(none)]> reset persist max_connections;
Query OK, 0 rows affected (0.00 sec)
root@localhost [(none)]>
复制
我们看下配置文件
[mysql@Tdongkf base8018]$ cat /mysqldata/my3307/data/mysqld-auto.cnf
{ "Version" : 1 , "mysql_server" : { } }
复制
之前的max_connections参数已经被移除了,我们看下默认配置文件;
[mysql@Tdongkf base8018]$ cat /mysql/my3307/my.cnf |grep max_conn
max_connections = 1500
复制
配置文件中还是原先的1500值;
所以,MySQL8.0之后的版本更加方便我们在线调整数据库参数并持久化到配置文件中,避免我们再手工修改的操作,也避免了手工操作出现失误的风险;不过,以后我们再检查数据库参数文件的时候,也别忘记了检查下数据文件目录下的mysqld_auto.cnf文件,毕竟,这个也是数据库最后加载会生效的文件;

完
=end=