How To: Restart MySQL

MariaDB-Foundation-vertical.png

If you’re hosting a website using the popular LAMP stack (Linux/Apache/MySQL/PHP), you’re going to have your services restart from time to time as part of maintenace. In this short post I’ll show you how to restart MySQL database.

As you will see from some of the data, I’m actually using MariaDB which aims to stay MySQL compatible in most of the ways, including the name and the syntax of running startup/shutdown scripts.

Confirm Status of MySQL service

On most of the modern Linux systems, you should probably use the systemctl command to confirm MySQL status:

root@dbm1:~ # systemctl status mysql
● mariadb.service - MariaDB database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
Drop-In: /etc/systemd/system/mariadb.service.d
└─migrated-from-my.cnf-settings.conf, timeout.conf
Active: active (running) since Mon 2018-08-20 00:43:33 IST; 4 months 27 days ago
Process: 11534 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
Process: 11414 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= || VAR=`/usr/bin/galera_recovery`; [ $? -eq 0 ] && systemctl set-environment _WSREP_START_POSITION=$VAR || exit 1 (code=exited, status=0/SUCCESS)
Process: 11411 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
Main PID: 11501 (mysqld)
Status: "Taking your SQL requests now..."
CGroup: /system.slice/mariadb.service
└─11501 /usr/sbin/mysqld --wsrep_start_position=b70c6b82-9064-11e5-9c67-3e39fe556c8b:45091479

Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.

What you’re looking for is the Active line of the output, which shows that the MySQL service is happily running since August 2018. Now, if it was stopped just now, you would see that the service is actually inactive:

root@dbm1:~ # systemctl status mysql
● mariadb.service - MariaDB database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
Drop-In: /etc/systemd/system/mariadb.service.d
└─migrated-from-my.cnf-settings.conf, timeout.conf
Active: inactive (dead) since Tue 2019-01-15 23:03:39 GMT; 8s ago
Process: 11534 ExecStartPost=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
Process: 11501 ExecStart=/usr/sbin/mysqld $MYSQLD_OPTS $_WSREP_NEW_CLUSTER $_WSREP_START_POSITION (code=exited, status=0/SUCCESS)
Process: 11414 ExecStartPre=/bin/sh -c [ ! -e /usr/bin/galera_recovery ] && VAR= || VAR=`/usr/bin/galera_recovery`; [ $? -eq 0 ] && systemctl set-environment _WSREP_START_POSITION=$VAR || exit 1 (code=exited, status=0/SUCCESS)
Process: 11411 ExecStartPre=/bin/sh -c systemctl unset-environment _WSREP_START_POSITION (code=exited, status=0/SUCCESS)
Main PID: 11501 (code=exited, status=0/SUCCESS)
Status: "MariaDB server is down"

Jan 15 23:03:34 dbm1.ts.im systemd[1]: Stopping MariaDB database server...
Jan 15 23:03:39 dbm1.ts.im systemd[1]: Stopped MariaDB database server.
Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.

Restart MySQL with systemctl

Just run this command to restart MySQL. If there are any issues, you’ll probably get an error:

root@dbm1:~ # systemctl restart mysql
root@dbm1:~ #

Restart MySQL using startup script

A more traditional and rather old-school approach is to stop or start MySQL using the /etc/init.d/mysql script. There is no point doing it in the systemctl-enabled Linux distros though, because this /etc/init.d/mysql script will actually use systemctl to manage the service, anyway:

root@dbm1:~ # /etc/init.d/mysql stop
Stopping mysql (via systemctl): [ OK ]
root@dbm1:~ # /etc/init.d/mysql start
Starting mysql (via systemctl): [ OK ]

That’s it for today!

See Also