MySQL如何查看服务器线程数

45次阅读
没有评论

共计 8425 个字符,预计需要花费 22 分钟才能阅读完成。

自动写代码机器人,免费开通

这篇文章主要介绍了 MySQL 如何查看服务器线程数,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让丸趣 TV 小编带着大家一起了解一下。

具体如下:

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

感谢你能够认真阅读完这篇文章,希望丸趣 TV 小编分享的“MySQL 如何查看服务器线程数”这篇文章对大家有帮助,同时也希望大家多多支持丸趣 TV,关注丸趣 TV 行业资讯频道,更多相关知识等着你来学习!

向 AI 问一下细节

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-04发表,共计8425字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)