5.2mysql主主复制的实现


主主复制的实现

主主复制:两个节点,都可以更新数据,并且互为主从
容易产生的问题:数据不一致;因此慎用
考虑要点:自动增长id
配置一个节点使用奇数id
另一个节点使用偶数id

主主复制的配置步骤:
(1) 各节点使用一个惟一server_id
(2) 都启动binary log和relay log
(3) 创建拥有复制权限的用户账号
(4) 定义自动增长id字段的数值范围各为奇偶
(5) 均把对方指定为主节点,并启动复制线程

实验过程

#在第一个master节点上实现
[root@master1 ~]#vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
server-id=8
log-bin
auto_increment_offset=1 #开始点
auto_increment_increment=2 #增长幅度
[root@master1 ~]#systemctl start mariadb
[root@master1 ~]#mysql
(root@localhost) [(none)]> show master logs;
(root@localhost) [(none)]> show master logs;
+——————–+———–+———–+
| Log_name | File_size | Encrypted |
+——————–+———–+———–+
| master1-bin.000001 | 179 | No |
| master1-bin.000002 | 179 | No |
| master1-bin.000003 | 156 | No |
+——————–+———–+———–+

 

在第二个master节点上实现
(root@localhost) [(none)]> create user ‘repluser’@’10.0.0.%’ identified by ‘Mmagedu0!’;

(root@localhost) [(none)]> grant replication slave on *.* to ‘repluser’@’10.0.0.%’;

[root@master2 ~]# vim /etc/my.cnf.d/mysql-server.cnf
[mysqld]
server-id=18
log-bin
auto_increment_offset=2 #开始点
auto_increment_increment=2 #增长幅度

[root@master2 ~]# systemctl restart mysqld

[root@master2 ~]#mysql
(root@localhost) [(none)]> change master to
->master_host=’10.0.0.8′,
->master_user=’repluser’,
->master_password=’Mmagedu0!’,
->master_port=3306,
->master_log_file=’master1-bin.000003′,
->master_log_pos=156;
Query OK, 0 rows affected, 9 warnings (0.05 sec)

(root@localhost) [(none)]> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)

(root@localhost) [(none)]> show master logs; #查看二进制位置
+——————–+———–+———–+
| Log_name | File_size | Encrypted |
+——————–+———–+———–+
| master2-bin.000001 | 156 | No |
+——————–+———–+———–+

 

在第一个master节点上实现
[root@master1 ~]# mysql -uroot
(root@localhost) [(none)]> change master to
-> master_host=’10.0.0.18′,
-> master_user=’repluser’,
-> master_password=’Mmagedu0!’,
-> master_port=3306,
-> master_log_file=’master2-bin.000001′,
-> master_log_pos=156;

(root@localhost) [(none)]> start slave;

 

二个节点上
(root@localhost) [(none)]> create database db1;

(root@localhost) [(none)]> use db1;

 

两个节点分别插入数据
(root@localhost) [db1]> create table t1(id int auto_increment primary key,name char(10));

在第二个节点上执行
(root@localhost) [db1]> insert t1 (name) values(‘user1’);

两个节点同时插入数据
(root@localhost) [db1]>insert t1 (name) values(‘user2’);
(root@localhost) [db1]>insert t1 (name) values(‘userX’);

 

两个节点同时创建数据库,发生复制冲突
(root@localhost) [db1]>select * from t1;
(root@localhost) [db1]> select * from t1;
+—-+——-+
| id | name |
+—-+——-+
| 2 | user1 |
| 4 | user2 |
| 6 | userX |
+—-+——-+

3 rows in set (0.001 sec)

(root@localhost) [db1]>create database db2;

(root@localhost) [db1]> show slave status/G
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 10.0.0.18
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master2-bin.000001
Read_Master_Log_Pos: 1445
Relay_Log_File: master1-relay-bin.000002
Relay_Log_Pos: 326
Relay_Master_Log_File: master2-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 1007
Last_Error: Error ‘Can’t create database ‘db1′; database exists’ on query. Default database: ‘db1’. Query: ‘create database db1’
Skip_Counter: 0
Exec_Master_Log_Pos: 156
Relay_Log_Space: 1826
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: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 1007
Last_SQL_Error: Error ‘Can’t create database ‘db1′; database exists’ on query. Default database: ‘db1’. Query: ‘create database db1’
Replicate_Ignore_Server_Ids:
Master_Server_Id: 18
Master_UUID: e0d457fa-0f0a-11ed-a5f5-000c29b6390a
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp: 220729 15:22:56
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set, 1 warning (0.00 sec)

 

原创文章,作者:端木书台,如若转载,请注明出处:https://blog.ytso.com/277974.html

(0)
上一篇 2022年7月31日
下一篇 2022年7月31日

相关推荐

发表回复

登录后才能评论