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

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.kler.cn/a/552505.html

相关文章:

  • eNSP防火墙综合实验
  • AI与大数据:双剑合璧的智能革命
  • 什么是UV环形光源
  • Flutter 状态管理:详细分析与实战
  • python爬虫系列课程2:如何下载Xpath Helper
  • 文件夹上传到github分支最后github上面还是没有文件和文件夹
  • liunx服务器本地部署deepseek-r1:1.5b通过网页链接使用
  • c/c++蓝桥杯经典编程题100道(23)最小生成树
  • 【设计模式】 代理模式(静态代理、动态代理{JDK动态代理、JDK动态代理与CGLIB动态代理的区别})
  • 垃圾回收器深度对比与调优策略
  • 传输层协议UDP,TCP
  • 进制和编码
  • 如何利用国内镜像从huggingface上下载项目
  • MacOS 15.3 卸载系统内置软件
  • ES6模块的异步加载是如何实现的?
  • 23种设计模式 - 模板方法
  • C语言结构体struct、联合体union和位域操作共同使用示例
  • 你对 CSS 预编译语言的理解是什么,有哪些区别?
  • 【强化学习的数学原理】第09课-策略梯度方法-笔记
  • 【进阶】微服务