How to Install MySQL on CentOS 7

MySQL is an open-source relational database management system that is popularly used to store, organize, and manage large volumes of data. CentOS 7 is a popular Linux distribution that is widely used for web servers and hosting purposes. In this article, we will explore how to install MySQL on CentOS 7.

Step 1: Update the System

Before installing MySQL on CentOS 7, it is recommended to update the system to ensure that all the packages are up-to-date. To do this, launch the terminal and run the following command:

Sudo yum update

This command will update all the packages installed on the system.

Step 2: Install MySQL

The next step is to install MySQL on CentOS 7. To do this, run the following command in the terminal:

Sudo yum install mysql-server

This command will download and install the MySQL server package along with its dependencies.

Step 3: Start MySQL Service

After installing MySQL on CentOS 7, the next step is to start the MySQL service. To do this, run the following command:

Sudo systemctl start mysqld

This command will start the MySQL service on the system.

Step 4: Secure MySQL Installation

By default, MySQL installation is not secure, and anyone can access the database without any authentication. Therefore, it is recommended to secure the MySQL installation by setting up a root password and removing the test database. To do this, run the following command:

Sudo mysql_secure_installation

This command will launch a script that will ask you a series of questions to secure the MySQL installation. Follow the instructions carefully and set the root password, remove the test database, and disallow remote root login.

Step 5: Verify MySQL Installation

After securing the MySQL installation, the next step is to verify that it is working correctly. To do this, run the following command:

Sudo systemctl status mysqld

This command will display the status of the MySQL service. If the service is running correctly, you should see a message that says “Active: active (running).”

Step 6: Connect to MySQL Server

To connect to the MySQL server, you need to use the MySQL client tool. To install the MySQL client tool, run the following command:

Sudo yum install mysql

Once the MySQL client tool is installed, you can connect to the MySQL server by running the following command:

Mysql -u root -p

This command will prompt you to enter the root password that you set earlier. Once you enter the password, you will be connected to the MySQL server.

Step 7: Create a Database

After connecting to the MySQL server, the next step is to create a database. To do this, run the following command:

CREATE DATABASE dbname;

Replace dbname with the name of the database that you want to create.

Step 8: Create a User

After creating the database, the next step is to create a user that can access the database. To do this, run the following command:

CREATE USER ‘username’@’localhost’ IDENTIFIED BY ‘password’;

Replace username and password with the desired username and password for the user.

Step 9: Grant Privileges

After creating the user, the next step is to grant privileges to the user so that they can access the database. To do this, run the following command:

GRANT ALL PRIVILEGES ON dbname.* TO ‘username’@’localhost’;

Replace dbname and username with the name of the database and the name of the user that you created earlier.

Step 10: Exit MySQL

After completing the above steps, the final step is to exit the MySQL prompt. To do this, run the following command:

Exit;

This command will exit the MySQL prompt and take you back to the terminal.

In this article, we explored how to install MySQL on CentOS 7. We covered the steps to install MySQL, start the MySQL service, secure the MySQL installation, connect to the MySQL server, create a database and a user, grant privileges, and exit the MySQL prompt. By following these steps, you can set up a MySQL server on CentOS 7 and start managing your data efficiently.