How to Install MySQL on a CentOS 7 Server?

There are three steps to install mysql on a centos7 server:

  • Download the MySQL repository

  • Install it

  • Configure it

Download the MySQL repository

  1. Update our system
sudo yum update
  1. Add MySQL repositories
sudo wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
  1. At the end of the download, you should see a confirmation message that .rpm file was saved.

  2. Prepare the repository so we could later install MySQL packages from it.

sudo rpm -Uvh mysql80-community-release-el7-3.noarch.rpm

Error of failed dependencies may arise if the machine is not fresh and past installation of mysql is done.

To resolve the issue ,remove/erase the past version of mysql

sudo rpm -e mysql57-community-release

Install Mysql:

  1. Using yum install command to install MySQL :

sudo yum install mysql-server -y

To fix this we need

rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2023

Configure MySQL:

  1. After installation we need to start it manually through the following command:

sudo systemctl start mysqld
  1. When installing MySQL on CentOS 7, a temporary root password is generated. Below is the command to see it:

     sudo grep 'password' /var/log/mysqld.log
    
  2. Run the MySQL secure installation script to set up security options:

    This script will prompt to configure the following:

    • Set the root password

    • Remove anonymous users

    • Disallow root login remotely

    • Remove test database and access to it

    • Reload privilege tables

    sudo mysql_secure_installation
  1. To ensure MySQL starts automatically upon server boot, enable the service:

     sudo systemctl enable mysqld
    
  2. We will get no response once MySQL starts so to check if it is working properly, use the command below:

sudo systemctl status mysqld
  1. Output may look something like this:

  2. Access MySQL shell using the root account:

     mysql -u root -p
    
  3. After finishing your MySQL tasks, exit the MySQL shell:

     exit;
    
  4. Restart MySQL

     sudo systemctl restart mysqld
    
0
Subscribe to my newsletter

Read articles from Ruchi Lamichhane directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ruchi Lamichhane
Ruchi Lamichhane

I am a tech ethusiast with passion for technology, embracing the world of continuous integration, automation, and collaboration to make a meaningful impact in the dynamic realm of DevOps.