1.MySQL互为主从配置
1.1 主从复制的原理图
1.2 实验环境
MySQL Server1 : 192.168.153.130
MySQL Server2 : 192.168.153.131
实验之前服务器均关闭防火墙和selinux
systemctl stop firewalld
setenforce 0
两台服务器均已安装mysql和mysql-server服务
安装命令:
yum -y install mysql mysql-server
1.3 修改 MySQL Server1 的配置文件 ,添加以下内容
[root@localhost ~]# vim /etc/my.cnf
1.4 修改完成之后重启mysql服务
systemctl restart mariadb
1.5 修改 MySQL Server2 的配置文件 ,添加以下内容
[root@localhost ~]# vim /etc/my.cnf
1.6 修改完成之后重启mysql服务
systemctl restart mariadb
1.7 进入 MySQL Server1,给MySQL Server2授权,并刷新授权表
[root@localhost ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 16
Server version: 5.5.68-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> grant replication slave on *.* to slave@'%' identified by '123';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
1.8 查看MySQL Server1的master状态
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 62055 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
MariaDB [(none)]>
1.9 进入MySQL Server2,关闭从服务器的复制功能,配置从服务器,与主库建立连接,之后再开启从服务器的复制功能,
MariaDB [(none)]> stop slave;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> change master to master_host='192.168.153.130',master_user='slave',master_password='123',master_log_file='mysql-bin.000001',master_log_pos=62055;
Query OK, 0 rows affected (0.08 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
1.10 查看MySQL Server2从服务的复制功能状态,双Yes即为成功
show slave status\G;