To install MySQL, run the following command from a terminal prompt:
sudo apt-get install mysql-server
During the installation process you will be prompted to enter a password for the MySQL root user.
Once the installation is complete, the MySQL server should be started automatically. You can run the following command from a terminal prompt to check whether the MySQL server is running:
sudo netstat -tap | grep mysql
When you run this command, you should see the following line or something similar:
tcp 0 0 localhost:mysql *:* LISTEN 2556/mysqld
If the server is not running correctly, you can type the following command to start it:
sudo service mysql restart
You can edit the¬†/etc/mysql/my.cnf¬†file to configure the basic settings -- log file, port number, etc. For example, to configure MySQL to listen for connections from network hosts, change the¬†bind-address¬†directive to the server's IP address:
bind-address = 192.168.0.5
Replace 192.168.0.5 with the appropriate address.
After making a change to¬†/etc/mysql/my.cnf¬†the MySQL daemon will need to be restarted:
sudo service mysql restart
If you would like to change the MySQL¬†root¬†password, in a terminal enter:
sudo dpkg-reconfigure mysql-server-5.5
The MySQL daemon will be stopped, and you will be prompted to enter a new password.
Whilst the default configuration of MySQL provided by the Ubuntu packages is perfectly functional and performs well there are things you may wish to consider before you proceed.
MySQL is designed to allow data to be stored in different ways. These methods are referred to as either database or storage engines. There are two main engines that you'll be interested in: InnoDB and MyISAM. Storage engines are transparent to the end user. MySQL will handle things differently under the surface, but regardless of which storage engine is in use, you will interact with the database in the same way.
Creating a tuned my.cnf file
There are a number of parameters that can be adjusted within MySQL's configuration file that will allow you to improve the performance of the server over time. For initial set-up you may find¬†Percona's my.cnf generating tool¬†useful. This tool will help generate a my.cnf file that will be much more optimised for your specific server capabilities and your requirements.
Do not¬†replace your existing my.cnf file with Percona's one if you have already loaded data into the database. Some of the changes that will be in the file will be incompatible as they alter how data is stored on the hard disk and you'll be unable to start MySQL. If you do wish to use it and you have existing data, you will need to carry out a mysqldump and reload:
mysqldump --all-databases --routines -u root -p > ~/fulldump.sql
Once the dump has been completed, shut down MySQL:
sudo service mysql stop
Now backup the original my.cnf file and replace with the new one:
sudo cp /etc/mysql/my.cnf /etc/mysql/my.cnf.backup
sudo cp /path/to/new/my.cnf /etc/mysql/my.cnf
Then delete and re-initialise the database space and make sure ownership is correct before restarting MySQL:
sudo rm -rf /var/lib/mysql/*
sudo chown -R mysql: /var/lib/mysql
sudo service mysql start
Finally all that's left is to re-import your data. To give us an idea of how far the import process has got you may find the 'Pipe Viewer' utility, pv, useful. The following shows how to install and use pv for this case, but if you'd rather not use it just replace pv with cat in the following command. Ignore any ETA times produced by pv, they're based on the average time taken to handle each row of the file, but the speed of inserting can vary wildly from row to row with mysqldumps:
sudo apt-get install pv
pv ~/fulldump.sql | mysql
Once that is complete all is good to go!
This is not necessary for all my.cnf changes. Most of the variables you may wish to change to improve performance are adjustable even whilst the server is running. As with anything, make sure to have a good backup copy of config files and data before making changes.
MySQL Tuner¬†is a useful tool that will connect to a running MySQL instance and offer suggestions for how it can be best configured for your workload. The longer the server has been running for, the better the advice mysqltuner can provide. In a production environment, consider waiting for at least 24 hours before running the tool. You can get install mysqltuner from the Ubuntu repositories:
sudo apt-get install mysqltuner
Then once its been installed, run it:
and wait for its final report. The top section provides general information about the database server, and the bottom section provides tuning suggestions to alter in your my.cnf. Most of these can be altered live on the server without restarting, look through the official MySQL documentation (link in Resources section) for the relevant variables to change in production. The following is part of an example report from a production database which shows there may be some benefit from increasing the amount of query cache:
-------- Recommendations -----------------------------------------------------
Run OPTIMIZE TABLE to defragment tables for better performance
Increase table_cache gradually to avoid file descriptor limits
Variables to adjust:
key_buffer_size (> 1.4G)
query_cache_size (> 32M)
table_cache (> 64)
innodb_buffer_pool_size (>= 22G)