Heartbeat+Drbd+MySQL高可用

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS PostgreSQL,集群系列 2核4GB
简介:

一、环境介绍

继续使用之前heartbeat+drbd+nfs的环境,192.168.49.0/24网段用来ssh远程连接,172.16.49.0/24用来做心跳连接,并且也做drbd同步数据使用。因为中间做了好多改变,这里再次给出环境的配置情况。

主机名 角色 IP地址
heartbeat01.contoso.com heartbeat+drbd+mysql(节点1)

eth0:192.168.49.133

eth1:172.16.49.133

heartbeat02.contoso.com heartbeat+drbd+mysql(节点2)

eth0:192.168.49.134

eth1:172.16.49.134

关于heartbeat和drbd的安装就不再描述了,之前的博文中有,下面只是贴出来之前两者的相关配置:

1、heartbeat的配置文件内容(下面以heartbeat02为例来列举,heartbeat01上的配置与heartbeat02相同)如下:

authkeys的内容:

[root@heartbeat02 ~]# egrep -v "#|^$" /etc/ha.d/authkeys 

auth 2

2 sha1 c6091592594cd14c

ha.cf的内容:

[root@heartbeat02 ~]# egrep -v "#|^$" /etc/ha.d/ha.cf

debugfile /var/log/ha-debug

logfile /var/log/ha-log

logfacility local1

keepalive 2

deadtime 30

warntime 10

initdead 60

udpport 694

mcast eth1 225.0.0.1 694 1 0

auto_failback on

node heartbeat01.contoso.com

node heartbeat02.contoso.com

respawn hacluster /usr/lib64/heartbeat/ipfail

haresources的内容:

[root@heartbeat02 ~]# egrep -v "#|^$" /etc/ha.d/haresources

heartbeat01.contoso.com  IPaddr::192.168.49.100/24/eth1 drbddisk::test Filesystem::/dev/drbd0::/data::ext4 nfs killnfsd  #这里是完成nfs高可用后的配置

2、drbd的配置文件内容(下面也是以heartbeat02为例,heartbeat01和heartbeat02的配置相同)如下:

[root@heartbeat02 ~]# cat /etc/drbd.d/test.res

resource test

 {

 startup {

 wfc-timeout 30;

 outdated-wfc-timeout 20;

 degr-wfc-timeout 30;

 }

net {

 cram-hmac-alg sha1;

 shared-secret sync_disk;

 }

syncer {

 rate 200M;

 al-extents 257;

 on-no-data-accessible io-error;

 }

 on heartbeat01.contoso.com {

 device /dev/drbd0;

 disk /dev/sdb1;

 address 172.16.49.133:7788;

 flexible-meta-disk internal;

 }

 on heartbeat02.contoso.com {

 device /dev/drbd0;

 disk /dev/sdb1;

 address 172.16.49.134:7788;

 meta-disk internal;

 }

 }

3、另外需要注意的一些点

1)编辑hosts文件,添加对应主机的解析

[root@heartbeat02 ~]# cat /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

172.16.49.133   heartbeat01.contoso.com   heartbeat01

172.16.49.134   heartbeat02.contoso.com   heartbeat02

2)添加时间同步定时任务和主机路由

[root@heartbeat02 ~]# tail -2 /etc/rc.local

/usr/sbin/ntpdate  time.nist.gov 210.72.145.44 64.147.116.229  #校准主机时间

route add -host 172.16.49.133 dev eth1   #两台主机都要添加一条主机路由,主机都是对方的ip

3)关闭iptables并禁用SELinux

[root@heartbeat02 ~]# iptables -L -n

Chain INPUT (policy ACCEPT)

target     prot opt source               destination         


Chain FORWARD (policy ACCEPT)

target     prot opt source               destination         


Chain OUTPUT (policy ACCEPT)

target     prot opt source               destination         

[root@heartbeat02 ~]# getenforce

Disabled

4)查看一下开机启动项

[root@heartbeat02 ~]# chkconfig --list|grep heartbeat

heartbeat       0:off 1:off 2:off 3:off 4:off 5:off 6:off

[root@heartbeat02 ~]# chkconfig --list|grep drbd

drbd            0:off 1:off 2:on 3:on 4:on 5:on 6:off

[root@heartbeat02 ~]# chkconfig --list|grep rpcbind

rpcbind         0:off 1:off 2:on 3:on 4:on 5:on 6:off

[root@heartbeat02 ~]# chkconfig --list|grep nfs

nfs             0:off 1:off 2:off 3:off 4:off 5:off 6:off

二、安装MySQL

这里网上有很多教程,有的是rpm包的方式安装,然后使用脚本初始化,有的可参考老男孩的脚本安装,而我这里直接使用yum安装MySQL,然后通过修改配置文件的方式,让MySQL的数据保存在drbd同步目录(/data)中。

1、节点1(heartbeat01)上MySQL的安装及配置

1)yum安装MySQL并测试本地启动

[root@heartbeat01 ~]# yum -y install mysql-devel mysql-server

[root@heartbeat01 ~]# /etc/init.d/mysqld start

Initializing MySQL database:  Installing MySQL system tables...

OK

Filling help tables...

OK


To start mysqld at boot time you have to copy

support-files/mysql.server to the right place for your system


PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !

To do so, start the server, then issue the following commands:


/usr/bin/mysqladmin -u root password 'new-password'

/usr/bin/mysqladmin -u root -h heartbeat01.contoso.com password 'new-password'


Alternatively you can run:

/usr/bin/mysql_secure_installation


which will also give you the option of removing the test

databases and anonymous user created by default.  This is

strongly recommended for production servers.


See the manual for more instructions.


You can start the MySQL daemon with:

cd /usr ; /usr/bin/mysqld_safe &


You can test the MySQL daemon with mysql-test-run.pl

cd /usr/mysql-test ; perl mysql-test-run.pl


Please report any problems with the /usr/bin/mysqlbug script!


                                                           [  OK  ]

Starting mysqld:                                           [  OK  ]

[root@heartbeat01 ~]# mysqladmin -u root password "123456"

[root@heartbeat01 ~]# mysql -uroot -p123456

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 3

Server version: 5.1.73 Source distribution


Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.


Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> quit

Bye

2)将heartbeat01作为drbd的Primary节点,然后挂载到/data下

[root@heartbeat01 lib]# cat /proc/drbd

version: 8.3.16 (api:88/proto:86-97)

GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build64R6, 2014-11-24 14:51:37

 0: cs:Connected ro:Secondary/Secondary ds:UpToDate/UpToDate C r-----

    ns:0 nr:0 dw:0 dr:0 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0

[root@heartbeat01 lib]# drbdadm -- --overwrite-data-of-peer primary test

[root@heartbeat01 lib]# cat /proc/drbd

version: 8.3.16 (api:88/proto:86-97)

GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build64R6, 2014-11-24 14:51:37

 0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----

    ns:0 nr:0 dw:0 dr:664 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0

[root@heartbeat01 lib]# mount /dev/drbd0 /data

[root@heartbeat01 data]# rm -rf *  #清空之前nfs同步的文件

3)编辑MySQL配置文件

[root@heartbeat01 ~]# cp /etc/my.cnf /etc/my.cnf.bak$(date +%F)

[root@heartbeat01 ~]# vi /etc/my.cnf

[root@heartbeat01 ~]# diff /etc/my.cnf.bak2016-09-26 /etc/my.cnf

2,3c2,3

< datadir=/var/lib/mysql

< socket=/var/lib/mysql/mysql.sock

---

> datadir=/data/lib/mysql

> socket=/data/lib/mysql/mysql.sock

9,10c9,10

< log-error=/var/log/mysqld.log

< pid-file=/var/run/mysqld/mysqld.pid

---

> log-error=/data/log/mysqld.log

> pid-file=/data/run/mysqld/mysqld.pid

[root@heartbeat01 ~]# cp /etc/init.d/mysqld /etc/init.d/mysqld.bak$(date +%F)

[root@heartbeat01 ~]# vi /etc/init.d/mysqld

[root@heartbeat01 ~]# diff /etc/init.d/mysqld.bak2016-09-26 /etc/init.d/mysqld

35c35

< lockfile=/var/lock/subsys/$prog

---

> lockfile=/data/lock/subsys/$prog

51c51

< get_mysql_option mysqld datadir "/var/lib/mysql"

---

> get_mysql_option mysqld datadir "/data/lib/mysql"

55c55

< get_mysql_option mysqld_safe log-error "/var/log/mysqld.log"

---

> get_mysql_option mysqld_safe log-error "/data/log/mysqld.log"

57c57

< get_mysql_option mysqld_safe pid-file "/var/run/mysqld/mysqld.pid"

---

> get_mysql_option mysqld_safe pid-file "/data/run/mysqld/mysqld.pid"

[root@heartbeat01 ~]# cp /usr/bin/mysqld_safe /usr/bin/mysqld_safe.bak$(date +%F)

[root@heartbeat01 ~]# vi /usr/bin/mysqld_safe

[root@heartbeat01 ~]# diff /usr/bin/mysqld_safe.bak2016-09-27 /usr/bin/mysqld_safe

286c286

<   DATADIR=/var/lib/mysql

---

>   DATADIR=/data/lib/mysql

436c436

< safe_mysql_unix_port=${mysql_unix_port:-${MYSQL_UNIX_PORT:-/var/lib/mysql/mysql.sock}}

---

> safe_mysql_unix_port=${mysql_unix_port:-${MYSQL_UNIX_PORT:-/data/lib/mysql/mysql.sock}}

4)拷贝MySQL目录文件

[root@heartbeat01 ~]# mkdir -p /data/{lib,run,lock,log}

[root@heartbeat01 ~]# cp -ar /var/lib/mysql /data/lib/

[root@heartbeat01 ~]# cp -ar /var/run/mysqld /data/run/

[root@heartbeat01 ~]# cp -ar /var/log/mysqld.log /data/log

[root@heartbeat01 ~]# chown -R mysql:mysql /data/lib

[root@heartbeat01 ~]# chown -R mysql:mysql /data/run

[root@heartbeat01 ~]# chown -R mysql:mysql /data/log

[root@heartbeat01 ~]# chown -R mysql:mysql /data/lock

[root@heartbeat01 /]# ln -s /data/lib/mysql/mysql.sock /var/lib/mysql/mysql.sock

#这一条命令是防止使用mysql命令时找不到/var/lib/mysql/mysql.sock文件

5)测试MySQL能否在/data下启动

[root@heartbeat01 /]# /etc/init.d/mysqld start

Starting mysqld:                                           [  OK  ]

[root@heartbeat01 /]# mysql -uroot -p123456

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 2

Server version: 5.1.73 Source distribution


Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.


Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| test               |

+--------------------+

3 rows in set (0.00 sec)

2、节点2(heartbeat02)上MySQL的安装及配置

1)安装MySQL

[root@heartbeat02 ~]# yum -y install mysql-devel mysql-server

2)配置MySQL

在节点2上不需要拷贝MySQL相关文件,因为相关文件已经在heartbeat01上拷贝到drbd的同步目录中了,也已经通过drbd同步到heartbeat02上了,所以这里就不需要拷贝目录了,只需要修改配置文件即可,当然,可以使用scp命令拷贝heartbeat01上修改好的配置文件。

[root@heartbeat02 ~]# mv /etc/my.cnf /etc/my.cnf.bak

[root@heartbeat02 ~]# scp heartbeat01:/etc/my.cnf /etc/my.cnf

root@heartbeat01's password: 

my.cnf 

[root@heartbeat02 ~]# mv /etc/init.d/mysqld /etc/init.d/mysqld.bak

[root@heartbeat02 ~]# scp heartbeat01:/etc/init.d/mysqld /etc/init.d/mysqld

root@heartbeat01's password: 

mysqld                                                        

[root@heartbeat02 ~]# mv /usr/bin/mysqld_safe /usr/bin/mysqld_safe.bak

[root@heartbeat02 ~]# scp heartbeat01:/usr/bin/mysqld_safe /usr/bin/mysqld_safe

root@heartbeat01's password: 

mysqld_safe                                                     [root@heartbeat02 ~]# ln -s /data/lib/mysql/mysql.sock /var/lib/mysql/mysql.sock

3)测试heartbeat02上的MySQL能否在/data下启动

要测试heartbeat02上的MySQL能否在drbd的同步目录下启动,就要把drbd的目录挂载到heartbeat02的/data目录下。

[root@heartbeat02 ~]# cat /proc/drbd

version: 8.3.16 (api:88/proto:86-97)

GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build64R6, 2014-11-24 14:51:

 0: cs:Connected ro:Secondary/Primary ds:UpToDate/UpToDate C r-----

    ns:0 nr:0 dw:0 dr:0 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0

[root@heartbeat02 ~]# drbdadm disconnect test

[root@heartbeat02 ~]# drbdadm connect test

[root@heartbeat02 ~]# cat /proc/drbd

version: 8.3.16 (api:88/proto:86-97)

GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build64R6, 2014-11-24 14:51:37

 0: cs:Connected ro:Secondary/Primary ds:UpToDate/UpToDate C r-----

    ns:0 nr:0 dw:0 dr:0 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0

[root@heartbeat02 ~]# drbdadm primary test

[root@heartbeat02 ~]# cat /proc/drbd

version: 8.3.16 (api:88/proto:86-97)

GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build64R6, 2014-11-24 14:51:37

 0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----

    ns:0 nr:0 dw:0 dr:664 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0

[root@heartbeat02 ~]# mount /dev/drbd0 /data

[root@heartbeat02 ~]# ll /data

total 16

drwxr-xr-x 3 mysql mysql 4096 Sep 27 01:17 lib

drwxr-xr-x 3 mysql mysql 4096 Sep 27 01:21 lock

drwxr-xr-x 2 mysql mysql 4096 Sep 27 01:18 log

drwxr-xr-x 3 mysql mysql 4096 Sep 27 01:17 run

[root@heartbeat02 ~]# /etc/init.d/mysqld start

Starting mysqld:                                           [  OK  ]

[root@heartbeat02 ~]# mysql -uroot -p123456

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 2

Server version: 5.1.73 Source distribution


Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.


Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| test               |

+--------------------+

3 rows in set (0.02 sec)


mysql> select Host,User,Password from mysql.user;

+-------------------------+------+-------------------------------------------+

| Host                    | User | Password                                  |

+-------------------------+------+-------------------------------------------+

| localhost               | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |

| heartbeat01.contoso.com | root |                                           |

| 127.0.0.1               | root |                                           |

| localhost               |      |                                           |

| heartbeat01.contoso.com |      |                                           |

+-------------------------+------+-------------------------------------------+

5 rows in set (0.00 sec)


mysql> quit

Bye

注意:只有在两个节点上手工启动MySQL成功后,才可以使用heartbeat做MySQL的高可用。上面的步骤成功后,开始在heartbeat中配置MySQL服务。

三、heartbeat实现MySQL高可用

1、检查节点的状态

[root@heartbeat01 ~]# cat /proc/drbd

version: 8.3.16 (api:88/proto:86-97)

GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build64R6, 2014-11-24 14:51:37

 0: cs:Connected ro:Secondary/Secondary ds:UpToDate/UpToDate C r-----

    ns:0 nr:24 dw:24 dr:0 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0

[root@heartbeat01 ~]# /etc/init.d/heartbeat status

heartbeat is stopped. No process

[root@heartbeat02 ~]# cat /proc/drbd

version: 8.3.16 (api:88/proto:86-97)

GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build64R6, 2014-11-24 14:51:37

 0: cs:Connected ro:Secondary/Secondary ds:UpToDate/UpToDate C r-----

    ns:0 nr:0 dw:0 dr:0 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0

[root@heartbeat02 ~]#  /etc/init.d/heartbeat status

heartbeat is stopped. No process

2、修改heartbeat的haresources文件

[root@heartbeat01 ~]# vi /etc/ha.d/haresources 

[root@heartbeat01 ~]# tail -1 /etc/ha.d/haresources 

heartbeat01.contoso.com  IPaddr::192.168.49.100/24/eth1 drbddisk::test Filesystem::/dev/drbd0::/data::ext4 mysqld

[root@heartbeat02 ~]# vi /etc/ha.d/haresources

[root@heartbeat02 ~]# tail -1 /etc/ha.d/haresources

heartbeat01.contoso.com  IPaddr::192.168.49.100/24/eth1 drbddisk::test Filesystem::/dev/drbd0::/data::ext4 mysqld

3、开启heartbeat服务,查看MySQL服务是否正常

[root@heartbeat01 ~]# /etc/init.d/heartbeat start

Starting High-Availability services: INFO:  Resource is stopped

Done.

[root@heartbeat02 ~]# /etc/init.d/heartbeat start

Starting High-Availability services: INFO:  Resource is stopped

Done.

4、在heartbeat01上进行检查

[root@heartbeat01 ~]# ip a|grep 49.100

    inet 192.168.49.100/24 scope global eth1

[root@heartbeat01 ~]# cat /proc/drbd

version: 8.3.16 (api:88/proto:86-97)

GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build64R6, 2014-11-24 14:51:37

 0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----

    ns:192 nr:24 dw:216 dr:3929 al:7 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0

[root@heartbeat01 ~]# ll /data

total 16

drwxr-xr-x 3 mysql mysql 4096 Sep 27 01:17 lib

drwxr-xr-x 3 mysql mysql 4096 Sep 27 01:21 lock

drwxr-xr-x 2 mysql mysql 4096 Sep 27 01:18 log

drwxr-xr-x 3 mysql mysql 4096 Sep 27 01:17 run

[root@heartbeat01 ~]# /etc/init.d/mysqld status

mysqld (pid  10360) is running...

[root@heartbeat01 ~]# mysql -uroot -p123456

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 2

Server version: 5.1.73 Source distribution


Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.


Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> select Host,User,Password from mysql.user;

+-------------------------+------+-------------------------------------------+

| Host                    | User | Password                                  |

+-------------------------+------+-------------------------------------------+

| localhost               | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |

| heartbeat01.contoso.com | root |                                           |

| 127.0.0.1               | root |                                           |

| localhost               |      |                                           |

| heartbeat01.contoso.com |      |                                           |

+-------------------------+------+-------------------------------------------+

5 rows in set (0.00 sec)


mysql> 

5、在heartbeat02上进行检查

[root@heartbeat02 ~]# ip a|grep 49.100

[root@heartbeat02 ~]# cat /proc/drbd

version: 8.3.16 (api:88/proto:86-97)

GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build64R6, 2014-11-24 14:51:37

 0: cs:Connected ro:Secondary/Primary ds:UpToDate/UpToDate C r-----

    ns:0 nr:208 dw:208 dr:0 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0

[root@heartbeat02 ~]# ll /data

total 0

[root@heartbeat02 ~]# /etc/init.d/mysqld status

mysqld is stopped

[root@heartbeat02 ~]# 

四、进行故障切换测试

1、在heartbeat01上创建一个数据库,看是否能同步到heartbeat02上

mysql> create database tb01;

Query OK, 1 row affected (0.02 sec)

mysql> use tb01;

Database changed

mysql> create table staff(id int primary key, name varchar2(40) null);

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'varchar2(40) null)' at line 1

mysql> create table staff (

    -> id int not null,

    -> name varchar(40) null,

    -> primary key (id));

Query OK, 0 rows affected (0.14 sec)


mysql> insert into staff values (01,'Tom');

Query OK, 1 row affected (0.01 sec)


mysql> insert into staff values (02,'Jerry');

Query OK, 1 row affected (0.00 sec)


mysql> select * from staff;

+----+-------+

| id | name  |

+----+-------+

|  1 | Tom   |

|  2 | Jerry |

+----+-------+

2 rows in set (0.00 sec)

                

mysql> quit

Bye

2、停止heartbeat01上的heartbeat服务,查看是否MySQL能切换到heartbeat02并运行正常

[root@heartbeat01 ~]# /etc/init.d/heartbeat stop

Stopping High-Availability services: Done.

1)在heartbeat01上

[root@heartbeat01 ~]# ip a |grep 49.100

[root@heartbeat01 ~]# cat /proc/drbd

version: 8.3.16 (api:88/proto:86-97)

GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build64R6, 2014-11-24 14:51:37

 0: cs:Connected ro:Secondary/Primary ds:UpToDate/UpToDate C r-----

    ns:708 nr:264 dw:972 dr:3957 al:8 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0

[root@heartbeat01 ~]# ll /data

total 0

[root@heartbeat01 ~]# /etc/init.d/mysqld status

mysqld dead but subsys locked  #这里是因为无法删除MySQL锁定文件所致,但不影响这里的测试

解决方法:

[root@heartbeat01 ~]# mv /var/lock/subsys/mysqld /tmp

[root@heartbeat01 ~]# /etc/init.d/mysqld status

mysqld is stopped

当然,也可以在/etc/init.d/mysqld脚本中的stop方法中加入“rm -f /var/lock/subsys/mysqld”一行即可。

2)在heartbeat02上

[root@heartbeat02 ~]# ip a|grep 49.100

    inet 192.168.49.100/24 scope global eth1

[root@heartbeat02 ~]# cat /proc/drbd

version: 8.3.16 (api:88/proto:86-97)

GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build64R6, 2014-11-24 14:51:37

 0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----

    ns:188 nr:708 dw:896 dr:3929 al:6 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0

[root@heartbeat02 ~]# ll /data

total 16

drwxr-xr-x 3 mysql mysql 4096 Sep 27 01:17 lib

drwxr-xr-x 3 mysql mysql 4096 Sep 27 01:21 lock

drwxr-xr-x 2 mysql mysql 4096 Sep 27 01:18 log

drwxr-xr-x 3 mysql mysql 4096 Sep 27 01:17 run

[root@heartbeat02 ~]# /etc/init.d/mysqld status

mysqld (pid  8578) is running...

[root@heartbeat02 ~]# mysql -uroot -p123456

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 3

Server version: 5.1.73 Source distribution


Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.


Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.


Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


mysql> select * from tb01.staff;

+----+-------+

| id | name  |

+----+-------+

|  1 | Tom   |

|  2 | Jerry |

+----+-------+

2 rows in set (0.04 sec)


mysql> quit

Bye


OK,可以看到,不仅heartbeat02上成功启动了MySQL服务,而且之前在heartbeat01上创建的数据库和表都已经同步过来。

3、重新启动heartbeat01的heartbeat服务,看一下heartbeat01是否能接管MySQL服务

[root@heartbeat01 ~]# ip a|grep 49.100

    inet 192.168.49.100/24 scope global eth1

[root@heartbeat01 ~]# cat /proc/drbd

version: 8.3.16 (api:88/proto:86-97)

GIT-hash: a798fa7e274428a357657fb52f0ecf40192c1985 build by phil@Build64R6, 2014-11-24 14:51:37

 0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----

    ns:884 nr:448 dw:1332 dr:7886 al:8 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0

[root@heartbeat01 ~]# ll /data

total 16

drwxr-xr-x 3 mysql mysql 4096 Sep 27 01:17 lib

drwxr-xr-x 3 mysql mysql 4096 Sep 27 01:21 lock

drwxr-xr-x 2 mysql mysql 4096 Sep 27 01:18 log

drwxr-xr-x 3 mysql mysql 4096 Sep 27 01:17 run

[root@heartbeat01 ~]# /etc/init.d/mysqld status

mysqld (pid  11668) is running...

可以看到,heartbeat01成功接管了所有的资源和服务,MySQL也已经在heartbeat01上运行了。

[root@heartbeat02 ~]# /etc/init.d/mysqld status

mysqld is stopped

另外,heartbeat02上的MySQL服务已经停止,没有出现mysqld dead but subsys locked的问题了。

4、查看故障切换中的heartbeat日志

[root@heartbeat01 ~]# cat /var/log/ha-log

Sep 27 05:56:50 heartbeat01.contoso.com heartbeat: [10991]: info: Pacemaker support: false

Sep 27 05:56:50 heartbeat01.contoso.com heartbeat: [10991]: WARN: Logging daemon is disabled --enabling logging daemon is recommended

Sep 27 05:56:50 heartbeat01.contoso.com heartbeat: [10991]: info: **************************

Sep 27 05:56:50 heartbeat01.contoso.com heartbeat: [10991]: info: Configuration validated. Starting heartbeat 3.0.4

Sep 27 05:56:50 heartbeat01.contoso.com heartbeat: [10992]: info: heartbeat: version 3.0.4

Sep 27 05:56:50 heartbeat01.contoso.com heartbeat: [10992]: info: Heartbeat generation: 1474533059

Sep 27 05:56:50 heartbeat01.contoso.com heartbeat: [10992]: info: glib: UDP multicast heartbeat started for group 225.0.0.1 port 694 interface eth1 (ttl=1 loop=0)

Sep 27 05:56:50 heartbeat01.contoso.com heartbeat: [10992]: info: G_main_add_TriggerHandler: Added signal manual handler

Sep 27 05:56:50 heartbeat01.contoso.com heartbeat: [10992]: info: G_main_add_TriggerHandler: Added signal manual handler

Sep 27 05:56:50 heartbeat01.contoso.com heartbeat: [10992]: info: G_main_add_SignalHandler: Added signal handler for signal 17

Sep 27 05:56:50 heartbeat01.contoso.com heartbeat: [10992]: info: Local status now set to: 'up'

Sep 27 05:56:52 heartbeat01.contoso.com heartbeat: [10992]: info: Link heartbeat02.contoso.com:eth1 up.

Sep 27 05:56:52 heartbeat01.contoso.com heartbeat: [10992]: info: Status update for node heartbeat02.contoso.com: status active

harc(default)[11000]: 2016/09/27_05:56:52 info: Running /etc/ha.d//rc.d/status status

Sep 27 05:56:53 heartbeat01.contoso.com heartbeat: [10992]: info: Comm_now_up(): updating status to active

Sep 27 05:56:53 heartbeat01.contoso.com heartbeat: [10992]: info: Local status now set to: 'active'

Sep 27 05:56:53 heartbeat01.contoso.com heartbeat: [10992]: info: Starting child client "/usr/lib64/heartbeat/ipfail" (498,499)

Sep 27 05:56:53 heartbeat01.contoso.com heartbeat: [11018]: info: Starting "/usr/lib64/heartbeat/ipfail" as uid 498  gid 499 (pid 11018)

Sep 27 05:56:53 heartbeat01.contoso.com heartbeat: [10992]: info: remote resource transition completed.

Sep 27 05:56:53 heartbeat01.contoso.com heartbeat: [10992]: info: remote resource transition completed.

Sep 27 05:56:53 heartbeat01.contoso.com heartbeat: [10992]: info: Local Resource acquisition completed. (none)

Sep 27 05:56:54 heartbeat01.contoso.com heartbeat: [10992]: info: heartbeat02.contoso.com wants to go standby [foreign]

Sep 27 05:56:59 heartbeat01.contoso.com heartbeat: [10992]: info: standby: acquire [foreign] resources from heartbeat02.contoso.com

Sep 27 05:56:59 heartbeat01.contoso.com heartbeat: [11038]: info: acquire local HA resources (standby).

ResourceManager(default)[11051]: 2016/09/27_05:56:59 info: Acquiring resource group: heartbeat01.contoso.com IPaddr::192.168.49.100/24/eth1 drbddisk::test Filesystem::/dev/drbd0::/data::ext4 mysqld

/usr/lib/ocf/resource.d//heartbeat/IPaddr(IPaddr_192.168.49.100)[11079]: 2016/09/27_05:56:59 INFO:  Resource is stopped

ResourceManager(default)[11051]: 2016/09/27_05:56:59 info: Running /etc/ha.d/resource.d/IPaddr 192.168.49.100/24/eth1 start

IPaddr(IPaddr_192.168.49.100)[11201]: 2016/09/27_05:56:59 INFO: Adding inet address 192.168.49.100/24 to device eth1

IPaddr(IPaddr_192.168.49.100)[11201]: 2016/09/27_05:56:59 INFO: Bringing device eth1 up

IPaddr(IPaddr_192.168.49.100)[11201]: 2016/09/27_05:56:59 INFO: /usr/libexec/heartbeat/send_arp -i 200 -r 5 -p /var/run/resource-agents/send_arp-192.168.49.100 eth1 192.168.49.100 auto not_used not_used

/usr/lib/ocf/resource.d//heartbeat/IPaddr(IPaddr_192.168.49.100)[11175]: 2016/09/27_05:56:59 INFO:  Success

ResourceManager(default)[11051]: 2016/09/27_05:56:59 info: Running /etc/ha.d/resource.d/drbddisk test start

/usr/lib/ocf/resource.d//heartbeat/Filesystem(Filesystem_/dev/drbd0)[11330]: 2016/09/27_05:56:59 INFO:  Resource is stopped

ResourceManager(default)[11051]: 2016/09/27_05:56:59 info: Running /etc/ha.d/resource.d/Filesystem /dev/drbd0 /data ext4 start

Filesystem(Filesystem_/dev/drbd0)[11412]: 2016/09/27_05:56:59 INFO: Running start for /dev/drbd0 on /data

/usr/lib/ocf/resource.d//heartbeat/Filesystem(Filesystem_/dev/drbd0)[11404]: 2016/09/27_05:56:59 INFO:  Success

ResourceManager(default)[11051]: 2016/09/27_05:56:59 info: Running /etc/init.d/mysqld  start

Sep 27 05:57:00 heartbeat01.contoso.com ipfail: [11018]: info: Ping node count is balanced.

Sep 27 05:57:00 heartbeat01.contoso.com heartbeat: [11038]: info: local HA resource acquisition completed (standby).

Sep 27 05:57:00 heartbeat01.contoso.com heartbeat: [10992]: info: Standby resource acquisition done [foreign].

Sep 27 05:57:00 heartbeat01.contoso.com heartbeat: [10992]: info: Initial resource acquisition complete (auto_failback)

Sep 27 05:57:01 heartbeat01.contoso.com heartbeat: [10992]: info: remote resource transition completed.




本文转自 jerry1111111 51CTO博客,原文链接:http://blog.51cto.com/jerry12356/1857090,如需转载请自行联系原作者
相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
1月前
|
存储 SQL 关系型数据库
Mysql高可用架构方案
本文阐述了Mysql高可用架构方案,介绍了 主从模式,MHA模式,MMM模式,MGR模式 方案的实现方式,没有哪个方案是完美的,开发人员在选择何种方案应用到项目中也没有标准答案,合适的才是最好的。
164 3
Mysql高可用架构方案
|
4月前
|
运维 容灾 关系型数据库
MySQL高可用方案--Xenon全解
MySQL高可用方案--Xenon全解
|
6月前
|
运维 容灾 关系型数据库
介绍几种 MySQL 官方高可用方案
MySQL 官方提供了多种高可用部署方案,从最基础的主从复制到组复制再到 InnoDB Cluster 等等。本篇文章以 MySQL 8.0 版本为准,介绍下不同高可用方案架构原理及使用场景。
1423 3
介绍几种 MySQL 官方高可用方案
|
4月前
|
SQL 关系型数据库 MySQL
orchestrator搭建mysql高可用
orchestrator搭建mysql高可用
56 0
|
4月前
|
缓存 关系型数据库 MySQL
如何实现mysql高可用集群
如何实现mysql高可用集群
58 0
|
4月前
|
安全 关系型数据库 MySQL
【MySQL】Orchestrator最简单的 mysql 高可用方案最细细细细~
【MySQL】Orchestrator最简单的 mysql 高可用方案最细细细细~
|
4月前
|
缓存 关系型数据库 MySQL
MySQL调优秘籍曝光!从索引到事务,全方位解锁高可用秘诀,让你的数据库性能飞起来!
【8月更文挑战第6天】MySQL是顶级关系型数据库之一,其性能直接影响应用的高可用性与用户体验。本文聚焦MySQL的高性能调优,从索引设计到事务管理,逐一解析。介绍如何构建高效索引,如联合索引`CREATE INDEX idx_order_customer ON orders(order_id, customer_id);`,以及索引覆盖查询等技术。
86 0
|
4天前
|
存储 Oracle 关系型数据库
数据库传奇:MySQL创世之父的两千金My、Maria
《数据库传奇:MySQL创世之父的两千金My、Maria》介绍了MySQL的发展历程及其分支MariaDB。MySQL由Michael Widenius等人于1994年创建,现归Oracle所有,广泛应用于阿里巴巴、腾讯等企业。2009年,Widenius因担心Oracle收购影响MySQL的开源性,创建了MariaDB,提供额外功能和改进。维基百科、Google等已逐步替换为MariaDB,以确保更好的性能和社区支持。掌握MariaDB作为备用方案,对未来发展至关重要。
18 3
|
4天前
|
安全 关系型数据库 MySQL
MySQL崩溃保险箱:探秘Redo/Undo日志确保数据库安全无忧!
《MySQL崩溃保险箱:探秘Redo/Undo日志确保数据库安全无忧!》介绍了MySQL中的三种关键日志:二进制日志(Binary Log)、重做日志(Redo Log)和撤销日志(Undo Log)。这些日志确保了数据库的ACID特性,即原子性、一致性、隔离性和持久性。Redo Log记录数据页的物理修改,保证事务持久性;Undo Log记录事务的逆操作,支持回滚和多版本并发控制(MVCC)。文章还详细对比了InnoDB和MyISAM存储引擎在事务支持、锁定机制、并发性等方面的差异,强调了InnoDB在高并发和事务处理中的优势。通过这些机制,MySQL能够在事务执行、崩溃和恢复过程中保持
22 3
|
4天前
|
SQL 关系型数据库 MySQL
数据库灾难应对:MySQL误删除数据的救赎之道,技巧get起来!之binlog
《数据库灾难应对:MySQL误删除数据的救赎之道,技巧get起来!之binlog》介绍了如何利用MySQL的二进制日志(Binlog)恢复误删除的数据。主要内容包括: 1. **启用二进制日志**:在`my.cnf`中配置`log-bin`并重启MySQL服务。 2. **查看二进制日志文件**:使用`SHOW VARIABLES LIKE &#39;log_%&#39;;`和`SHOW MASTER STATUS;`命令获取当前日志文件及位置。 3. **创建数据备份**:确保在恢复前已有备份,以防意外。 4. **导出二进制日志为SQL语句**:使用`mysqlbinlog`
28 2