How to start and stop mysql using the systemctl command.
**All the below steps are performed as the ROOT user
=> Below are the setting in the my.cnf file of this demo environment.
cat /etc/my.cnf [mysqld] basedir=/u01/mysql/base datadir=/u01/mysql/data log_error=/u01/mysql/logs/error.log port=3306 socket=/u01/mysql/data/mysql.sock
1. Go to the MySQL base directory location and add basedir path and datadir path to the file mysql.server inside support-files directory.
cd /u01/mysql/base/support-files vi mysql.server basedir=/u01/mysql/base datadir=/u01/mysql/data
2. Execute the file to check the status.
root@Server:/u01/mysql/base/support-files# ./mysql.server status * MySQL running (7046) root@Server:/u01/mysql/base/support-files# ./mysql.server stop Shutting down MySQL root@Server:/u01/mysql/base/support-files# ./mysql.server start Starting MySQL .Logging to '/u01/mysql/data/Server.err'. ...... * root@Server:/u01/mysql/base/support-files# ./mysql.server stop Shutting down MySQL
3. Copy mysql.server file to /etc/init.d directory and rename to mysql
root@Server:/u01/mysql/base/support-files# cp -r mysql.server /etc/init.d/ root@Server:/u01/mysql/base/support-files# cd /etc/init.d/ root@Server:/etc/init.d# mv mysql.server mysql
4. Check the status and start the mysql services.
root@Server:~# /etc/init.d/mysql status ○ mysql.service - LSB: start and stop MySQL Loaded: loaded (/etc/init.d/mysql; generated) Active: inactive (dead) Docs: man:systemd-sysv-generator(8) root@Server:~# root@Server:~# /etc/init.d/mysql start Starting mysql (via systemctl): mysql.service. root@Server:~# root@Server:~# /etc/init.d/mysql status ● mysql.service - LSB: start and stop MySQL Loaded: loaded (/etc/init.d/mysql; generated) Active: active (exited) since Fri 2024-03-08 15:29:07 NZDT; 3s ago Docs: man:systemd-sysv-generator(8) Process: 7426 ExecStart=/etc/init.d/mysql start (code=exited, status=0/SUCCESS) CPU: 904ms Mar 08 15:29:07 Server systemd[1]: Starting LSB: start and stop MySQL... Mar 08 15:29:07 Server mysql[7426]: Starting MySQL Mar 08 15:29:07 Server mysql[7426]: * Mar 08 15:29:07 Server systemd[1]: Started LSB: start and stop MySQL. Mar 08 15:29:07 Server mysql[7434]: 2024-03-08T02:29:07.885088Z mysqld_safe A mysqld process already exists root@Server:~#
5. Stop and start the MySQL services using the systemctl command
root@Server:~# systemctl start mysql root@Server:~# root@Server:~# systemctl status mysql ● mysql.service - LSB: start and stop MySQL Loaded: loaded (/etc/init.d/mysql; generated) Active: active (running) since Sun 2024-03-10 16:25:45 NZDT; 6s ago Docs: man:systemd-sysv-generator(8) Process: 60214 ExecStart=/etc/init.d/mysql start (code=exited, status=0/SUCCESS) Tasks: 40 (limit: 6967) Memory: 461.3M CPU: 13.370s CGroup: /system.slice/mysql.service ├─60228 /bin/sh /u01/mysql/base/bin/mysqld_safe --datadir=/u01/mysql/data --pid-file=/u01/mysql/data/MySQLServer.pid └─60408 /u01/mysql/base/bin/mysqld --basedir=/u01/mysql/base --datadir=/u01/mysql/data --plugin-dir=/u01/mysql/base/lib/plugin --user=mysql --lo> Mar 10 16:25:34 MySQLServer systemd[1]: Starting LSB: start and stop MySQL... Mar 10 16:25:34 MySQLServer mysql[60214]: Starting MySQL Mar 10 16:25:45 MySQLServer mysql[60214]: .......... * Mar 10 16:25:45 MySQLServer systemd[1]: Started LSB: start and stop MySQL. lines 1-16/16 (END) root@Server:~#