MariaDB的主从复制、主主复制、半同步复制

本文详述了MariaDB的主从复制、双主复制及半同步复制的配置过程,包括安装、配置、授权及测试步骤。确保服务器时间同步,数据库版本一致,关注防火墙和SELinux设置,以实现高效稳定的数据库复制。
摘要由CSDN通过智能技术生成

这篇文章主要详细介绍了mariadb的主从复制、主主复制、半同步复制的概念和方法。

参考http://www.jb51.net/article/97786.htm

主从服务器的时间要同步,数据库版本最好是一致的,以免造成函数处理、日志读取、日志解析等发生异常。
以下三个主从复制的设置是独立的。
注意防火墙和selinux的影响。

1、简单主从复制的实现

1.1 服务器1操作

1)安装mariadb-server

[root@localhost ~]# yum -y install mariadb-server

2)编辑/etc/my.cnf文件

在[mysqld]段的最后添加以下内容

[root@localhost ~]# vim /etc/my.cnf
skip_name_resolve = ON
innodb_file_per_table = ON    
server-id = 1 (id号不能跟从服务器相同)    
log-bin = master-log (自定义二进制日志文件名)

3)授权可以复制本地数据库信息的主机

[root@localhost ~]# systemctl start mariadb.service (启动mariadb server)

[root@localhost ~]# mysql
MariaDB [(none)]> grant replication slave,replication client on *.* to 'repluser'@'10.1.51.%' identified by 'replpasswd';
 MariaDB [(none)]> flush privileges;

MariaDB [(none)]> show master status\G (查看主服务器的状态信息,在从服务器中要用到)
*************************** 1. row ***************************
File: master-log.000003 (正在使用的二进制日志文件)
Position: 497 (所处的位置)
Binlog_Do_DB:
Binlog_Ignore_DB:

1.2 从服务器的配置

1)安装mariadb-server

[root@localhost ~]# yum -y install mariadb-server

2)编辑/etc/my.cnf文件

在[mysqld]段的最后添加以下内容

[root@localhost ~]# vim /etc/my.cnf
skip_name_resolve = ON
innodb_file_per_table = ON
server-id = 2 (id号不能跟主服务器相同)
relay-log = slave-log (自定义二进制日志文件名)

3)设置要从哪个主服务器的那个位置开始同步

[root@localhost ~]# systemctl start mariadb.service

[root@localhost ~]# mysql
 MariaDB [(none)]> change master to master_host='10.1.51.60',master_user='repluser',master_password='replpasswd',master_log_file='master-log.000003',master_log_pos=497;

MariaDB [(none)]> start slave; (启动复制功能)
MariaDB [(none)]> show slave status\G (查看从服务器的状态,下面显示的是部分内容)
 Master_Host: 10.1.51.60
 Master_User: repluser
 Master_Port: 3306
 Connect_Retry: 60
 Master_Log_File: master-log.000003
 Read_Master_Log_Pos: 497
 Relay_Log_File: slave-log.000002
 Relay_Log_Pos: 530
 Relay_Master_Log_File: master-log.000003
 Slave_IO_Running: Yes
 Slave_SQL_Running: Yes
 Master_Server_Id: 1

1.3 测试

1)在主服务器导入事先准备好的数据库

[root@localhost ~]# mysql < hellodb.sql

2)在从服务器查看是否同步

MariaDB [(none)]> show databases;
+--------------------+
| Database   |
+--------------------+
| information_schema |
| hellodb   |(数据库已经同步)
| mysql    |
| performance_schema |
| test    |
+--------------------+
MariaDB [(none)]> use hellodb;
MariaDB [hellodb]> show tables; (hellodb数据库的表也是同步的)
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes   |
| coc    |
| courses   |
| scores   |
| students   |
| teachers   |
| toc    |
&
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值