当前位置: 首页 > news >正文

mysql开启gtid并配置主从

默认主从都开启了bin log.
1.主从都在/etc/my.cnf中加入并重启服务
gtid_mode = ON
enforce_gtid_consistency = ON
2.在主库创建用户并授权

create user slave identified with mysql_native_password by '123456'
mysql>GRANT REPLICATION SLAVE ON *.* to 'slave'@'%' identified by '123456'; //如果用户已存在,会改密码,如果不存在会直接创建这个用户,所以不需要create user单独创建用户
mysql>FLUSH PRIVILEGES;

2.在从库10.153.35.5中执行
CHANGE MASTER TO
MASTER_HOST='10.153.35.2',
MASTER_USER='slave',
MASTER_PASSWORD='123456',
MASTER_AUTO_POSITION = 1;

3.在从库开启复制线程
mysql> SHOW SLAVE STATUS\G

             Slave_IO_Running: No
            Slave_SQL_Running: No

mysql> START SLAVE IO_THREAD;
Query OK, 0 rows affected (0.01 sec)
mysql> START SLAVE SQL_THREAD;
Query OK, 0 rows affected (0.01 sec)

3.在从库开启复制线程
mysql> SHOW SLAVE STATUS\G

             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

如果从库报错,需要调过这个报错的事务,怎么处理?

第一种方式使用GTID_NEXT进行单个事务跳跃:
从库:
stop slave sql_thread;
SET @@SESSION.GTID_NEXT= '0f7a8bd3-d7cd-11ef-9ed5-00505695e953:5'; <<===GTID对应第三步查出来的失败事务的GTID
show global variables like '%gtid%'; <<===查看全局GTID信息
begin;commit; <<===执行空事务
SET @@SESSION.GTID_NEXT=automatic;                          <<===GTID自动执行
start slave sql_thread; <<===启动SQL线程
show slave status\G; <<===确认事务已跳过    

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.153.35.2
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 1740
               Relay_Log_File: t3-hkucm-hkucmsdb-db-oracle01-st01-relay-bin.000003
                Relay_Log_Pos: 454
        Relay_Master_Log_File: mysql-bin.000004
             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: 1740
              Relay_Log_Space: 1577
              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: 123454
                  Master_UUID: 0f7a8bd3-d7cd-11ef-9ed5-00505695e953
             Master_Info_File: /data/mysql/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: 0f7a8bd3-d7cd-11ef-9ed5-00505695e953:5-7
            Executed_Gtid_Set: 0f7a8bd3-d7cd-11ef-9ed5-00505695e953:1-7
                Auto_Position: 1
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
1 row in set (0.00 sec)

ERROR:
No query specified

2.使用gtid_purged跳过多个事务。
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.153.35.2
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 1925
               Relay_Log_File: t3-hkucm-hkucmsdb-db-oracle01-st01-relay-bin.000003
                Relay_Log_Pos: 454
        Relay_Master_Log_File: mysql-bin.000004
             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: 1051
                   Last_Error: Error 'Unknown table 'mysql.test1'' on query. Default database: 'mysql'. Query: 'DROP TABLE `test1` /* generated by server */'
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 1740
              Relay_Log_Space: 3257
              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: 1051
               Last_SQL_Error: Error 'Unknown table 'mysql.test1'' on query. Default database: 'mysql'. Query: 'DROP TABLE `test1` /* generated by server */'
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 123454
                  Master_UUID: 0f7a8bd3-d7cd-11ef-9ed5-00505695e953
             Master_Info_File: /data/mysql/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: 250217 16:20:07
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set: 0f7a8bd3-d7cd-11ef-9ed5-00505695e953:1-8
            Executed_Gtid_Set:
                Auto_Position: 1
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
1 row in set (0.00 sec)


stop slave;
reset master;        
set global gtid_purged='0f7a8bd3-d7cd-11ef-9ed5-00505695e953:5-8';    

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.153.35.2
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 1925
               Relay_Log_File: t3-hkucm-hkucmsdb-db-oracle01-st01-relay-bin.000005
                Relay_Log_Pos: 454
        Relay_Master_Log_File: mysql-bin.000004
             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: 1925
              Relay_Log_Space: 1949
              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: 123454
                  Master_UUID: 0f7a8bd3-d7cd-11ef-9ed5-00505695e953
             Master_Info_File: /data/mysql/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: 0f7a8bd3-d7cd-11ef-9ed5-00505695e953:1-8
            Executed_Gtid_Set: 0f7a8bd3-d7cd-11ef-9ed5-00505695e953:1-8
                Auto_Position: 1
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
1 row in set (0.00 sec)

ERROR:
No query specified

mysql>
    

http://www.lryc.cn/news/538361.html

相关文章:

  • redis sentinel模式 与 redis 分片集群 配置
  • 2025最新在GitHub上搭建个人图床,保姆级图文教程,实现图片高效管理
  • Web后端 - Maven管理工具
  • 【python语言应用】最新全流程Python编程、机器学习与深度学习实践技术应用(帮助你快速了解和入门 Python)
  • 《探秘Windows 11驱动开发:从入门到实战》
  • 搭建Deepseek推理服务
  • Golang GC 三色标记法
  • 重新出发的LLM本地部署——DeepSeek加持下的Ollama+OpenWebUI快速部署
  • 【第3章:卷积神经网络(CNN)——3.5 CIFAR-10图像分类】
  • Django后台新建管理员
  • 【第12章:深度学习与伦理、隐私—12.2 数据隐私保护与差分隐私技术的实现与应用】
  • 索引----数据库
  • 【ISO 14229-1:2023 UDS诊断全量测试用例清单系列:第十七节】
  • nodejs:express + js-mdict 网页查询英汉词典,能显示图片
  • matlab汽车动力学半车垂向振动模型
  • 2025.2.16
  • 图像生成GAN和风格迁移
  • 【06】泛型
  • 1-13 tortoiseGit忽略文件与文件夹
  • ASR强力模型「Whisper」:解密Whisper:AI驱动的语音识别新时代 -
  • 【机器学习】向量化使得简单线性回归性能提升
  • 【kafka系列】消费者
  • HackerRank C++面试,中等难度题目 - Attribute Parser
  • 【ARM】解决ArmDS Fast Models 中部分内核无法上电的问题
  • 节目选择器安卓软件编写(针对老年人)
  • 蓝桥杯之图
  • 中兴光猫修改SN,MAC,修改地区,异地注册,改桥接,路由拨号
  • 【kafka系列】Kafka如何保证消息不丢失?
  • AtCoder Beginner Contest 393 —— E - GCD of Subset 补题 + 题解 python
  • vue3响应式丢失解决办法(三)