一文详解怎么设置MariaDB主主复制
来源:51cto
时间:2023-02-24 13:07:54 141浏览 收藏
golang学习网今天将给大家带来《一文详解怎么设置MariaDB主主复制》,感兴趣的朋友请继续看下去吧!以下内容将会涉及到服务器、复制、MariaDB等等知识点,如果你是正在学习数据库或者已经是大佬级别了,都非常欢迎也希望大家都能给我建议评论哈~希望能帮助到大家!
MariaDB Master-Master 复制服务器,可提高速度并减少延迟。使用replication功能,两个独立的 MySQL 服务器充当一个集群。服务器相互同步,以便在发生故障时,其他服务器可以接管并且不会丢失数据。
环 境
OS:CentOS 8.5
MariaDB: MariaDB 10.3.28
两台主机名称如下:
Hostname: MasterA ,IP:192.168.232.130
Hostname: MasterB ,IP:192.168.232.131
安装MariaDB
使用下面命令在两天服务器中安装mariadb服务:
# MasterA中安装:
[root@MasterA ~]# yum -y install mariadb mariadb-server
# MasterB中安装:
[root@MasterB ~]# yum -y install mariadb mariadb-server
启动mariadb服务:
[root@MasterA ~]# systemctl enable mariadb --now
[root@MasterB ~]# systemctl enable mariadb --now

编辑my.cnf配置文件
编辑/etc/my.cnf.d/mariadb-server.cnf配置文件
修改MasterA节点的mariadb-server.cnf配置文件:
[root@MasterA my.cnf.d]# vim mariadb-server.cnf
在mysqld部分下面添加server-id,log-bin 和 log-basename

保存配置,并重启MasterA的MariaDB服务。
[root@MasterA ~]# systemctl restart mariadb
在MasterA的数据库中创建一个帐户,用户名为replica_user,密码为123456,指定slave的IP地址为192.168.232.131,也就是MasterB的IP地址。
MariaDB [(none)]> grant replication slave on *.* to 'replica_user'@192.168.232.131 identified by '123456';
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.001 sec)

修改MasterB节点的mariadb-server.cnf配置文件:
[root@MasterB ~]# vim /etc/my.cnf.d/mariadb-server.cnf
在mysqld部分下面添加server-id,log-bin 和 log-basename

保存配置,并重启MasterA的MariaDB服务。
[root@MasterA ~]# systemctl restart mariadb
在MasterB的数据库中创建一个帐户,用户名为replica_user,密码为123456,指定slave的IP地址为192.168.232.130,也就是MasterA的IP地址。
MariaDB [(none)]> grant replication slave on *.* to 'replica_user'@192.168.232.130 identified by '123456';
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.001 sec)

配置双主复制
首先进入MasterA操作系统,进入数据库,使用show master status;查看二进制日志名称和pos值:
[root@MasterA ~]# mysql -u root -p123456
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 19
Server version: 10.3.28-MariaDB-log MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
MariaDB [(none)]> show master status;
+---------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------------+----------+--------------+------------------+
| master01-bin.000004 | 5078 | | |
+---------------------+----------+--------------+------------------+
1 row in set (0.000 sec)
MariaDB [(none)]>

在MasterB系统中进入数据库,指定MasterA服务器的信息,并指定刚才从MasterA获取的bin-log文件名和position值,并启动slave:
MariaDB [(none)]> change master to master_host='192.168.232.130',
-> master_user='replica_user',
-> master_password='123456',
-> master_log_file='master01-bin.000004',
-> master_log_pos=5078;
Query OK, 0 rows affected (0.007 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.002 sec)

查看slave状态是否有报错:

看到上图中,Slave_IO_Running和Slave_SQL_Running都为yes,Last_Error没有错误信息。
其次,在MasterB的数据库中查询master相关信息:
MariaDB [(none)]> show master status;
+---------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+---------------------+----------+--------------+------------------+
| master02-bin.000001 | 1080 | | |
+---------------------+----------+--------------+------------------+
1 row in set (0.000 sec)

在MasterA系统中进入数据库,指定MasterB服务器的信息,并指定刚才从MasterB获取的bin-log文件名和position值,并启动slave:
MariaDB [(none)]> change master to master_host='192.168.232.131',
-> master_user='replica_user',
-> master_password='123456',
-> master_log_file='master02-bin.000001',
-> master_log_pos=1080;
Query OK, 0 rows affected (0.010 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.002 sec)

查看slave状态是否有报错:

看到上图中,Slave_IO_Running和Slave_SQL_Running都为yes,Last_Error没有错误信息。
任意一台数据库,创建数据库后,另一台也可以看到了。下面实在MasterB中创建的数据库:
MariaDB [test_replica]> create database mydb;
Query OK, 1 row affected (0.000 sec)

在MasterA中查看是否有mydb数据库:

下面是在MasterA中创建数据库:
MariaDB [(none)]> create database mydb_02;
Query OK, 1 row affected (0.000 sec)
在MasterB中查看是否有mydb_02数据库:

下面实例将MasterA数据库中的test_replica库备份,并导入到MasterB的数据库中,然后在MasterB中的数据库中添加数据,查看是否会同步:

[root@MasterA ~]# mysqldump -u root -p123456 test_replica > a.sql
[root@MasterA ~]# scp a.sql root@192.168.232.131:~
切换到MasterB操作系统,创建一个数据库名称为test_replica:
[root@MasterB ~]# mysql -u root -p123456 -e 'create database test_replica;'
[root@MasterB ~]# mysql -u root -p123456 -e 'show databases;'

将MasterA到处的数据导入到MasterB系统中的test_replica库中:
[root@MasterB ~]# mysql -u root -p123456 test_replica a.sql

看到已经导入了数据表。
下面在MasterB中,进入test_replica库,向Admins表添加数据,然后在MasterA中查看是否也存在同样数据:
MariaDB [test_replica]> insert into Admins values ('user01','123');
Query OK, 1 row affected (0.002 sec)

在MasterA中查看test_replica数据库中的Admins表:
[root@MasterA ~]# mysql -u root -p123456 -e 'select * from test_replica.Admins;'
+--------+------+
| Aname | Apwd |
+--------+------+
| admin | 123 |
| user01 | 123 |
+--------+------+

可以看到数据存在。下面在MasterA中向Admins表添加数据,查看MasterB数据库中是否会同步:
MariaDB [test_replica]> insert into Admins values ('user02','321') ;
Query OK, 1 row affected (0.002 sec)

在MasterB中也可以看到刚刚创建的信息:
[root@MasterB ~]# mysql -u root -p123456 -e 'select * from test_replica.Admins;'
+--------+------+
| Aname | Apwd |
+--------+------+
| admin | 123 |
| user01 | 123 |
| user02 | 321 |
+--------+------+

这样就完成啦。
文中关于mysql的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《一文详解怎么设置MariaDB主主复制》文章吧,也可关注golang学习网公众号了解相关技术文章。
-
102 收藏
-
347 收藏
-
245 收藏
-
456 收藏
-
420 收藏
-
443 收藏
-
202 收藏
-
365 收藏
-
223 收藏
-
334 收藏
-
224 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 507次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习