实验环境:CentOS7

#安装mariadb-server
#主数据库:
[root@~ localhost]#vim /etc/my.cnf.d/server.cnf
[mysqld]
skip_name_resolve=ON
innodb_file_per_table=ON
autocommit=0
log_bin=master-log

server_id=1

#从数据库:
[root@~ localhost]#vim /etc/my.cnf.d/server.cnf
[mysqld]
skip_name_resolve=ON
innodb_file_per_table=ON
autocommit=0
server_id=10
relay_log=relay-log
read_only=ON

#将两台设备加入ansible进行管理:
[root@~ localhost]#ansible app -m shell -a 'systemctl start mariadb.service'
[root@~ localhost]#ansible app -m shell -a 'ss -ntl'
[root@~ localhost]#ansible app -m shell -a 'ntpdate 178.19.0.1'

#主服务器:
MariaDB [(none)]> GRANT REPLICATION CLIENT,REPLICATION SLAVE ON *.* TO 'repluser'@'172.16.254.47' IDENTIFIED BY '123456';
MariaDB [(none)]> GRANT REPLICATION CLIENT,REPLICATION SLAVE ON *.* TO 'repluser'@'172.16.252.142' IDENTIFIED BY '123456'; 
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-log.000005 |      750 

#从服务器;
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='172.16.252.142',
MASTER_USER='repluser',MASTER_PASSWORD='123456',
MASTER_PORT=3306,MASTER_LOG_FILE='master-log.000005',MASTER_LOG_POS=750; MariaDB [(none)]> SHOW SLAVE STATUS\G; MariaDB [(none)]> START SLAVE;

##主主复制:

 

#第一台主数据库系统:二进制日志和中继日志都开启
[root@~ localhost]#vim /etc/my.cnf.d/server.cnf 
[mysqld]
skip_name_resolve=ON
innodb_file_per_table=ON
autocommit=0
log_bin=master-log
server_id=1
relay_log=relay-log

#自动增长的ID,以奇数增长
auto_increment_offset=1
auto_increment_increment=2
#给另一服务器授权
MariaDB [(none)]> grant replication client,replication slave  on *.* to 'back'@'172.16.254.47' identity by '123456';


MariaDB [(none)]> flush privileges;
#记录file和binlog_do_db为另一个服务器使用
MariaDB [(none)]> show master status;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-log.000003 |      481 
MariaDB [(none)]> start slave;

#另一数据库服务器:
[root@~ localhost]#vim /etc/my.cnf.d/server.cnf 
[mysqld]
skip_name_resolve=ON
innodb_file_per_table=ON
autocommit=0
server_id=20
relay_log=relay-log
#read_only=ON
log_bin=master-log

#以偶数增长的ID
auto_increment_offset=2
auto_increment_increment=2
MariaDB [(none)]> grant replication client,replication slave on *.* to 'back'@'172.16.252.142' identity by '123456';
MariaDB [(none)]> change master to master_host='172.16.252.142',master_user='back',master_port=3306,master_log_file='master-log.000003',master_log_pos=481,master_password='123456';
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> start slave;