MySQL主从复制

1.理解MySQL主从复制原理。

MySQL的主从复制和MySQL的读写分离两者有必然的联系,首先要部署主从复制,只有主从复制完成了,才能在此基础上进行数据的读写分离

1)MySQL支持的复制类型
(1)基于语句的复制:在主服务器上执行的SQL语句,在从服务器上执行同样的语句。MySQL默认采用基于语句的复制,效率比较高。但是必须开启二进制日志功能;
(2)基于行的复制:把改变的内容复制过去,而不是把命令在从服务器上执行一遍;
(3)混合类型的复制:默认采用基于语句的复制,一旦发现基于语句无法精确复制时,就会采用基于行的复制;

2)复制的工作过程

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

复制过程有一个很重要的限制,即复制在Slave上是串行化的,也就是说Master上的并行更新操作不能在Slave上并行操作。
 

2.完成MySQL主从复制。

主库配置
1)设置server-id值并开启binlog参数
[root@bogon ~]# vim /etc/my.cnf
server-id=1
log-bin=mysql-bin
[root@bogon ~]# systemctl restart mysqld
登录MySQL   //授权用户
mysql> grant replication slave on *.* to 'rep'@'192.168.231.%' identified by 'RedHat@123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)



从库配置
[root@mysql1 ~]# vim /etc/my.cnf
[root@mysql1 ~]# systemctl restart mysqld

server-id=2

mysql> change master to master_host='192.168.231.130',
MASTER_PORT=3306,
master_user='rep',
master_password='RedHat@123',
master_log_file='mysql-bin.000002',
master_log_pos=154;

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql>  show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.231.130
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql1-relay-bin.000003
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000002
             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: 154
              Relay_Log_Space: 528
              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: 28de4399-1b00-11ee-91eb-000c2935aef2
             Master_Info_File: /usr/local/mysql-5.7.14-linux-glibc2.5-x86_64/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)
必须两个yes才配置成功
    Slave_IO_Running: Yes
    Slave_SQL_Running: Yes

你可能感兴趣的:(mysql,数据库)