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

吾爱主题 阅读:85 2024-04-05 14:00:31 评论:0

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

mysql重启命令:

?
1 /etc/init.d/mysql restart

MySQL服务器的线程数需要在一个合理的范围之内,这样才能保证MySQL服务器健康平稳地运行。Threads_created表示创建过的线程数,通过查看Threads_created就可以查看MySQL服务器的进程状态。

?
1 2 3 4 5 6 7 8 9 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配置:

?
1 2 3 4 5 6 mysql> show variables like 'thread_cache_size' ; + -------------------+-------+ | Variable_name | Value | + -------------------+-------+ | thread_cache_size | 64 | + -------------------+-------+

示例中的服务器还是挺健康的。

解析MySQL与连接数相关的几个参数

MySQL的variables和status是管理维护的利器,就类似Oracle的spfile和v$表。

MySQL通过系统变量记录很多配置信息,比如最大连接数max_connections:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 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范围内修改这个参数:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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的输出:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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.

?
1 2 3 4 5 6 7 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命令,另外可以直接查询这个系统变量的值:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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 参数:

?
1 2 3 4 5 6 7 mysql> show variables like 'max_tmp%' ; + ----------------+-------+ | Variable_name | Value | + ----------------+-------+ | max_tmp_tables | 32  | + ----------------+-------+ 1 row in set (0.00 sec)

这个参数指定的是单个客户端连接能打开的临时表数目。查看当前已经打开的临时表信息:

?
1 2 3 4 5 6 7 8 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进程会在错误日志里记录警告信息。

?
1 2 3 4 5 6 7 8 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)

对应的,有两个状态变量记录了当前和历史的文件打开信息:

?
1 2 3 4 5 6 7 8 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参数查看当前分配的线程数量:

?
1 2 3 4 5 6 7 8 9 10 11 12 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命令:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 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数据库计有所帮助。

原文链接:http://www.cnblogs.com/mr-amazing/p/3842454.html

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容