mysql常用命令以及小技巧
对于一个数据库开发者来说,牢固扎实的基础是十分重要的,golang学习网就来带大家一点点的掌握基础知识点。今天本篇文章带大家了解《mysql常用命令以及小技巧》,主要介绍了小技巧、mysql命令,希望对大家的知识积累有所帮助,快点收藏起来吧,否则需要时就找不到了!
1. 清理二进制日志
purge master logs to 'log-bin.004193'; #表示直接清理到4193位置
2. mysqldump不锁表
在使用mysqldump
备份mysql
数据时,要尽量去从库拿,如果有需求去主库,可以加 --single-transaction
参数不锁表,不加此参数有可能会把主库的表全锁了!!导致业务出现故障
mysqldump --single-transaction --compact -uroot -p(password) -h(dbip) -d (databasesname) > /tmp/test.sql
3. mysql跳过空事务
问题出现:
Slave_IO_State: Waiting for master to send event Master_Host: 10.187.97.219 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: log-bin.000047 Read_Master_Log_Pos: 61907358 Relay_Log_File: relay.000114 Relay_Log_Pos: 61906291 Relay_Master_Log_File: log-bin.000047 Slave_IO_Running: Yes Slave_SQL_Running: No Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 1062 Last_Error: Coordinator stopped because there were error(s) in the worker(s). The most recent failure being: Worker 1 failed executing transaction '81a58913-993d-11eb-94c7-00e0ed7ae706:37497' at master log log-bin.000047, end_log_pos 61906370. See error log and/or performance_schema.replication_applier_status_by_worker table for more details about this failure or others, if any. Skip_Counter: 0 Exec_Master_Log_Pos: 61906082 Relay_Log_Space: 61907849 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: NULL Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 1062 Last_SQL_Error: Coordinator stopped because there were error(s) in the worker(s). The most recent failure being: Worker 1 failed executing transaction '81a58913-993d-11eb-94c7-00e0ed7ae706:37497' at master log log-bin.000047, end_log_pos 61906370. See error log and/or performance_schema.replication_applier_status_by_worker table for more details about this failure or others, if any. Replicate_Ignore_Server_Ids: Master_Server_Id: 5345323 Master_UUID: 81a58913-993d-11eb-94c7-00e0ed7ae706 Master_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: 210413 05:25:50 Master_SSL_Crl: Master_SSL_Crlpath: #master# Retrieved_Gtid_Set: 81a58913-993d-11eb-94c7-00e0ed7ae706:2-37500 #slave# Executed_Gtid_Set: 119cf71e-993c-11eb-94bd-00e0ed93753c:1-3, #81a58913-993d-11eb-94c7-00e0ed7ae706:1-37496 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)
开始处理:首先我们知道
Executed_Gtid_Set: 119cf71e-993c-11eb-94bd-00e0ed93753c:1-3,
81a58913-993d-11eb-94c7-00e0ed7ae706:1-37496
是slave 已经执行过的事务。
其中:
81a58913-993d-11eb-94c7-00e0ed7ae706:1-37496
是已经回放了的从master 同步的事务, 119cf71e-993c-11eb-94bd-00e0ed93753c:1-3
, 是在 slave 上 执行的事务
而下面这个是从master
同步的事务,等待slave
Retrieved_Gtid_Set: 81a58913-993d-11eb-94c7-00e0ed7ae706:2-37500
执行停止slave
mysql> stop slave; Query OK, 0 rows affected (0.00 sec)
检查当前 gtid 相关信息
mysql> show variables like '%gtid%'; +----------------------------------+----------------------------------------+ | Variable_name | Value | +----------------------------------+----------------------------------------+ | binlog_gtid_simple_recovery | ON | | enforce_gtid_consistency | ON | | gtid_executed | | | gtid_executed_compression_period | 1000 | | gtid_mode | ON | | gtid_next | AUTOMATIC | | gtid_owned | | | gtid_purged | 81a58913-993d-11eb-94c7-00e0ed7ae706:1 | | session_track_gtids | OFF | +----------------------------------+----------------------------------------+ 9 rows in set (0.00 sec)
将事务指向37496 的下一个事务,即 37497,注意规范,把 :1-37396 的 1 去掉
mysql> set gtid_next='81a58913-993d-11eb-94c7-00e0ed7ae706:37497'; Query OK, 0 rows affected (0.00 sec) mysql> show variables like '%gtid%'; +----------------------------------+--------------------------------------------+ | Variable_name | Value | +----------------------------------+--------------------------------------------+ | binlog_gtid_simple_recovery | ON | | enforce_gtid_consistency | ON | | gtid_executed | | | gtid_executed_compression_period | 1000 | | gtid_mode | ON | | gtid_next | 81a58913-993d-11eb-94c7-00e0ed7ae706:37497 | | gtid_owned | 81a58913-993d-11eb-94c7-00e0ed7ae706:37497 | | gtid_purged | 81a58913-993d-11eb-94c7-00e0ed7ae706:1 | | session_track_gtids | OFF | +----------------------------------+--------------------------------------------+ 9 rows in set (0.01 sec)
跳过空事务:
mysql> begin; Query OK, 0 rows affected (0.00 sec) mysql> commit; Query OK, 0 rows affected (0.00 sec)
查询gtid 信息:
mysql> show variables like '%gtid%'; +----------------------------------+--------------------------------------------+ | Variable_name | Value | +----------------------------------+--------------------------------------------+ | binlog_gtid_simple_recovery | ON | | enforce_gtid_consistency | ON | | gtid_executed | | | gtid_executed_compression_period | 1000 | | gtid_mode | ON | | gtid_next | 81a58913-993d-11eb-94c7-00e0ed7ae706:37497 | | gtid_owned | | | gtid_purged | 81a58913-993d-11eb-94c7-00e0ed7ae706:1 | | session_track_gtids | OFF | +----------------------------------+--------------------------------------------+ 9 rows in set (0.00 sec)
设置自动分配 GTID:
ysql> set gtid_next='AUTOMATIC'; Query OK, 0 rows affected (0.00 sec)
4. 番外
我们知道一个新的事务在提交后会被分配一个新的GTID,当该事务在从库上被应用时会保留主库上的GTID
我们可以通过设定gtid_next
的值来改变这种行为
1 AUTOMATIC
当设置为AUTOMATIC
时(默认值)时,系统会自动分配一个GTID,如果事务回滚或者没有写入到二进制文件时则不会分配
2 具体的GTID值
我们可以设置该变量为一个具体的有效的GTID,这时服务器会将该GTID分配给下一个事务,就算该事务没有被写入二进制日志或者为空事务,该GTID也会被分配并加入到gtid_executed
变量中
这里需要注意的是,如果该变量值不为AUTOMATIC
,我们需要手动的为每个事务指定GTID,否则该事务会失败,你可以将其改为AUTOMATIC
,让服务器自动分配
启动 slave:
mysql> start slave; Query OK, 0 rows affected (0.03 sec) mysql> show variables like '%gtid%'; +----------------------------------+----------------------------------------+ | Variable_name | Value | +----------------------------------+----------------------------------------+ | binlog_gtid_simple_recovery | ON | | enforce_gtid_consistency | ON | | gtid_executed | | | gtid_executed_compression_period | 1000 | | gtid_mode | ON | | gtid_next | AUTOMATIC | | gtid_owned | | | gtid_purged | 81a58913-993d-11eb-94c7-00e0ed7ae706:1 | | session_track_gtids | OFF | +----------------------------------+----------------------------------------+ 9 rows in set (0.00 sec) mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.187.97.219 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: log-bin.000047 Read_Master_Log_Pos: 61907358 Relay_Log_File: relay.000115 Relay_Log_Pos: 448 Relay_Master_Log_File: log-bin.000047 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 61907358 Relay_Log_Space: 61908058 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 5345323 Master_UUID: 81a58913-993d-11eb-94c7-00e0ed7ae706 Master_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: 81a58913-993d-11eb-94c7-00e0ed7ae706:2-37500 Executed_Gtid_Set: 119cf71e-993c-11eb-94bd-00e0ed93753c:1-3, 81a58913-993d-11eb-94c7-00e0ed7ae706:1-37500 Auto_Position: 1 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)
5. mysql8.0使用mysqldump导出数据
导出指定库的所有数据:
./bin/mysqldump -uroot -padmin123 -S status/mysql3306.sock aaa5 --set-gtid-purged=OFF --single-transaction > /root/aaa5.sql
注释:
- 1.数据库前面不需要加任何参数,如果加-d就是只导出表结构
- 2.当数据库开启
gtid
后需要在导出数据时把gtid关掉,即加--set-gtid-purged=OFF
参数,因为gtid是唯一的,再插入时会报错 - 3.加参数--
single-transaction
不锁表
终于介绍完啦!小伙伴们,这篇关于《mysql常用命令以及小技巧》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布数据库相关知识,快来关注吧!

- 上一篇
- Mysql直接查询存储的Json字符串中的数据

- 下一篇
- MGR集群搭建及配置过程
-
- 小巧的黑米
- 这篇文章内容真是及时雨啊,很详细,真优秀,码起来,关注作者大大了!希望作者大大能多写数据库相关的文章。
- 2023-05-20 14:57:20
-
- 傻傻的玫瑰
- 受益颇多,一直没懂这个问题,但其实工作中常常有遇到...不过今天到这,帮助很大,总算是懂了,感谢up主分享技术贴!
- 2023-01-14 19:21:23
-
- 数据库 · MySQL | 2天前 |
- MySQL设置中文界面,超简单教程来了!
- 332浏览 收藏
-
- 数据库 · MySQL | 2天前 | mysql 索引提示
- MySQL进阶必看!FORCE/USE/IGNOREINDEX用法大揭秘
- 182浏览 收藏
-
- 数据库 · MySQL | 2天前 |
- 手把手教你写MySQL存储过程,小白也能轻松上手
- 163浏览 收藏
-
- 数据库 · MySQL | 2天前 | mysql group by
- MySQL分组查询优化:GROUPBY原理+索引优化超全解析
- 324浏览 收藏
-
- 数据库 · MySQL | 2天前 |
- MySQL设置中文语言,轻松拥有中文界面
- 211浏览 收藏
-
- 数据库 · MySQL | 2天前 |
- MySQL建库语句从入门到精通:创建数据库+设置字符集&排序规则(附实例)
- 176浏览 收藏
-
- 数据库 · MySQL | 2天前 |
- 从零开始学MySQL数据库操作,小白轻松变大神!
- 496浏览 收藏
-
- 数据库 · MySQL | 2天前 |
- MySQL插入日期到时间字段,轻松搞定日期格式
- 484浏览 收藏
-
- 数据库 · MySQL | 2天前 | mysql 数据压缩
- MySQL怎么实现高效压缩存储?表压缩+列式存储详细解读
- 272浏览 收藏
-
- 数据库 · MySQL | 2天前 | mysql JOIN优化
- MySQL优化JOIN操作:七大技巧教你提升关联查询速度
- 106浏览 收藏
-
- 数据库 · MySQL | 2天前 |
- MySQL出现中文乱码?超详细解决方案一次性搞定
- 211浏览 收藏
-
- 数据库 · MySQL | 2天前 |
- MySQL主从复制这样配!搞懂这些参数,replication稳了~
- 131浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 484次学习
-
- 茅茅虫AIGC检测
- 茅茅虫AIGC检测,湖南茅茅虫科技有限公司倾力打造,运用NLP技术精准识别AI生成文本,提供论文、专著等学术文本的AIGC检测服务。支持多种格式,生成可视化报告,保障您的学术诚信和内容质量。
- 25次使用
-
- 赛林匹克平台(Challympics)
- 探索赛林匹克平台Challympics,一个聚焦人工智能、算力算法、量子计算等前沿技术的赛事聚合平台。连接产学研用,助力科技创新与产业升级。
- 50次使用
-
- 笔格AIPPT
- SEO 笔格AIPPT是135编辑器推出的AI智能PPT制作平台,依托DeepSeek大模型,实现智能大纲生成、一键PPT生成、AI文字优化、图像生成等功能。免费试用,提升PPT制作效率,适用于商务演示、教育培训等多种场景。
- 58次使用
-
- 稿定PPT
- 告别PPT制作难题!稿定PPT提供海量模板、AI智能生成、在线协作,助您轻松制作专业演示文稿。职场办公、教育学习、企业服务全覆盖,降本增效,释放创意!
- 54次使用
-
- Suno苏诺中文版
- 探索Suno苏诺中文版,一款颠覆传统音乐创作的AI平台。无需专业技能,轻松创作个性化音乐。智能词曲生成、风格迁移、海量音效,释放您的音乐灵感!
- 60次使用
-
- MySQL主从切换的超详细步骤
- 2023-01-01 501浏览
-
- Mysql-普通索引的 change buffer
- 2023-01-25 501浏览
-
- MySQL高级进阶sql语句总结大全
- 2022-12-31 501浏览
-
- Mysql报错:message from server: * is blocked because of many
- 2023-02-24 501浏览
-
- 腾讯云大佬亲码“redis深度笔记”,不讲一句废话,全是精华
- 2023-02-22 501浏览