How to Install MySQL on CentOS, Debian & Ubuntu?

Installing MySQL on CentOS-Guide

MySQL is an important part of almost every PHP application. It is an open-source database management system which uses a relational database and SQL (Structured Query Language) to manage its data.

The below guide will explain how to install MySQL specifically on a different OS distribution.

Installation on CentOS7

By default, MariaDB gets installed in CentOS7 when using YUM. To install MySQL, you will need to make use of RPM installation method.

1. Download the RPM of the required MySQL version from the MySQL community Yum Repository. In this context, we will be installing MySQL version 5.7

wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm

2. Install the RPM package using the below command

rpm -ivh mysql57-community-release-el7-9.noarch.rpm

This adds the required MySQL yum repositories to the server.

3. Install MySQL server

yum install mysql-server

4. Once the MySQL server has been installed, start the MySQL service.

systemctl start mysqld

The status of the MySQL service can be verified by using below command.

systemctl status mysqld

5. Once the MySQL service is confirmed to be running, you would need to set the root password for the MySQL service. This can be done using the inbuilt MySQL security script.

Before running the security script, you need to retrieve the default root password, which can be done by executing the below command.

grep ‘temporary password’ /var/log/mysqld.log

6. Once you have got the default password, run the MySQL security script using below command.

mysql_secure_installation

You will be prompted to enter the default password. Once it is entered, you will once again be prompted to set up a new strong password.

[root@server ~]# mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:

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

New password:
Re-enter new password:

Enter Y for all subsequent questions to secure the MySQL service.

7. Test the MySQL service.

mysqladmin -u root -p version

Below output would be displayed confirming successful installation of MySQL service.

mysqladmin Ver 8.42 Distrib 5.7.27, for Linux on x86_64
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.

Server version 5.7.27
Protocol version 10
Connection Localhost via UNIX socket
UNIX socket /var/lib/mysql/mysql.sock
Uptime: 22 min 16 sec

Threads: 1 Questions: 13 Slow queries: 0 Opens: 113 Flush tables: 1 Open tables: 106 Queries per second avg: 0.009

Installation on Ubuntu/Debian

The latest version of MySQL is included in the APT package repository by default. Follow the below steps to install MySQL version 5.7 in Ubuntu.

1. Update the APT package repository index.

sudo apt update

2. Install MySQL package with the following command.

sudo apt install mysql-server

3. The MySQL service will start automatically, once the installation is completed. To check the status of the MySQL server, type:

sudo systemctl status mysql

4. Once the MySQL service is confirmed to be running, you would need to set the root password for the MySQL service. This can be done by running inbuilt MySQL security script command.

mysql_secure_installation

In the first prompt, you will be asked to configure the VALIDATE PASSWORD PLUGIN which is used to test the strength of the MySQL users’ passwords and improve the security. Press ENTER to skip this step.

Enter Y for all remaining subsequent questions to secure the MySQL service.

5. Test the MySQL service by logging in as root user.

sudo mysql

Below output would be displayed confirming successful installation of MySQL service.

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.27-0ubuntu0.18.04.1 (Ubuntu)

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>

GET STARTED WITH OUR HOSTING SUPPORT SERVICES


Rohith Krishnan

Rohith SK is an MSC computer science graduate living in Cochin, Kerala. As a technology enthusiast, he is always on the lookout for the latest trends and developments in the field, with a particular interest in cloud computing and DevOps updates. Apart from his passion for technology, Rohith SK is an avid reader and enjoys spending his free time exploring different genres of literature. He believes that reading is one of the best ways to expand one's knowledge and understanding of the world. With his expertise in computer science and a passion for technology, Rohith SK regularly contributes articles and blog posts on the latest trends and updates in the industry. His articles offer insights and valuable perspectives on the various aspects of cloud computing and DevOps, and are widely read and appreciated by readers all over the world. As an experienced technology writer and researcher, Rohith SK's articles are well-researched, informative, and easy to understand, making them accessible to readers of all levels of technical knowledge. Whether you're a beginner looking to learn more about the latest trends in technology, or an experienced professional seeking insights and updates, Rohith's articles are sure to provide valuable information and insights.

Leave a Reply