{"id":15,"date":"2013-07-08T00:25:39","date_gmt":"2013-07-07T23:25:39","guid":{"rendered":"http:\/\/188.166.119.205\/?p=15"},"modified":"2026-11-10T05:52:05","modified_gmt":"2026-11-10T05:52:05","slug":"mysql-connections-listing","status":"publish","type":"post","link":"https:\/\/banyek.net\/?p=15&lang=en","title":{"rendered":"MySQL connections listing"},"content":{"rendered":"<p>Currently, at Kinja we are in a middle of big architectural change on database servers, so I have run into a problem regarding this. Sometimes I have to check current connections on database servers, to see what schemas are in use, what servers using a given DB server, or even which users are connected to the database server.<\/p>\n<p>Previously when I had to determine connected hosts, I just used a one-liner script in bash, what parse through the output of netstat and listed the number of connections from given servers like this:<\/p>\n<pre><code>[root@sql-slave1 banyek]# netstat | grep mysql | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c\n      1 app01.bfc.kinja-ops.c\n     83 app05.bfc.kinja-ops.c\n     84 app09.bfc.kinja-ops.c\n      9 dbcacti.bfc.kinja-ops\n      1 nagios.bfc.kinja-ops.\n      1 sql-master1.bfc.kinja\n<\/code><\/pre>\n<p>This was enough to quickly see the connected hosts, but the output wasn\u2019t too chatty, and there are a lot of information which were hidden.<\/p>\n<p><!--more--><\/p>\n<p>If you want to monitor connections to given server you can use in mysql prompt a \u2018SHOW PROCESSLIST\u2019 or a \u2018SHOW FULL PROCESSLIST\u2019 command, this prints all the data what you needed, but it can provide too many information, but can be really annoying if you are only curious to certain information.<\/p>\n<p>Lucky thing, that in Percona MySQL server there is a schema named \u2018INFORMATION_SCHEMA\u2019 in your database, where you can query connection information via SQL, so the output can be exactly what you needed.<\/p>\n<p>In fact, it is not really easy to use when you need a quick check, or even when you have to repeatedly check the information. For example, if you need all connection information about hostnames, connection counts, users and schemas, you can use the following query:<\/p>\n<pre><code>SELECT\n        COUNT(*) AS conn_count,\n        SUBSTRING_INDEX(host,':',1) AS ip,\n        user,\n        db\n    FROM\n        INFORMATION_SCHEMA.PROCESSLIST\n    GROUP BY\n        ip,\n        user,\n        db\n    ORDER BY\n        conn_count, ip\n    DESC\n<\/code><\/pre>\n<p>Pretty, huh?<\/p>\n<p>So, I\u2019ve decided to ease my work and wrote a script called <a href=\"https:\/\/github.com\/banyek\/Utilities\/blob\/master\/checkconn.pl\">checkconn.pl<\/a>.<\/p>\n<p>This script can list the active connections to given database server, and you can filter the output for hosts, schemas, users only if you need only a smaller subset of given data.<\/p>\n<pre><code>[root@sql-slave2 banyek]# \/usr\/local\/bin\/checkconn.pl -h\nUsage: .\/checkconnections.pl [ -h ] [ -u user ] [ -p password ] [ -s ] [ -c ] [ -r ] [ -d ]\n  if none of -s -c -d are given, script provides a full connection list\n -h                 this help screen\n -u username        username with connect to mysql. Defaults 'root'\n -p password        password with connect to mysql. If not provided,asks for it.\n -s                 show connections by server\n -c                 show connections by users\n -d                 show connections by database\n<\/code><\/pre>\n<p>Example outputs:<\/p>\n<pre><code>[root@sql-slave2 banyek]# \/usr\/local\/bin\/checkconn.pl -s\nConnections                  Hostname\n=====================================\n          81  app06.bfc.kinja-ops.com\n          74  app10.bfc.kinja-ops.com\n           2\n           1                localhost\n           1 nagios.bfc.kinja-ops.com\n           1  app02.bfc.kinja-ops.com\n\n[root@sql-slave2 banyek]# \/usr\/local\/bin\/checkconn.pl -d\nConnections             Database\n=====================================\n         120                    kinja\n          25                      sso\n           8                 hyperion\n           3\n           3                    chomp\n           1       information_schema\n\n[root@sql-slave2 banyek]# \/usr\/local\/bin\/checkconn.pl -c\nConnections                  Username\n=====================================\n         120                    kinja\n          25                  ssoUser\n           8                   webro2\n           3                 chomp_ro\n           2              system user\n           1                     root\n           1                 newrelic\n\n[root@sql-slave2 banyek]# \/usr\/local\/bin\/checkconn.pl\nConnections                  Hostname            Username            Database\n=============================================================================\n           1                localhost                root  information_schema\n           1 nagios.bfc.kinja-ops.com            newrelic\n           1  app10.bfc.kinja-ops.com            chomp_ro               chomp\n           1  app06.bfc.kinja-ops.com            chomp_ro               chomp\n           1  app02.bfc.kinja-ops.com            chomp_ro               chomp\n           2                                  system user\n           4  app10.bfc.kinja-ops.com              webro2            hyperion\n           4  app06.bfc.kinja-ops.com              webro2            hyperion\n          10  app10.bfc.kinja-ops.com             ssoUser                 sso\n          15  app06.bfc.kinja-ops.com             ssoUser                 sso\n          59  app10.bfc.kinja-ops.com               kinja               kinja\n          61  app06.bfc.kinja-ops.com               kinja               kinja\n\n[root@sql-slave2 banyek]#\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Currently, at Kinja we are in a middle of big architectural change on database servers, so I have run into a problem regarding this. Sometimes I have to check current connections on database servers, to see what schemas are in use, what servers using a given DB server, or even which users are connected to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[134],"tags":[144,126,234],"class_list":["post-15","post","type-post","status-publish","format-standard","hentry","category-tech-en","tag-mysql-en","tag-oldblog-en","tag-perl-en"],"_links":{"self":[{"href":"https:\/\/banyek.net\/index.php?rest_route=\/wp\/v2\/posts\/15","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/banyek.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/banyek.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/banyek.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/banyek.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=15"}],"version-history":[{"count":1,"href":"https:\/\/banyek.net\/index.php?rest_route=\/wp\/v2\/posts\/15\/revisions"}],"predecessor-version":[{"id":447,"href":"https:\/\/banyek.net\/index.php?rest_route=\/wp\/v2\/posts\/15\/revisions\/447"}],"wp:attachment":[{"href":"https:\/\/banyek.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=15"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/banyek.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=15"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/banyek.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=15"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}