MySQL设置数据库为只读(数据库是只读怎么修改)
wptr33 2025-05-03 16:58 12 浏览
前言:
默认情况下,我们的 MySQL 实例是可读写的。但有些情况下,我们可以将整个实例设置为只读状态,比如做迁移维护的时候或者将从库设为只读。本篇文章我们来看下 MySQL 设置只读相关知识。
1.关于 read_only 参数
MySQL系统中,提供有 read_only 和 super_read_only 两个只读参数,参考官方文档,这里介绍下这两个参数的作用:
read_only 参数默认不开启,开启后会阻止没有 super 权限的用户执行数据库变更操作。开启后,普通权限用户执行插入、更新、删除等操作时,会提示 --read-only 错误。但具有 super 权限的用户仍可执行变更操作。
super_read_only 参数同样默认关闭,开启后不仅会阻止普通用户,也会阻止具有 super 权限的用户对数据库进行变更操作。
read_only 和 super_read_only 是有关联的,二者之间的关系如下:
- 设置 super_read_only=on ,也就隐式地设置了 read_only=on。
- 设置 read_only=off ,也就隐式地设置了 super_read_only=off。
- 可以单独开启 read_only 而不开启 super_read_only。
不过,从库开启 read_only 并不影响主从同步,即 salve 端仍然会读取 master 上的日志,并且在 slave 实例中应用日志,保证主从数据库同步一致。(经测试,从库端开启 super_read_only 仍不影响主从同步。)
下面我们具体来操作下,看下 read_only 参数的用法:
# 查看 read_only 参数
mysql> show global variables like '%read_only%';
+-----------------------+-------+
| Variable_name | Value |
+-----------------------+-------+
| innodb_read_only | OFF |
| read_only | OFF |
| super_read_only | OFF |
| transaction_read_only | OFF |
| tx_read_only | OFF |
+-----------------------+-------+
# 动态修改 read_only 参数 (若想重启生效 则需将 read_only = 1 加入配置文件中)
mysql> set global read_only = 1;
Query OK, 0 rows affected (0.00 sec)
mysql> show global variables like 'read_only';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| read_only | ON |
+---------------+-------+
# read_only 开启的情况下 操作数据
# 使用超级权限用户
mysql> create table tb_a (a int);
Query OK, 0 rows affected (0.05 sec)
# 使用普通权限用户
mysql> create table tb_b (b int);
ERROR 1290 (HY000): The MySQL server is running with the --read-only option so it cannot execute this statement
# 开启 super_read_only,再次使用超级权限用户来操作数据
mysql> set global super_read_only = 1;
Query OK, 0 rows affected (0.00 sec)
mysql> show global variables like 'super_read_only';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| super_read_only | ON |
+-----------------+-------+
mysql> create table tb_c (c int);
ERROR 1290 (HY000): The MySQL server is running with the --super-read-only option so it cannot execute this statement
# 关闭 read_only 参数
mysql> set global read_only = 0;
Query OK, 0 rows affected (0.00 sec)
2.flush tables with read lock 设置
除了 read_only 参数外,执行 flush tables with read lock 也可将数据库设置为只读状态,那么二者有什么区别呢?我们先来了解下 flush tables with read lock 的作用。
执行此命令会给数据库加全局读锁,使得数据库处于只读状态,以下语句会被阻塞:数据更新语句(增删改)、数据定义语句(建表、修改表结构等)和更新类事务的提交语句。下面我们来具体实验下:
# 执行FTWRL
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.02 sec)
# 进行数据变更操作
mysql> insert into tb_a values (1);
ERROR 1223 (HY000): Can't execute the query because you have a conflicting read lock
# 解锁
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into tb_a values (1);
Query OK, 1 row affected (0.01 sec)
值得注意的是,从库端执行 flush tables with read lock 会导致 SQL 线程卡住,主备延迟。与开启 read_only 参数不同的是,执行 flush tables with read lock 后,其余客户端执行数据变更操作会持续等待而不是立即报错,极其容易引起数据库 hang 住,执行这个命令还是要小心的。
以个人数据库运维经验来讲,一般只有从库需要设置只读状态,从库端建议开启 read_only 或 super_read_only,避免人为写入。flush tables with read lock 适用于进行数据迁移时,可以保证数据库不发生数据改变,不过要注意及时解锁。
总结:
本篇文章主要介绍了 MySQL 只读状态相关知识,其实除了从库外,其余实例很少设置全局只读,只是遇到某种需求的情况下需要将数据库设为只读状态,写本篇文章的目的也是遇到此类需求时,可以有个参考。
相关推荐
- SpringBoot 3 + Flutter3 实战低代码运营管理-10章
-
获课》aixuetang.xyz/5075/三天构建运营管理系统:SpringBoot3+Flutter3高效开发方法论...
- SpringBoot探针实现:从零构建应用健康监控利器
-
SpringBoot探针实现:从零构建应用健康监控利器声明本文中的所有案例代码、配置仅供参考,如需使用请严格做好相关测试及评估,对于因参照本文内容进行操作而导致的任何直接或间接损失,作者概不负责。本文...
- Spring Batch中的JobRepository:批处理的“记忆大师”是如何工作
-
一、JobRepository是谁?——批处理的“档案馆”JobRepository是SpringBatch的“记忆中枢”,负责记录所有Job和Step的执行状态。它像一位严谨的档案管理员,把任务执...
- 还在为 Spring Boot3 技术整合发愁?一文解锁大厂都在用的实用方案
-
你在使用SpringBoot3开发后端项目时,是不是常常陷入这样的困境?想提升项目性能和功能,却不知道该整合哪些技术;好不容易选定技术,又在配置和使用上频频踩坑。其实,这是很多互联网大厂后端开发...
- 一文吃透!Spring Boot 项目请求日志记录,这几招你绝对不能错过!
-
在互联网应用开发的高速赛道上,系统的稳定性、可维护性以及安全性是每一位开发者都必须关注的核心要素。而请求日志记录,就如同系统的“黑匣子”,能够为我们提供排查故障、分析用户行为、优化系统性能等关键信息...
- spring-boot-starter-actuator简单介绍
-
SpringBootActuator是SpringBoot的一个功能强大的子项目,它提供了一些有用的监控和管理SpringBoot应用程序的端点。SpringBootActuat...
- 使用SpringBoot钩子或Actuator实现优雅停机
-
服务如何响应停机信号在java中我们可以直接利用通过Runtime...
- 28-自定义Spring Boot Actuator指标
-
上篇我们学习了《27-自定义SpringBootActuator健康指示器》,本篇我们学习自定义SpringBootActuator指标(Metric)。...
- 如何在Spring Boot中整合Spring Boot Actuator进行服务应用监控?
-
监控是确保系统稳定性和性能的关键组成部分,而在SpringBoot中就提供了默认的应用监控方案SpringBootActuator,通过SpringBootActuator提供了开箱即用的应...
- 「Spring Boot」 Actuator Endpoint
-
Actuator官网地址:https://docs.spring.io/spring-boot/docs/2.5.6/reference/html/actuator.html目的监控并管理应用程序...
- Spring Boot Actuator监控功能全面剖析
-
SpringBootActuator监控功能全面剖析在现代企业级Java开发中,SpringBoot以其轻量化、高效率的特性深受开发者青睐。而作为SpringBoot生态系统的重要组成部分,S...
- 1000字彻底搞懂SpringBootActuator组件!
-
SpringBootActuator组件SpringBootActuator通过HTTPendpoints或者JMX来管理和监控SpringBoot应用,如服务的审计、健康检查、指标统计和...
- JavaScript数据类型(javascript数据类型介绍)
-
基本数据类型BooleanNullNumberStringSymbolUndefined对象数据类型ObjectArray定义:JavaScript数组是内置的对象之一,它可以用一个变量来存储多个同种...
- 能运行,不代表它是对的:5 个潜伏在正常功能下的 JavaScript 错误
-
JavaScript的动态性和复杂性意味着,代码虽然表面上正常运行,但一些深层次、隐蔽的陷阱往往让人意想不到,梳理了几个JavaScript开发中难以发现的隐蔽错误,旨在帮助我们写出更健壮、更可...
- 一周热门
-
-
C# 13 和 .NET 9 全知道 :13 使用 ASP.NET Core 构建网站 (1)
-
因果推断Matching方式实现代码 因果推断模型
-
git pull命令使用实例 git pull--rebase
-
面试官:git pull是哪两个指令的组合?
-
git 执行pull错误如何撤销 git pull fail
-
git pull 和git fetch 命令分别有什么作用?二者有什么区别?
-
git fetch 和git pull 的异同 git中fetch和pull的区别
-
git pull 之后本地代码被覆盖 解决方案
-
还可以这样玩?Git基本原理及各种骚操作,涨知识了
-
git命令之pull git.pull
-
- 最近发表
-
- SpringBoot 3 + Flutter3 实战低代码运营管理-10章
- SpringBoot探针实现:从零构建应用健康监控利器
- Spring Batch中的JobRepository:批处理的“记忆大师”是如何工作
- Github霸榜的SpringBoot全套学习教程,从入门到实战,内容超详细
- 还在为 Spring Boot3 技术整合发愁?一文解锁大厂都在用的实用方案
- 一文吃透!Spring Boot 项目请求日志记录,这几招你绝对不能错过!
- spring-boot-starter-actuator简单介绍
- 使用SpringBoot钩子或Actuator实现优雅停机
- 28-自定义Spring Boot Actuator指标
- 如何在Spring Boot中整合Spring Boot Actuator进行服务应用监控?
- 标签列表
-
- git pull (33)
- git fetch (35)
- mysql insert (35)
- mysql distinct (37)
- concat_ws (36)
- java continue (36)
- jenkins官网 (37)
- mysql 子查询 (37)
- python元组 (33)
- mybatis 分页 (35)
- vba split (37)
- redis watch (34)
- python list sort (37)
- nvarchar2 (34)
- mysql not null (36)
- hmset (35)
- python telnet (35)
- python readlines() 方法 (36)
- munmap (35)
- docker network create (35)
- redis 集合 (37)
- python sftp (37)
- setpriority (34)
- c语言 switch (34)
- git commit (34)