如何实时查看mysql当前连接数?

一、命令mysqladmin

1、查看当前所有连接的详细:

mysqladmin -uroot -p -h127.0.0.1 processlist

2、只查看当前连接数(Threads就是连接数.):

mysqladmin  -uroot -p -h127.0.0.1 status

3、查看当前所有连接的详细资料:

[zabbix@test160 bin]$ mysqladmin -uroot -p123456 processlist
Warning: Using a password on the command line interface can be insecure.
+--------+--------+-----------+--------+---------+------+-------+------------------+
| Id     | User   | Host      | db     | Command | Time | State | Info             |
+--------+--------+-----------+--------+---------+------+-------+------------------+
| 3      | zabbix | localhost | zabbix | Sleep   | 73   |       |                  |
| 8      | zabbix | localhost | zabbix | Sleep   | 1    |       |                  |
| 9      | zabbix | localhost | zabbix | Sleep   | 15   |       |                  |
| 10     | zabbix | localhost | zabbix | Sleep   | 2    |       |                  |
| 11     | zabbix | localhost | zabbix | Sleep   | 1    |       |                  |
| 12     | zabbix | localhost | zabbix | Sleep   | 16   |       |                  |
| 14     | zabbix | localhost | zabbix | Sleep   | 18   |       |                  |

4、只查看当前连接数(Threads就是连接数.):

[zabbix@test160 bin]$ mysqladmin -uroot -p123456 status 

Uptime: 604807  Threads: 22  Questions: 9091506  Slow queries: 0  Opens: 240  Flush tables: 1  Open tables: 233  Queries per second avg: 15.032

二、 Mysql命令

1、show status  查看所有状态参数

mysql> show status like '%connect%';
+-----------------------------------------------+--------+
| Variable_name                                 | Value  |
+-----------------------------------------------+--------+
| Aborted_connects                              | 7      |
| Connection_errors_accept                      | 0      |
| Connection_errors_internal                    | 0      |
| Connection_errors_max_connections             | 0      |
| Connection_errors_peer_address                | 0      |
| Connection_errors_select                      | 0      |
| Connection_errors_tcpwrap                     | 0      |
| Connections                                   | 181632 |
| Max_used_connections                          | 29     |
| Performance_schema_session_connect_attrs_lost | 0      |
| Ssl_client_connects                           | 0      |
| Ssl_connect_renegotiates                      | 0      |
| Ssl_finished_connects                         | 0      |
| Threads_connected                             | 22     |
+-----------------------------------------------+--------+
14 rows in set (0.00 sec)

Threads_connected 当前的连接数 ,也就是我们经常使用show processlist看见那个数值

Connections 试图连接到(不管是否成功)

MYSQL服务器的连接总数

Max_used_connections 服务器启动后已经同时使用过的连接最大数量(并发)

Aborted_connects 

2、show processlist  

显示当前正在执行的mysql连接

mysql> show processlist;  #通常只会显示100条
或者用
show full processlist;#显示全部

看一下所有连接进程,注意查看进程等待时间以及所处状态 是否locked 如果进程过多,就把进程打印下来,

然后查看

mysql -e 'show full processlist;' > 111 #在SQLyog打开更加方便查看

找非locked的进程,一般就是当前执行中卡死,导致后面的进程排队的原因。

三、修改mysql最大连接数

打开my.ini,

修改max_connections=100(默认为100)。

max_connections = 1000 保存,

重启MySQL 如果我们想查看这台服务器设置。

#vi /etc/my.cnf

set-variable=max_user_connections=30 这个就是单用户的连接数

set-variable=max_connections=800 这个是全局的限制连接数

四、总结

此次查询只需要掌握mysql中show status,show process list命令,以及命令mysqladmin

posted @ 2018-03-12 10:26  Gringer  阅读(682)  评论(0)    收藏  举报