MYSQL Hacker Notes

See the Ruby file that’s making that SQL query

Assuming you’re using the mysql or mysql2 gem.

First install this gem: https://github.com/tobyhs/sql_tagger

Open mysql

$ mysql

Show the general log variable and location of the general log file:

mysql> SHOW VARIABLES LIKE ‘%general%’;
+------------------+--------------------------------------+
| Variable_name | Value |
+------------------+--------------------------------------+
| general_log | OFF |
| general_log_file | /usr/local/var/mysql/MonkeyMan.log |
+------------------+--------------------------------------+

Flip on the log:

SET global general_log = 1;

Tail the log

$ tail -f /usr/local/var/mysql/MonkeyMan.log

Now you can see the SQL query as well as the line of code that made that query.

/* app/controllers/users_controller.rb:5:in */ SELECT * FROM users;

See the running threads in your MySQL server

Use this if you get a “too many connections” error message. Also let’s you see the hosts making connections to your mysql server. You can read more about it here.

mysql> SHOW FULL processlist\G;
*************************** 1. row ***************************
Id: 92
User: root
Host: localhost
db: awesome_app_development
Command: Sleep
Time: 67536
State:
Info: NULL
*************************** 2. row ***************************
Id: 99
User: root
Host: hackerland.org
db: NULL
Command: Query
Time: 35343
State:
Info: