在/etc/mysql/my.cnf中设置

max_connections=512

进入Mysql查看发现还是214

mysql> show global variables like '%max_connecti%';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections |   214 |
+-----------------+-------+
1 row in set (0.01 sec)

google了一番后,说是跟open_files_limit有关,每个connect都会打开几个文件,查了下open_files_limit为1024。

mysql> show global variables like '%open_files_limit%';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| open_files_limit |  1024 |
+------------------+-------+
1 row in set (0.00 sec)

在/etc/mysql/my.cnf中加上

open_files_limit = 4096

重启mysql后查看

mysql> show global variables like '%max_connecti%';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections |  512  |
+-----------------+-------+
1 row in set (0.01 sec)
mysql> show global variables like '%open_files_limit%';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| open_files_limit |  4096 |
+------------------+-------+
1 row in set (0.00 sec)

open_files_limit最大值也是有限制的,设置太大也会变成默认1024,可以在/etc/security/limits.conf加上并重启系统

* soft nofile 65536
* hard nofile 65536

还有就是启动mysql的service加上LimitNOFILE=65535并重启mysql,这样可以解除open file的限制,

LimitNOFILE=65535