×


Install MySQL on CentOS 7 Server - Step by Step Process ?

MySQL is an open-source database management system, often installed as part of the popular LEMP (Linux, Nginx, MySQL/MariaDB, PHP/Python/Perl) stack. 

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related MySQL queries.

In this context, we shall look into how to install MySQL on CentOS 7 systems.


How to Install MySQL on CentOS ?

Before proceeding with this installation procedure, we need to ensure that you are using a non-root user with sudo privileges.

MySQL is not available in default CentOS 7 repositories so we will install the package from the MySQL Yum Repository.

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.

Start by enabling 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.


How to Manage MySQL Services ?

Here, you can 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
      Docs: man:mysqld(8)
            http://dev.mysql.com/doc/refman/en/using-systemd.html
  Main PID: 423 (mysqld)
    CGroup: /system.slice/mysqld.service
            └─423 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

It will show you output as above and you can see that service is active or not.


How to Secure and Configure 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:

$ sudo mysql_secure_installation

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.

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.


How to Test MySQL ?

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
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


[Need urgent assistance in fixing MySQL errors? We can help you. ]


Conclusion

This article covers how to install and secure a MySQL server on a CentOS 7 server. MySQL is one of the most widely used database management systems for websites and server applications.


To start the MySQL server daemon with the following command:

$ sudo systemctl start mysqld

systemctl doesn't display the outcome of all service management commands, so to be sure we succeeded, we'll use the following command:

$ sudo systemctl status mysqld

To configure MySQL, run the following command:

$ sudo mysql_secure_installation

To Current MySQL Version, run the command:

$ mysql -u root -p


Terms used in Managing MySQL User Permissions:

  • SELECT – users can read through the database using the select command.
  • CREATE – they can generate new tables.
  • DROP – allows users to remove tables.
  • DELETE – users can take out rows from tables.
  • INSERT – lets users add in rows into tables.
  • UPDATE – enable them to update the rows.
  • GRANT OPTION – they can grant or remove the privileges of other users.