How to Install MySQL on Linux Server

MySQL is one of the most popular open-source relational database management systems (RDBMS) and is widely used for managing and storing data. It is known for its speed, reliability, and ease of use. MySQL supports many high-traffic websites and applications. Installing MySQL on a Linux server is a simple process and provides various features to manage complex data needs.

Install MySQL on Linux

This article will guide you through the steps to install MySQL on a Linux server. We will cover installations for popular distributions like Ubuntu, CentOS, and Fedora. We will also discuss how to configure MySQL for the best performance, secure the installation, and start the service. By the end of this guide, you will have a fully functional MySQL server ready for use.

Prerequisites

Before installing MySQL on your Linux server, ensure you have the following:

  • A Linux server (Ubuntu, CentOS, Fedora, etc.)
  • A user with sudo privileges
  • Terminal or SSH access to your server
  • A stable internet connection to download packages

Install MySQL on Various Linux Distributions

MySQL installation slightly varies depending on the Linux distribution you are using. Below are the steps to install MySQL on some of the most popular Linux distributions: Ubuntu/Debian, CentOS/RHEL, and Fedora.

Install MySQL on Ubuntu/Debian

  • Update your system’s package list:
sudo apt update
  • Install MySQL server:
sudo apt install mysql-server -y
  • Start and enable MySQL to start on boot:
sudo systemctl start mysql
sudo systemctl enable mysql
  • Verify the installation:
sudo systemctl status mysql

Install MySQL on CentOS/RHEL

  • Install MySQL on CentOS 7/RHEL 7 (via MySQL’s official repo):
sudo yum install https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm -y 
sudo yum install mysql-server -y 
  • For CentOS 8/RHEL 8, use:
sudo dnf install mysql-server -y
  • Start and enable MySQL:
sudo systemctl start mysqldsudo systemctl enable mysqld
  • Check MySQL status:
sudo systemctl status mysqld

Install MySQL on Fedora

  • Update system packages:
sudo dnf update -y
  • Install MySQL server:
sudo dnf install mysql-server -y
  • Start and enable MySQL:
sudo systemctl start mysqld sudo systemctl enable mysqld
  • Check MySQL status:
sudo systemctl status mysqld

Read: How to Install Nginx on a Linux Server

Securing MySQL Installation

After installing MySQL, it’s important to secure your installation by setting a root password and making a few adjustments to enhance the security of your MySQL server.

Step 1: Run the MySQL Secure Installation Script

MySQL has a built-in security script that helps secure your installation by removing insecure default settings.

To run the script, use the following command:

sudo mysql_secure_installation

You will be prompted to:

  • Set a root password: If you haven’t set a password for the root user, you’ll be asked to do so. Choose a strong, unique password.
  • Remove anonymous users: Recommended. This prevents users without a defined username from connecting to MySQL.
  • Disallow root login remotely: Recommended. This prevents the root user from logging in from outside the local machine.
  • Remove the test database and access to it: Recommended. The test database is often used for experimentation and should not be on production systems.
  • Reload privilege tables now? Say “Yes” to apply the changes immediately.

Step 2: Verify MySQL Root User Authentication

Ensure that the root user can authenticate properly. You can log into MySQL as the root user with:

sudo mysql -u root -p

Enter the root password when prompted, and you’ll be logged into the MySQL shell.

Check Out | How to Install Apache Web Server in Linux

Basic MySQL Configuration

After installing and starting MySQL, you might want to configure it for your specific needs. This section will guide you through basic MySQL configuration.

Edit the MySQL Configuration File

The MySQL configuration file is typically located at /etc/mysql/my.cnf on Ubuntu/Debian systems and /etc/my.cnf on CentOS/RHEL/Fedora. You can edit it to adjust performance settings or other MySQL options.

To edit the configuration file, nano text editor:

sudo nano /etc/mysql/my.cnf

For CentOS/RHEL/Fedora, use:

sudo nano /etc/my.cnf

Configure MySQL to Allow Remote Connections

By default, MySQL only allows connections from localhost. If you need to allow remote connections to your MySQL server, you will need to edit the bind-address directive in the configuration file.

Find the bind-address line and change it to:

bind-address = 0.0.0.0

This allows MySQL to accept connections from any IP address. Be sure to allow remote connections only if you have proper security measures (like a firewall).

Managing MySQL

Once MySQL is installed and configured, you must manage its service. This section will guide you through the basic commands to start, stop, and check the status of MySQL.

  • Start MySQL

If MySQL is not running, you can start it with the following command:

sudo systemctl start mysql

For CentOS/RHEL:

sudo systemctl start mysqld
  • Stop MySQL

To stop the MySQL service, use:

sudo systemctl stop mysql

For CentOS/RHEL:

sudo systemctl stop mysqld
  • Restart MySQL

If you make changes to the configuration or need to restart MySQL for any other reason, use:

sudo systemctl restart mysql

For CentOS/RHEL:

sudo systemctl restart mysqld

Step 5: Enable MySQL to Start on Boot

If you want MySQL to start automatically when your server reboots, run:

sudo systemctl enable mysql

For CentOS/RHEL:

sudo systemctl enable mysqld

Conclusion

Installing MySQL on a Linux server is a straightforward process that can be completed in just a few steps. Once MySQL is installed, securing and configuring the installation according to your needs will ensure your database runs smoothly and securely. Whether you are setting up MySQL for personal projects or production environments, this guide has provided you with the necessary tools and knowledge.

By now, you should have MySQL up and running on your server, with basic configurations in place. You can explore more advanced configurations, optimize performance, and integrate MySQL with other applications from here.

If you have any issues or need further assistance, refer to MySQL’s official documentation or seek help from the MySQL community.

Leave A Comment