1 mysql主从同步,准备2台服务器,已经安装好mysql
主:mysql1:192.168.12.101
从:mysql2:192.168.12.102
2 主从同步需要开启binl-og日志,并且从mysql2需要大于等于主上的库.
cat /etc/my.cnf
[mysqld] server_id = 99 #值随意,做表示,主和从不一样即可 log-bin=mas #启bin-log日志, 可以直接log-bin
binlog_format="MM"
systemctl stop mysql
systemctl start mysql
3 mysql1:
授权可以登录的账号,设置密码:
grant replication slave on *.* to user1@"192.168.12.101" indentified by "123456";
在MySQL2测试登录:
mysql -h192.168.12.101 -uuser1 -p123456
登录后:
show grants; #可以看到用户的权限
mysql1:
show master status;
记住在mysql2上需要.
mysql2:
cat /etc/my.cnf
[mysqld] server_id = 100 systemctl stop mysql systemctl start mysql
mysql> change master to
master_host="192.168.12.101", master_user="user1", master_password="123456", master_log_file="mas.000002", master_log_pos=332; mysql>show slave statusG; mysql> start slave; mysql>show slave statusG;
查看是否配置成功:
当都没YES的时候表示配置成功.没有配置成功看报错
4 测试 mysql1: 创建库.表,插入数据,在mysql2检验.