How to Install MySQL on Linux: A Step-by-Step Guide

Prerequisites

A Linux distribution installed such as Ubuntu, CentOS, Fedora, or Debian
Root or sudo privileges
Basic command line knowledge
Basic understanding of Linux
Text editor knowledge on how to use basic text editors like nano or vim can be helpful
An internet connection since MySQL will be downloaded from a repository

Introduction

MySQL is one of the most popular relational database management systems used by developers, businesses, and web administrators. Whether you're setting up a local development environment or preparing a production server, installing MySQL on Linux is a crucial skill for managing databases effectively.

In this step-by-step guide, we'll walk you through the process of installing MySQL on a Linux system. From the initial installation to configuring your MySQL server, we'll cover everything you need to get up and running. This guide is suitable for users of various Linux distributions, including Ubuntu, CentOS, and Debian, and will ensure that you have MySQL installed and ready to use in no time.

Update Your Package Index

First, it's a good practice to update your package index to ensure that you're installing the latest version of MySQL available in your package manager's repository.

For Ubuntu/Debian

sudo apt update

For CentOS/RHEL

sudo yum update
sudo dnf update # CentOS 8 or newer

Install MySQL

Now, let's install MySQL based on your Linux distribution.

On Ubuntu/Debian

Install the MySQL server package using apt

sudo apt install mysql-server

On CentOS/RHEL

You may need to install the MySQL repository first before you can install the server

sudo yum localinstall https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

sudo dnf localinstall https://dev.mysql.com/get/mysql80-community-release-el8-1.noarch.rpm # CentOS 8 or newer

After that, install MySQL:

sudo yum install mysql-community-server

sudo dnf install mysql-community-server # CentOS 8 or newer

Start MySQL Service

Once the installation is complete, you need to start the MySQL service and enable it to start automatically on boot.

On Ubuntu/Debian

sudo systemctl start mysql

sudo systemctl enable mysql

On CentOS/RHEL

sudo systemctl start mysqld

sudo systemctl enable mysqld

Checking the Status of MySQL Service (mysqld) on Linux

sudo systemctl status mysqld

Run MySQL Secure Installation

MySQL comes with a built-in security script to help secure your installation. Run the following command:

sudo mysql_secure_installation

This script will prompt you to set up a root password and configure other security options, such as removing insecure default settings.

Log in to MySQL

Once MySQL is installed and secured, you can log in to the MySQL command-line interface to verify that it’s running.

On Ubuntu/Debian

sudo mysql

On CentOS/RHEL

sudo mysql -u root -p

Enter the root password you set up earlier. You should now be inside the MySQL shell.

Verify MySQL Installation

Once inside the MySQL shell, you can verify the installation by checking the MySQL version:

SELECT VERSION();

This should return the version of MySQL that is installed.

Allow Remote Access (Optional)

Create a new MySQL user with remote access privileges

To create a user that can connect from any host (replace username and password with your desired values):

CREATE USER 'username'@'%' IDENTIFIED BY 'password';

The % symbol allows the user to connect from any IP address. If you want to restrict it to a specific IP, replace % with the desired IP (e.g., 'username'@'192.168.1.100' ).

Grant the user necessary privileges

Grant the new user privileges to access a specific database. For example, to allow full privileges on a specific database (replace database_name with the actual database name):

GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'%';

If you want the user to have global privileges, replace database_name.* with *.*

Flush privileges

After granting privileges, execute the following command to apply the changes:

FLUSH PRIVILEGES;

Configure Firewall

If your server has a firewall enabled, you need to allow MySQL's default port (3306) through the firewall.

On Ubuntu/Debian (using UFW)

sudo ufw allow 3306

On CentOS/RHEL (using firewalld)

sudo firewall-cmd --permanent --add-port=3306/tcp
sudo firewall-cmd --reload

Test MySQL Connection using telnet

You can test if MySQL is accessible remotely by connecting from another machine using the following command (replace with your server's IP)

You can use the following command to check if the MySQL server is accessible on port 3306:

telnet host_IP 3306

Replace host_IP with the IP address of the MySQL server you're trying to connect to.

If the connection fails, you will see something like this:

Trying <host_IP>... telnet: Unable to connect to remote host: Connection refused

Or if the port is blocked by a firewall:

telnet: Unable to connect to remote host: No route to host

MySQL error log file locations

On Ubuntu

/var/log/mysql/error.log

  • This is the standard location for MySQL's error log on Ubuntu (and other Debian-based distributions).

On CentOS

/var/log/mysqld.log

  • CentOS typically uses this location for the MySQL error log file, though it could vary depending on your version of MySQL and how it was configured.

Final Thoughts

Installing MySQL on Linux is a simple and efficient process that provides a powerful relational database management system for your projects. By following the step-by-step guide, you can ensure that your MySQL installation is secure, up-to-date, and configured to meet the needs of your applications.

Remember to choose the appropriate version and repository based on your Linux distribution, and don't forget to secure your MySQL installation after setup. Whether you're running a development environment or a production server, MySQL offers a reliable and scalable solution for managing your data.

With the foundational setup complete, you can now dive deeper into database management, performance optimization, and more advanced features of MySQL. Happy coding and database managing!

Popular (all time)

Related articles

How to Edit Your WordPress Admin Username and Author Slug via MySQL

In this guide, we’ll walk you through the process of editing both the admin username and author slug using MySQL. Whether you’re looking to strengthen your site's security or simply personalize your author URL, this straightforward method will help you make the changes with ease.

Getting Started with React

In this guide, we will cover the basics of setting up a local development environment, starting a simple React project, and the basics of how it works.

How to Simplify Your Terminal with Custom Bash Aliases

By creating custom shortcuts for your most-used commands, you can save time, reduce errors, and make your terminal experience faster and more enjoyable. In this guide, we’ll show you how to create and manage your own Bash aliases to simplify your terminal workflow and boost your productivity.