当前位置:首页 > 文章列表 > 数据库 > MySQL > mysql常用命令以及小技巧

mysql常用命令以及小技巧

来源:脚本之家 2023-01-07 12:02:16 0浏览 收藏

对于一个数据库开发者来说,牢固扎实的基础是十分重要的,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学习网公众号也会发布数据库相关知识,快来关注吧!

版本声明
本文转载于:脚本之家 如有侵犯,请联系study_golang@163.com删除
Mysql直接查询存储的Json字符串中的数据Mysql直接查询存储的Json字符串中的数据
上一篇
Mysql直接查询存储的Json字符串中的数据
MGR集群搭建及配置过程
下一篇
MGR集群搭建及配置过程
查看更多
最新文章
查看更多
课程推荐
  • 前端进阶之JavaScript设计模式
    前端进阶之JavaScript设计模式
    设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
    542次学习
  • GO语言核心编程课程
    GO语言核心编程课程
    本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
    508次学习
  • 简单聊聊mysql8与网络通信
    简单聊聊mysql8与网络通信
    如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
    497次学习
  • JavaScript正则表达式基础与实战
    JavaScript正则表达式基础与实战
    在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
    487次学习
  • 从零制作响应式网站—Grid布局
    从零制作响应式网站—Grid布局
    本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
    484次学习
查看更多
AI推荐
  • 茅茅虫AIGC检测:精准识别AI生成内容,保障学术诚信
    茅茅虫AIGC检测
    茅茅虫AIGC检测,湖南茅茅虫科技有限公司倾力打造,运用NLP技术精准识别AI生成文本,提供论文、专著等学术文本的AIGC检测服务。支持多种格式,生成可视化报告,保障您的学术诚信和内容质量。
    25次使用
  • 赛林匹克平台:科技赛事聚合,赋能AI、算力、量子计算创新
    赛林匹克平台(Challympics)
    探索赛林匹克平台Challympics,一个聚焦人工智能、算力算法、量子计算等前沿技术的赛事聚合平台。连接产学研用,助力科技创新与产业升级。
    50次使用
  • SEO  笔格AIPPT:AI智能PPT制作,免费生成,高效演示
    笔格AIPPT
    SEO 笔格AIPPT是135编辑器推出的AI智能PPT制作平台,依托DeepSeek大模型,实现智能大纲生成、一键PPT生成、AI文字优化、图像生成等功能。免费试用,提升PPT制作效率,适用于商务演示、教育培训等多种场景。
    58次使用
  • 稿定PPT:在线AI演示设计,高效PPT制作工具
    稿定PPT
    告别PPT制作难题!稿定PPT提供海量模板、AI智能生成、在线协作,助您轻松制作专业演示文稿。职场办公、教育学习、企业服务全覆盖,降本增效,释放创意!
    54次使用
  • Suno苏诺中文版:AI音乐创作平台,人人都是音乐家
    Suno苏诺中文版
    探索Suno苏诺中文版,一款颠覆传统音乐创作的AI平台。无需专业技能,轻松创作个性化音乐。智能词曲生成、风格迁移、海量音效,释放您的音乐灵感!
    60次使用
微信登录更方便
  • 密码登录
  • 注册账号
登录即同意 用户协议隐私政策
返回登录
  • 重置密码