How to install MySQL on CentOS

This is a step-by-step guide of what you need to do in order to install and setup MySQL on a new server.

We’ll prepare a fresh CentOS 6 system (64 bit) for use as a database server. All you need is access to an SSH client and your server root credentials.

Preparing the System

The first thing I’d like to do is make sure I have all available updates. So once I log in I run

This can take some time so grab a coffee while you wait. Once yum has finished, let’s check if MySQL is already installed on your system:

If you get “command not found” then you know you need to install MySQL.

Installing MySQL and MySQL Server

MySQL consists of two parts: the client and the server. In order for our system to run the daemon which will process external requests we need both on our system. Here’s how we get those:

This will take a minute or two. No need to restart your server, all you need to to is start MySQL with

and it will be available for use. You’ll see the following message:

Securing the installation

Looks like we need to do two things: set a root password and secure the installation. You can do these steps manually, but MySQL is rather nice in that it provides a script which you can use to secure your installation. Note that this path may be different on your system:

The script will ask you the following questions:

  • current root password (in our case it’s not set so hit enter)
  • remove anonymous users (say yes)
  • disallow remote login (in our case we want remote login active so we say no here, but if you’re using MySQL on a system which will not need this then say yes here)
  • remove test database and access to it (say yes)
  • reload all privieleges (say yes)

Now you can access MySQL with the following command:

Starting MySQL at boot time

You will likely need to make sure MySQL is running when you reboot the server, it’s cumbersome to start it manually every time you do that. This will take care of it:

We’re done – MySQL is now running on your server and yours to populate.

Have fun šŸ˜‰

Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.

20 thoughts on “How to install MySQL on CentOS

  1. Another item I always do is update the default my.cnf. By default, a number of features, such as query caching, is not enabled.

    If you look in:
    /usr/share/doc/mysql-server-5.0.95/

    You will find some example my.cnf files. I typically use my-large.cnf as a starting point for systems with 4GB of RAM or less. Use my-huge.cnf for systems with more than this.

    Once deployed, check out tools like http://mysqltuner.pl and http://hackmysql.com/mysqlreport to better optimize your installation.

  2. hi, i am trying to set up mysql, and i still don’t see how to get to the actual webpage to set up everything. i set everything up right as far as i know. but i still need help getting to the actual program

  3. Any suggestions for the following command and response?

    [root@LiunxCentos arasu]# service mysql start
    mysql: unrecognized service
    [root@LiunxCentos arasu]#

    1. Sounds like MySQL is not installed, or your system does not recognize the service command. You can also try this:

      /etc/init.d/mysqld restart

      If you get a similar error message, then MySQL is not installed.

      1. Yeah, you actually wrote ‘service mysql restart’ in the tutorial, I imagine its tripped up a few others, myself included. Only took me 30 secs on google to figure out its ‘service mysqld restart’..Good article anwyays, thanks

        1. Actually, don’t change it – on some installations the service is called mysqld, on some the service is called mysql

          If you install from the current packages as per your tutorial, the service is called mysql

  4. hai frnds. . . ..
    iam facing problem with secure installation of mysql in centos 6.5
    while secure installation of mysql : /usr/bin/mysql_secure_installation
    am getting error as:Enter current password for root (enter for none):
    ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)
    am entered as enter button only,i got the above error

    1. Try setting a password via the mysqladmin command as described by the friendly install message. Also, check if MySQL is actually running – you can’t connect if the service is down.

Add your voice!