一、流程步骤
1、master 创建slave用户,用于slave读取master日志
2、设置master 和slave 节点ID
3、查看master 和slave 信息
4、slave 设置master信息
5、slave 查看连接信息
6、验证主从
二、操作
1、 数据库连接
master :192.168.148.129,3308,root/root
slave:192.168.148.129,3309,root/root
2、设置master 和slave 节点ID
编辑my.cnf,在[mysqld] 下增加配置
master:server-id=0
slave:server-id=1
重启master 和 slave
3、创建同步用户:
master:执行SQL语句
1)创建用户:create user 'localslave'@'%' identified by 'localslave';
2)用户授权:grant all privileges on *.* to 'localslave'@'%';
4、查看master信息

5、slave 设置 master 数据库信息
执行SQL语句
CHANGE MASTER TO master_host = '127.0.0.1',
master_user = 'localslave',
master_port = 3308,
master_password = 'localslave',
master_log_file = 'binlog.000014',
master_log_pos = 417;
6、查看slave 信息

注意:确认master信息没有问题,IO 和SQL为YES说明成功,建立连接可能要等一会时间
7、在master下新建一个数据库
CREATE DATABASE `sharding` CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_general_ci';
创建成功,可在slave 下也看到创建的数据库。

网友评论