在MySQL Shell里 重启MySQL 8.4实例

本文涉及的产品
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 Tair(兼容Redis),内存型 2GB
简介: 在MySQL Shell里 重启MySQL 8.4实例

前一段时间看到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 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 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实例
1.png

8.根据这个脚本的逻辑修改systemd启动脚本

[root@mysql8_3 ~]# vim /usr/lib/systemd/system/mysqld83308.service

RestartPreventExitStatus=1

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重启效果
2.png

这两种方法均能实现,原理来自官方脚本

参考:

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

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。   相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情: https://www.aliyun.com/product/rds/mysql 
相关文章
|
1月前
|
关系型数据库 MySQL 数据库
自建数据库如何迁移至RDS MySQL实例
数据库迁移是一项复杂且耗时的工程,需考虑数据安全、完整性及业务中断影响。使用阿里云数据传输服务DTS,可快速、平滑完成迁移任务,将应用停机时间降至分钟级。您还可通过全量备份自建数据库并恢复至RDS MySQL实例,实现间接迁移上云。
|
1月前
|
存储 弹性计算 关系型数据库
如何通过控制台创建RDS MySQL实例
本文介绍了通过控制台创建RDS MySQL实例的详细步骤,包括准备工作、选择计费方式、地域、实例规格、存储空间等关键配置,并指导用户完成下单与实例查看。
|
2月前
|
存储 关系型数据库 MySQL
【赵渝强老师】MySQL数据库的多实例环境
MySQL多实例是指在一台服务器上运行多个MySQL服务,通过不同端口提供独立的数据服务。各实例共享安装程序,但使用各自的配置文件和数据文件,实现资源高效利用。本文详细介绍了如何通过“mysqld_multi”工具配置和启动多个MySQL实例,并演示了目录创建、初始化、配置文件修改及实例启动等操作步骤。
|
7月前
|
关系型数据库 MySQL Shell
MySQL 备份 Shell 脚本:支持远程同步与阿里云 OSS 备份
一款自动化 MySQL 备份 Shell 脚本,支持本地存储、远程服务器同步(SSH+rsync)、阿里云 OSS 备份,并自动清理过期备份。适用于数据库管理员和开发者,帮助确保数据安全。
|
10月前
|
关系型数据库 MySQL Linux
升级到MySQL 8.4,MySQL启动报错:io_setup() failed with EAGAIN
当MySQL 8.4启动时报错“io_setup() failed with EAGAIN”时,通常是由于系统AIO资源不足所致。通过增加AIO上下文数量、调整MySQL配置、优化系统资源或升级内核版本,可以有效解决这一问题。上述解决方案详细且实用,能够帮助管理员快速定位并处理此类问题,确保数据库系统的正常运行。
327 9
|
11月前
|
关系型数据库 MySQL 数据库
【赵渝强老师】启动与关闭MySQL数据库实例
MySQL数据库安装完成后,可以通过命令脚本启动、查看状态、配置开机自启、查看自启列表及关闭数据库。本文提供了详细的操作步骤和示例代码,并附有视频讲解。
146 0
|
12月前
|
存储 关系型数据库 MySQL
mysql 8.0 的 建表 和八种 建表引擎实例
mysql 8.0 的 建表 和八种 建表引擎实例
166 0
|
SQL 关系型数据库 MySQL
mysql数据库备份shell
mysql数据库备份shell
116 0
|
关系型数据库 MySQL Shell
MySQL【实践 01】Linux 环境 MySQL 数据库备份 shell 脚本(脚本源码及说明+定时任务配置+数据库恢复测试)粘贴可以
MySQL【实践 01】Linux 环境 MySQL 数据库备份 shell 脚本(脚本源码及说明+定时任务配置+数据库恢复测试)粘贴可以
337 0
|
SQL 关系型数据库 Shell
MySQL数据库备份的shell脚本
linux系统下 MySQL的备份脚本
1705 0

相关产品

  • 云数据库 RDS MySQL 版
  • 推荐镜像

    更多