yum -y install mariadb-server
**编辑配置文件**
主服务器 192.168.0.200
```shell
[root@192 ~]# vim /etc/my.cnf #加入下方俩行
server-id=1
log-bin=mysql-bin
[root@192 ~]# systemctl restart mariadb
[root@192 ~]# MariaDB [(none)]> grant all on *.* to tom@'%' identified by '123';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 | 442 | | |
+------------------+----------+--------------+------------------+
从服务器 192.168.0.201
[root@192 ~]# vim /etc/my.cnf
[root@192 ~]# systemctl restart mariadb
[root@192 ~]# mysql
MariaDB [(none)]> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
MariaDB [(none)]> change master to
-> master_host='192.168.0.200',
-> master_user='tom',
-> master_password='123',
-> master_log_file='mysql-bin.000003',
-> master_log_pos=442;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.200
Master_User: tom
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 442
Relay_Log_File: mysql-relay.000002
Relay_Log_Pos: 529
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
配置主主反之
脚本
vim mysql.#!/bin/bash
num=$(mysql -e 'show slave status\G' | grep Running | grep Yes | awk -F ':' '{print $2}' | wc -l)
if [ $num -eq 2 ];then
echo "mysql主从状态正常"
else
echo "主从状态有误,请迅速检查"
fi
[root@192 ~]# sh mysql.sh
mysql主从状态正常