MySQL的异步复制和半同步复制

MySQL的异步复制和半同步复制


MySQL主从复制

主机名主机地址角色
node1   192.168.2.201   Master  
node2   192.168.2.202   Slave  
node3   192.168.2.203   Slave  

本文使用CentOS7.1,数据库:MariaDB-5.5.50
注意:本文关闭了selinux,以及iptables。

一、异步复制: 相关知识点:

MySQL的异步复制是MySQL自带的数据同步功能,在公司里面也是也就最为常见的。

Master服务器中需要开启二进制日志binlog,从服务器需要开启中继日志relay-log。

二进制日志binlog的主要功能是:记录令数据库内容产生改变的语句,如insert语句;二进制日志在备份还原的时候至关重要。

中继日志relay-log则是从服务器中开启,作用是从主服务器的二进制日志中复制,并在在从服务器本地执行一次,达到与主服务器内容一致的效果。

一般MySQL复制是放在内网中进行的,因为MySQL的同步并没有进行加密。而且相比较于在公网传输,在内网中丢包的概率较低,带宽也高。

Master节点:Node1 (1) 启动二进制日志; [mysqld] log_bin=mysql-bin (2) 为当前节点设置一个全局唯一的ID号; [mysqld] server_id=1 (3) 授权创建仅有复制权限的用户账号; mysql> GRANT REPLCATION SLAVE, REPLICATION CLIENT ON *.* TO 'repuser'@'192.168.2.%' IDENTIFIED BY 'repuser'; (4)查看Master端的二进制日志记录到哪里,用于决定Slave复制的起始位置 MariaDB [(none)]> show master status; +-------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------------------+----------+--------------+------------------+ | Master-log.000001 | 245 | | | +-------------------+----------+--------------+------------------+ #Slave服务器如果是一个空的数据库而主服务器不为空: #在同步之前可以先用Master的全量备份,恢复到Slave数据库中。 #然后再从备份那一刻记录的Position开始复制。 #假设Master和Slave都为空,上面的情况就表示Slave从二进制日志Master-log.000001的245开始复制 从节点:Node2设置 (1) 启动中继日志; [mysqld] relay_log=relay-log relay_log_index=relay-log.index (2) 为当前节点设置一个全局惟的ID号; [mysqld] server_id=2 #node3把这里改为3 (3) Slave服务器打开只读模式; [mysqld] read_only = 1 (4) 使用有复制权限的用户账号连接至主服务器,并启动复制线程; #注意:上面的是在/etc/my.cnf的配置文件中添加,下面mysql>的则是在mysql服务器中运行的命令 mysql> CHANGE MASTER TO MASTER_HOST='192.168.2.201', MASTER_USER='repuser', MASTER_PASSWORD='repuser', MASTER_LOG_FILE='Master-log.000001', MASTER_LOG_POS=245; (5)在从服务器中开启复制线程 mysql> START SLAVE; 查看从服务器的状态信息 mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.2.201 Master_User: repuser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: Master-log.000003 Read_Master_Log_Pos: 1379 Relay_Log_File: relay-log.000002 Relay_Log_Pos: 1414 Relay_Master_Log_File: Master-log.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: 1379 Relay_Log_Space: 1702 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 1 row in set (0.00 sec)

插入几条命令之后,从以上的状态信息中可以看得到我们的Master服务器是192.168.2.201
拥有复制功能的账号是repuser,现在复制到Master-log.000003的Position 1379这个位置;
上面的输出结果中,我在复制完成之后使用了flush logs手动地滚动了二进制日志,所以二进制去到000003

二、半同步复制 相关知识点:

半同步复制是由谷歌研发的一种数据库主从复制方式。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/7ed30b2ef5fd63abd8579dc44a809907.html