一、mysql最大连接数的设置
1:查看当前mysql支持的最大连接数
performance_schema 表里
mysql> show variables like 'max_connections';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 500 |
+-----------------+-------+
1 row in set (0.00 sec)
2:查看目前的连接数
mysql> show status like '%thread%';
+------------------------------------------+-------+
| Variable_name | Value |
+------------------------------------------+-------+
| Delayed_insert_threads | 0 |
| Performance_schema_thread_classes_lost | 0 |
| Performance_schema_thread_instances_lost | 0 |
| Slow_launch_threads | 0 |
| Threads_cached | 127 |
| Threads_connected | 4 |---4个
| Threads_created | 70239 |
| Threads_running | 4 |
+------------------------------------------+-------+
8 rows in set (0.00 sec)
3:说明
对比两个值,说明mysql的连接数没有问题
4:如果目前连接数接近500了,那就得调高最大连接了
4.1 临时设置,不用重启数据库就生效,但是一重启数据库就失效了,得重新设置
设置:set global max_connections=1000;
查看验证:show variables like 'max_connections';
4.2 永久设置,修改配置文件,重启数据库生效
vim /etc/my.cnf
[mysqld]
max_connections = 1000
二、影响mysql最大连接数的设置
对连接数有影响的设置(决定了可同时打开的表的数量和要使用的文件描述符)
1:所有的MySQL线程一共能同时打开多少个表(table_open_cache)
查看当前的打开表的数目
mysql> show status like 'open_tables';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Open_tables | 304 |
+---------------+-------+
1 row in set (0.00 sec)
查看当前支持打开表的数目
mysql> show variables like 'table_open%';
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| table_open_cache | 1024 |--支持1024
| table_open_cache_instances | 1 |
+----------------------------+-------+
2 rows in set (0.00 sec)
注意:
Open_tables就是当前打开表的数目,通过flush tables命令可以关闭当前打开的表。
这个值如果过大,并且如果没有经常的执行flush tables命令,可以考虑增加table_open_cache参数的大小
2:查看当前已打开的临时表的信息(max_tmp_tables)
mysql> show global status like '%tmp%table%';
+-------------------------+-------+
| Variable_name | Value |
+-------------------------+-------+
| Created_tmp_disk_tables | 40 |
| Created_tmp_tables | 15235 |
+-------------------------+-------+
2 rows in set (0.00 sec)
查看单个客户端连接能打开的临时表数目
mysql> show variables like 'max_tmp%';
+----------------+-------+
| Variable_name | Value |
+----------------+-------+
| max_tmp_tables | 32 |
+----------------+-------+
1 row in set (0.00 sec)
说明:内存中的临时表变大的时候,也可能被MySQL自动转移到磁盘上(由tmp_table_size和max_heap_table_size参数决定)。
3:查看当前支持打开文件描述符的个数
mysql> show variables like 'open_files%';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| open_files_limit | 65535 |
+------------------+-------+
1 row in set (0.00 sec)
查看当前和历史的文件打开信息
mysql> show global status like '%open%file%';
+-----------------------+-------+
| Variable_name | Value |
+-----------------------+-------+
| Innodb_num_open_files | 75 |
| Open_files | 28 |---当前
| Opened_files | 2754 |---历史打开的
+-----------------------+-------+
3 rows in set (0.00 sec)
修改系统配置文件配合使用,增大允许打开的文件数
vim /etc/security/limits.conf
* soft nproc 65535
* hard nproc 65535
* soft nofile 65535
* hard nofile 65535
说明:
open_files_limit 这个值一般要设置的尽量大,就是设为没有报Too many open files错误的最大值
三、数据库配置文件
[mysqld]
max_connections = 1000 --mysl最大连接数
table_open_cache = 1024 --同时打开表的数量
open_files_limit = 65535 --允许打开的文件数
tmp_table_size = 128M --临时表128M时转移到磁盘上