Install MySQL 5.6 , 5.7 , 8.0 on CentOS 7

Install MySQL 5.6 , 5.7 , 8.0 on CentOS

In this tutorial, we will learn how to install MySQL 5.6, 5.7, and 8.0 on CentOS 7.

CentOS 7 MySQL is one of the most popular open-source relational database management systems. MySQL is no longer available in CentOS’s repositories and MariaDB has been considered as the default database system.

If you want to install MariaDB instead of MySQL check out our tutorials for the installation of MariaDB.

Note :- You have to install only one specific MySQL version as per your website requirement.

Prerequisites

Make sure you are login into the server as a root user or other user with sudo privileges. The Server must have CentOS 7 system installed in it.

As we discussed above that the MySQL is not available in the default CentOS 7 repositories. So, we manually install the packages from the MySQL Yum Repository.

Install MySQL 8.0 on CentOS 7

Install MySQL on your CentOS 7 server follow these steps:

  • Enable the MySQL 8.0 repository, use the below command:
sudo yum localinstall https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm -y
  • To Install the MySQL 8.0, use the below command:
sudo yum install mysql-community-server -y

It will automatically download and install the MySQL 8.0

  • Enable the MySQL service
sudo systemctl enable mysqld
  • Restart the MySQL service
sudo systemctl restart mysqld
  • Check the Status of MySQL Service
sudo systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2021-01-03 11:36:36 UTC; 3s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 18086 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 18033 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 18089 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─18089 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

Install MySQL 5.7 on CentOS 7

To install MySQL 5.7 on your CentOS 7 server follow these steps:

  • To Enable the MySQL 5.7 repository, use the below command:
sudo yum localinstall https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm -y
  • Install MySQL 5.7 package, use below command:
sudo yum install mysql-community-server -y
  • Enable  MySQL service
sudo systemctl enable mysqld
  • Restart  MySQL service
sudo systemctl restart mysqld
  • Check the Status of MySQL Service
sudo systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2021-01-03 11:36:36 UTC; 3s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 18086 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 18033 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 18089 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─18089 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

Install MySQL 5.6 on CentOS 7

  • Download the MySQL 5.6 package for the CentOS 7:
sudo yum install http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm -y
  • Check that all the MySQL repository is effectively active by running:
sudo yum repolist enabled | grep "mysql.*-community.*"
  • Install the MySQL 5.6
sudo yum install mysql-community-server -y
  • Enable  MySQL service
sudo systemctl enable mysqld
  • Restart  MySQL service
sudo systemctl restart mysqld
  • Check the Status of MySQL Service
sudo systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2021-01-03 11:36:36 UTC; 3s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 18086 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 18033 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 18089 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─18089 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
  • Enter in the MySQL server:
mysql

After entering, MySQL will show the initial screen:

Your MySQL connection id is 7
Server version: 5.6.50 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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.

It will not ask for the password, it will automatically authenticate the user with
auth_socket.

  • Update the Root User password of the MySQL server, Enter the below command in the MySQL and replace new_password with your desire password.
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('new_password');

Securing MySQL

After starting the MySQL service, run the following command to secure the MySQL server.

Note:-We did not require generating the password for MySQL 5.6, We already set a new password. Simple run the MySQL secure command for MySQL 5.6 then Enter your changed Password.

  • For the First time MySQL server generate a temporary password. To find the temporary password run the below command:
sudo grep -r password /var/log/mysqld.log

Sometimes it shows an error that the directory is not found then you have to create those directories manually.

  • The output should look something like this:
2021-01-03T09:01:46.669138Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: hE>xhjtd;1oe
  • Copy the temporary password.
  • Run the Secure Installation command to setup the MySQL server.
sudo mysql_secure_installation
Securing the MySQL server deployment.

Enter password for user root:

After entering a temporary password, you need to set a new password. Password must be very strong. Use Random Password Generator to generate a strong password.

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

New password:

Re-enter new password:

It will also ask to Remove anonymous users, Disallow root login remotely, Remove test database and access to it, Reload privilege tables now. You should Type Y (Yes) for all the questions.

Uninstall MySQL Packages

If you want to remove MySQL packages from CentOS 7, You just need to follow some easy steps.

  • To uninstall MySQL, follow the below command:
sudo yum remove mysql mysql-server -y
  • Remove the trash files of MySQL after uninstallation.
sudo rm -r /var/lib/mysql
sudo rm -r /etc/mysql

Read Also: How to Install Multiple PHP on CentOS 7/8 and Ubuntu 20.04

Conclusion

In this tutorial, we have learned how we can easily install and uninstall MySQL’s different versions through Repositories. MySQL is used as a database server with many applications, and it’s widely used. This how to install MySQL on CentOS has three versions steps. So all in one package for MySQL installation.

If you guys have any quires related to this tutorial, let me know in the comments.