How to Install MySQL on Linux

Data is the king of today's world. Businesses become more and more data-driven, and digital transformation steers development. But what does data mean to the growth of your business? Literally everything! That’s why storing, accessing, and analyzing data assume a large dimension nowadays, and the importance of having a proper database management system has soared infinitely high. In this article, we will see how to install MySQL on Linux.

"Why MySQL and why Linux?" you would ask. Because it is, without exaggeration, the best combination for the effective development of database-centered software products. Both solutions are reliable, secure, and open-source. Here we will walk through the prerequisites for installing MySQL on Linux, have an overview of the peculiarities of installation via the MySQL Yum repository, and explore how to install MySQL on Linux from the command line.

The advantages of using Linux

Linux is an extremely popular operating system, just like Windows or macOS. However, unlike either of its rivals, Linux is open-source, which means that anyone can run, study, modify, and redistribute its source code. Today Linux is everywhere – half of the professional developers use Linux-based operating systems, and, moreover, Linux powers 85% of smartphones.

Benefits of using Linux for your business

  • Low cost
  • High performance
  • Ease of use
  • Security
  • Flexibility
  • Regular updates
  • Variety of distributions
  • Support for all common programming languages

What are the prerequisites for installing MySQL on Linux?

The prerequisites for the installation of MySQL on Linux are as follows:

  • Stable network connection
  • A system running Linux
  • Access to the terminal
  • A user account with sudo privileges

Before you start

There are several ways to install MySQL on Linux which depend primarily on the Linux distribution you are running. Thus, before starting to install MySQL, check your Linux distro.

For example, the MySQL Yum repository for Oracle Linux, Red Hat Enterprise Linux, and CentOS provides RPM packages for installing MySQL Server and client, MySQL Workbench, MySQL Utilities, etc. The MySQL APT repository provides .deb packages for installing and managing MySQL server, client, and other components on Debian and Ubuntu platforms.

In this article, we will demonstrate to you how to install MySQL on CentOS 7.

How to install MySQL from a Yum repository

Note
The following instructions refer only to the new installation of MySQL. In case you already have MySQL installed, you will need to upgrade or replace the installed MySQL package.

1 - Set up a Yum repository

To begin with, we need to add a MySQL Yum repository to our system's repository list. For this, we run the following command.

sudo rpm -Uvh https://repo.mysql.com/mysql80-community-release-el7-3.noarch.rpm
Download the MySQL repository by executing the following command

2 - Install the latest version of MySQL

Since the Yum repository we’ve just downloaded has multiple repository configurations for multiple MySQL versions, we need to disable all repositories in the MySQL repo file. For this, we run the following command.

sudo sed -i 's/enabled=1/enabled=0/' /etc/yum.repos.d/mysql-community.repo

Now that we have downloaded the package for our Linux platform and disabled the repositories, we need to install MySQL server using the following command.

sudo yum --enablerepo=mysql80-community install mysql-community-server

3 - Start the MySQL service

All we need to do now is to start the MySQL service using the following command. Provide the password when the system asks for it.

service mysqld start
Install MySQL on Linux - start mysql server

When you install MySQL server, the root user account is granted a temporary password; to be able to work with MySQL on Linux, you need to get this default password. To show the password, run the following command and provide a password for the Linux user when asked.

sudo grep "A temporary password" /var/log/mysqld.log
How to install MySQL on Linux

Afterward, you will need this temporary password to change the password of the root user account.

Config the MySQL root user

Now that we have started the MySQL service and got the temporary password for the root user, we can proceed to install MySQL and configure the root user.

First of all, run the following command to secure your MySQL server.

mysql_secure_installation 

The system will ask you for the current password of the root account. Provide the temporary password you got earlier. Next, you will need to enter and confirm a new password for the root user account. After that, the system will prompt a bunch of questions, and we recommend you to answer 'Yes' to each one.

  • Remove anonymous users?
  • Disallow root login remotely?
  • Remove test database and access to it?
  • Reload privilege tables now?
Install MySQL on Linux - answer installer questions

Afterward, it is recommended to restart and enable the MySQL service again.

service mysqld restart

If necessary, you can configure the MySQL service to start automatically at the system startup.

chkconfig mysqld on

Log in to MySQL

Run the following command to connect to MySQL server. When asked for the password for the root user, enter the password and press Enter.

mysql -u root -p
How to install MySQL on Linux - connect to MySQL

Run the show databases; command to display all databases residing on the current server.

Install MySQL on Linux - show databases command

Choose another MySQL release version (5.6, 5.7)

To install a different MySQL release version, you will first need to uninstall the current version and remove all the packages associated with it. Then you will be able to download and install the required version.

Follow the link to find out how to uninstall MySQL 8.0 from your Linux system.

Having removed the previous version from your system, you can proceed to download and install the required MySQL release version.

Suppose, we want to install MySQL 5.7.

1. We add the MySQL 5.7 repository to our CentOS 7 system.

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

2. Next, we install MySQL server 5.7.

sudo yum install mysql-community-server

3. Finally, we check the current MySQL version to make sure that the downgrade from MySQL 8.0 to MySQL 5.7 has been successful.

mysql –version
Install MySQL on Linux - check MySQL version

How to update MySQL with Yum

By running the following command, you will have Yum update all MySQL components on your system. Note that the process might take some time.

sudo yum update

After the update by Yum, MySQL server will be restarted. It is recommended to run the following command to check and resolve any possible incompatibilities.

mysql_upgrade

With Yum, you can also update only a specific component. For this, run the following command to list all the installed packages for the MySQL components.

sudo yum list installed | grep "^mysql"

Then identify the package to be updated and run the following command.

sudo yum update package-name

Installing MySQL without Yum

In case your Linux distribution doesn't support Yum, you can try using the APT repository.

Note
The Oracle Linux, Red Hat Enterprise Linux, CentOS, and Fedora distributions work well with the MySQL Yum repository, while Debian and Ubuntu distros require the APT repository.

If you are a Debian or Ubuntu user, to install MySQL to your Linux system, take the following steps.

1. Download the MySQL repository by executing the following command.

wget https://dev.mysql.com/get/mysql-apt-config_0.8.12-1_all.deb

2. Next, install the package.

sudo dpkg -i mysql-apt-config_0.8.12-1_all.deb

3. In the package configuration window, specify your system and the version of the MySQL server you want to install. Click OK to confirm.

4. Then, update the APT repository.

sudo apt update

5. And finally, run the following command to install MySQL server and client. Press Y to begin the installation, then insert a password for the root user when asked.

sudo apt install -f mysql-client=8.0* mysql-community-server=8.0* mysql-server=8.0*

How to install MySQL on any Linux distribution

All in all, the algorithm of installing MySQL on any Linux distribution can be narrowed down to three major steps.

Step 1: Download MySQL server and client

The command for downloading a MySQL package will depend on the Linux distribution you are using.

For RHEL/CentOS 8

wget https://repo.mysql.com/mysql80-community-release-el8

For RHEL/CentOS 7

wget https://repo.mysql.com/mysql80-community-release-el7

For Fedora 35

wget https://dev.mysql.com/get/mysql80-community-release-fc35-1.noarch.rpm

For Fedora 34

# wget https://dev.mysql.com/get/mysql80-community-release-fc34-1.noarch.rpm

For Ubuntu 20.04, 22.04, or later

wget https://dev.mysql.com/get/mysql-apt-config_0.8.12-1_all.deb

For Debian 11

wget https://dev.mysql.com/get/mysql-apt-config_0.8.20-1_all.deb
-1.noarch.rpm

For Debian 10

wget -q http://repo.mysql.com/mysql-apt-config_0.8.15-1_all.deb

Step 2: Remove the default MySQL (if it exists)

And again, the command to uninstall MySQL from your Linux system will depend on the Linux distribution you are using.

For the CentOS and RedHat systems

sudo yum remove mysql mysql-server

For the Ubuntu and Debian systems

sudo apt remove mysql mysql-server 

For the Fedora 22+ systems

sudo dnf remove mysql mysql-server 

Step 2: Install MySQL package

Now you are all set for installing MySQL on your Linux system.

For the CentOS and RedHat systems

sudo yum install mysql-community-server 

For the Ubuntu and Debian systems

sudo apt-get install mysql-server 

For the Fedora 22+ systems

sudo dnf install mysql-community-server 

Need a reliable solution to manage your
MySQL databases?

Get the most advanced IDE on the market - dbForge Studio for MySQL

Using a MySQL GUI tool on Linux

Handling relational database management systems on Linux requires a certain level of expertise. But equipping yourself with the right tool will help you do the job much faster and easier. One of the best GUI tools for MySQL on the market today that covers Windows, Linux, and macOS platforms is dbForge Studio for MySQL – a robust integrated environment, which is suitable for effectively managing even the most demanding MySQL development projects.

Database Design

dbForge Studio for MySQL offers tools that help you instantly create database diagrams, objects, and entire projects, as well as efficiently compare and sync database schemas and table data.

Make database migration faster
2X
Saved time

Database Development

You can write, debug, and run SQL queries faster using the built-in code completion and formatting tools, as well as measure and optimize query performance in a matter of moments.

Make database migration faster
2X
Faster coding speed

Database Management

An integrated set of database management tools help import and export table data, migrate, compare, and synchronize databases, as well as generate meaningful data for effective testing.

Make database migration faster
16X
Faster database migration

Database Administration

With the help of dbForge Studio, you can manage accounts, set up permissions, monitor open sessions and their activity, as well as perform table maintenance. In addition, it is possible to back up and restore databases to/from your backup files.

Make database migration faster
+40%
Daily performance

Data Analysis and Reporting

dbForge Studio makes data analysis and reporting far less time-consuming with a slew of auxiliary tools.

Make database migration faster
300%
Efficiency boost

How to connect to MySQL server on Linux from dbForge Studio for MySQL

  1. On the Database menu, select New Connection.
  2. On the Database Connection Properties> General tab, specify the connection details:
    • Type: Select the connection type from the drop-down list.
    • Host: Provide the host name
    • Port: Provide the port number
    • User and Password: Enter the user credential details respectively. By default, the password is saved automatically. If you don’t want to save the password, clear the Allow saving password checkbox.
    • Database: Enter the name of a MySQL database you want to connect to or select it from the drop-down list.
    • Optional: Connection Name: The connection name is generated automatically from the host name. If you want to create a distinctive name for the connection, type the new name.

Click Test Connection to verify the connection details you have provided. Click Connect to connect to a MySQL server.

How to allow remote connections to MySQL server on Linux

Note
The instructions provided in this section apply to the CentOS Linux distribution.

1. Log in to your MySQL server and run the following command to determine the location of the MySQL configuration file.

>mysql --help | grep "Default options" -A 1

Then, run the below command to uninstall your MySQL server and agree to remove MySQL packages:

sudo apt-get purge mysql-server mysql-client

2. Edit the configuration file by adding the bind address and save the changes. Add the following line:

bind-address=server.ip.

or

bind-address = 0.0.0.0

3. Open the 3306 port in the firewall by executing the following command.

>iptables -A INPUT -i eth0 -p tcp --destination-port 3306 -j ACCEPT

4. Then save the configurations using the following commands.

>service iptables save
>firewall-cmd --zone=public --add-port=3306/tcp --permanent
>firewall-cmd --reload

5. Create a user with access from a specific IP address (for example, jordans@'192.168.1.100') or from any host ('username'@'%').

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

6. And finally, grant privileges to the newly created user.

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

To finish with, execute the following command.

FLUSH PRIVILEGES;

How to uninstall MySQL from Linux

To remove MySQL from the Linux system completely, you first need to uninstall MySQL server.

sudo yum remove mysql mysql-server

And then remove all the packages associated with MySQL. For this, first run the command to list all the installed packages for the MySQL components.

rpm -qa | grep mysql

Next, run the following command to remove each of the listed packages.

yum remove package-name
Uninstall MySQL on Linux