How to Install MySQL on CentOS 6

Updated by Linode

Contribute on GitHub

View Project | View File | Edit File

MySQL is a popular database management system used for web and server applications. This guide will introduce how to install, configure and manage MySQL on a Linode running CentOS 6.

This guide is written for a non-root user. Commands that require elevated privileges are prefixed with sudo. If you’re not familiar with the sudo command, you can check our Users and Groups guide.

Before You Begin

  1. Ensure that you have followed the Getting Started and Securing Your Server guides, and the Linode’s hostname is set.

    To check your hostname run:

    1
    2
    hostname
    hostname -f
    

    The first command should show your short hostname, and the second should show your fully qualified domain name (FQDN).

  2. Update your system:

    1
    sudo yum update
    

Install and Start MySQL

  1. Install MySQL and tell it which runlevels to start on:

    1
    2
    sudo yum install mysql-server
    sudo /sbin/chkconfig --levels 235 mysqld on 
    
  2. Then to start the MySQL server:

    1
    sudo service mysqld start
    

    MySQL will bind to localhost (127.0.0.1) by default. Please reference our MySQL remote access guide for information on connecting to your databases using SSH.

Allowing unrestricted access to MySQL on a public IP not advised, but you may change the address it listens on by modifying the bind-address parameter in /etc/my.cnf. If you decide to bind MySQL to your public IP, you should implement firewall rules that only allow connections from specific IP addresses.

Harden MySQL Server

  1. Run the mysql_secure_installation script to address several security concerns in a default MySQL installation.

    1
     sudo mysql_secure_installation
    

You will be given the choice to change the MySQL root password, remove anonymous user accounts, disable root logins outside of localhost, and remove test databases. It is recommended that you answer yes to these options. You can read more about the script in in MySQL Reference Manual.

Using MySQL

The standard tool for interacting with MySQL is the mysql client which installs with the mysql-server package. The MySQL client is used through a terminal.

Root Login

  1. To log in to MySQL as the Root User:

    1
    mysql -u root -p
    
  2. When prompted, enter the root password you assigned when the mysql_secure_installation script was run.

    You’ll then be presented with the MySQL monitor display:

    1
    2
    3
    4
    5
    6
    7
     Welcome to the MySQL monitor.  Commands end with ; or \g.
     Your MySQL connection id is 1
     Server version: 5.0.45 Source distribution
    
     Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
    
     mysql>
    
  3. To generate a list of commands for the MySQL prompt, enter \h. You’ll then see:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
     List of all MySQL commands:
     Note that all text commands must be first on line and end with ';'
     ?         (\?) Synonym for `help'.
     clear     (\c) Clear command.
     connect   (\r) Reconnect to the server. Optional arguments are db and host.
     delimiter (\d) Set statement delimiter. NOTE: Takes the rest of the line as new delimiter.
     edit      (\e) Edit command with $EDITOR.
     ego       (\G) Send command to mysql server, display result vertically.
     exit      (\q) Exit mysql. Same as quit.
     go        (\g) Send command to mysql server.
     help      (\h) Display this help.
     nopager   (\n) Disable pager, print to stdout.
     notee     (\t) Don't write into outfile.
     pager     (\P) Set PAGER [to_pager]. Print the query results via PAGER.
     print     (\p) Print current command.
     prompt    (\R) Change your mysql prompt.
     quit      (\q) Quit mysql.
     rehash    (\#) Rebuild completion hash.
     source    (\.) Execute an SQL script file. Takes a file name as an argument.
     status    (\s) Get status information from the server.
     system    (\!) Execute a system shell command.
     tee       (\T) Set outfile [to_outfile]. Append everything into given outfile.
     use       (\u) Use another database. Takes database name as argument.
     charset   (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
     warnings  (\W) Show warnings after every statement.
     nowarning (\w) Don't show warnings after every statement.
    
     For server side help, type 'help contents'
    
     mysql>
    

Create a New MySQL User and Database

  1. In the example below, testdb is the name of the database, testuser is the user, and password is the user’s password.

    1
    2
    3
     create database testdb;
     create user 'testuser'@'localhost' identified by 'password';
     grant all on testdb.* to 'testuser' identified by 'password';
    

    You can shorten this process by creating the user while assigning database permissions:

    1
    2
     create database testdb;
     grant all on testdb.* to 'testuser' identified by 'password';
    
  2. Then exit MySQL.

    1
    exit
    

Create a Sample Table

  1. Log back in as testuser.

    1
    mysql -u testuser -p
    
  2. Create a sample table called customers. This creates a table with a customer ID field of the type INT for integer (auto-incremented for new records, used as the primary key), as well as two fields for storing the customer’s name.

    1
    2
    use testdb;
    create table customers (customer_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, first_name TEXT, last_name TEXT);
    
  3. Then exit MySQL.

    1
    exit
    

Reset the MySQL Root Password

If you forget your root MySQL password, it can be flushed and then reset.

  1. Stop the current MySQL server instance, then restart it with an option to not ask for a password.

    1
    2
     sudo /etc/init.d/mysqld stop
     sudo mysqld_safe --skip-grant-tables &
    
  2. Reconnect to the MySQL server with the MySQL root account.

    1
     mysql -u root
    
  3. Use the following commands to reset root’s password. Replace password with a strong password.

    1
    2
    3
    4
     use mysql;
     update user SET PASSWORD=PASSWORD("password") WHERE USER='root';
     flush privileges;
     exit
    
  4. Then restart MySQL.

    1
     sudo service mysqld restart
    

You’ll now be able to log in again using mysql -u root -p.

Tune MySQL

MySQL Tuner is a Perl script that connects to a running instance of MySQL and provides configuration recommendations based on workload. Ideally, the MySQL instance should have been operating for at least 24 hours before running the tuner. The longer the instance has been running, the better advice MySQL Tuner will give.

  1. Download MySQL Tuner to your home directory.

    1
     wget https://raw.githubusercontent.com/major/MySQLTuner-perl/master/mysqltuner.pl
    
  2. To run it:

    1
     perl ./mysqltuner.pl
    

    The output will show two areas of interest: General recommendations and Variables to adjust.

MySQL Tuner is an excellent starting point to optimize a MySQL server but it would be prudent to perform additional research for configurations tailored to the application(s) utilizing MySQL on your Linode.

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

This guide is published under a CC BY-ND 4.0 license.