这篇文章主要详细介绍了mariadb的主从复制、主主复制、半同步复制的概念和方法。
主从服务器的时间要同步,数据库版本最好是一致的,以免造成函数处理、日志读取、日志解析等发生异常。
以下三个主从复制的设置是独立的。
注意防火墙和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 |
&