欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

004.MySQL双主+Keepalived高可用

程序员文章站 2022-09-04 18:58:23
一 基础环境 主机名 系统版本 MySQL版本 主机IP Master01 CentOS 6.8 MySQL 5.6 172.24.8.10 Master02 CentOS 6.8 MySQL 5.6 172.24.8.11 VIP 172.24.8.12 主机名 系统版本 MySQL版本 主机IP ......

 

一 基础环境

主机名
系统版本
mysql版本
主机ip
master01
centos 6.8
mysql 5.6
172.24.8.10
master02
centos 6.8
mysql 5.6
172.24.8.11
vip
172.24.8.12

二 实际部署

2.1 安装mysql

  1 [root@master01 ~]# yum list installed | grep mysql	#查看是否存在其他mysql组件
  2 [root@master01 ~]# yum -y remove mysql-libs.x86_64	#为避免冲突引发报错,卸载已存在的组件
 
  1 [root@master01 study]#  yum localinstall mysql-commu* -y
  2 [root@master01 ~]# chkconfig --list | grep mysqld		#查看mysql是否加入启动项
  3 [root@master01 ~]# chkconfig mysqld on
 

2.2 初始化mysql

  1 [root@master01 ~]# service mysqld start
  2 [root@master01 ~]# mysql_secure_installation
  3 [root@master01 ~]# service iptables stop
  4 [root@master01 ~]# chkconfig iptables off
  5 [root@master01 ~]# vi /etc/sysconfig/selinux
  6 selinux=disabled
 
注意:以上操作在master02主机上也需要执行。
安装参考:http://www.cnblogs.com/007sx/p/7083143.html

2.3 master01 my.cf配置

  1 [root@master01 ~]# vi /etc/my.cnf
  2 [mysqld]
  3 ……
  4 server-id=1				#设置主服务器master的id
  5 log-bin=mysql-bin			#配置二进制变更日志命名格式
  6 replicate-wild-ignore-table=mysql.%
  7 replicate-wild-ignore-table=test.%
  8 replicate-wild-ignore-table=information_schema.%
 
注意:
master开启二进制日志后默认记录所有库所有表的操作,可以通过配置来指定只记录指定的数据库甚至指定的表的操作,具体在mysql配置文件的[mysqld]可添加修改如下选项:
方法一:
  1 # 不同步哪些数据库
  2 binlog-ignore-db = mysql
  3 binlog-ignore-db = test
  4 binlog-ignore-db = information_schema
  5 # 只同步哪些数据库,除此之外,其他不同步
  6 binlog-do-db = mysqltest
 
方法二:
建议采用以下方式配置过滤:
  1 replicate-wild-ignore-table=mysql.%	#从库配置不同步表
  2 replicate-wild-do-table=test.%		#从库配置需要同步的表
 

提示:不要在主库使用binlog-ignore-db和binlog-do-db,也不要在从库使用replicate-wild-ignore和replicate-wild-do-table。

2.4 创建账号

  1 [root@master01 ~]# mysql -uroot -p
  2 enter password:
  3 mysql> grant replication slave on *.* to 'repl_user'@'172.24.8.11' identified by 'x12345678';
  4 mysql> grant all privileges on *.* to 'root'@'172.24.8.%'  identified by 'x120952576' with grant option;
  5 mysql> flush privileges;
  6 [root@master01 ~]# service mysqld restart
  7 [root@master01 ~]# mysql -uroot -p
  8 enter password:
  9 mysql> show master status;
 
master01:
file:mysql-bin.000001
position:120

2.5 smaster02 my.cf配置配置

  1 [root@master02 ~]# vi /etc/my.cnf
  2 [mysqld]
  3 server-id=2				#设置主服务器master的id
  4 log-bin=mysql-bin			#配置二进制变更日志命名格式
  5 replicate-wild-ignore-table=mysql.%
  6 replicate-wild-ignore-table=test.%
  7 replicate-wild-ignore-table=information_schema.%
  8 read_only=1
 

2.6 创建账号

  1 [root@master02 ~]# mysql -uroot -p
  2 enter password:
  3 mysql> grant replication slave on *.* to 'repl_user'@'172.24.8.10' identified by 'x12345678';
  4 mysql> grant all privileges on *.* to 'root'@'172.24.8.%'  identified by 'x120952576' with grant option;
  5 mysql> flush privileges;
  6 [root@master02 ~]# service mysqld restart
  7 [root@master02 ~]# mysql -uroot -p
  8 enter password:
  9 mysql> show master status;

master02:

file:mysql-bin.000001
position:120

三 启动主从

3.1 手动同步

如果master01和master02已经存在数据,则在开启主备复制之前,需要将master01和master02手动同步一次(/var/lib/mysql整个目录打包tar.gz),具体方法略。
注意:本实验都是重新安装的mysql,可直接启动同步。

3.2 启动master01的slave功能

  1 [root@master01 ~]# service mysqld restart
  2 [root@master01 ~]# mysql -uroot -p
  3 enter password:
  4 mysql> change master to master_host='172.24.8.11',
  5 master_user='repl_user',
  6 master_password='x12345678',
  7 master_log_file='mysql-bin.000001',
  8 master_port=3306,
  9 master_log_pos=120;
 10 mysql> start slave;
 11 mysql> show slave status\g			#查看slave状态
 

3.3 启动master02的slave功能

  1 [root@master02 ~]# service mysqld restart
  2 [root@master02 ~]# mysql -uroot -p
  3 enter password:
  4 mysql> change master to master_host='172.24.8.10',
  5 master_user='repl_user',
  6 master_password='x12345678',
  7 master_log_file='mysql-bin.000001',
  8 master_log_pos=120;
  9 mysql> start slave;
 10 mysql> show slave status\g			#查看slave状态
 
提示:
slave的i/o和sql线程都已经开始运行,而且seconds_behind_master不再是null。日志的位置增加了,意味着一些事件被获取并执行了。如果你在master上进行修改,你可以在slave上看到各种日志文件的位置的变化,同样,你也可以看到数据库中数据的变化。

四 安装keepalived

4.1 下载

  1 [root@master01 ~]# wget http://www.keepalived.org/software/keepalived-1.3.6.tar.gz
  2 [root@master01 ~]# tar -zvxf keepalived-1.3.6.tar.gz -c /tmp/
  3 [root@master01 ~]# cd /tmp/keepalived-1.3.6
  4 [root@master01 keepalived-1.3.6]# ./configure --prefix=/usr/local/keepalived/ --sysconf=/etc --with-init=sysv
  5 #注:(upstart|systemd|sysv|suse|openrc) #根据你的系统选择对应的启动方式
  6 [root@master01 keepalived-1.3.6]# make && make install
  7 [root@master01 ~]# ln -s /usr/local/keepalived/sbin/keepalived /sbin
  8 [root@master01 ~]# chmod u+x /etc/init.d/keepalived
  9 [root@master01 ~]# chkconfig --add keepalived
 10 [root@master01 ~]# chkconfig --level 35 keepalived on
 
注意:master02上也需要如上操作。
若出现以下提示,需要执行:yum -y install openssl-devel。
提示:也可采用yum install -y keepalived安装(个人不建议)。

4.2 master01配置keepalived

默认情况下keepalived启动时会去/etc/keepalived目录下找配置文件。
  1 [root@master01 ~]# vim /etc/keepalived/keepalived.conf
  2 ! configuration file for keepalived
  3 global_defs {
  4    notification_email {
  5     x120952576@126.com
  6 #表示keepalived在发生诸如切换操作时发送email给哪些地址,邮件地址可以多个,每行一个。
  7    }
  8    notification_email_from alexandre.cassen@firewall.loc
  9    smtp_server 172.24.8.10
 10    smtp_connect_timeout 30
 11    router_id lvs_devel
 12 }
 13 vrrp_instance vi_1 {
 14     state backup
 15     interface eth0
 16     virtual_router_id 51
 17 #这里设置vrid,这里非常重要,相同的vrid为一个组,他将决定多播的mac地址
 18     priority 100
 19     advert_int 1
 20     nopreempt
 21 #不抢占,只在优先级高的机器上设置即可,优先级低的机器不设置
 22     authentication {
 23         auth_type pass
 24         auth_pass 1111
 25     }
 26 track_script {
 27     check_mysqld		#执行定义的监控脚本
 28 }
 29     virtual_ipaddress {
 30         172.24.8.12
 31     }
 32 }
 33 vrrp_script check_mysqld {
 34     script"/etc/keepalived/mysqlcheck/keepalived_check_mysql.sh "
 35     interval 2
 36 }
 

4.3 创建检测脚本

  1 [root@master01 ~]# mkdir -p /etc/keepalived/mysqlcheck/
  2 [root@master01 ~]# vi /etc/keepalived/mysqlcheck/keepalived_check_mysql.sh
  3 #!/bin/bash
  4 mysql=/usr/bin/mysql
  5 mysql_host=localhost
  6 mysql_user=root
  7 mysql_password=x120952576
  8 check_time=3
  9 #mysql  is workingmysql_ok is 1 , mysql down mysql_ok is 0
 10 mysql_ok=1
 11 function check_mysql_helth (){
 12 $mysql -h $mysql_host -u $mysql_user -p${mysql_password} -e "show status;" >/dev/null 2>&1
 13 if [ $? = 0 ] ;then
 14         mysql_ok=1
 15 else
 16         mysql_ok=0
 17 fi
 18 return $mysql_ok
 19 }
 20 while [ $check_time -ne 0 ]
 21 do
 22         let "check_time -= 1"
 23         check_mysql_helth
 24         if [ $mysql_ok = 1 ] ; then
 25                 check_time=0
 26                 exit 0
 27         fi
 28         if [ $mysql_ok -eq 0 ] &&  [ $check_time -eq 0 ]
 29         then
 30                 /etc/init.d/keepalived stop
 31                 exit 1
 32         fi
 33         sleep 1
 34 done
 35 [root@master01 ~]# chmod u+x /etc/keepalived/mysqlcheck/keepalived_check_mysql.sh
 36 [root@master01 ~]# service keepalived start
 

4.4 master02配置keepalived

  1 [root@master01 ~]# scp /etc/keepalived/keepalived.conf root@127.24.8.11:/etc/keepalived/
参考master01配置,去掉nopreempt选项,priority设置比master01低即可。

4.5 创建检测脚本

  1 [root@master02 ~]# mkdir -p /etc/keepalived/mysqlcheck/
  2 [root@master01 ~]# scp /etc/keepalived/mysqlcheck/keepalived_check_mysql.sh root@172.24.8.11:/etc/keepalived/mysqlcheck/
  3 [root@master02 ~]# chmod u+x /etc/keepalived/mysqlcheck/keepalived_check_mysql.sh
  4 [root@master02 ~]# service keepalived start
 

五 功能测试

5.1 测试vip

  1 [root@test ~]# mysql -uroot -h172.24.8.12 -p
  1 mysql> show variables like "%hostname%";
  2 mysql> show variables like "%server_id%";
 

5.2 同步测试

  1 [root@test ~]# mysql -uroot -p
  2 enter password:
  3 mysql> create database mysqltest;
  4 mysql> use mysqltest;
  5 mysql> create table user(id int(5),name char(10));
  6 mysql> insert into user values (00001,'zhangsan');
  7 在slave从服务器上进行验证:
  8 [root@master02 ~]# mysql -uroot -p
  9 enter password:
 10 mysql> show databases;
 11 mysql> select * from mysqltest.user;
 

5.3 测试keepalived切换

  1 [root@master01 ~]# service mysqld stop				#停止master01的mysql
  2 [root@master01 ~]# tail -f /var/log/messages			#观察master01的日志
 
  1 [root@master02 ~]# tail -f /var/log/messages			#观察master02的日志
  1 [root@client ~]# mysql -uroot -h172.24.8.12 -px120952576	#客户端连接vip
注意:已经成功切换,在切换过程中可能中断几秒。
参考: