mysql主从同步

主从同步的工作原理

复制的工作过程

1. 在每个事务更新数据完成之前,Master 将这些改变记录进二进制日志。写入二进制日志完成后,Master 通知存储引擎提交事务
2. Slave 将 Master 的 Binary log 复制到其中继日志( Relay log) 。首先,Slave 开始一个工作线程——I/O 线程, I/O 线程在 Master 上打开一个普通的连接,然后开始 Binlog dump process。Binlog dumpprocess 从 Master 的二进制日志中读取事件,如果已经跟上Master,它会睡眠并等待 Master 产生新的事件。I/O 线程将这些事件写入中继日志。
3. SQL slave thread(SQL 从线程)处理该过程的最后一步。 SQL 线程从中继日志读取事件,并重放其中的事件而更新 Slave 数据,使其与Master 中的数据保持一致。 只要该线程与 I/O 线程保持一致,中继日志通常会位于 OS 的缓存中,所以中继日志的开销很小

环境: 

系统:centos7

主库主机:192.168.11.11

从库主机:192.168.11.12 

端口都是:3306

#搭建mysql 5.7
#两台节点都要安装

5.7安装
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm

yum -y install mysql57-community-release-el7-10.noarch.rpm

yum -y install mysql-community-server  --nogpgcheck

systemctl start mysqld.service

grep 'password' /var/log/mysqld.log看初始密码

mysql -u root -p
#配置主从
vim /etc/my.cnf
[mysqld]
server_id=1     #各个节点的ID不能一样
log_bin=mysql-bin    #打开logbin日志

重启mysql
###主 MySQL 服务器

mysql> show master status;

mysql> grant replication slave on *.* to
 'slave'@'192.168.11.%' identified by 'jjy';
#创建slave用户密码jjy,只能在192.168.11.0网段连接,而且只有同步权限

mysql> flush privileges;

mysql> select User,Host from mysql.user;

mysql> show grants for 'slave'@'192.168.11.%';
###从 MySQL 服务器
mysql> change master to
 master_host='192.168.11.11',master_user='slave',master_pa
ssword='jjy',master_port=3306,master_log_file='mysqlbin.000001',master_log_pos=120;

mysql> start slave;

mysql> show slave status\G        #确认同步状态
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

#如果结果是no,可以看下面加粗报错信息进行更改
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.70.132
                  Master_User: zty
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 837
               Relay_Log_File: mysql-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: No
            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: 837
              Relay_Log_Space: 120
              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: 1593
                Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs; these UUIDs must be different for replication to work.
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: 
             Master_Info_File: 
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 170927 14:35:51
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
#停止,重启,启动主从同步 最后可以验证
mysql> stop slave;
mysql> reset slave;
mysql> start slave;