This is a recurrent question made by our MySQL Support customers:
Logging all the attempts or just the failed ones is a very important task on some scenarios. Unfortunately, there are not too many audit capabilities in MySQL Community so the first option to audit MySQL’s authentication process is to get all the information we need from logs.
The first option is the General Query Log. Let’s see an example:
Enable the log:
|
1 |
general_log_file = /var/log/mysql/mysql.log<br>general_log = 1 |
User correctly authenticated:
|
1 |
121227 8:31:49 38 Connect root@localhost on <br> 38 Query select @@version_comment limit 1 |
User not correctly authenticated:
|
1 |
121227 8:32:18 39 Connect root@localhost on <br> 39 Connect Access denied for user 'root'@'localhost' (using password: YES) |
The problem of the General Query Log is that it will log everything so it can cause performance degradation and you will have to deal with very large files on high loaded servers. general_log variable is dynamic so a solution could be enabling and disabling the log just when it’s needed.
If you only care about failed attempts to login then there is another different and less problematic approach. From 5.5 it’s possible to log access denied messages to the error log.
We just need to enable log_warnings with a value greater than 1:
|
1 |
log_warnings = 2 |
Then check the error log:
|
1 |
121227 8:44:21 [Warning] Access denied for user 'root'@'localhost' (using password: YES) |
If you are using Percona Server then there is a third option to get information about our users, the User Statistics. As with the previous options we can get the number of connections and failed connections made by a particular user but not the date and time of those attempts. Besides that information we can get other statistics that can be very useful if MySQL is running on a multi-tenant environment or we need to control how resources are used.
Let’s seen an example, first we enable User Statistics in my.cnf:
5.5
|
1 |
userstat = 1 |
5.1
|
1 |
userstat_running = 1 |
Then we get the information about a particular user:
|
1 |
mysql> select * from user_statistics where user='root'G<br>*************************** 1. row ***************************<br> USER: root<br> TOTAL_CONNECTIONS: 25<br>CONCURRENT_CONNECTIONS: 0<br> CONNECTED_TIME: 464<br> BUSY_TIME: 96<br> CPU_TIME: 19<br> BYTES_RECEIVED: 62869617<br> BYTES_SENT: 14520<br> BINLOG_BYTES_WRITTEN: 0<br> ROWS_FETCHED: 783051<br> ROWS_UPDATED: 1017714<br> TABLE_ROWS_READ: 1484751<br> SELECT_COMMANDS: 14<br> UPDATE_COMMANDS: 103<br> OTHER_COMMANDS: 3556<br> COMMIT_TRANSACTIONS: 0<br> ROLLBACK_TRANSACTIONS: 0<br> DENIED_CONNECTIONS: 2<br> LOST_CONNECTIONS: 16<br> ACCESS_DENIED: 0<br> EMPTY_QUERIES: 0<br> TOTAL_SSL_CONNECTIONS: 0 |
Here we can see that root has done 25 total connections. Two denied connections (bad password) and 16 lost connections (not closed properly). Apart from that information we get the connection time, bytes received and sent, rows accessed, commands executed and so on. Very valuable information.
It is important to mention that these tables are stored in INFORMATION_SCHEMA and that means that after a mysqld restart all the information will be lost. So if you really need that information you should copy it to another table or export to a csv for further analysis.
We don’t have too many audit capabilities in MySQL Community so logging all events and then filter them with custom-made scripts is the best solution we have nowadays. If you are using Percona Server you can get more detailed information about what a particular user is doing. All options can be combined to meet your needs.
Resources
RELATED POSTS