mysql数据同步原理_MySQL数据同步原理及实现

主主复制配置

拓扑

a4c960737c9c6c4d9aef115e957e3d8c.png

互为主从:两个节点各自都要开启binlog和relay log

配置文件

[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=mysql-bin    #开启二进制日志

relay_log=relay-log  #开启中继日志

auto_increment_offset=1

auto_increment_increment=2

[mysqld_safe]

log-error=/var/log/mariadb/mariadb.log

pid-file=/var/run/mariadb/mariadb.pid

!includedir /etc/my.cnf.d

都授权有复制全的用户账号,并把对方指定为主节点

Master1的配置

MariaDB [(none)]> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'cpuser'@'10.1.72.200' IDENTIFIED BY 'userpass';

Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;

Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SHOW MASTER STATUS;

+------------------+----------+--------------+------------------+

| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000003 |      506 |              |                  |

+------------------+----------+--------------+------------------+

1 row in set (0.01 sec)

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='10.1.72.200',MASTER_USER='cpuser',MASTER_PASSWORD='userpass',MASTER_LOG_FILE='mysql-bin.000003',MASTER_LOG_POS=506;

Query OK, 0 rows affected (0.03 sec)

Master2的配置

12345678910111213141516 MariaDB [(none)]> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'cpuser'@'10.1.72.100' IDENTIFIED BY 'userpass';

Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;

Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='10.1.72.100',MASTER_USER='cpuser',MASTER_PASSWORD='userpass',MASTER_LOG_FILE='mysql-bin.000003',MASTER_LOG_POS=506;

Query OK, 0 rows affected (0.04 sec)

MariaDB [(none)]> show master status;

+------------------+----------+--------------+------------------+

| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000003 |      506 |              |                  |

+------------------+----------+--------------+------------------+

1 row in set (0.01 sec)

在两个节点上同时启动IO和SQL Thread

MariaDB [(none)]> START SLAVE;

Query OK, 0 rows affected (0.00 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: mysql-bin.000003

Read_Master_Log_Pos: 506

Relay_Log_File: relay-log.000002

Relay_Log_Pos: 529

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: 506

Relay_Log_Space: 817

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)

验证

在Master1上创建数据库test1,可同步至Master2上,相反在Master2上创建test2也可同步至Master1

a1d623e61f047a8a980246d708bdb7e0.png

半同步复制配置

拓扑

5a4c6e362b8c00bdb261e6d072a8fd55.png

半同步的插件在/usr/lib64/mysql/plugin下, master用的semisync_master.so,slave用的semisync_slave.so

配置Master

MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';

Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> SET GLOBAL rpl_semi_sync_master_enabled=1;

Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> SET GLOBAL rpl_semi_sync_master_timeout=2000;

Query OK, 0 rows affected (0.00 sec)

配置Slave

MariaDB [(none)]> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';

Query OK, 0 rows affected (0.03 sec)

MariaDB [(none)]> SET GLOBAL rpl_semi_sync_slave_enabled=1;

Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> STOP SLAVE;

Query OK, 0 rows affected (0.02 sec)

MariaDB [(none)]> START SLAVE;

Query OK, 0 rows affected (0.00 sec)

验证

432822a67cc6367bff53a63fa2c9b065.png

0b1331709591d260c1c78e86d0c51c18.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值