Mysql互为主从+keepalived实现高可用性
Posted gageshen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql互为主从+keepalived实现高可用性相关的知识,希望对你有一定的参考价值。
1)规则:
1) db-51 192.168.4.51 mysql 5.7
2) db-52 192.168.4.52 Mysql 5.7
vip: 192.168.4.50
2)创建Mysql互为主从:
1、在192.168.4.51 (db-51)安装Mysql,创建同步复制用户(用于192.168.4.52(db-52)主机同步数据)
1 [root@db-51 ~]#tar -xf mysql-5.7.17.tar -C mysql 2 [root@db-51 ~]#yum -y install net-tools perl-Data-Dumper perl-JSON 3 [root@db-51 mysql]#cd mysql 4 [root@db-51 mysql]#rpm -Uvh mysql-community-*.rpm 5 [root@db-51 mysql]#systemctl start mysqld 6 [root@db-51 mysql]#grep password /var/log/mysqld.log 7 [root@db-51 mysql]#mysql -uroot -p\'y8glj!u=ldQ%\' 8 > set global validate_password_policy=0; 9 > set global validate_password_length=6; 10 > set password=\'123456\'; //5.7开始设置mysql数据root用户密码 11 > exit 12 [root@db-51 mysql]# vim /etc/my.cnf 13 validate_password_policy=0 14 validate_password_length=6 15 16 server_id=51 //设置mysql的server id 17 log_bin=/var/lib/mysql/mysql-master-51 18 binlog_format=mixed 19 20 21 [root@db-51 mysql]#systemctl stop firewalld 22 [root@db-51 mysql]#systemctl disable firewalld 23 [root@db-51 mysql]#systemctl restart mysqld 24 [root@db-51 mysql]#mysql -uroot -p 25 > grant replication slave on *.* to repl@\'%\' identified by \'123456\'; //创建主从复制用户 26 > show warnings; 27 > show grants for repl@\'%\'; //查看用户权限 28 > flush table with read lock; //给服务加读写锁,注意:关闭本连接session后就自动解锁了。 29 > show master status; //查看本机作为主库状态信息。
2、在192.168.4.52 (db-52)安装Mysql ,设置同步主库信息,同时创建同步复制用户(用于192.168.4.51 (db-51)主机同步数据)。
1 [root@db-52 ~]#tar -xf mysql-5.7.17.tar -C mysql 2 [root@db-52 ~]# yum -y install net-tools perl-Data-Dumper perl-JSON 3 [root@db-52 ~]#cd mysql 4 [root@db-52 mysql]#rpm -Uvh mysql-community-*.rpm 5 [root@db-52 mysql]#cd ~ 6 [root@db-52 ~]#systemctl start mysqld 7 [root@db-52 ~]#grep password /var/log/mysqld.log 8 [root@db-52 ~]#mysql -uroot -p\'k:sv!HWtV7d>\' 9 > set global validate_password_policy=0; 10 > set global validate_password_length=6; 11 > set password=\'123456\'; //5.7开始 12 > exit 13 [root@db-52 ~]#vim /etc/my.cnf 14 [mysqld] 15 validate_password_policy=0 16 validate_password_length=6 17 18 server_id=52 19 log_bin=/var/lib/mysql/mysql-master-52 20 binlog_format=mixed 21 22 [root@db-52 ~]#systemctl stop firewalld 23 [root@db-52 ~]#systemctl disable firewalld 24 [root@db-52 ~]#systemctl restart mysqld 25 [root@db-52 ~]#mysql -uroot -p 26 > reset slave; //重置从库服务,即清空slave服务 27 > show slave status; 28 > change master to //配置主库 29 -> master_host=\'192.168.4.51\', 30 -> master_user=\'repl\', 31 -> master_password=\'123456\', 32 -> master_log_file=\'mysql-master-51.000001\', 33 -> master_log_pos=437 34 -> ; 35 > start slave; //启动slave服务 36 > show slave status\\G 37 > create user repl@\'%\' identified by \'123456\'; //创建主从复制用户 38 > grant replication slave on *.* to repl@\'%\'; //给用户授权 39 > show grants for repl@\'%\'; //查看用户权限 40 > show master status; //查看本机作为主库信息。
3、在192.168.4.51 (db-51)上Mysql中设置同步主库信息,创建表及记录查看是否同步。
1 > unlock tables; //释放锁 2 > reset slave //重置slave 3 > show slave status\\G 4 > change master to 5 -> master_host=\'192.168.4.52\', 6 -> master_user=\'repl\', 7 -> master_password=\'123456\', 8 -> master_log_file=\'mysql-master-52.000001\', 9 -> master_log_pos=595; 10 > start slave; //开启slave从库同步服务 11 > show slave status\\G 12 > show databases; 13 > create database mygame; 14 > use mygame 15 > create table t1( 16 -> id int(4) not null auto_increment primary key, 17 -> name char(10) 18 -> ); 19 > insert into t1(name) values("Tom"),("Jave");
4、在192.168.4.52 (db-52)上Mysql中 创建表及记录查看是否同步。(检查操作)
[root@db-52 ~]#mysql -uroot -p > show databases; > use mygame > insert into t1(name) values("Bom"),("Baly"); > select * from t1; > create table t2 select name from t1 where false; //复制表结构 > desc t2; > insert into t2 values("tom"),("poly"); > select * from t2;
5、在192.168.4.51 (db-51)上Mysql中 创建表及记录查看是否同步。(检查操作)
1 [root@db-51 ~]mysql -uroot -p 2 > show databases; 3 > use mygame 4 > desc t2; 5 > insert into t2 values("tom"),("Jam"); 6 > select * from t2;
注1:让互为主从库,有各自的自增变量:
1 #主库1配置my.cnf: 2 [root@db-1 ~]#vim /etc/my.cnf 3 auto_increment_increment = 2 4 auto_increment_offset = 2 5 log-bin =/mysql_multi_case/3306/mysqld-bin 6 log-slave-updates = 1 //级联服务 7 8 主库2配置my.cnf: 9 [root@db-2 ~]#vim /etc/my.cnf 10 auto_increment_increment = 2 11 auto_increment_offset = 1 12 log-bin =/mysql_multi_case/3307/mysqld-bin 13 log-slave-updates = 1 //级联服务 14 15 16 17 注意: 18 auto_increment_increment 控制列中的值的增量值,也就是步长。 19 auto_increment_offset 确定AUTO_INCREMENT列值的起点,也就是初始值。 20 21 上述两个变量:可以在全局以及session级别设置这2个变量 。
Mysql自增案例:(摘自:https://blog.csdn.net/lcrxxoo/article/details/80438803)
1 --创建演示表,使用auto_increment子句 2 root@localhost[tempdb]> create table t1(id int not null auto_increment primary key, col varchar(20)); 3 4 --插入记录 5 root@localhost[tempdb]> insert into t1(col) values(\'robin\'),(\'fred\'),(\'jack\'),(\'james\'); 6 7 --下面可以看到id列起始值为1,增量为1 8 root@localhost[tempdb]> select * from t1; 9 +----+-------+ 10 | id | col | 11 +----+-------+ 12 | 1 | robin | 13 | 2 | fred | 14 | 3 | jack | 15 | 4 | james | 16 +----+-------+ 17 18 --设置步长为5 19 root@localhost[tempdb]> set session auto_increment_increment=5; 20 21 root@localhost[tempdb]> show variables like \'%auto_incre%\'; 22 +--------------------------+-------+ 23 | Variable_name | Value | 24 +--------------------------+-------+ 25 | auto_increment_increment | 5 | 26 | auto_increment_offset | 1 | 27 +--------------------------+-------+ 28 29 --清空表t1 30 root@localhost[tempdb]> truncate table t1; 31 32 --再次插入记录 33 root@localhost[tempdb]> insert into t1(col) values(\'robin\'),(\'fred\'),(\'jack\'),(\'james\'); 34 35 --如下查询可以看到步长以5位基数发生变化 36 root@localhost[tempdb]> select * from t1; 37 +----+-------+ 38 | id | col | 39 +----+-------+ 40 | 1 | robin | 41 | 6 | fred | 42 | 11 | jack | 43 | 16 | james | 44 +----+-------+ 45 46 --设置初始值为5 47 root@localhost[tempdb]> set session auto_increment_offset=5; 48 49 root@localhost[tempdb]> show variables like \'%auto_incre%\'; 50 +--------------------------+-------+ 51 | Variable_name | Value | 52 +--------------------------+-------+ 53 | auto_increment_increment | 5 | 54 | auto_increment_offset | 5 | 55 +--------------------------+-------+ 56 57 root@localhost[tempdb]> truncate table t1; 58 59 root@localhost[tempdb]> insert into t1(col) values(\'robin\'),(\'fred\'),(\'jack\'),(\'james\'); 60 61 --下面是新的结果 62 root@localhost[tempdb]> select * from t1; 63 +----+-------+ 64 | id | col | 65 +----+-------+ 66 | 5 | robin | 67 | 10 | fred | 68 | 15 | jack | 69 | 20 | james | 70 +----+-------+
参考:
https://blog.51cto.com/superpcm/2094958
https://blog.51cto.com/superpcm/2095731
https://www.cnblogs.com/DBArtist/p/auto_increment.html
https://blog.csdn.net/leshami/article/details/39779509#
以上是关于Mysql互为主从+keepalived实现高可用性的主要内容,如果未能解决你的问题,请参考以下文章
MySQL 5.6通过Keepalived+互为主从实现高可用架构
MySQL高可用,MySQL互为主从+keepalived。