MySQL is an open-source and widely used database management system. With CentOS 7 release MySQL is replaced with MariaDB as default database system. This guide will show you how to install MySQL on CentOS 7 systems.
Before starting, make sure you are logged in to a CentOS 7 server with a non-root user with sudo privileges.
Install MySQL on CentOS 7
MySQL is not available in default CentOS 7 repositories so we will install the package from the MySQL Yum Repository. Below you will learn how to install MySQL 8.0 and MySQL 5.7 versions.
The latest version of MySQL is version 8.0. Below given repositories of both version 8.0 and 5.7 so choose repository to enable whichever version you wants to install. Follow below steps to install it on your CentOS 7 server.
First, enable the MySQL yum repository by typing:
For MySQL 8.0 Version
sudo yum localinstall https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
For MySQL 5.7 Version
sudo yum localinstall https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
Now, install MySQL package same as any other package using yum by type:
sudo yum install mysql-community-server
During installation it may ask you to import MySQL GPG key. Type y and hit Enter to continue.
Manage MySQL Services
Afterwards, when installation finish, Start MySQL service by below command:
sudo systemctl enable mysqld
Now enable MySQL service to automatically start on boot with:
sudo systemctl start mysqld
You can check status of MySQL service by:
sudo systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2019-03-29 01:39:01 UTC; 1 days ago
Main PID: 423 (mysqld)
└─423 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
It will show you output as below and you can see that service is active or not:
Securing & Configuring MySQL
MySQL includes a default security script which will improve security of MySQL installation. By this script it will make changes in default options like remote root logins and sample users and test database. Run the following security script:
You will be prompted to enter your root user password of MySQL and also it will ask to set new root password. The password needs to be at least eight characters long and to contain at least one uppercase letter, one lowercase letter, one number, and one special character.
The existing password for the user account root has expired. Please set a new password.
Re-enter new password:
After that, it will prompt few more questions. For all next questions press Y and hit Enter key for each. By this it will remove anonymous users, test database, disable remote root logins and load these new rules so that MySQL immediately respects the changes you have made.
We can connect MySQL via command line using MySQL client which is installed a dependency of the MySQL server package. You can login to MySQL using following command as root user :
sudo mysql -uroot -p
It will prompt to enter the root password which you have previously set. Once you will enter password it will be shown output as following :
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 276099
Server version: 8.0.11 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
In this guide, you learnt how to install and secure a MySQL server on a CentOS 7 server. If you have any suggestion or questions, you can post it using comment box below.