How to Install MySQL on CentOS 6/7

MySQL is the world’s most popular open source relational database management system. MySQL is no longer available in the CentOS’s repositories and MariaDB has become the default database system. MariaDB is a backward compatible, binary drop-in replacement of MySQL.

In this post, we will teach you how to install MySQL on a CentOS 6/7 Linux System.

Prerequisites

Oracle currently offers both stable versions (MySQL 8.0 and MySQL 5.7). You should install only one MySQL version on your CentOS 7 server.

In the following sections, we will show you how to install MySQL 8.0 and MySQL 5.7.

Installing MySQL 8.0 on CentOS 6/7

Step 1. Add MySQL Repository

MySQL is not available in the default CentOS 7 repositories so we will be installing the packages from the MySQL Yum Repository.

Enable the MySQL 8.0 repository with the following command:

#CentOS 7
rpm -ivh https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
#CentOS 6
rpm -ivh https://dev.mysql.com/get/mysql80-community-release-el6-1.noarch.rpm

Step 2. Installing MySQL 8.0

Install MySQL 8.0 package with yum:

sudo yum install mysql-community-server

During the installation yum may prompt you to import the MySQL GPG key. Type y and hit Enter.

Step 3. Starting MySQL 8.0

Once the installation is completed, you can start MySQL server and enable it to automatically start on boot with the following command:

#CentOS 7
sudo systemctl start mysqld
sudo systemctl enable mysqld
#CentOS 6
sudo service mysqld start
sudo chkconfig mysqld on

Step 4. Verify MySQL status

Verify that MySQL server is started using the following command.

#CentOS 7
sudo systemctl status mysqld
#CentOS 6
sudo service mysqld status

The output should look something like this:

● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2019-08-06 00:09:36 EDT; 21s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
 Main PID: 8452 (mysqld)
   Status: "SERVER_OPERATING"
   CGroup: /system.slice/mysqld.service
           └─8452 /usr/sbin/mysqld

Aug 06 00:09:23 server.itzgeek.local systemd[1]: Starting MySQL Server...
Aug 06 00:09:36 server.itzgeek.local systemd[1]: Started MySQL Server.

Step 5. Securing MySQL 8.0

In CentOS System, initial MySQL root password can be found in /var/log/mysqld.log. You can use below command to take the password from the log file.

cat /var/log/mysqld.log | grep -i 'temporary password'

Output:

2019-08-06T00:09:29.549979Z 1 [Note] A temporary password is generated for root@localhost: Ad*Kn(6R7q0h

Run the mysql_secure_installation command to improve the security of our MySQL installation,This command takes care of setting the root password, removing anonymous users, disallow root login remotely, etc.

sudo mysql_secure_installation

After entering the temporary password you will be asked to set a new password for user root. The password needs to be at least 8-characters long and to contain at least one uppercase letter, one lowercase letter, one number, and one special character.

Output:

The existing password for the user account root has expired. Please set a new password.

New password:

Re-enter new password:

The script will also ask you to remove the anonymous user, restrict root user access to the local machine and remove the test database. You should answer “Y” (yes) to all questions.

Step 6. Work with MySQL Server

Login to MySQL server with the root user and its password.

mysql -u root -p

You will be prompted to enter the root password you have previously set when the mysql_secure_installation script was run.

Output:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.11 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

At this point, the installing of MySQL 8.0 is complete.

Installing MySQL 5.7 on CentOS 6/7

Step 1. Add MySQL Repository

Enable the MySQL 5.7 repository with the following command:

#CentOS 7
sudo yum localinstall https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
#CentOS 6
rpm -ivh https://dev.mysql.com/get/mysql57-community-release-el6-1.noarch.rpm

Step 2. Installing MySQL 5.7

Install MySQL 5.7 package with yum:

sudo yum install mysql-community-server

During the installation yum may prompt you to import the MySQL GPG key. Type y and hit Enter.

Step 3. Starting MySQL 5.7

Once the installation is completed, you can start MySQL server and enable it to automatically start on boot with the following command:

#CentOS 7
sudo systemctl start mysqld
sudo systemctl enable mysqld
#CentOS 6
sudo service mysqld start
sudo chkconfig mysqld on

Step 4. Verify MySQL status

Verify that MySQL server is started using the following command.

#CentOS 7
sudo systemctl status mysqld
#CentOS 6
sudo service mysqld status

The output should look something like this:

● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2019-08-06 00:09:36 EDT; 21s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
 Main PID: 8452 (mysqld)
   Status: "SERVER_OPERATING"
   CGroup: /system.slice/mysqld.service
           └─8452 /usr/sbin/mysqld

Aug 06 00:09:23 server.itzgeek.local systemd[1]: Starting MySQL Server...
Aug 06 00:09:36 server.itzgeek.local systemd[1]: Started MySQL Server.

Step 5. Securing MySQL 5.7

In CentOS System, initial MySQL root password can be found in /var/log/mysqld.log. You can use below command to take the password from the log file.

cat /var/log/mysqld.log | grep -i 'temporary password'

Output:

2019-08-06T00:09:29.549979Z 1 [Note] A temporary password is generated for root@localhost: Ad*Kn(6R7q0h

Run the mysql_secure_installation command to improve the security of our MySQL installation,This command takes care of setting the root password, removing anonymous users, disallow root login remotely, etc.

sudo mysql_secure_installation

After entering the temporary password you will be asked to set a new password for user root. The password needs to be at least 8-characters long and to contain at least one uppercase letter, one lowercase letter, one number, and one special character.

Step 6. Work with MySQL Server

Login to MySQL server with the root user and its password.

mysql -u root -p

You will be prompted to enter the root password you have previously set when the mysql_secure_installation script was run.

Output:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

At this point, the installing of MySQL 5.7 is complete.

Conclusion

In this post, we’ve shown you how to install and secure a MySQL server on a CentOS 7 server. We have also shown you how to connect to the MySQL shell.

Feel free to leave a comment if you have any questions.

Leave a Reply

Your email address will not be published. Required fields are marked *