在MySQL Shell里 重启MySQL 8.4实例

前一段时间看到MySQL官方视频的Oracle工程师在mysql shell里面重启mysql实例,感觉这个操作很方便,所以来试试,下面为该工程师的操作截图

1.MySQL Shell 通过root用户连上mysql,shutdown mysql实例

复制代码
[root@mysql8_3 bin]# mysqlshMySQL Shell 8.4.5Copyright (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 --mysqlCreating a Classic session to 'root@localhost'Fetching global names for auto-completion... Press ^C to stop.Your MySQL connection id is 377Server version: 8.4.4-commercial MySQL Enterprise Server - CommercialNo 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 queryThe 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 > \reconnectAttempting 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 ServerLoaded: loaded (/usr/lib/systemd/system/mysqld83308.service; enabled; vendor preset: disabled)Active: inactive (dead) since Thu 2025-04-24 12:00:39 CST; 57s agoProcess: 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 ServerLoaded: loaded (/usr/lib/systemd/system/mysqld83308.service; enabled; vendor preset: disabled)Active: active (running) since Thu 2025-04-24 12:02:22 CST; 2s agoMain PID: 16417 (mysqld)Tasks: 17 (limit: 22962)Memory: 579.5MCGroup: /system.slice/mysqld83308.service└─16417 /u01/mysql3308/base/bin/mysqld --defaults-file=/u01/mysql3308/my.cnf4月24 12:02:22 mysql8_3.52 systemd[1]: Started MySQL Server.[root@mysql8_3 bin]#

4.我们在mysqlsh里执行重启命令,报错了

复制代码
[root@mysql8_3 bin]# mysqlshMySQL Shell 8.4.5Copyright (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 --mysqlCreating a Classic session to 'root@localhost'Fetching global names for auto-completion... Press ^C to stop.Your MySQL connection id is 44Server version: 8.4.4-commercial MySQL Enterprise Server - CommercialNo 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/bashexport MYSQLD_PARENT_PID=$$export MYSQLD_RESTART_EXIT=16while true ; dobin/mysqld mysqld options hereif [ $? -ne $MYSQLD_RESTART_EXIT ]; thenbreakfiDone

6.我们根据官方文档创建mysql 启动脚本并启动数据库,当然启动前先要停止mysql实例

复制代码
[root@mysql8_3 mysql3308]# cat start.sh#!/bin/bashexport MYSQLD_PARENT_PID=$$export MYSQLD_RESTART_EXIT=16while true ; do/u01/mysql3308/base/bin/mysqld --defaults-file=/u01/mysql3308/my.cnfif [ $? -ne $MYSQLD_RESTART_EXIT ]; thenbreakfi

[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 ServerDocumentation=mysqld.serviceAfter=network.targetAfter=syslog.target[Install]WantedBy=multi-user.target[Service]RestartForceExitStatus=16Environment=MYSQLD_PARENT_PID=1User=mysqlGroup=mysqlExecStart=/u01/mysql3308/base/bin/mysqld --defaults-file=/u01/mysql3308/my.cnfLimitNOFILE = 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 ServerLoaded: loaded (/usr/lib/systemd/system/mysqld83308.service; enabled; vendor preset: disabled)Active: active (running) since Thu 2025-04-24 12:19:43 CST; 2s agoMain PID: 17310 (mysqld)Tasks: 30 (limit: 22962)Memory: 672.4MCGroup: /system.slice/mysqld83308.service└─17310 /u01/mysql3308/base/bin/mysqld --defaults-file=/u01/mysql3308/my.cnf4月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

相关推荐
在未来等你9 小时前
SQL进阶之旅 Day 21:临时表与内存表应用
sql·mysql·postgresql·database·temporary-table·memory-table·sql-optimization
小Tomkk10 小时前
阿里云 RDS mysql 5.7 怎么 添加白名单 并链接数据库
数据库·mysql·阿里云
寒山李白13 小时前
MySQL复杂SQL(多表联查/子查询)详细讲解
sql·mysql·子查询·多表联查
冰橙子id13 小时前
centos7编译安装LNMP架构
mysql·nginx·架构·centos·php
玛奇玛丶13 小时前
面试官:千万级订单表新增字段怎么弄?
后端·mysql
天天摸鱼的java工程师14 小时前
从被测试小姐姐追着怼到运维小哥点赞:我在项目管理系统的 MySQL 优化实战
java·后端·mysql
Clang's Blog14 小时前
一键搭建 WordPress + MySQL + phpMyAdmin 环境(支持 PHP 版本选择 & 自定义配置)
数据库·mysql·php·wordpr
异常君15 小时前
高并发数据写入场景下 MySQL 的性能瓶颈与替代方案
java·mysql·性能优化
RestCloud15 小时前
如何通过ETLCloud实现跨系统数据同步?
数据库·数据仓库·mysql·etl·数据处理·数据同步·集成平台
程序员岳焱16 小时前
Java 与 MySQL 性能优化:MySQL 慢 SQL 诊断与分析方法详解
后端·sql·mysql