MySQL主从复制原理以及实现

一、主从复制的原理

MySQL数据库的主从复制是基于MySQL二进制日志,通过bin log日志复制需要同步的从服务器上。主要包括三个线程(2个I/O线程,1个SQL线程)。它和MySQL的读写分离有着必然的联系,首先要部署主从复制,完成之后才能进行数据的读写分离。

一台服务器档当主服务器(Master),接受来自来自用户的内容更新,而一个或多个其他的服务器来当从服务器(Slaver),接受来自Master上binlog文件的日志内容,解析出SQL,重新更新到Slave。使得主从服务器的数据达到一致。

二、主从复制的过程

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上并行操作。

三、如何实现主从复制

在主服务器( master )上
启用二进制日志
选择一个唯一的server-id
创建具有复制权限的用户
在从服务器( slave )上
启用中继日志
(二进制日志可开启,也可不开启)
选择一个唯一的server-id
连接至主服务器,并开始复制

四、MySQL主从复制实现

1、环境准备

主库(MySQL Master):IP为192.168.226.145 port为3306

从库(MySQL slave): IP为192.168.226.146 port为3306

2、主库配置

复制代码
1)server_id设置并开启binlog参数

[root@localhost ~]# vim /etc/my.cnf
server_id=1
log_bin=mysql_bin

2)重启mysql服务
[root@localhost ~]# systemctl restart mysqld

3)登录MySQL 授权用户
mysql>  grant replication slave on *.* to 'rep1'@'192.168.226.%' ide
Query OK, 0 rows affected, 1 warning (0.00 sec)

查看信息
mysql> mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec

3、从库配置

复制代码
1)设置server_id值,并重启mysql服务

[root@localhost ~]# vim /etc/my.cnf
server_id=2
[root@localhost ~]# systemctl restart mysqld

2)设置主从库同步

mysql> change master to
MASTER_HOST='192.168.226.145',
MASTER_PORT=3306,
MASTER_USER='rep1',
MASTER_PASSWORD='RedHat@123',
MASTER_LOG_FILE='mysql-bin.000003',
MASTER_LOG_POS=154;

3)启动从库同步开关

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.226.145
                  Master_User: rep1
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 154
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000003
             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: 531
              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: c6e51c1a-1bc0-11ee-8e0b-000c29f9b40d
             Master_Info_File: /var/lib/mysql/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)
相关推荐
Ein hübscher Kerl.28 分钟前
虚拟机上安装 MariaDB 及依赖包
数据库·mariadb
长征coder42 分钟前
AWS MySQL 读写分离配置指南
mysql·云计算·aws
醇醛酸醚酮酯1 小时前
Qt项目锻炼——TODO清单(二)
开发语言·数据库·qt
ladymorgana1 小时前
【docker】修改 MySQL 密码后 Navicat 仍能用原密码连接
mysql·adb·docker
PanZonghui1 小时前
Centos项目部署之安装数据库MySQL8
linux·后端·mysql
GreatSQL社区2 小时前
用systemd管理GreatSQL服务详解
数据库·mysql·greatsql
掘根2 小时前
【MySQL进阶】错误日志,二进制日志,mysql系统库
数据库·mysql
weixin_438335402 小时前
基础知识:mysql-connector-j依赖
数据库·mysql
小明铭同学2 小时前
MySQL 八股文【持续更新ing】
数据库·mysql
Mr_Xuhhh2 小时前
信号与槽的总结
java·开发语言·数据库·c++·qt·系统架构