当前位置: 移动技术网 > IT编程>数据库>Mysql > MySQL服务器线程数的查看方法详解

MySQL服务器线程数的查看方法详解

2018年04月09日  | 移动技术网IT编程  | 我要评论

本文实例讲述了mysql服务器线程数的查看方法。分享给大家供大家参考,具体如下:

mysql重启命令:

/etc/init.d/mysql restart

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.1
connection 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 sec
threads: 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)

这 里的table_open_cache 参数是64,这就是说所有的mysql线程一共能同时打开64个表,我们可以搜集系统的打开表的数量的历史记录和这个参数来对比,决定是否要增加这个参数 的大小。查看当前的打开表的数目的办法一个是用上边提到过的status命令,另外可以直接查询这个系统变量的值:

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)

这 里有open_tables就是当前打开表的数目,通过flush tables命令可以关闭当前打开的表。而全局范围内查看的opened_tables是个历史累计值。 这个值如果过大,并且如果没有经常的执行flush tables命令,可以考虑增加table_open_cache参数的大小。

接下来看max_tmp_tables 参数:

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)

也 可以对比这两个值来判断临时表的创建位置,一般选取blob和text列、group by 和 distinct语句的数据量超过512 bytes,或者union的时候select某列的数据超过512 bytes的时候,就直接在磁盘上创建临时表了,另外内存中的临时表变大的时候,也可能被mysql自动转移到磁盘上(由tmp_table_size和 max_heap_table_size参数决定)。

继续原来的讨论,增加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 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为每个连接分配线程来处理,可以通过threads_connected参数查看当前分配的线程数量:

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的命令杀死它。

更多关于mysql相关内容感兴趣的读者可查看本站专题:《mysql查询技巧大全》、《mysql事务操作技巧汇总》、《mysql存储过程技巧大全》、《mysql数据库锁相关技巧汇总》及《mysql常用函数大汇总

希望本文所述对大家mysql数据库计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网