配置主库
#修改my.cnf配置文件
vim /etc/my.cnf
server_id=1
log-bin=mysql-bin
#重启数据库
service mysqld restart
进入数据库
#建立备份账号
grant replication slave on *.* to 'backup'@'%' identified by '123456';
use mysql
select user,authentication_string,host from user;
flush privileges;
show master status\G
配置从库
#修改my.cnf配置文件
vim /etc/my.cnf
server_id=2
read_only = 1
master_info_repository=TABLE
relay_log_info_repository=TABLE
#重启数据库
service mysqld restart
进入数据库
stop slave;
change master to
master_host='192.168.1.222',
master_user='backup',
master_password='123456',
master_log_file='mysql-bin.000002',
master_log_pos=154;
start slave;
#查看slave从机的状态
show slave status \G
创建新库确认同步正确
create database test001;
show databases;
my.cnf参数
#主库
expire_logs_days= 7 #自动清理 7 天前的log文件,可根据需要修改
binlog_do_db= mysql #仅写入bin日志的数据库 多个写多行
binlog-ignore-db = mysql #限制写入bin日志的数据库 多个写多行
#从库
replicate-ignore-db = mysql #忽略同步库 多个写多行
replicate_do_db = mysql #仅同步库 多个写多行
replicate_wild_do_table=mysql.% #仅同步表格 多个写多行
replicate_wild_ignore_table=mysql.% #忽略同步表格 多个写多行