为mysql master主机新建一slave并监控slave一致性

搭建环境

master:172.16.18.114,正式服务器,MySQL运行中
现要为master新建一slave,172.16.18.165,并监控其延迟

步骤如下:

1. 配置master服务器

  • 修改my.cnf:
log-bin=mysql-bin
sync_binlog = 0
server-id  = 1
binlog_format = ROW

如果主库原来没有设置这些参数,那么设置后要重启主库mysql

2. 配置slave服务器

  • 修改my.cnf:
server-id  = 3
log-bin=mysql-bin
sync_binlog = 0
binlog_format = ROW
log_slave_updates = 1
read_only = 1;

#有多个数据库要过滤,一定要写多条,不能在一条里用','分隔,不会生效
replicate-do-db=3alogic
replicate-do-db=boss
replicate-do-db=cbvacs
replicate-do-db=elitel_access
replicate-do-db=wollar
replicate-do-db=wxqyh
replicate-do-db=percona
#replicate-ignore-db=test

设置后要重启从库mysql

  • 删除备库上需要同步的相关数据库
drop database if exists 3alogic;
drop database if exists boss;
drop database if exists cbvacs;
drop database if exists elitel_access;
drop database if exists wollar;
drop database if exists wxqyh;
drop database if exists percona;

  • 设置从库的master信息
    change master to master_host='172.16.18.114',master_user='user',master_password='pass';

    3. 将master当前数据全量备份到slave
    mysqldump --single-transaction --databases 3alogic boss cbvacs elitel_access wollar wxqyh percona --master-data=1 | mysql --host=172.16.18.165
    #--single-transaction参数会将当前事务设置为repeat-read
    #--master-data会在备份文件中增加change master master_log_file=XXX,master_log_pos=xxx
    
    or
    
    mysqldump --single-transaction --databases 3alogic boss cbvacs elitel_access wollar wxqyh percona --master-data=1 > /tmp/114.sql
    mysql --host=172.16.18.165 < /tmp/114.sql

    4. start slave

    在从库mysql上start slave

    start slave;
    show slave status\G
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 172.16.18.114
                      Master_User: root
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000005
              Read_Master_Log_Pos: 14950195
                   Relay_Log_File: localhost-relay-bin.000003
                    Relay_Log_Pos: 26245
            Relay_Master_Log_File: mysql-bin.000005
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
                  Replicate_Do_DB: 3alogic,boss,elitel_access,cbvacs,wollar,wxqyh,percona
              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: 14950195
                  Relay_Log_Space: 26405
                  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: 1
    1 row in set (0.01 sec)

    说明slave运行正常,可以在master上show master status,对比下Master_Log_Pos

    5. 监控slave延迟

    借助percona tookit工具pt-heartbeat来检查slave落后master时间
    该工具是在master运行

    <code class="hljs apache has-numbering"><span class="hljs-keyword"></span></code><pre name="code" class="html">pt-heartbeat -D 3alogic --update --user=user --password=pass -h172.16.18.114 --create-table --interval=1 --daemonize
    #上面这条命令会在master的3alogic库下建立一个heartbeat的表,并每1S更新一次该表数据
    #可以加参数--interval=60来指定更新频率,该值越小监控延时情况越精确
    
    pt-heartbeat -D 3alogic --monitor --user=user --password=user -h172.16.18.165 --interval=1
    0.01s [  0.77s,  0.15s,  0.05s ]
    0.01s [  0.77s,  0.15s,  0.05s ]
    0.01s [  0.77s,  0.15s,  0.05s ]
    0.01s [  0.77s,  0.15s,  0.05s ]
    0.01s [  0.77s,  0.15s,  0.05s ]
    0.01s [  0.77s,  0.15s,  0.05s ]
    0.01s [  0.77s,  0.15s,  0.05s ]
    0.01s [  0.77s,  0.15s,  0.05s ]
    0.01s [  0.77s,  0.15s,  0.05s ]
    0.01s [  0.77s,  0.15s,  0.05s ]
    0.01s [  0.77s,  0.15s,  0.05s ]
    
    #上面这条命令会连接到slave检查延迟情况
    #--interval=1的情况下,延迟应该小于1S


     

    6. 定期检查主从数据是否一致

    借助percona tookit工具pt-table-checksum来检查主从数据是否一致,用pt-table-sync来同步不一致的数据。该工具是在master运行。

    • pt-table-checksum
    pt-table-checksum --nocheck-replication-filters --databases 3alogic --replicate percona.checksums --create-replicate-table --no-check-binlog-format --replicate-check-only
        #--nocheck-replication-filters 从库有Replicate_Do_DB时需要增加该参数
        #--no-check-binlog-format 如果binlog_format=ROW需要设置该参数
        #--replicate percona.checksums 
        #--create-replicate-table 
        #上面两个参数,第一次运行时需要,会在主库上建立库percona和表checksums并同步到slave上,因此slave上Replicate_Do_DB必须含有percona,下次运行时可以不加这两个参数
        #--replicate-check-only 只显示不同结果,该参数只是从slave的checksums表中获取获取不同结果的数据,并不会再次检查主从数据是否一致,因些使用该参数检查之前最好先执行一条不带该选项的检查命令
    
    pt-table-checksum --nocheck-replication-filters --databases 3alogic --recursion-method=dsn=h=172.16.18.114,D=test,t=dsns
        #默认情况下该工具会在master上利用show processlist得到slave连接信息
        #上面的命令是将slave信息保存在test库的dsns表中,表结构如下:
    
        CREATE TABLE `dsns` (
          `id` int(11) NOT NULL AUTO_INCREMENT,
          `parent_id` int(11) DEFAULT NULL,
          `dsn` varchar(255) NOT NULL,
          PRIMARY KEY (`id`)
        );
        INSERT INTO dsns (parent_id,dsn) values(1,'h=172.16.18.165,u=user,p=pass,P=3306');
        #可以用这种方式来修改slave特殊的端口


    • pt-table-sync
      pt-table-sync --print --user=user --password=pass  h=localhost,D=3alogic,t=log h=172.16.18.165
      #只同步一个表
      #这种默认状态下,工具会直接操作如果备库更新数据,不安全,如果备库开启了log_bin则会出错,要加上--replicate或--sync-to-master选项 
      
      pt-table-sync --print --user=user --password=pass --replicate=percona.checksums --databases=3alogic --tables=log h=localhost h=172.16.18.165
      # 只同步一个表
      
      pt-table-sync --print --replicate=percona.checksums --databases=3alogic h=localhost h=172.16.18.165
      #同步一个库
      
      pt-table-sync --execute --user=root --password=cpyf --replicate=percona.checksums h=localhost,D=3alogic,t=log h=172.16.18.116
      #--print 是只打印出结果
      #--execute 才真正同步
      #前一个h是master,后一个h是slave
      #--replicate=percona.checksums依赖于percona.checksums的结果
      
      ./pt-table-sync --print --sync-to-master --databases=3alogic --tables=log 172.16.18.116
      ./pt-table-sync --execute --sync-to-master --databases=3alogic --tables=log 172.16.18.116
      #这个命令不依赖于percona.checksums



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值