How to Install MySQL on CentOS, Debian & Ubuntu?
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.
This guide will walk you through how to install MySQL on CentOS, Debian, and Ubuntu, covering every step from setup to security configuration.
The below guide will explain how to install MySQL specifically on a different OS distribution.
For performance tuning of your MySQL server, refer to our MySQL performance optimization guide
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>