Optimizing MySQL For VPS / Dedicated Server
Tuning Server Parapeters
MySQL is an open source database software based on the SQL vocabulary which can be employed in combination with most server-side languages, but which is most commonly employed with PHP. MySQL has become the de facto standard for high-traffic web sites because of its high performance, stability and ease of use.
You can Tune your MySQL server parapeters to try to optimize your configuration. As a reminder, you can check the current values of server parameters with this:
You can see the effects of your server configuration by looking at the output of this:
There's a number of performance tuning scripts that will analyze your server settings and current status and give you information on recommended changes that you should make. You shouldn't necessarily follow all of the suggestions, but it's worthwhile to take a look at anyway.
1) You need ssh access to your server. If you don't have root ssh access, you will need to contact your web host.
2) To connect to your server:
If you are on Windows, you can use PuTTY.
In the Session section:
- Enter yourdomain.com or your ip as Host Name
- Select SSH2 as protocol
- Click Open
You can now login secure to your server.
If this is your first time using PuTTy, then read this page for more details on how to set up PuTTy.
Using a MySQL Performance Tuning Analyzer Script
MySQL Performance Tuning Primer Script is a script that analyzes your MySQL performance and, based on the statistics it gathers, gives recommendations which variables you should adjust in order to increase performance. That way, you can tune your my.cnf file to tease out the last bit of performance from your MySQL server and make it work more efficiently.
This script takes information from "SHOW STATUS LIKE" and "SHOW VARIABLES LIKE" to produce sane recommendations for tuning server variables. It is compatible with all versions of MySQL 3.23 and higher (including 5.1).
Before you continue, make sure your MySQL server has at least 48 hours uptime. It's very important for the script in order to adjust variables corectly.
While logged into your server through ssh, type:
wget http://day32.com/MySQL/tuning-primer.sh to download it
then chmod u+x tuning-primer.sh to set the proper permissions,
and finally ./tuning-primer.sh to run the program. The script will analyze your server and suggest the proper settings.
You should carefully read the output, especially the recommendations at the end. It shows exactly which variables you should adjust in the [mysqld] section of your my.cnf (on Debian and Ubuntu the full path is /etc/mysql/my.cnf). Whenever you change your my.cnf, make sure that you restart MySQL. You can then run MySQL Script again to see if it has further recommendations to improve the MySQL performance. This way, you can optimize MySQL step by step.
Blue color means the name of the server parameter checked. White color is common text. Green color means that value/parameter is set OK while red one means there are some deficiencies found and recommendations for better performance are offered.
How to edit my.cnf
There are two editors that are pre-installed on your server that you can use. They are called "nano" and "vi". Of the two, nano will be easier to use, because its key combinations are provided right on the screen as you work. To edit a file, first make a back-up:
cp /etc/my.cnf /etc/my.cnf.old
You can edit your mysql configuration (/etc/my.cnf) by typing:
vi /etc/my.cnf or nano /etc/my.cnf.
When finished, ctrl X back out and type "y" to save and overwrite the file.
Then restart mysql
/etc/init.d/mysql restart (or /etc/init.d/mysqld restart)
The settings below should work well for a server with 512MB Guaranteed RAM:
This changes can be made on the MySQL DB but any mysqld restarts would revert the changes back again.
SET GLOBAL query_cache_type=1;
SHOW VARIABLES LIKE 'query_cache_type';
Tuning MySQL Performance with MySQLTuner
MySQLTuner is a Perl script that analyzes your MySQL performance and, based on the statistics it gathers, gives recommendations which variables you should adjust in order to increase performance. That way, you can tune your my.cnf file to tease out the last bit of performance from your MySQL server and make it work more efficiently.
You can download the MySQLTuner script as follows:
In order to run it, we must make it executable:
chmod +x mysqltuner.pl
Afterwards, we can run it. You need your MySQL root password for it:
The script should now run and display various stats and recommendations. Below is a example:
Run OPTIMIZE TABLE to defragment tables for better performance
To optimise all tables in a single database;
mysqlcheck --optimize -p -u user_name database_name
To optimise all tables in all databases;
mysqlcheck --optimize -A -p -u user_name
Repair, check, optimize
mysqlcheck --auto-repair --check --optimize --all-databases