设为首页 加入收藏

TOP

MySQL-5.7.10主主同步的安装和配置(二)
2017-04-24 10:23:16 】 浏览:693
Tags:MySQL-5.7.10 同步 安装 配置
bsp;其中user为用于同步的用户名,peer为另一主的IP地址,password为user的MySQL密码,这3个的值需根据实际进行修改。


注意,两主机器都需要创建同步用户。并确保peer为另一主的IP地址,其它user和password两主建议保持相同,示例(假设两主IP分别为192.168.1.1和192.168.1.2):


? 192.168.1.1上执行:


grant replication slave,file on *.* to 'replication'@'192.168.1.2' identified by '123456';


flush privileges;


? 192.168.1.2上执行:


grant replication slave,file on *.* to 'replication'@'192.168.1.1' identified by '123456';


flush privileges;


4.2. my.cnf


修改/etc/my.cnf,实现主主配置。


如果不存在/etc/my.cnf,则复制support-files/my-default.cnf生成my.cnf,两台机器的my.cnf分别配置为(不难看到,只有server_id和auto_increment_increment两项不同):


机器A


机器B


server-id=1


user=mysql


log-bin=mysql-bin


log-slave-updates


slave-skip-errors=all


sync_binlog=1


auto_increment_increment=1


auto_increment_offset=1


server-id=2


user=mysql


log-bin=mysql-bin


log-slave-updates


slave-skip-errors=all


sync_binlog=1


auto_increment_increment=2


auto_increment_offset=1


4.3. 配置项说明


配置项


配置项说明


server-id


不能相同!唯一标识号,值位于1~2^32-1之间


user


这个可以不指定,则使用mysqld_safe指定的用户,或者mysqld_safe默认的用户mysql


log-bin


启用二进制日志文件


log-slave-updates


配置从库上的更新操作是否写二进制文件,需要和log-bin一起使用


slave-skip-errors


值为all表示让从库跳过所有错误(但不能跳过所有DDL所引起的主从错误),也可以只跳过指定的错误,如:--slave-skip-errors=1062,1053;也可以配置只跳过DDL错误,如:--slave-skip-errors=ddl_exist_errors,这等同于:


--slave-skip-errors=1007,1008,1050,1051,1054,1060,1061,1068,1094,1146


sync_binlog


值为1表示主机每次提交事务的时候把二进制日志的内容同步到磁盘上


auto_increment_increment


auto_increment_offset


和auto_increment_offset一起用于主主同步,用来错开自增,防止键值冲突,所以auto_increment_increment和auto_increment_offset两者,至少要有一项值不同。


 上述配置会导致同步所有的数据库,借助下列配置项也可以选择性的同步或不同步:


配置项


配置项说明


binlog-do-db=test1


binlog-do-db=test2


表示只同步数据库test1和test2,如果还想同步test3,只需要新增一行:binlog-do-db=test3即可


binlog-ignore-db=db1


binlog-ignore-db=db2


表示不同步数据库db1和db2,如果还有db3不想同步,新增一行:binlog-ignore-db=db3即可


 相关配置项(对于主从同步,只需要在从上配置):replicate-do-db、replicate-ignore-db、replicate_wild_do_table和replicate_wild_ignore_table。


4.4. 设置同步关系


分别重启MySQL,进入MySQL Cli,执行命令“show master status\G”,记住“File”和“Position”的值,如:


mysql> show master status\G


*************************** 1. row ***************************


            File: mysql-bin.000004


        Position: 682


    Binlog_Do_DB: test


 Binlog_Ignore_DB: mysql


Executed_Gtid_Set:


1 row in set (0.00 sec)


 设置同步关系(两个主都需要设置):


stop slave;


change master to master_host='peer',master_user='user',master_password='password',master_log_file='mysql-bin.000004', master_log_pos=682;


 如果不先执行“stop slave;”,则可能遇到如下错误:


ERROR 3021 (HY000): This operation cannot be performed with a running slave io thread; run STOP SLAVE IO_THREAD FOR CHANNEL '' first.


 这里,peer、user和password三者的取值为“创建同步用户”时指定的值。设置示例:


change master to master_host='192.168.1.2',master_user='replication',master_password='123456',master_log_file='mysql-bin.000004', master_log_pos=682;


 由于前面一步调用“stop slave;”,停止了复制。在完成后,需再启动复制:


start slave;


 4.5. 验证


执行命令“show slave status\G;”查看复制状态,如果出现“Slave_IO_Running: Yes”和“Slave_SQL_Running: Yes”,则表示状态正常。


进一步,可以分别创建一个表,如:create table test1 (a int)和create table test2 (b int)。再分别使用show tables查看是否同步过去。


5. 常见错误


1) TIMESTAMP with implicit DEFAULT value is deprecated


执行MySQL的“bin/mysqld --initialize --user=mysql”时报的错误。


原因是从MySQL 5.6版本开始,

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Redis主从配置及HA方案 下一篇CentOS 6.8源码安装Redis

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目