305

When I run show status like 'Con%' it shows the number of connections, which is 9972 and constantly growing. Is this an active number of connections or connections made in total?

DanM7
  • 2,203
  • 3
  • 28
  • 46
Michael
  • 3,449
  • 4
  • 19
  • 14

9 Answers9

570

According to the docs, it means the total number throughout history:

Connections

The number of connection attempts (successful or not) to the MySQL server.

You can see the number of active connections either through the Threads_connected status variable:

Threads_connected

The number of currently open connections.

mysql> show status where `variable_name` = 'Threads_connected';
+-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| Threads_connected | 4     |
+-------------------+-------+
1 row in set (0.00 sec)

... or through the show processlist command:

mysql> show processlist;
+----+------+-----------------+--------+---------+------+-------+------------------+
| Id | User | Host            | db     | Command | Time | State | Info             |
+----+------+-----------------+--------+---------+------+-------+------------------+
|  3 | root | localhost       | webapp | Query   |    0 | NULL  | show processlist | 
|  5 | root | localhost:61704 | webapp | Sleep   |  208 |       | NULL             | 
|  6 | root | localhost:61705 | webapp | Sleep   |  208 |       | NULL             | 
|  7 | root | localhost:61706 | webapp | Sleep   |  208 |       | NULL             | 
+----+------+-----------------+--------+---------+------+-------+------------------+
4 rows in set (0.00 sec)
Community
  • 1
  • 1
kiiwii
  • 7,593
  • 2
  • 17
  • 9
  • Does a connection represent a user? So if there are 200 users on a page that makes database queries will be 200 connections? – Diego Queiroz Nov 21 '19 at 12:05
  • Yes, can someone clarify it. I too have same question. Also, does these variables reset after every MySQL Services restart or after Server reboot ? – LonelyRogue Jan 24 '20 at 18:18
  • @Diego Queiroz It's not that simple. If the developer both paid close attention and if there's only 1 database that needs to be connected to per user, then yes, each of these will be a user. But it's possible there are other utilities running or backup services that will also need to connect. – Ryan Shillington Jun 02 '20 at 20:50
  • @LonelyRogue Yes, after the server restarts, every connection needs to reconnect. Many programs/libraries will auto-connect so the # of connections might fill up quickly without explicitly doing anything. But MySQL won't keep any old connections after a restart. – Ryan Shillington Jun 02 '20 at 20:52
  • mysql -e "SHOW STATUS WHERE `variable_name` = 'Threads_connected';" -bash: variable_name: command not found ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '= 'Threads_connected'' at line 1 – xpredo Sep 09 '20 at 14:29
160
SHOW STATUS WHERE `variable_name` = 'Threads_connected';

This will show you all the open connections.

mistahenry
  • 8,554
  • 3
  • 27
  • 38
  • mysql -e "SHOW STATUS WHERE `variable_name` = 'Threads_connected';" -bash: variable_name: command not found ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '= 'Threads_connected'' at line 1 – xpredo Sep 09 '20 at 14:29
33

You can also do

SHOW STATUS WHERE `variable_name` = 'Max_used_connections';
SherylHohman
  • 16,580
  • 17
  • 88
  • 94
saurabh
  • 2,463
  • 1
  • 18
  • 8
  • 1
    `max_used_connections` is also useful – coolnodje Apr 25 '17 at 11:58
  • Shouldn't max_user_connections [refer](https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_max_user_connections) to the maximum number of user allowed to connect to the database at the same time? This reports the fixed variable of mysql, not an actual operative value. – Fed C Sep 28 '22 at 10:30
27

This is the total number of connections to the server till now. To find current conection status you can use

mysqladmin -u -p extended-status | grep -wi 'threads_connected\|threads_running' | awk '{ print $2,$4}'

This will show you:

Threads_connected 12

Threads_running 1  

Threads_connected: Number of connections

Threads_running: connections currently running some sql
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
Pranab Sharma
  • 729
  • 10
  • 12
20

To see a more complete list you can run:

show session status;

or

show global status;

See this link to better understand the usage.

If you want to know details about the database you can run:

status;
Mugur 'Bud' Chirica
  • 4,246
  • 1
  • 31
  • 34
9

In order to check the maximum allowed connections, you can run the following query:

SHOW VARIABLES LIKE "max_connections";

To check the number of active connections, you can run the following query:

SHOW VARIABLES LIKE "max_used_connections";

Hope it helps.

Mujtaba
  • 143
  • 1
  • 8
  • 4
    `SHOW VARIABLES LIKE "max_used_connections";` returned `Empty set (0.00 sec)` but SHOW STATUS WHERE `variable_name` = 'Max_used_connections'; with "`" around "variable_name" worked – Markus Jul 08 '20 at 16:24
  • `SHOW STATUS WHERE variable_name = "max_used_connections";` – Cyril N. Jul 05 '21 at 14:29
  • 2
    max_used_connections shows the maximum number of connections used since the database started not the current active connections. Source: https://dev.mysql.com/doc/refman/8.0/en/server-status-variables.html#statvar_Max_used_connections – Marcelo Lacerda Sep 20 '21 at 11:11
2

As per doc http://dev.mysql.com/doc/refman/5.0/en/server-status-variables.html#statvar_Connections

Connections

The number of connection attempts (successful or not) to the MySQL server.

Harsh Gupta
  • 197
  • 6
0

A GUI solutions of MySQL queries:

If someone wants to show all active connection without any MySQL code, then you can use Session Manager in dbForge Studio:

Simply, open Database > Session Manager in the upper menu, and you'll see all active connections:

It will do the same thing that is done by these MySQL commands:

show full processlist;
SELECT * FROM information_schema.PROCESSLIST p;
SELECT * FROM performance_schema.threads t;
j.developer
  • 474
  • 2
  • 5
  • 15
-3

It should be the current number of active connections. Run the command processlist to make sure.

URL for reference: http://www.devdaily.com/blog/post/mysql/how-show-open-database-connections-mysql

EDIT: Number of DB connections opened Please take a look here, the actual number of threads (connections) are described here!

Community
  • 1
  • 1
ChrisH
  • 1,283
  • 1
  • 9
  • 22
  • 2
    I've found the reference links useful. For example, `show status like '%onn%';` is a very useful query. – viddik13 Oct 09 '16 at 21:37
  • 2
    This answer is wrong and should be deleted. See the other answers. – dr_ Nov 14 '16 at 09:18
  • From https://dev.mysql.com/doc/refman/8.0/en/server-status-variables.html#statvar_Connections: "The number of connection attempts (successful or not) to the MySQL server". It is NOT the number of active connections. – PYB Nov 14 '19 at 14:47
  • All sorts of connect stats, Aborted_connects, Connections are cumulative. Max_used_connections is the high water mark, Threads_connected is current, but show processlist gives id values as well as what is going on. – mckenzm Jan 27 '22 at 01:00