参考 mysql搭建安装完mysql后
https://blog.51cto.com/popli3/2357211

服务器A 服务器B
192.168.0.104 192.168.0.105

在服务器A(192.168.0.104)上:

  1. 打开log-bin和server-id=1选项

    [root@minion-2 ~]# vi /etc/my.cnf
    log-bin
    server-id=1

  2. 授权账户并重启

    mysql> grant super,reload,replication slave on *.* to [email protected] identified by 'slave';
    mysql> flush privileges;
    mysql>quit
    [root@minion-2 ~]# service mysqld restart

在服务器B(192.168.0.105)上:

  1. 打开server-id=2和log-bin

    [root@minion-2 ~]# vi /etc/my.cnf
    log-bin
    server-id=2

  2. 开启change master 功能,这边的host是你主的地址,user和pass都是上一步grant设置的

    mysql>change master to master_host='192.168.0.104',master_user='slave',master_password='slave';
    mysql> start slave;


至此单向的主从完毕,接下来是反过来操作一遍


在服务器B(192.168.0.105)上:

mysql> grant super,reload,replication slave on *.* to [email protected] identified by 'slave';

mysql> flush privileges;

在服务器A(192.168.0.104)上:

mysql>change master to master_host='192.168.0.105',master_user='slave',master_password='slave';
mysql> start slave;