# 先重启一下服务shell> service mysqld restart# 登录到mysqlshell> mysql -uroot -p # 创建数据库同步用户,并授予相应的权限mysql> grant replication slave, replication client on *.* to "repl"@"192.168.1.206" identified by "root123456";# 刷新授权表信息mysql> flush privileges;# 查看binlog文件的position(偏移)和File(日志文件)的值,从机上需要用到mysql> show master status;+----------------------+----------+--------------+------------------+-------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+----------------------+----------+--------------+------------------+-------------------+| edu-mysql-bin.000001 |120 || mysql| |+----------------------+----------+--------------+------------------+-------------------+1 row in set (0.00 sec)
shell> vim /etc/my.cnf在[mysqld]中增加以下配置项:server_id=206binlog-ignore-db=mysqllog-bin=edu-mysql-binbinlog_cache_size=1Mbinlog_format=mixedexpire_logs_days=7slave_skip_errors=1062relay_log=edu-mysql-relay-binlog_slave_updates=1#ID自增从2开始,双数auto_increment_increment=2auto_increment_offset=2
3.2 Master配置
# 先重启一下服务shell> service mysqld restart# 登录到mysqlshell> mysql -uroot -p # 创建数据库同步用户,并授予相应的权限(只允许repl用户从192.168.1.205上登录)mysql> grant replication slave, replication client on *.* to "repl"@"192.168.1.205" identified by "root123456";# 刷新授权表信息mysql> flush privileges;# 查看binlog文件的position(偏移)和File(日志文件)的值,从机上需要用到mysql> show master status;+----------------------+----------+--------------+------------------+-------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |+----------------------+----------+--------------+------------------+-------------------+| edu-mysql-bin.000001 |439 || mysql| |+----------------------+----------+--------------+------------------+-------------------+1 row in set (0.00 sec)这时可以启动节点1(205)的slave服务
# 登录205创建一个数据库shell> mysql -u root -pmysql> create database if not exists mydb default character set utf8 collate utf8_general_ci;mysql> create table user (id int, username varchar(30), password varchar(30));mysql> insert into user values (1, "yangxin", "123456");# 下面是在206节点上的操作#1、登录206查询所有库,是否包含mydb数据库#2、切换到mydb库,是否包含user表,并有一条数据#3、在206的mydb.user表插入一条数据,查看205是否同步过去mysql> insert into user values (2,"yangxin2","123456")详细过程如下图所示:本文永久更新链接地址