ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

5.2mysql主主复制的实现

2022-07-31 02:01:24  阅读:164  来源: 互联网

标签:5.2 Log 主主 Master mysql db1 master root localhost


主主复制的实现

主主复制:两个节点,都可以更新数据,并且互为主从
容易产生的问题:数据不一致;因此慎用
考虑要点:自动增长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)

 

标签:5.2,Log,主主,Master,mysql,db1,master,root,localhost
来源: https://www.cnblogs.com/biaoming534/p/16536320.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有