1. Overview

MySQL is an open-source DBMS used to store, organize, and retrieve data. The MySQL server may need to restart in order to apply updates or make configuration modifications.

In this tutorial, we’ll show how to restart a MySQL server on Linux.

2. Restart MySQL on Linux

In Linux, MySQL might need to be restarted for a number of reasons. Some common reasons for this include applying updates or patches to MySQL software. It is also useful in troubleshooting or making configuration adjustments to the server.

Before restarting the server, it is best to take a database snapshot. MySQL restarts can cause data loss, so it is important to keep a backup in case of an unexpected event. There are various methods to backup data, including using the mysqldump tool or making a copy of the data records.

2.1. Using the mysql Service

In Linux, the systemctl service is one of the most common ways to restart a MySQL server. MySQL services simplify the management and processing of requests from clients. Also, it loads configuration files and manages the runtime environment. Let’s look at the command to restart the MySQL server using the service:

$ sudo systemctl restart mysqld

The above command restarts the server. It is important to note that restarting will temporarily disrupt any ongoing connections to the database. Let’s check the status of the server:

$ sudo systemctl status mysqld
   mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2023-01-01 07:18:22 UTC; 1s ago
   ...
   CGroup: /system.slice/mysqld.service
           └─16625 /usr/sbin/mysqld

In the above output, we can see that the server is up and running.

2.2. Using the init.d Script

Another way to restart a MySQL server on Linux is using the init.d script. System V init scripts typically reside in /etc/init.d and manage system services on Linux systems. To restart the MySQL server using this method, execute the following command:

$ sudo /etc/init.d/mysql restart

By running this command, it will prompt us to provide root user credentials and then gently reboots the server.

2.3. Using the mysqld Command

We can also use the mysqld command directly in the terminal to restart the server. The mysqld command only allows us to start the server, so we must first stop the server. To gracefully stop the server, we’ll use the mysqladmin command:

$ sudo mysqladmin -u root -p shutdown

Here, we’ll be prompted for the password to stop the server. Also, we can use the SHUTDOWN command in a connection terminal to stop a running MySQL server. To demonstrate, let’s look at the command:

mysql> SHUTDOWN;

In the command above, the server closes all active connections and stops running. As soon as the server stops, it won’t accept any new connections or process any queries. Furthermore, remember that proper authorization is required to execute the above command via a connected MySQL server. Now, let’s look at the command to start the server again:

$ sudo mysqld

Using the above steps, we’ll successfully restart the MySQL server.

3. Conclusion

In this article, we discussed different ways to restart a MySQL server on Linux. First, we looked at why restarting a MySQL server is critical. After that, we also discussed different ways to restart the server gracefully.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.