service iptables stop 关闭防火墙

——————主库相关配置——————

主配置文件内容

server-id = 1 # 实例id,保证整个集群里唯一
log-bin=mysql-bin #binlog日志的存储文件名
read-only=0 #是否只读,1代表只读,0代表读写
binlog-ignore-db=mysql # 忽略的数据库,不需要同步的数据库

重启mysql

service mysql restart

创建同步数据的账号,并进行授权

mysql> grant replication slave on *.* to 'fengguowuhen'@'112.36.66.216' identified by 'fg123456a@';
flush privileges;

查看主库状态

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000007 |    54996 |              | mysql            |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

——————从库相关配置——————

从库配置文件

server_id=2
log-bin=mysqlbin
replicate-do-db=testdb #需要同步的数据库
replicate-ignore-db=mysql  #不需要同步的数据库
replicate-ignore-db=information_schema #不需要同步的数据库
replicate-ignore-db=performance_schema  #不需要同步的数据库

重启mysql

service mysql restart
mysql> change master to master_host='101.43.205.81',master_port=3306,master_user='fengguowuhen',master_password='fg123456a@',master_log_file='mysql-bin.000007',master_log_pos=106657;
start slave;
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 101.43.205.81
                  Master_User: fengguowuhen
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000007
          Read_Master_Log_Pos: 125896
               Relay_Log_File: WIN-I70ME5UDN05-relay-bin.000004
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000007
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: testdb
          Replicate_Ignore_DB: mysql,information_schema,performance_schema
           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: 125896
              Relay_Log_Space: 1377
              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
                  Master_UUID: 6fce53da-b7e0-11ed-85ef-5254005977c8
             Master_Info_File: D:\phpstudy_pro\Extensions\MySQL5.7.26\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:
            Executed_Gtid_Set:
                Auto_Position: 0
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
1 row in set (0.00 sec)
mysql> show variables like 'binlog_format%';   # 从库的binlog_format为ROW,主库为MIXED
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW   |
+---------------+-------+
1 row in set, 1 warning (0.00 sec)

发表评论

邮箱地址不会被公开。 必填项已用*标注