DBA Diaries

Thoughts and experiences of a DBA working with SQL Server and MySQL

How to Kill All MySQL Processes For a Specific User

Posted on June 17, 2014 Written by Andy Hayes Leave a Comment

So here is a scenario, you have a number of poorly performing MySQL database queries which are consuming resources on your server. Users are complaining and you need to do something fast. Having viewed the output of SHOW FULL PROCESSLIST, you can see that they are coming from one user and have been running for a long time.

You need a way to kill all MySQL processes and quickly! Running kill (process identifier) for each connection manually will not cut the mustard. It will take too long and you need to fix this problem now!

So having identified the user where these connections are coming from, thankfully with the help of a little bit of SQL and the information_schema.processes table, you can kill all MySQL connections in one fast sweep and alleviate the load on your database server. Here’s how:

Use the information_schema.processlist table to identify the problem threads

An incredibly useful table, it will show you all the processes against your MySQL instance enabling you to identify queries at a user and database level and importantly, what query they are executing (INFO column) and for how long (TIME column).

Run this:

SELECT * FROM processlist\G;
*************************** 1. row ***************************
     ID: 1
   USER: root
   HOST: localhost:60049
     DB: information_schema
COMMAND: Query
   TIME: 0
  STATE: executing
   INFO: select * from processlist

This output is for demonstration purposes but lets imagine I had many of these queries in the list.

Use CONCAT to kill many MySQL processes

SELECT CONCAT('KILL ',id,';') AS run_this FROM information_schema.processlist WHERE user='root' AND info = 'SELECT * FROM processlist';

The resulting output would like this

+------------------------+
| run_this               |
+------------------------+
| KILL 1;                |
| KILL 2;                |
| KILL 3;                |
| KILL 4;                |
+------------------------+

You now have a list of the threads you want to kill off. What is the best way to now execute this? A couple of options spring to mind. I’m sure there are other ways to do this too.

  1. You could copy and paste this list into a text editor, remove the pipe and plus symbols from the output and then execute the list against MySQL.
  2. You could also add an extra step to the CONCAT query which copies the statements to a file and then run that file back through MySQL.

Lets look at option 2. You will write a file to a location which MySQL has permission to read and write from.

SELECT CONCAT('KILL ',id,';') AS run_this FROM information_schema.processlist WHERE user='root' AND info = 'SELECT * FROM processlist' INTO OUTFILE '/tmp/kill_process.txt';

Edit the file and remove the top line that has been created by the output as it will be an invalid SQL statement. In my example it’s “run_this” as the column header.

You can now run this file into MySQL which will execute all the statements within to kill all MySQL processes as identified in your export.

SOURCE /tmp/kill_process.txt

Or you can import it to kill multiple MySQL queries at once.

 mysql -uroot -p{yourpassword} < /tmp/kill_process.txt

Filed Under: All Articles, MySQL Administration Tagged With: mysql

MySQL SHOW USERS? – How to List All MySQL Users and Privileges

Posted on June 16, 2014 Written by Andy Hayes 1 Comment

mysql show usersThere are a number of useful SHOW commands in MySQL. Sadly, there isn’t one for mysql SHOW USERS nor is there one for mysql LIST USERS. This is a little inconsistent when you consider that there are other commands such as SHOW DATABASES, SHOW VARIABLES, SHOW TABLES, SHOW GRANTS and others.

However there are ways to do it. You can get back a list of MySQL user accounts by querying the mysql.users table and if you want to find out which privileges each MySQL user has, then this is possible as well.

So let us take a look at what is possible….

Querying mysql.users

You can get back a list of users by running a simple select statement against the mysql.user table.

On my test system, this returns 3 records. As you can see, two of them are for user root.

SELECT * FROM mysql.user;
+------+
| user |
+------+
| andy |
| root |
| root |
+------+
3 rows in set (0.00 sec) 

This is because root can connect from multiple hosts.

SELECT user, host FROM mysql.user;
+------+-----------+
| user | host      |
+------+-----------+
| andy | {3a76cbff1b5ce5ace3bd34bbdd68f71285d81fb9af60114c7a6db80d3a8688de}         |
| root | {3a76cbff1b5ce5ace3bd34bbdd68f71285d81fb9af60114c7a6db80d3a8688de}         |
| root | localhost |
+------+-----------+
3 rows in set (0.00 sec)

Show privileges for a specific MySQL user using SHOW GRANTS

In order to do this, you can use SHOW GRANTS

SHOW GRANTS FOR 'root'@'localhost';
+------------------------------------------------------+
| Grants for [email protected]                            |
+------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost'....|
+------------------------------------------------------+
1 row in set (0.00 sec)

Show user privileges for all MySQL users using SHOW GRANTS

You first have to build up a list of SHOW GRANTS statements for each user in your mysql.users table.

SELECT CONCAT('SHOW GRANTS FOR ''',user,'''@''',host,''';') FROM mysql.user;

This will give you the following output.

+------------------------------------------------------+
| CONCAT('SHOW GRANTS FOR ''',user,'''@''',host,''';') |
+------------------------------------------------------+
| SHOW GRANTS FOR 'andy'@'{3a76cbff1b5ce5ace3bd34bbdd68f71285d81fb9af60114c7a6db80d3a8688de}';                          |
| SHOW GRANTS FOR 'root'@'{3a76cbff1b5ce5ace3bd34bbdd68f71285d81fb9af60114c7a6db80d3a8688de}';                          |
| SHOW GRANTS FOR 'root'@'localhost';                  |
+------------------------------------------------------+
3 rows in set (0.00 sec)

You could copy and paste each statement and execute each line to get a list. That is a bit fiddly and time consuming. One way of achieving this more quickly would be to output the results to a file and then read the file back in line by line.

You can use OUTFILE and SOURCE to do this.

SELECT CONCAT('SHOW GRANTS FOR ''',user,'''@''',host,''';') FROM mysql.user INTO outfile '/tmp/show_grants.txt';

Now read the file you have just produced

SOURCE /tmp/show_grants.txt

Produces the following output.

+--------------------------------------+
| Grants for [email protected]{3a76cbff1b5ce5ace3bd34bbdd68f71285d81fb9af60114c7a6db80d3a8688de}                    |
+--------------------------------------+
| GRANT USAGE ON *.* TO 'andy'@'{3a76cbff1b5ce5ace3bd34bbdd68f71285d81fb9af60114c7a6db80d3a8688de}' ....|
+--------------------------------------+
1 row in set (0.00 sec)

+--------------------------------------+
| Grants for [email protected]{3a76cbff1b5ce5ace3bd34bbdd68f71285d81fb9af60114c7a6db80d3a8688de}                    |
+--------------------------------------+
| GRANT USAGE ON *.* TO 'root'@'{3a76cbff1b5ce5ace3bd34bbdd68f71285d81fb9af60114c7a6db80d3a8688de}' ....|
+--------------------------------------+
1 row in set (0.00 sec)

+-------------------------------------------------------+
| Grants for [email protected]                             |
+-------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' ....|
+-------------------------------------------------------+
1 row in set (0.00 sec)

Using information_schema.user_privileges

Another way to list privileges for users would be to query user_privileges in information_schema

SELECT * FROM information_schema.user_privileges;
+--------------------+---------------+-------------------------+--------------+
| GRANTEE            | TABLE_CATALOG | PRIVILEGE_TYPE          | IS_GRANTABLE |
+--------------------+---------------+-------------------------+--------------+
| 'root'@'localhost' | NULL          | SELECT                  | YES          |
| 'root'@'localhost' | NULL          | INSERT                  | YES          |
| 'root'@'localhost' | NULL          | UPDATE                  | YES          |
| 'root'@'localhost' | NULL          | DELETE                  | YES          |
| 'root'@'localhost' | NULL          | CREATE                  | YES          |
| 'root'@'localhost' | NULL          | DROP                    | YES          |
| 'root'@'localhost' | NULL          | RELOAD                  | YES          |
| 'root'@'localhost' | NULL          | SHUTDOWN                | YES          |
| 'root'@'localhost' | NULL          | PROCESS                 | YES          |
| 'root'@'localhost' | NULL          | FILE                    | YES          |
| 'root'@'localhost' | NULL          | REFERENCES              | YES          |
| 'root'@'localhost' | NULL          | INDEX                   | YES          |
| 'root'@'localhost' | NULL          | ALTER                   | YES          |
| 'root'@'localhost' | NULL          | SHOW DATABASES          | YES          |
| 'root'@'localhost' | NULL          | SUPER                   | YES          |
| 'root'@'localhost' | NULL          | CREATE TEMPORARY TABLES | YES          |
| 'root'@'localhost' | NULL          | LOCK TABLES             | YES          |
| 'root'@'localhost' | NULL          | EXECUTE                 | YES          |
| 'root'@'localhost' | NULL          | REPLICATION SLAVE       | YES          |
| 'root'@'localhost' | NULL          | REPLICATION CLIENT      | YES          |
| 'root'@'localhost' | NULL          | CREATE VIEW             | YES          |
| 'root'@'localhost' | NULL          | SHOW VIEW               | YES          |
| 'root'@'localhost' | NULL          | CREATE ROUTINE          | YES          |
| 'root'@'localhost' | NULL          | ALTER ROUTINE           | YES          |
| 'root'@'localhost' | NULL          | CREATE USER             | YES          |
| 'root'@'localhost' | NULL          | EVENT                   | YES          |
| 'root'@'localhost' | NULL          | TRIGGER                 | YES          |
| 'root'@'{3a76cbff1b5ce5ace3bd34bbdd68f71285d81fb9af60114c7a6db80d3a8688de}'         | NULL          | USAGE                   | NO           |
| 'andy'@'{3a76cbff1b5ce5ace3bd34bbdd68f71285d81fb9af60114c7a6db80d3a8688de}'         | NULL          | USAGE                   | NO           |
+--------------------+---------------+-------------------------+--------------+
29 rows in set (0.00 sec)

Filed Under: All Articles, MySQL Administration Tagged With: mysql

List Tables for a Specific Database Using MySQL SHOW TABLES

Posted on June 15, 2014 Written by Andy Hayes Leave a Comment

This post demonstrates how to list all tables for a specific MySQL database. I will use SHOW TABLES for this.

First, login as user root to your MySQL instance.

mysql -uroot -pYourPassword

Now change to the database you want to show tables for.

USE mysql;

Database changed

Now you can produce the list.

Run this:

SHOW TABLES;

+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| servers                   |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
23 rows in set (0.14 sec)

As with SHOW DATABASES, you can look for specific tables or tables containing specific text in their names.

SHOW TABLES LIKE 'slow_log';

+----------------------------+
| Tables_in_mysql (slow_log) |
+----------------------------+
| slow_log                   |
+----------------------------+
1 row in set (0.00 sec)

With wildcards…

SHOW TABLES LIKE 'time{3a76cbff1b5ce5ace3bd34bbdd68f71285d81fb9af60114c7a6db80d3a8688de}';

+---------------------------+
| Tables_in_mysql (time{3a76cbff1b5ce5ace3bd34bbdd68f71285d81fb9af60114c7a6db80d3a8688de})   |
+---------------------------+
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
+---------------------------+
5 rows in set (0.00 sec)

Filed Under: All Articles, MySQL Administration Tagged With: mysql

How to List MySQL Databases Using SHOW DATABASES

Posted on June 14, 2014 Written by Andy Hayes Leave a Comment

Want a list of MySQL databases? In this quick guide, I demonstrate how to show a list of databases in MySQL using SHOW DATABASES.

First, login as user root to your MySQL instance.

mysql -uroot -pYourPassword

Now that you are logged in, you simply need to run the SHOW DATABASES command.

SHOW DATABASES;

Produces the following list.

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+

As I am logged in as root, I can see all databases. If you run this as another user, you will only see the databases that the user has access to.

You can also look for specific databases which is useful if your server contains many databases.

SHOW DATABASES LIKE 'mysql';
+------------------+
| Database (mysql) |
+------------------+
| mysql            |
+------------------+
1 row in set (0.00 sec)

Or you can use a wildcard to look for databases containing specific text.

SHOW DATABASES LIKE 'my{3a76cbff1b5ce5ace3bd34bbdd68f71285d81fb9af60114c7a6db80d3a8688de}';
+----------------+
| Database (my{3a76cbff1b5ce5ace3bd34bbdd68f71285d81fb9af60114c7a6db80d3a8688de}) |
+----------------+
| mysql          |
+----------------+
1 row in set (0.00 sec)

How to Show MySQL databases in MySQL Workbench

There are two ways, those listed above as seen here

mysql show databases

And you can also browse a list of available MySQL schemas but this does not include databases such as mysql or performance_schema

mysql list schemas

Filed Under: All Articles, MySQL Administration Tagged With: mysql

  • « Previous Page
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • Next Page »

Categories

  • All Articles (82)
  • Career Development (8)
  • MySQL Administration (18)
  • MySQL Performance (2)
  • SQL Server Administration (24)
  • SQL Server News (3)
  • SQL Server Performance (14)
  • SQL Server Security (3)
  • SQL Tips and Tricks (19)

Top 10 Popular Posts

  • Using sp_change_users_login to fix SQL Server orphaned users
  • How to shrink tempdb
  • MySQL SHOW USERS? – How to List All MySQL Users and Privileges
  • How to Transfer Logins to Another SQL Server or Instance
  • How to Delete Millions of Rows using T-SQL with Reduced Impact
  • T-SQL – How to Select Top N Rows for Each Group Using ROW_NUMBER()
  • New T-SQL features in SQL Server 2012 – OFFSET and FETCH
  • How to Kill All MySQL Processes For a Specific User
  • Using exec sp_who2 to help with SQL Server troubleshooting
  • How to move tempdb

Recent Posts

  • How to Setup MySQL Master Master Replication
  • How To Use SQL to Convert a STRING to an INT
  • How to set up MySQL Replication Tutorial
  • How to Use SQL CASE for Conditional Logic in Your SQL Queries
  • Using ISNULL in SQL Server to Replace NULL Values

Search

Connect

  • Twitter
  • Facebook
  • Google+
  • RSS

About

  • Cookie Policy
  • Disclaimer
  • About
Copyright © ‘2021’ DBA Diaries built on the Genesis Framework

This site uses cookies. We assume you are happy with cookies but click the link if you are not. Close