5.7-基于Binlog+Position的复制搭建

基本环境

  Master Slave
MySQL版本 MySQL-5.7.16-X86_64 MySQL-5.7.16-X86_64
IP 192.168.56.156 192.168.56.157
Port 3306 3306

数据库环境的部署:
两边安装好相同的数据库软件,初始化,可以启动起来。
 
检查事项:
两边防火墙是否开启,对应的端口号是否通(可以通过telnet的方式,或者远程登录的方式验证)
 
主库配置:

开启 log_bin 、server_id、log_slave_updates 参数。
 
另外需要注意的是:
binlog_format 控制二进制文件格式,生成环境建议使用row格式。
log_bin_basename 控制二进制文件的路径和命名规则。
server_id建议使用:端口号+IP最后一位的形式。
 
主库创建复制账号:
create user 'repl'@'192.168.56.%' identified by 'oracle';
grant replication slave on *.* to 'repl'@'192.168.56.%';
flush privileges;
 
创建完之后,在另外一台服务器测试是否能连接上。
mysql -h 192.168.56.156 -urepl -p
 
如果原来的MySQL已经存在创建好的数据库,需要用mysqldump工具导出再导入到从库中。
mysqldump 需要加--single-transaction 和 --master-data=2  参数。
具体命令如下:
mysqldump -uroot -pJimstars --single-transaction --master-data=2 --triggers --routines --events --all-databases > /data/backup/`date +%Y%m%d-%H%M`-all_bak.sql 
 
 
从库配置:
 
开启 log_bin 、server_id、log_slave_updates 参数。
注意:主库和从库的server_id不能一样。
 
mysql> show master status G;
*************************** 1. row ***************************
             File: mysql-bin.000005
         Position: 194
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: b16b53da-c1a1-11e6-915f-000c29355816:1-21
1 row in set (0.02 sec)
 
在从库上执行:
change master to master_host='192.168.56.156',master_user='repl',master_password='oracle',master_port=3306,master_log_file='mysql-bin.000005',master_log_pos=194;
 
在从库上查看状态:
mysql> show slave statusG;
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.56.156
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000005
          Read_Master_Log_Pos: 194
               Relay_Log_File: relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000005
             Slave_IO_Running: No
            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: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 194
              Relay_Log_Space: 154
              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: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 0
                  Master_UUID: 
             Master_Info_File: /data/mysql/3306/data/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: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 6350cf26-c1a2-11e6-a787-000c299e3b2b:1-5
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)
 
在从库上执行:
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
 
再次在从库上查看状态:
mysql> show slave statusG;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.56.156
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000005
          Read_Master_Log_Pos: 809
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 935
        Relay_Master_Log_File: mysql-bin.000005
             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: 809
              Relay_Log_Space: 1136
              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: 1563306
                  Master_UUID: b16b53da-c1a1-11e6-915f-000c29355816
             Master_Info_File: /data/mysql/3306/data/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: b16b53da-c1a1-11e6-915f-000c29355816:22-24
            Executed_Gtid_Set: 6350cf26-c1a2-11e6-a787-000c299e3b2b:1-5,
b16b53da-c1a1-11e6-915f-000c29355816:22-24
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)
 
关注上面 Slave_IO_Running 和 Slave_SQL_Running都是 Yes,表示已经连接上了master,说明已经成功。
 
 
 
 
 
 
 
 
 
 
原文地址:https://www.cnblogs.com/l10n/p/9400746.html