原理介绍:master将改变记录到二进制日志中,slave将日志拷贝到中继日志,slave通过中继日志同步master的操作。
环境:master ip 192.168.1.128
slave ip 192.168.1.129
注意:主从机数据库安装版本最好一致
配置过程
1.master
1)修改配置文件
vim /etc/mysql/mysql.conf.d/mysqld.cnf
[mysqld]
server-id = 1 //数字随意设置,只要主从id不重复即可
log-bin = master-bin //开启二进制日志
log-bin-index = master-bin.index
skip-name-resolve
binlog-do-db = my_data //需要备份的数据库名
binlog-ignore-db= mysql,information_schema //需要忽略的数据库名
2)重启服务
sudo /etc/init.d/mysql restart
3)授权一个实现复制数据的用户
mysql> grant replication root on *.* to slave@192.168.1.129 identified by '123456';
//此处的IP为从服务器的IP
2.slave
1)修改配置文件
vim /etc/mysql/mysql.conf.d/mysqld.cnf
[mysqld]
server-id = 2
relay_log = slave-relay-bin //开启中继日志
relay_log_index = slave-relay-bin.index //一定要保证和master不一致
2)重启服务
sudo /etc/init.d/mysql restart
3.master上查看
注意数据master-bin.000024和Position 658 这两个数据
4.在slave上同步数据
change master to master_host='192.168.1.128',
master_port=3306,
master_user='root',
master_password='123456',
master_log_file='master-bin.000024',
master_log_pos=658;
5.开启从服务,并查看从的状态
当Slave_IO_Running: Yes Slave_SQL_Running: Yes主从数据库即同步