Installing MySQL on CentOS

A concise summary can be found at the end of the article.

You might think installing MySQL on CentOS is as easy as:

$ sudo yum install mysql-community-server

But you might find the following error:

No package mysql-community-server available.

That's because the mysql-community-server package is not available in the default repositories which came with the distribution.

You can view all the repositories your system currently has knowledge of in the /etc/yum.repos.d/ directory:

$ cd /etc/yum.repos.d/; ll;
total 32
-rw-r--r--. 1 root root 1664 Dec  9  2015 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 Dec  9  2015 CentOS-CR.repo
-rw-r--r--. 1 root root  649 Dec  9  2015 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  290 Dec  9  2015 CentOS-fasttrack.repo
-rw-r--r--. 1 root root  630 Dec  9  2015 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 Dec  9  2015 CentOS-Sources.repo
-rw-r--r--. 1 root root 1952 Dec  9  2015 CentOS-Vault.repo
-rw-r--r--. 1 root root  300 Apr 18 21:09 google-cloud.repo

Adding the MySQL Yum Repository

CentOS uses the YUM repository to house its packages. So first, we must add the MySQL Yum repository to our list of repositories.

So first, go to the Downloads page and find the RPM package which fits your version of CentOS.

Since CentOS is based on Red Hat Enterprise Linux (RHEL) and we are running version 7, we'd choose the Red Hat Enterprise Linux 7 / Oracle Linux 7 (Architecture Independent), RPM Package version.

To see how the myriad of Linux distributions evolved, check out this link.

Click on the 'Download' button, which will bring you to a link prompting you to sign in or create an account. At the bottom of the page you'll find a link which says No thanks, just start my download. Right-click on the link and copy the link address.

In your production server, run wget to download the .rpm package.

$ wget
$ ll
total 12
-rw-rw-r--. 1 username username 9116 Mar 11 09:13 mysql57-community-release-el7-8.noarch.rpm

Run sudo yum install wget if it's not already installed.

Next, we will install the package.

$ sudo rpm -Uvh mysql57-community-release-el7-8.noarch.rpm 
warning: mysql57-community-release-el7-8.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY  
Preparing...                          ################################# [100%]  
Updating / installing...  
   1:mysql57-community-release-el7-8  ################################# [100%]

You can also run sudo yum localinstall mysql57-community-release-el7-8.noarch.rpm, both works.

Next, run yum update (or sudo yum update to run as an administrative user) to ensure the package is the latest version.

Installing MySQL Community Server

Now if we try installing the mysql-community-server, it should succeed.

$ sudo yum install mysql-community-server

Running the MySQL Daemon

The term 'daemon' sounds scary, but it's just a process that runs in the background without the need of human interaction. A web server process would be a daemon, as it runs quietly in the background - it'd be weird if it required your input for every request it receives!

The same is true for our database, there's a daemon that runs in the background and handles requests.

When we say 'MySQL', what we usually mean is the MySQL client, an interface to interact with the MySQL server. The MySQL Daemon is that server.

So to use MySQL, we must initialize this daemon.

$ sudo service mysqld start
Redirecting to /bin/systemctl start  mysqld.service

We can check that it's running properly by checking its status:

$ sudo service mysqld status
Redirecting to /bin/systemctl status  mysqld.service  
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2016-06-22 05:46:31 UTC; 3min 25s ago
  Process: 27785 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/ $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 27712 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 27790 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─27790 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/

Jun 22 05:46:25 cent-m6 systemd[1]: Starting MySQL Server...  
Jun 22 05:46:31 cent-m6 systemd[1]: Started MySQL Server.  

We can see that it's active from this line:

active (running) since Wed 2016-06-22 05:46:31 UTC; 3min 25s ago

Setting the Superuser password

Since MySQL 5.7, when we first initialize the daemon, it will create a superuser account for us with a temporary password. To ensure security, we should change it.

The password is printed in the log file, so to retrieve it, we can run:

$ sudo grep 'temporary password' /var/log/mysqld.log

Now log in to MySQL as root using the password:

$ mysql -uroot -p

And change the password to one you can remember:

> ALTER USER 'root'@'localhost' IDENTIFIED BY 'n3WpAs5w*rD'; 

The validate_password plugin (installed by default) requires that the password be at least 8 characters long, and contain at least one upper case letter, one lower case letter, one digit, and one special character.


Need it quick? Follow these steps!

  1. Downloads the release package for your operating system.
  2. Install the package - sudo rpm -Uvh mysql57-community-release-el7-8.noarch.rpm
  3. Install MySQL - sudo yum install mysql-community-server
  4. Start the MySQL daemon - sudo service mysqld start
  5. Find out the superuser password - sudo grep 'temporary password' /var/log/mysqld.log
  6. Log in to MySQL - mysql -uroot -p
  7. Set a new password for the superuser - ALTER USER 'root'@'localhost' IDENTIFIED BY 'n3WpAs5w*rD';

Daniel Li

Full-stack Web Developer in Hong Kong. Founder of Brew.

Hong Kong