DRBD+Heartbeat+Mysql:配置mysql的高可用
说明:
今天接着研究DRBD的第二个应用,利用DRBD+Heartbeat+Mysql:配置mysql的高可用
环境:
[root@dbm137 ~]# cat /etc/issueCentOS release 6.4 (Final)Kernel \r on an \m[root@dbm137 ~]# uname -r2.6.32-358.el6.i686
dbm137 | 192.168.186.137 | dbm137.51.com | primary | DRBD+Heartbeat+Mysql |
dbm138 | 192.168.186.138 | dbm138.51.com | secondary | DRBD+Heartbeat+Mysql |
vip | 192.168.186.200 |
准备工作和安装DRBD:
安装配置mysql:
安装mysql:(dbm137,dbm138)
##安装依赖包:[root@dbm ~]# yum -y install gcc gcc-c++ autoconf automake bison ncurses-devel libtool-ltdl-devel* cmake make
[root@dbm ~]# mkdir -p /opt/mysql/data #创建数据库的临时数据目录[root@dbm ~]# cd /usr/local/src/[root@dbm src]# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.22.tar.gz[root@dbm src]# tar -zxf mysql-5.6.22.tar.gz[root@dbm src]# cd mysql-5.6.22[root@dbm mysql-5.6.22]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/opt/mysql/data -DSYSCONFDIR=/usr/local/mysql -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS:STRING=utf8,gbk -DWITH_DEBUG=0##注意-DCMAKE_INSTALL_PREFIX,-DMYSQL_DATADIR和-DSYSCONFDIR选项的值[root@dbm mysql-5.6.22]# make #这个过程可能会很久[root@dbm mysql-5.6.22]# make install[root@dbm ~]# groupadd mysql #创建mysql用户组[root@dbm ~]# useradd -s /sbin/nologin -g mysql mysql[root@dbm ~]# chown -R mysql.mysql /opt/mysql #授权数据目录[root@dbm ~]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/opt/mysql/data --defaults-file=/usr/local/mysql/my.cnf --user=mysql #初始化数据库
配置mysql:
dbm137(Primary)
[root@dbm137 ~]# mount /dev/drbd0 /data/ #配置前先把DRBD设备挂载[root@dbm137 ~]# df -hFilesystem Size Used Avail Use% Mounted on/dev/mapper/VolGroup-lv_root 19G 3.4G 14G 20% /tmpfs 58M 0 58M 0% /dev/shm/dev/sda1 477M 43M 409M 10% /boot/dev/drbd0 8.8G 21M 8.3G 1% /data[root@dbm137 ~]# mkdir -p /data/mysql/binlog #创建二进制日志目录[root@dbm137 ~]# mkdir -p /data/mysql/data #创建数据库的数据目录[root@dbm137 ~]# cp -a /opt/mysql/data/* /data/mysql/data/ #拷贝数据到数据库目录[root@dbm137 ~]# chown -R mysql.mysql /data/mysql/ #授权mysql目录[root@dbm137 ~]# cd /data/mysql/data/[root@dbm137 data]# lltotal 110604-rw-rw---- 1 mysql mysql 12582912 Jul 2 12:00 ibdata1-rw-rw---- 1 mysql mysql 50331648 Jul 2 12:00 ib_logfile0-rw-rw---- 1 mysql mysql 50331648 Jul 2 12:00 ib_logfile1drwx------ 2 mysql mysql 4096 Jul 2 12:00 mysqldrwx------ 2 mysql mysql 4096 Jul 2 12:00 performance_schemadrwx------ 2 mysql mysql 4096 Jul 2 12:00 test[root@dbm137 ~]# mv /usr/local/mysql/my.cnf /usr/local/mysql/my.cnf.old[root@dbm137 ~]# vi /usr/local/mysql/my.cnf #修改配置文件[mysqld]basedir = /usr/local/mysql #安装路径datadir = /data/mysql/data #数据目录port = 3306server-id = 1 #此值不要去修改pid-file = /data/mysql/data/mysql.pid #进程文件socket = /var/lib/mysql/mysql.sockdefault_storage_engine = InnoDBlog-bin = /data/mysql/binlog/mysql-binlog #二进制文件expire_logs_days = 14max_binlog_size = 5Gbinlog_cache_size = 64Mmax_binlog_cache_size = 128Mslow_query_loglong_query_time = 2slow_query_log_file = /data/mysql/data/slow.log #slow日志文件open_files_limit = 65535innodb = FORCEinnodb_buffer_pool_size = 100M #注意这个参数innodb_log_file_size = 1Gquery_cache_size = 0thread_cache_size = 64table_definition_cache = 512table_open_cache = 512max_connections = 200sort_buffer_size = 10Mmax_allowed_packet = 6Msql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES[client]#socket = /var/lib/mysql/mysql.sock##注意:innodb_buffer_pool_size:##主要作用是缓存innodb表的索引,数据,插入数据时的缓冲##默认值:128M##专用mysql服务器设置此值的大小:系统内存的70%-80%最佳##如果你的系统内存不大,查看这个参数,把它的值设置小一点吧(若值设置大了,启动会报错)
[root@dbm137 ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld #拷贝启动脚本[root@dbm137 ~]# /etc/init.d/mysqld start #启动mysql服务Starting MySQL.................................... SUCCESS! ##注意:##千万不要,千万不要设置mysql开机自动启动,因为mysql服务的启动由heartbeat统一管理[root@dbm137 ~]# ps -ef | grep mysqlroot 16284 1 0 12:46 pts/1 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql/data --pid-file=/data/mysql/data/mysql.pidmysql 16783 16284 6 12:46 pts/1 00:00:03 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mysqld.log --open-files-limit=65535 --pid-file=/data/mysql/data/mysql.pid --socket=/var/lib/mysql/mysql.sock --port=3306root 16878 1286 0 12:47 pts/1 00:00:00 grep mysql[root@dbm137 ~]# vi /etc/profile #修改PATH路径##在最后添加:export PATH=$PATH:/usr/local/mysql/bin[root@dbm137 ~]# source /etc/profile #立即生效
dbm138(secondary)
##下面的操作一定要看仔细##先进行DRBD设备的主备切换,把dbm137切换为seocndary,把dbm138切换为primary##在dbm137(primary)端执行[root@dbm137 ~]# /etc/init.d/mysqld stop #将137的mysql服务stop掉Shutting down MySQL. SUCCESS! [root@dbm137 ~]# umount /dev/drbd0 #卸载DRBD设备[root@dbm137 ~]# drbdadm secondary r0 #切换137为secondary[root@dbm137 ~]# cat /proc/drbd | grep roversion: 8.3.16 (api:88/proto:86-97) 0: cs:Connected ro:Secondary/Secondary ds:UpToDate/UpToDate C r-----
##在dbm138(secondary)端执行[root@dbm138 ~]# drbdadm primary r0 #把138切换为primary[root@dbm138 ~]# cat /proc/drbd | grep roversion: 8.3.16 (api:88/proto:86-97) 0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----[root@dbm138 ~]# mount /dev/drbd0 /data/ #挂载DRBD设备[root@dbm138 ~]# df -hFilesystem Size Used Avail Use% Mounted on/dev/mapper/VolGroup-lv_root 19G 3.4G 14G 20% /tmpfs 58M 0 58M 0% /dev/shm/dev/sda1 477M 43M 409M 10% /boot/dev/drbd0 8.8G 2.1G 6.3G 25% /data[root@dbm138 ~]# cd /data/mysql/data/ #查看mysql数据[root@dbm138 data]# lltotal 2109468-rw-rw---- 1 mysql mysql 56 Jul 2 12:46 auto.cnf-rw-rw---- 1 mysql mysql 12582912 Jul 2 13:02 ibdata1-rw-rw---- 1 mysql mysql 1073741824 Jul 2 13:02 ib_logfile0-rw-rw---- 1 mysql mysql 1073741824 Jul 2 12:46 ib_logfile1drwx------ 2 mysql mysql 4096 Jul 2 12:00 mysqldrwx------ 2 mysql mysql 4096 Jul 2 12:00 performance_schema-rw-rw---- 1 mysql mysql 185 Jul 2 12:46 slow.logdrwx------ 2 mysql mysql 4096 Jul 2 12:00 test[root@dbm138 ~]# cd /data/mysql/binlog/ #查看二进制文件[root@dbm138 binlog]# lltotal 8-rw-rw---- 1 mysql mysql 285 Jul 2 13:02 mysql-binlog.000001-rw-rw---- 1 mysql mysql 39 Jul 2 12:46 mysql-binlog.index[root@dbm138 ~]# mv /usr/local/mysql/my.cnf /usr/local/mysql/my.cnf.old[root@dbm138 ~]# vi /usr/local/mysql/my.cnf #修改配置文件[mysqld]basedir = /usr/local/mysql #安装路径datadir = /data/mysql/data #数据目录port = 3306server-id = 1 #此值不要去修改,因为主备mysql同一时刻只有一个被启动pid-file = /data/mysql/data/mysql.pid #进程文件socket = /var/lib/mysql/mysql.sockdefault_storage_engine = InnoDBlog-bin = /data/mysql/binlog/mysql-binlog #二进制文件expire_logs_days = 14max_binlog_size = 5Gbinlog_cache_size = 10Mmax_binlog_cache_size = 20Mslow_query_loglong_query_time = 2slow_query_log_file = /data/mysql/data/slow.log #slow日志文件open_files_limit = 65535innodb = FORCEinnodb_buffer_pool_size = 100M #注意这个参数innodb_log_file_size = 1Gquery_cache_size = 0thread_cache_size = 64table_definition_cache = 512table_open_cache = 512max_connections = 200sort_buffer_size = 10Mmax_allowed_packet = 6Msql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES[client]#socket = /var/lib/mysql/mysql.sock##注意:##主备节点要保证配置文件my.cnf两边一模一样,避免出现问题[root@dbm138 ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld #拷贝启动脚本##注意:##千万不要,千万不要设置mysql开机自动启动,因为mysql服务的启动由heartbeat统一管理[root@dbm138 ~]# /etc/init.d/mysqld start #启动mysql服务Starting MySQL.................................... SUCCESS! [root@dbm137 ~]# ps -ef | grep mysqlroot 16284 1 0 12:46 pts/1 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql/data --pid-file=/data/mysql/data/mysql.pidmysql 16783 16284 6 12:46 pts/1 00:00:03 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mysqld.log --open-files-limit=65535 --pid-file=/data/mysql/data/mysql.pid --socket=/var/lib/mysql/mysql.sock --port=3306root 16878 1286 0 12:47 pts/1 00:00:00 grep mysql[root@dbm138 ~]# cd /data/mysql/binlog/ #查看这次的二进制文件[root@dbm138 binlog]# ll #接着dbm137又重新生成一个.000002,正常total 12-rw-rw---- 1 mysql mysql 285 Jul 2 13:02 mysql-binlog.000001-rw-rw---- 1 mysql mysql 120 Jul 2 13:28 mysql-binlog.000002-rw-rw---- 1 mysql mysql 78 Jul 2 13:28 mysql-binlog.index[root@dbm138 ~]# vi /etc/profile #修改PATH路径##在最后添加:export PATH=$PATH:/usr/local/mysql/bin[root@dbm138 ~]# source /etc/profile #立即生效
安装配置HeartBeat:
安装HeartBeat:(dbm137,dbm138)
这里采用yum的方式安装HeartBeat(推荐)
centos6.4默认不带HeartBeat软件包,需要安装epel源
[root@dbm ~]# cd /usr/local/src/[root@dbm src]# wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm[root@dbm src]# rpm -ivh epel-release-6-8.noarch.rpm [root@dbm src]# yum -y install heartbeatLoaded plugins: fastestmirrorLoading mirror speeds from cached hostfileError: Cannot retrieve metalink for repository: epel. Please verify its path and try again
注意:yum安装时报错,解决方法:
[root@dbm ~]#vi /etc/yum.repos.d/epel.repo#将所有baseurl行的注释去掉#将所有mirrorlist行注释掉[root@dbm src]# yum -y install heartbeat
配置HeartBeat:(dbm137,dbm138)
注意:此时dbm138是Primary
##配置前的准备工作:##stop掉两个节点的mysql服务,会由heartbeat统一管理[root@dbm ~]# /etc/init.d/mysqld stopShutting down MySQL.. SUCCESS!##卸载两个节点的DRBD设备,会由heartbeat统一管理[root@dbm ~]# umount /dev/drbd0
Heartbeat配置共涉及以下几个文件:/etc/ha.d/ha.cf #主配置文件/etc/ha.d/haresources #资源文件/etc/ha.d/authkeys #认证相关/etc/ha.d/resource.d/mysqld #mysql启动脚本,由HeartBeat管理
编辑主配置文件ha.cf[root@dbm137 ~]# vi /etc/ha.d/ha.cf (dbm137)logfile /var/log/ha-log#定义HA的日志名及存放位置logfacility local0keepalive 2#心跳发送时间间隔为2秒deadtime 10#死亡时间为10秒,备用节点10秒内没有检测到主机心跳,确认对方故障ucast eth0 192.168.186.138#ucast eth1 xxx.xxx.xxx.xxx#IP地址指定为对方IP#使用内网和外网两条心跳线,确保心跳的高可用auto_failback off#服务器正常后由新主服务器接管资源,另一台服务器放弃该资源,因为切换一次成本很高node dbm137.51.com dbm138.51.com#定义节点,指定主机名 hostname
[root@dbm138 ~]# vi /etc/ha.d/ha.cf (dbm138)logfile /var/log/ha-log#定义HA的日志名及存放位置logfacility local0keepalive 2#心跳发送时间间隔为2秒deadtime 10#死亡时间为10秒,备用节点10秒内没有检测到主机心跳,确认对方故障ucast eth0 192.168.186.137#ucast eth1 xxx.xxx.xxx.xxx#IP地址指定为对方IP#使用内网和外网两条心跳线,确保心跳的高可用auto_failback off#服务器正常后由新主服务器接管资源,另一台服务器放弃该资源,因为切换一次成本很高node dbm137.51.com dbm138.51.com#定义节点,指定主机名 hostname
编辑双机互联验证文件authkeys :(dbm137,dbm138)
[root@dbm ~]# vi /etc/ha.d/authkeysauth 11 crc
#需要将 /etc/ha.d/authkeys设为600的权限[root@dbm ~]# chmod 600 /etc/ha.d/authkeys
编辑集群资源文件haresources:(dbm137,dbm138)
[root@dbm~]# vi /etc/ha.d/haresourcesdbm138.51.com IPaddr::192.168.186.200/24/eth0 drbddisk::r0 Filesystem::/dev/drbd0::/data::ext4 mysqld##两台主机dbm137和dbm138的此文件内容一模一样,不要擅自把dbm137修改为dbm137.51.com##主机名设置为此时的主节点(Primary)的主机名,即dbm138.51.com##Ipaddr:绑定虚拟ip,且绑定在eth0上##drbddisk:指定drbd的资源r0,进行主备切换##Filesystem:指定drbd的设备/dev/drbd0,挂载点/data,文件系统ext4,进行drbd设备的挂载##mysqld:指定mysql的启动脚本,由heartbeat管理
编辑mysql脚本文件mysqld:(dbm137,dbm138)
[root@dbm ~]# cp -a /etc/init.d/mysqld /etc/ha.d/resource.d/
启动HeartBeat:(dbm137,dbm138)
注意:先在主节点上启动(dbm138是primary)
dbm138(Primary)[root@dbm138 ~]# /etc/init.d/heartbeat startStarting High-Availability services: INFO: Resource is stoppedDone.[root@dbm138 ~]# chkconfig heartbeat on[root@dbm138 ~]# ps -ef | grep heartbeatroot 17255 1 0 14:39 ? 00:00:00 heartbeat: master control processroot 17259 17255 0 14:39 ? 00:00:00 heartbeat: FIFO reader root 17260 17255 0 14:39 ? 00:00:00 heartbeat: write: ucast eth0root 17261 17255 0 14:39 ? 00:00:00 heartbeat: read: ucast eth0 root 17265 1283 0 14:39 pts/0 00:00:00 grep heartbeat[root@dbm138 ~]# ps -ef | grep mysql #查看mysql服务有没有启动root 17943 1 0 14:39 ? 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql/data --pid-file=/data/mysql/data/mysql.pidmysql 18437 17943 8 14:39 ? 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mysqld.log --open-files-limit=65535 --pid-file=/data/mysql/data/mysql.pid --socket=/var/lib/mysql/mysql.sock --port=3306root 18470 1283 0 14:39 pts/0 00:00:00 grep mysql[root@dbm138 ~]# df -h #查看DRBD设备有没有挂载Filesystem Size Used Avail Use% Mounted on/dev/mapper/VolGroup-lv_root 19G 3.4G 14G 20% /tmpfs 58M 0 58M 0% /dev/shm/dev/sda1 477M 43M 409M 10% /boot/dev/drbd0 8.8G 2.1G 6.3G 25% /data
dbm137(Secondary)[root@dbm137 ~]# /etc/init.d/heartbeat startStarting High-Availability services: INFO: Resource is stoppedDone.[root@dbm137 ~]# chkconfig heartbeat on[root@dbm137 ~]# ps -ef | grep heartbeatroot 17508 1 0 14:43 ? 00:00:00 heartbeat: master control processroot 17512 17508 0 14:43 ? 00:00:00 heartbeat: FIFO reader root 17513 17508 0 14:43 ? 00:00:00 heartbeat: write: ucast eth0root 17514 17508 0 14:43 ? 00:00:00 heartbeat: read: ucast eth0 root 17538 1271 0 14:43 pts/0 00:00:00 grep heartbeat[root@dbm137 ~]# ps -ef | grep mysql #备用节点mysql服务没有启动root 17540 1271 0 14:43 pts/0 00:00:00 grep mysql[root@dbm137 ~]# df -h #备用节点DRBD设备没有挂载Filesystem Size Used Avail Use% Mounted on/dev/mapper/VolGroup-lv_root 19G 3.4G 14G 20% /tmpfs 58M 0 58M 0% /dev/shm/dev/sda1 477M 43M 409M 10% /boot
测试:
测试一:客户端是否可以成功访问数据库
##数据库primary端(dbm138)[root@dbm138 ~]# cat /proc/drbd | grep ro #确认是Primary节点version: 8.3.16 (api:88/proto:86-97) 0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----[root@dbm138 ~]# mysql -p123456 #登陆数据库Warning: Using a password on the command line interface can be insecure.Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 3Server version: 5.6.22-log Source distributionCopyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> grant replication slave on *.* to 'client'@'192.168.186.136' identified by '123456'; #创建数据库账号并授权Query OK, 0 rows affected (0.03 sec)mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)
##客户端192.168.186.136[root@scj136 ~]# mysql -h192.168.186.200 -uclient -p123456 #连接数据库,ip是虚拟ip200Warning: Using a password on the command line interface can be insecure.Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 4Server version: 5.6.22-log Source distributionCopyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> show databases; #访问成功+--------------------+| Database |+--------------------+| information_schema || test |+--------------------+2 rows in set (0.12 sec)
测试二:主节点DRBD服务stop掉
把主节点DRBD服务stop掉,看Secondary节点变化,客户端是否仍可以继续访问数据库
测试三:主节点mysql服务stop掉
把主节点mysql服务stop掉,看Secondary节点变化,客户端是否仍可以继续访问数据库
##把主节点mysql服务stop掉后,Secondary节点没有任何变换,且192.168.186.136不能正常访问数据库了
##解决方法:##主备节点都要写[root@dbm ~]# vi /opt/monitor/mysql/monitormysql.sh#!/bin/bash#监控mysql服务的运行情况while truedo drbdstatus=`cat /proc/drbd 2> /dev/null | grep ro | tail -n1 | awk -F':' '{print $4}' | awk -F'/' '{print $1}'` #判断drbd的状态 mysqlstatus=`/etc/init.d/mysqld status | grep -c SUCCESS` #判断mysql是否运行 if [ -z $drbdstatus ];then sleep 10 continue elif [ $drbdstatus == 'Primary' ];then #若drbd是Primary状态 if [ $mysqlstatus -eq 0 ];then #若mysql未运行 /etc/init.d/mysqld start &> /dev/null #启动mysql服务 /etc/init.d/mysqld start &> /dev/null newmysqlstatus=`/etc/init.d/mysqld status | grep -c SUCCESS` #再次判断mysql是否成功启动 if [ $newmysqlstatus -eq 0 ];then #若mysql未运行,也就是无法启动 /etc/init.d/heartbeat stop &> /dev/null #将heartbeat服务stop掉,目的是自动切换到另一台备用机 /etc/init.d/heartbeat stop &> /dev/null #stop掉heartbeat服务后,会卸载DRBD设备,停止mysql服务,进行主备切换 fi fi fi sleep 5done
##注意:不要将此监控脚本放到/data/目录下,挂载drbd设备时,会把此脚本覆盖掉[root@dbm ~]# chmod u+x /opt/monitor/mysql/monitormysql.sh[root@dbm ~]#nohup bash /opt/monitor/mysql/monitormysql.sh & #放在后台运行##别忘了设置开机自动启动
测试四:主节点heartbeat服务stop掉或重启
把主节点heartbeat服务stop掉或重启,看Secondary节点变化,客户端是否仍可以继续访问数据库
##主节点(primary)dbm138[root@dbm138 ~]# /etc/init.d/heartbeat restart #重启heartbeat[root@dbm138 ~]# df -h #DRBD设备已卸载Filesystem Size Used Avail Use% Mounted on/dev/mapper/VolGroup-lv_root 19G 3.4G 14G 20% /tmpfs 58M 0 58M 0% /dev/shm/dev/sda1 477M 43M 409M 10% /boot[root@dbm138 ~]# ps -ef | grep mysql #mysql服务已关闭root 20784 1283 0 05:27 pts/0 00:00:00 grep mysql
##备用节点(secondary)dbm137[root@dbm137 ~]# df -h #DRBD设备已挂载Filesystem Size Used Avail Use% Mounted on/dev/mapper/VolGroup-lv_root 19G 3.4G 14G 20% /tmpfs 58M 0 58M 0% /dev/shm/dev/sda1 477M 43M 409M 10% /boot/dev/drbd0 8.8G 2.1G 6.3G 25% /data[root@dbm137 ~]# cat /proc/drbd #主备已切换version: 8.3.16 (api:88/proto:86-97)GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build32R6, 2014-11-24 14:49:06 0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r----- ns:11930328 nr:1408 dw:2494876 dr:9454779 al:633 bm:576 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0[root@dbm137 ~]# ps -ef | grep mysql #mysql服务已启动root 20633 1 0 05:26 ? 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql/data --pid-file=/data/mysql/data/mysql.pidmysql 21127 20633 3 05:26 ? 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mysqld.log --open-files-limit=65535 --pid-file=/data/mysql/data/mysql.pid --socket=/var/lib/mysql/mysql.sock --port=3306root 21440 1271 0 05:26 pts/0 00:00:00 grep mysql
##客户端192.168.186.136[root@scj136 ~]# mysql -h192.168.186.200 -uclient -p123456Warning: Using a password on the command line interface can be insecure.Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 2Server version: 5.6.22-log Source distributionCopyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> show databases; #访问仍正常+--------------------+| Database |+--------------------+| information_schema || test |+--------------------+2 rows in set (0.07 sec)
测试五:把主节点服务器关机或重启
把主节点服务器关机或重启,看Secondary节点的变化,192.168.186.136是否仍可访问数据库;等主节点重新启动后,看Secondary节点(新主节点)的变化
##主节点关机后主备节点切换正常,客户端访问正常##主节点恢复正常后,主备节点不会再去切换,由Secondary节点(新主节点)继续对外提供服务