CentOSLinuxTutorials

How To Install MySQL Server on CentOS 7

Install MySQL Server on CentOS 7

In this tutorial, we will show you how to install and configure MySQL on your CentOS 7 server. For those of you who didn’t know, MySQL is a relational database management system (RDBMS) that runs as a server providing multi-user access to a number of databases. The MySQL source code is freely available because it was originally developed as freeware. MySQL is written in C and C++ and is compatible with all major operating systems. MySQL can be used for a variety of applications but is most commonly found on Web servers.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple. I will show you the step-by-step installation of MySQL Server in the CentOS 7 server.

Prerequisites

  • A server running one of the following operating systems: CentOS 7.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • A non-root sudo user or access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Install MySQL Server on CentOS 7

Step 1. It’s always a good practice to start with an updated system to ensure compatibility and security. Run the following command to update your CentOS 7 system:

sudo yum update

Keeping your system up-to-date not only resolves potential vulnerabilities but also ensures that you have the latest package versions available for a smooth installation process.

Step 2. Adding the MySQL Yum Repository.

While CentOS provides its own repositories, it’s recommended to use the official MySQL Yum repository for the latest MySQL packages and updates. Follow these steps to add the MySQL Yum repository:

wget https://dev.mysql.com/get/mysql80-community-release-el7-5.noarch.rpm

Install the downloaded package with the following command:

sudo rpm -Uvh mysql80-community-release-el7-5.noarch.rpm

By using the MySQL Yum repository, you’ll have access to the latest MySQL versions, bug fixes, and security updates, ensuring a stable and secure installation.

Step 3. Install MySQL on CentOS 7.

With the MySQL Yum repository added, you can now proceed to install the MySQL Server package. Run the following command:

yum -y update
yum install mysql-server

To start the MySQL service and ensure that it automatically starts on system boot, run the following commands:

sudo systemctl start mysqld
sudo systemctl enable mysqld

Step 4. Configuring MySQL.

After installing MySQL Server, it’s crucial to secure your installation by running the MySQL secure installation script. This script will guide you through a series of security-related configurations, including setting a root password, removing anonymous users, and disabling remote root login.

mysql_secure_installation

Configure it like this:

- Set root password? [Y/n] y
- Remove anonymous users? [Y/n] y
- Disallow root login remotely? [Y/n] y
- Remove test database and access to it? [Y/n] y
- Reload privilege tables now? [Y/n] y

Next, we will need to log in to the MySQL console and create a database. Run the following command:

mysql -u root -p

Step 5. Configure Firewall for MySQL.

It is highly recommended that the ProfitBricks firewall and/or local Linux firewall be used to restrict access to the MySQL server. Only hosts requiring connectivity to the MySQL server should be granted network access:

firewall-cmd --permanent --zone=trusted --add-source=192.0.2.10/32
firewall-cmd --permanent --zone=trusted --add-port=3306/tcp
firewall-cmd  --reload

Step 6. Creating a New Database and User

After installing and configuring MySQL Server, you’ll likely want to create a new database and user for your applications or projects. Follow these steps:

mysql -u root -p

Create a new database:

CREATE DATABASE your_database_name;

Create a new user and grant privileges:

CREATE USER 'your_username'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_username'@'localhost';

Substitute your_username and your_password with the desired username and password for the new user. Replace your_database_name with the name of the database you created in the previous step.

By granting all privileges, the new user will have full access to the specified database. You can adjust the privileges according to your requirements.

Congratulations! You have successfully installed MySQL. Thanks for using this tutorial for installing MySQL Server in CentOS 7 system. For additional help or useful information, we recommend you check the official MySQL website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button