mariadb的主从复制、主主复制、半同步复制配置详解
主从服务器的时间要同步,数据库版本最好是一致的,以免造成函数处理、日志读取、日志解析等发生异常。 以下三个主从复制的设置是独立的。 注意防火墙和selinux的影响。 1、简单主从复制的实现(1)主服务器的配置 1)安装mariadb-server
2)编辑/etc/my.cnf文件
在[mysqld]段的最后添加以下内容
innodb_file_per_table = ON 3)授权可以复制本地数据库信息的主机 [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 statusG (查看主服务器的状态信息,在从服务器中要用到) (2)从服务器的配置1)安装mariadb-server
2)编辑/etc/my.cnf文件
在[mysqld]段的最后添加以下内容
innodb_file_per_table = ON 3)设置要从哪个主服务器的那个位置开始同步 [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; (启动复制功能) (3)测试1)在主服务器导入事先准备好的数据库
2)在从服务器查看是否同步 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 |
+-------------------+
2、双主复制的实现(1)服务器1的配置 1)安装mariadb-server
2)编辑/etc/my.cnf文件
在[mysqld]段的最后添加以下内容
innodb_file_per_table = ON 3)在服务器2上查看的master状态 show master statusG
*************************** 1. row ***************************
File: master-log.000003
Position: 422
Binlog_Do_DB:
Binlog_Ignore_DB:
4)启动mariadb server并进行如下配置 [root@localhost ~]# mysql
MariaDB [(none)]> grant replication slave,replication client on . to 'repluser'@'10.1.51.%' identified by 'replpasswd'; MariaDB [(none)]> change master to master_host='10.1.51.50',master_log_pos=422; MariaDB [(none)]> start slave; MariaDB [(none)]> SHOW SLAVE STATUSG (仅是部分内容) (2)服务器2的配置 1)安装mariadb-server
2)编辑/etc/my.cnf文件
skip_name_resolve = ON 3)在服务器1查看master状态 show master statusG 4)启动mariadb server并配置 [root@localhost ~]# mysql
MariaDB [(none)]> grant replication slave,replication client on . to 'repluser'@'10.1.51.%' identified by 'replpasswd'; MariaDB [(none)]> change master to master_host='10.1.51.60',master_log_pos=245; MariaDB [(none)]> start slave; MariaDB [(none)]> show slave statusG (仅是部分内容) (3)测试 1)在任意一台服务器上创建mydb数据库
2)在另一台服务器上查看 show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| performance_schema |
| test |
+--------------------+
3、半同步复制的实现(1)在主服务器上的配置 1)安装mariadb-server
2)编辑/etc/my.cnf
skip_name_resolve = ON 3)授权可以复制本地数据库信息的主机 [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 statusG (查看主服务器的状态信息,在从服务器中要用到) 4)安装rpl semi sync_master插件,并启用 MariaDB [(none)]> install plugin rpl_semi_sync_master soname 'semisync_master.so';
MariaDB [(none)]> set global rpl_semi_sync_master_enabled = ON; 补充: show plugins;(可查看插件是否激活) (2)从服务器的配置 1)安装mariadb-server
2)编辑/etc/my.cnf文件
在[mysqld]段的最后添加以下内容
innodb_file_per_table = ON 3)设置要从哪个主服务器的那个位置开始同步 [root@localhost ~]# mysql
MariaDB [(none)]> change master to master_host='10.1.51.60',master_log_pos=245; 4)安装rpl semi sync_slave插件并启用 MariaDB [(none)]> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
MariaDB [(none)]> set global rpl_semi_sync_slave_enabled = ON; MariaDB [(none)]> start slave; 完成上面配置后,可以在主服务器上查看半同步复制的相关信息,命令如下: show global status like '%semi%';
Rpl_semi_sync_master_clients 1 (从服务器有一台)
(3)测试 测试以个人实际情况而定
2)在主服务器上查看半同步复制的状态 show master status;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-log.000003 | 8102 | | |
+-------------------+----------+--------------+------------------+
MariaDB [hellodb]> show global status like '%semi%'; 3)在从服务器上查看是否同步 show databases;
MariaDB [(none)]> use hellodb;
MariaDB [hellodb]> select * from students;
补充:基于上面的半同步复制配置复制的过滤器,复制过滤最好在从服务器上设置,步骤如下 (1)从服务器的配置 1)关闭mariadb server
2)编辑/etc/my.cnf文件 补充:常用的过滤选项如下
Replicate_Ignore_DB= 3)重启mariadb server
4)重启mariadb server后,半同步复制功能将被关闭,因此要重新启动 show global variables like '%semi%';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled | OFF |
| rpl_semi_sync_slave_trace_level | 32 |
+---------------------------------+-------+
MariaDB [(none)]> set global rpl_semi_sync_slave_enabled = ON; (2)测试 1)主服务器上的hellodb数据库创建一个新表semitable
2)在从服务器上查看hellodb数据库是否有semitable use hellodb
MariaDB [hellodb]> show tables;(并没有)
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
+-------------------+
3)在主服务器上创建mydb数据库,并为其创建一个tbl1表
4)在从服务器上查看mydb数据库的是否有tbl1表 use mydb;
MariaDB [mydb]> show tables; (可以查看到)
+----------------+
| Tables_in_mydb |
+----------------+
| tbl1 |
+----------------+
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |