How to Restart Mysql
Tuesday, Aug 13, 2024 | 4 minutes read | Update at Tuesday, Aug 13, 2024
MySQL is very popular opensource database used by millions of users. After installing MySQL we can manage the database service via command line interface. There are different ways to restart MySQL database server or service. To restart MySQL in Linux, you can use one of the following methods depending on your system’s service manager (either systemd
or init.d
):
1. Using systemctl
(for systems with systemd
):
The defacto command to manage MySQL database server is systemctl
. We can use the systemctl command in order to restart MySQL database service. The service management requires root privileges so we provide the sudo
command before the systemctl command.
sudo systemctl restart mysql
This command works on most modern Linux distributions (e.g., Ubuntu, CentOS, Debian) that use systemd
.
2. Using service
command:
Another command to restart mysql is service
. Similar to the systemctl we should provide the sudo command and use the restart
after the mysql
service name.
sudo service mysql restart
This command works on systems that use service
scripts, including older versions of Ubuntu and other distributions.
3. Using /etc/init.d/
script directly:
We can use the init.d
system manager in order to start mysql service like below.
sudo /etc/init.d/mysql restart
This command directly invokes the init script to restart MySQL, which may be used on older systems that do not use systemd
.
4. Using systemctl
with MariaDB:
MariaDB
is the fork of the MySQL and their are named and used interchangibly. The MySQL can be replaced with the MariaDB. We can restart MariaDB with the following systemctl command which is the same with the MySQL.
sudo systemctl restart mariadb
If you are using MariaDB (a popular fork of MySQL), replace mysql
with mariadb
.
5. Using service
command with MariaDB:
We can use the service command in order to restart MariaDB service like below.
sudo service mariadb restart
This command works similarly for MariaDB on systems using service
.
6. Check MySQL status before and after restarting:
Sometimes the restart of the MySQL service may not completed properly. We should check the status of the MySQL service with the systemctl status
command.
sudo systemctl status mysql
sudo systemctl restart mysql
sudo systemctl status mysql
This allows you to verify the status of MySQL before and after the restart.
7. Restarting MySQL within a Docker container:
The MySQL service may be running in a Docker container.We can restart the Docker container in order to start MySQL service in the container. We should replace the container_name
in the following example.
docker exec -it <container_name> service mysql restart
If MySQL is running inside a Docker container, use this command to restart it.
8. Using initctl
(for Upstart systems):
The initctl
command is old style service management command which can be also used to restart MySQL.
sudo initctl restart mysql
Some older distributions use Upstart; this command restarts MySQL in such systems.
9. Restarting MySQL with the MySQL admin command:
MySQL admin tools provide the mysqladmin
command which can be used to stop MySQL service and then we can start the service with the systemctl
command like below. Event this example seems to be weird it is an alternative.
sudo mysqladmin shutdown
sudo systemctl start mysql
This method manually shuts down and then starts MySQL again.
10. Restarting MySQL through a remote SSH session:
Some times we may need to restart a MySQL service on a remote host or service. We can use the ssh
command to execute service restart command which is systemctl
. We should also provide the user
and remote_host
information to the ssh command to connect properly. Keep in mind that the remote system user has required privileges like root or sudo to restart MySQL.
```bash
ssh user@remote_host 'sudo systemctl restart mysql'
```
*This command allows you to restart MySQL on a remote server via SSH.*
These examples cover various ways to restart MySQL depending on the specific Linux distribution, service manager, or environment.