主数据库配置
# 修改my.cnf 下的[mysqld]
# 修改外部能连接
bind-address = 0.0.0.0
mysqlx-bind-address = 0.0.0.0
# 开启日志
log_bin = mysql-bin
# 设置服务id,主从不能一致
server-id = 1
# 设置需要同步的数据库
binlog_do_db = user_db
# 屏蔽系统库同步 例如mysql数据库
binlog_ignore_db = mysql,information_schema,performance_schema
# 进入mysql执行下面sql语句
create user slave@'%' identified by '123456';
GRANT ALL PRIVILEGES ON *.* TO 'slave'@'%' WITH GRANT OPTION;
# 如果从数据库连接不上
# ALTER USER 'slave'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
flush privileges;
show master status ;
从数据库配置
# 修改外部能连接
bind-address = 0.0.0.0
mysqlx-bind-address = 0.0.0.0
log_bin = mysql-bin
server-id = 2
binlog-format=ROW
replicate_wild_do_table = user_db.%
replicate_wild_ignore_table = mysql.%,information_schema.%,performance_schema.%
# 进入mysql执行下面sql语句
stop slave;
# 这里的MASTER_LOG_FILE、MASTER_LOG_POS是主数据库执行 show master status得到的
CHANGE MASTER TO
MASTER_HOST='192.168.161.129',
MASTER_USER='slave',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000453',
MASTER_LOG_POS=1149;
start slave ;
# 查看是否生效,
show slave status \G;
# Slave_IO_Running: Yes,Slave_SQL_Running: Yes 代表成功