MySQL tweaking

The commands and conf file locations below assume you're on a typical Linux system. BSD places items in /usr/local/etc/ instead of /etc and you may want to use "locate" or "whereis" just to make sure you've got the right file/program.

% netstat -n -a |grep 3306 > filename.txt
Tells you what's connected to us. Remove "> filename.txt" to just view it on screen.

% mtop
MyTop is very useful for watching abusive or rogue queries.

Hit the k key then type (or copy/paste) the process number to kill it.
Hit the h key to show connections only from one host.
Hit the u key to show connections only from one user.

Run mtop with this addition to show all idle connections too.

% mtop --idle

service mysqld restart
Restarts MySQL gracefully

Commands run within MySQL

% mysql
mysql> show processlist\g
Shows you every process list at the moment.
mysql> status
Gives you output of various details, like threads, questions, slow queries, opens, flush tables, open tables, queries per second avg.
mysql> set wait_timeout =
Set the for the number of seconds you want MySQL to wait for an inactive connection before it disconnects it. There's reason to leave it relatively high, but dropping it can free up connections during stressful periods. 300 seconds is likely high enough. It's default is 28800 seconds, far too long if you're running out of available connections.

mysql> \q
To quit MySQL

You'll want to memorize these.

Some mtop goodies you can use:

s - change the number of seconds to delay between updates
m - toggle manual refresh mode on/off
d - filter display with regular expression (user/host/db/command/state/info)
F - fold/unfold column names in select statement display
h - display process for only one host
u - display process for only one user
i - toggle all/non-Sleeping process display
o - reverse the sort order
k - kill processes; send a kill to a list of ids
e - explain a process; show query optimizer info
z - zoom in on a process; show sql statement detail
f - flush stats (reset show status variables)
t - show mysqld stats (show status/mysqladmin ext)
T - show short/important status
v - show mysqld variables (show variables/mysqladmin vars)
r - show replication status for master/slaves

% vi /etc/my.cnf
Some of the items you may want to tweak.

log = /mnt/log/mysql_general_query.log
Add a # before this line and restart MySQL if it starts running out of space on /mnt or needs more resources. Logging to this degree is very taxing.

wait_timeout = 300
If you're running out of connections, set this lower or leave it alone. It won't break anything, but makes the old sleeping connections die off.

innodb_buffer_pool_size = 12000M
You don't want to set this to more than 80% of your RAM.

interactive-timeout = 300
This shouldn't negatively effect anything. You can make it lower to limit connections. It kills off connections that are hanging on.

It's best to make changes like innodb buffers and time outs from the command line so you don't have to kill the server to make them take effect. Only the log file requires a daemon restart.

% mysql
mysql> SET GLOBAL wait_timeout=600;
Sets the global wait_timeout to whatever you need. This is seconds. Less drops connections faster.

mysql> SET GLOBAL interactive_timeout=600;
Sets the interactive_timeout to whatever you need. This is seconds. Less drops connections faster.

mysql> show processlist;
Shows all processes. Can give you insight into instant problems.

mysql> show global variables;
This tells you what the global variables are. This command less the global marker is less useful because it only tells you what the root mysql dB settings are and it's different.