前一段時間看到MySQL官方視頻的Oracle工程師在mysql shell里面重啟mysql實例,感覺這個操作很方便,所以來試試,下面為該工程師的操作截圖
1.MySQL Shell?通過root用戶連上mysql,shutdown mysql實例
[root@mysql8_3?bin]# mysqlsh
MySQL Shell?8.4.5
Copyright (c)?2016,?2025, Oracle?and/or?its affiliates.
Oracle?is?a registered trademark?of?Oracle Corporation?and/or?its affiliates.
Other names may be trademarks?of?their respective owners.
Type?'\help'?or?'\?'?for?help;?'\quit'?to?exit.
MySQL?SQL?>?\connect?--mysql
Creating a Classic session?to?'root@localhost'
Fetching?global?names?for?auto-completion... Press?^C?to?stop.
Your MySQL connection id?is?377
Server version:?8.4.4-commercial MySQL Enterprise Server?-?Commercial
No?default?schema selected; type \use?<schema>?to?set?one.
MySQL localhost?SQL?>?select?user();
+----------------+
|?user()|
+----------------+
|?root@localhost?|
+----------------+
1?row?in?set?(0.0008?sec)
MySQL ?localhost ?SQL?>?shutdown;
Query OK,?0?rows?affected (0.0005?sec)
MySQL ?localhost ?SQL?>?select?user();
ERROR:?2013?(HY000): Lost connection?to?MySQL server during query
The?global?session got disconnected..
Attempting?to?reconnect?to?'mysql://root@/tmp%2Fmysql.sock'........
......
The?global?session could?not?be reconnected automatically.
Please use?'\reconnect'?instead?to?manually reconnect.
MySQL ?SQL?>
MySQL ?SQL?>?\reconnect
Attempting?to?reconnect?to?'mysql://root@/tmp%2Fmysql.sock'..............
The?global?session could?not?be reconnected automatically.
Please use?'\reconnect'?instead?to?manually reconnect.
MySQL ?SQL?>
2.我們從系統上看一下mysql服務,看來默認在mysqlsh里shutdown mysql實例可以使用
[root@mysql8_3 bin]# systemctl status mysqld83308.service
● mysqld83308.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld83308.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Thu 2025-04-24 12:00:39 CST; 57s ago
Process: 16169 ExecStart=/u01/mysql3308/base/bin/mysqld --defaults-file=/u01/mysql3308/my.cnf (code=exited, status=0/SUCCESS)
Main PID: 16169 (code=exited, status=0/SUCCESS)
4月24 11:57:13 mysql8_3.52 systemd[1]: Started MySQL Server.
4月24 12:00:39 mysql8_3.52 systemd[1]: mysqld83308.service: Succeeded.
[root@mysql8_3 bin]#
3.我們啟動一下mysql服務
[root@mysql8_3 bin]# systemctl start mysqld83308.service
[root@mysql8_3 bin]# systemctl status mysqld83308.service
● mysqld83308.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld83308.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2025-04-24 12:02:22 CST; 2s ago
Main PID: 16417 (mysqld)
Tasks: 17 (limit: 22962)
Memory: 579.5M
CGroup: /system.slice/mysqld83308.service
└─16417 /u01/mysql3308/base/bin/mysqld --defaults-file=/u01/mysql3308/my.cnf
4月24 12:02:22 mysql8_3.52 systemd[1]: Started MySQL Server.
[root@mysql8_3 bin]#
4.我們在mysqlsh里執行重啟命令,報錯了
[root@mysql8_3?bin]# mysqlsh
MySQL Shell?8.4.5
Copyright (c)?2016,?2025, Oracle?and/or?its affiliates.
Oracle?is?a registered trademark?of?Oracle Corporation?and/or?its affiliates.
Other names may be trademarks?of?their respective owners.
Type?'\help'?or?'\?'?for?help;?'\quit'?to?exit.
MySQL ?SQL?>?\connect?--mysql
Creating a Classic session?to?'root@localhost'
Fetching?global?names?for?auto-completion... Press?^C?to?stop.
Your MySQL connection id?is?44
Server version:?8.4.4-commercial MySQL Enterprise Server?-?Commercial
No?default?schema selected; type \use?<schema>?to?set?one.
MySQL ?localhost ?SQL?>?select?user();
+----------------+
|?user()|
+----------------+
|?root@localhost?|
+----------------+
1?row?in?set?(0.0004?sec)
MySQL ?localhost ?SQL?>?restart;
ERROR:?3707?(HY000): Restart server failed (mysqld?is?not?managed?by?supervisor process).
MySQL ?localhost ?SQL?>
5.看看官方文檔的實現腳本
#!/bin/bash
export?MYSQLD_PARENT_PID=$$
export?MYSQLD_RESTART_EXIT=16
while?true?;?do
bin/mysqld mysqld options here
if?[ $? -ne?$MYSQLD_RESTART_EXIT?];?then
break
fi
Done
6.我們根據官方文檔創建mysql?啟動腳本并啟動數據庫,當然啟動前先要停止mysql實例
[root@mysql8_3 mysql3308]# cat start.sh
#!/bin/bash
export?MYSQLD_PARENT_PID=$$
export?MYSQLD_RESTART_EXIT=16
while?true?;?do
/u01/mysql3308/base/bin/mysqld --defaults-file=/u01/mysql3308/my.cnf
if?[ $? -ne?$MYSQLD_RESTART_EXIT?];?then
break
fi
[root@mysql8_3 mysql3308]# chmod +x start.sh
[root@mysql8_3 mysql3308]# ./start.sh
7.測試,通過使用官方提供的腳本格式編寫的start.sh腳本啟動數據庫,能夠實現mysqlsh重啟mysql實例
8.根據這個腳本的邏輯修改systemd啟動腳本
[root@mysql8_3 ~]# vim /usr/lib/systemd/system/mysqld83308.service
# This service is actually a systemd target,
# but we are using a service since targets cannot be reloaded.
[Unit]
Description=MySQL Server
Documentation=mysqld.service
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
RestartForceExitStatus=16
Environment=MYSQLD_PARENT_PID=1
User=mysql
Group=mysql
ExecStart=/u01/mysql3308/base/bin/mysqld --defaults-file=/u01/mysql3308/my.cnf
LimitNOFILE?=?5000
9.重啟服務
[root@mysql8_3 bin]# systemctl daemon-reload
[root@mysql8_3 bin]# systemctl start mysqld83308.service
[root@mysql8_3 bin]# systemctl status mysqld83308.service
● mysqld83308.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld83308.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2025-04-24 12:19:43 CST; 2s ago
Main PID: 17310 (mysqld)
Tasks: 30 (limit: 22962)
Memory: 672.4M
CGroup: /system.slice/mysqld83308.service
└─17310 /u01/mysql3308/base/bin/mysqld --defaults-file=/u01/mysql3308/my.cnf
4月24 12:19:43 mysql8_3.52 systemd[1]: Started MySQL Server.
10.測試使用systemd啟動腳本的mysqlsh重啟效果
這兩種方法均能實現,原理來自官方腳本
參考:
https://dev.mysql.com/doc/refman/8.4/en/restart.html
https://www.freedesktop.org/software/systemd/man/latest/systemd.exec.html#Environment
https://www.jinbuguo.com/systemd/systemd.service.html