MySQL数据同步方式:
Synchronous Replication 同步复制
Asynchronous Replication 异步复制
Semisynchronous Replication 半同步复制
同步复制:指的是客户端连接到MySQL主服务器写入一段数据,MySQL主服务器同步给MySQL从服务器需要等待从服务器发出同步完成的响应才返回客户端OK, 这其中等待同步的过程是阻塞的, 如果有N台从服务器, 效率极低
异步复制: 指的是客户端连接到MySQL主服务器写入一段数据,MySQL主服务器将写入的数据发送给MySQL从服务器, 然后直接返回客户端OK, 可能从服务器的数据会和主服务不一致
半同步复制:指的是客户端连接到MySQL主服务器写入一段数据, MySQL主服务器只将数据同步复制给其中一台从服务器, 半同步复制给其他的从服务器, 来达到其中一台从服务器完全同步的效果
MySQL复制原理
建立主从关系后, 主服务器如果有数据修改之后, BINLOG会更新, 从服务通过IO-Thread读取主服务器的BINLOG到本地的Relay Log, 再通过SQL-Thread对其进行重放(replay), 从而同步到本地
MySQL简单复制
拓扑:
Master配置文件
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
skip_name_resolve = ON
innodb_file_per_table = ON
server_id=1 #主服务器ID
log_bin=master-bin #开启二进制日志
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
!includedir /etc/my.cnf.d
Slave配置文件
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
skip_name_resolve = ON
innodb_file_per_table = ON
server_id=2 #从服务器ID
relay_log=relay-log #开启中继日志
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
!includedir /etc/my.cnf.d
在主Master上面查看当前正在使用的二进制日志,并记录下position
MariaDB [(none)]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000003 | 245 | | |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
主Master上创建用户并赋予权限
MariaDB [(none)]> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'cpuser'@'10.1.%.%' IDENTIFIED BY '
userpass';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
配置Slave
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='10.1.72.100',MASTER_USER='cpuser',MASTER_PASSWORD='userpass',MASTER_LOG_FILE='master-bin.000001',MASTER_LOG_POS=245;
Query OK, 0 rows affected (0.03 sec)
MariaDB [(none)]> SLAVE START; #启动IO和SQL Thread
Query OK, 0 rows affected (0.02 sec)
MariaDB [(none)]> SHOW SLAVE STATUS\G #查看相应信息
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.1.72.100
Master_User: cpuser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 245
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 530
Relay_Master_Log_File: master-bin.000001
Slave_IO_Running: Yes #IO Thread已启动
Slave_SQL_Running: Yes #SQL Thread已启动
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: 245
Relay_Log_Space: 818
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上创建一个数据库
Slave验证