首页 > 代码库 > MySQL服务器的线程数查看方法
MySQL服务器的线程数查看方法
MySQL服务器的线程数需要在一个合理的范围之内,这样才能保证MySQL服务器健康平稳地运行。Threads_created表示创建过的线程数,通过查看Threads_created就可以查看MySQL服务器的进程状态。
- mysql> show global status like ‘Thread%‘;
- +-------------------+-------+
- | Variable_name | Value |
- +-------------------+-------+
- | Threads_cached | 46 |
- | Threads_connected | 2 |
- | Threads_created | 570 |
- | Threads_running | 1 |
- +-------------------+-------+
如果我们在MySQL服务器配置文件中设置了thread_cache_size,当客户端断开之后,服务器处理此客户的线程将会缓存起来以响应下一个客户而不是销毁(前提是缓存数未达上限)。
Threads_created表示创建过的线程数,如果发现Threads_created值过大的话,表明MySQL服务器一直在创建线程,这也是比较耗资源,可以适当增加配置文件中thread_cache_size值,查询服务器
thread_cache_size配置:
- mysql> show variables like ‘thread_cache_size‘;
- +-------------------+-------+
- | Variable_name | Value |
- +-------------------+-------+
- | thread_cache_size | 64 |
- +-------------------+-------+
示例中的服务器还是挺健康的。
解析MySQL与连接数相关的几个参数
MySQL的variables和status是管理维护的利器,就类似Oracle的spfile和v$表。
MySQL通过系统变量记录很多配置信息,比如最大连接数max_connections:
mysql> show variables like ‘%connect%‘;
+--------------------------+-----------------+
| Variable_name | Value |
+--------------------------+-----------------+
| character_set_connection | utf8 |
| collation_connection | utf8_general_ci |
| connect_timeout | 10 |
| init_connect | SET NAMES utf8 |
| max_connect_errors | 10 |
| max_connections | 200 |
| max_user_connections | 0 |
+--------------------------+-----------------+
7 rows in set (0.00 sec)
这个参数是指同时连接上来的客户端数量,在5.1版本里默认的值是151,那么实际支持的连接数是这个值加一,也就是152,因为要为系统管理员登录上来查看信息保留一个连接。这个参数的大小要综合很多因素来考虑,比如使用的平台所支持的线程库数量(windows只能支持到2048)、服务器的配置(特别是内存大小)、每个连接占用资源(内存和负载)的多少、系统需要的响应时间等。一般Linux系统支持到几百并发是没有任何问题的。可以在global或session范围内修改这个参数:
mysql> set global max_connections=151;
Query OK, 0 rows affected (0.00 sec)mysql> show variables like ‘%connect%‘;
+--------------------------+-----------------+
| Variable_name | Value |
+--------------------------+-----------------+
| character_set_connection | utf8 |
| collation_connection | utf8_general_ci |
| connect_timeout | 10 |
| init_connect | SET NAMES utf8 |
| max_connect_errors | 10 |
| max_connections | 151 |
| max_user_connections | 0 |
+--------------------------+-----------------+
7 rows in set (0.00 sec)
但是要注意的是,连接数的增加会带来很多连锁反应,需要在实际中避免由此产生的负面影响。
首先我们看一下status的输出:
mysql> status
--------------
mysql Ver 14.14 Distrib 5.1.49, for pc-linux-gnu (i686) using readline 5.1Connection id: 255260
Current database: mysql
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ‘‘
Using delimiter: ;
Server version: 5.1.49-log MySQL Community Server (GPL)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /var/lib/mysql/mysql.sock
Uptime: 161 days 3 hours 42 min 38 secThreads: 14 Questions: 160655492 Slow queries: 71 Opens: 8124 Flush tables: 3 Open tables: 64 Queries per second avg: 11.538
--------------
这里有个Open tables输出时64,这就是说当前数据库打开的表的数量是64个,要注意的是这个64并不是实际的64个表,因为MySQL是多线程的系统,几个不同的并发连接可能打开同一个表,这就需要为不同的连接session分配独立的内存空间来存储这些信息以避免冲突。因此连接数的增加会导致MySQL需要的文件描述符数目的增加。另外对于MyISAM表,还会建立一个共享的索引文件描述符。
那么在MySQL数据库层面,有几个系统参数决定了可同时打开的表的数量和要使用的文件描述符,那就是table_open_cache、max_tmp_tables和open_files_limit.
mysql> show variables like ‘table_open%‘;+------------------+-------+| Variable_name | Value |+------------------+-------+| table_open_cache | 64 |+------------------+-------+1 row in set (0.00 sec)
mysql> show status like ‘open%‘;+--------------------------+-------+| Variable_name | Value |+--------------------------+-------+| Open_files | 3 || Open_streams | 0 || Open_table_definitions | 8 || Open_tables | 8 || Opened_files | 91768 || Opened_table_definitions | 0 || Opened_tables | 0 |+--------------------------+-------+7 rows in set (0.00 sec)
mysql> show global status like ‘open%‘;+--------------------------+-------+| Variable_name | Value |+--------------------------+-------+| Open_files | 3 || Open_streams | 0 || Open_table_definitions | 10 || Open_tables | 11 || Opened_files | 91791 || Opened_table_definitions | 1211 || Opened_tables | 8158 |+--------------------------+-------+7 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> show global status like ‘%tmp%table%‘;+-------------------------+-------+| Variable_name | Value |+-------------------------+-------+| Created_tmp_disk_tables | 10478 || Created_tmp_tables | 25860 |+-------------------------+-------+2 rows in set (0.00 sec)
继续原来的讨论,增加table_open_cache或max_tmp_tables 参数的大小后,从操作系统的角度看,mysqld进程需要使用的文件描述符的个数就要相应的增加,这个是由open_files_limit参数控制的。但是这个参数是OS限制的,所以我们设定的值并不一定总是生效。如果OS限制MySQL不能修改这个值,那么置为0。如果是专用的MySQL服务器上,这个值一般要设置的尽量大,就是没有报Too many open files错误的最大值,这样就能一劳永逸了。当操作系统无法分配足够的文件描述符的时候,mysqld进程会在错误日志里记录警告信息。
mysql> show variables like ‘open_files%‘;
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| open_files_limit | 1024 |
+------------------+-------+
1 row in set (0.00 sec)
mysql> show global status like ‘%open%file%‘;+---------------+-------+| Variable_name | Value |+---------------+-------+| Open_files | 3 || Opened_files | 91799 |+---------------+-------+2 rows in set (0.01 sec)
mysql> show status like ‘%thread%‘;
+------------------------+--------+
| Variable_name | Value |
+------------------------+--------+
| Delayed_insert_threads | 0 |
| Slow_launch_threads | 0 |
| Threads_cached | 0 |
| Threads_connected | 14 |
| Threads_created | 255570 |
| Threads_running | 2 |
+------------------------+--------+
6 rows in set (0.00 sec)
比较这个threads_connected参数和前面提到的max_connections参数,也可以作为目前的系统负载的参照,决定是否需要修改连接数。
如果查看每个thread的更详细的信息,可以使用processlist命令:
mysql> show processlist;
+--------+-----------+--------------------+----------+-------------+----------+----------------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+--------+-----------+--------------------+----------+-------------+----------+----------------------------------------------------------------+------------------+
| 8293 | repl | 192.168.0.33:47208 | NULL | Binlog Dump | 11574424 | Has sent all binlog to slave; waiting for binlog to be updated | NULL |
| 140991 | mogile | 192.168.0.33:41714 | mogilefs | Sleep | 0 | | NULL |
| 140992 | mogile | 192.168.0.33:41715 | mogilefs | Sleep | 3 | | NULL |
| 140993 | mogile | 192.168.0.33:41722 | mogilefs | Sleep | 2 | | NULL |
| 140994 | mogile | 192.168.0.33:41723 | mogilefs | Sleep | 1 | | NULL |
| 140995 | mogile | 192.168.0.33:41724 | mogilefs | Sleep | 3 | | NULL |
| 254914 | mogile | 192.168.0.33:43028 | mogilefs | Sleep | 11074 | | NULL |
| 254915 | mogile | 192.168.0.33:43032 | mogilefs | Sleep | 11091 | | NULL |
| 255144 | mogile | 192.168.0.33:47514 | mogilefs | Sleep | 11090 | | NULL |
| 255157 | mogile | 192.168.0.33:47535 | mogilefs | Sleep | 11087 | | NULL |
| 255162 | mogile | 192.168.0.33:47549 | mogilefs | Sleep | 11074 | | NULL |
| 255260 | root | localhost | mysql | Query | 0 | NULL | show processlist |
| 255352 | maopaodev | 192.168.0.78:55399 | maopaodb | Sleep | 3172 | | NULL |
| 255353 | maopaodev | 192.168.0.78:55400 | NULL | Sleep | 8926 | | NULL |
+--------+-----------+--------------------+----------+-------------+----------+----------------------------------------------------------------+------------------+
14 rows in set (0.00 sec)
执行这个命令需要有Process_priv权限,具体的权限分配信息可以查看mysql.user表。
对于影响系统运行的thread,可以狠一点,用kill connection|query threadid的命令杀死它。