heartbeat+DRBD+mysql高可用集群实战

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

 

heartbeat+DRBD+mysql高可用集群实战

四台主机

主机名                 IP地址             用途

dbm128                               172.16.0.128                      heartbeat+DRBD+MYSQLprimary节点

dbm129                               172.16.0.129                      heartbeat+DRBD+mysql secondary节点

dbs130                                 172.16.0.130                      slave(mater_host=172.16.0.233)

dbs131                                 172.16.0.131                      slave(mater_host=172.16.0.233)

配置前的准备

1.设置hostname及解析

编辑/etc/hosts文件,添加如下内容:

172.16.0.128   dbm128

172.16.0.129   dbm129

2.新增加一块/dev/sdb5硬盘存放DRBD数据(两台都增加)

命令如下:

[root@dbm128 ~]# fdisk /dev/sdb

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel

Building a new DOS disklabel. Changes will remain in memory only,

until you decide to write them. After that, of course, the previous

content won't be recoverable.

 

The number of cylinders for this disk is set to 6527.

There is nothing wrong with that, but this is larger than 1024,

and could in certain setups cause problems with:

1) software that runs at boot time (e.g., old versions of LILO)

2) booting and partitioning software from other OSs

   (e.g., DOS FDISK, OS/2 FDISK)

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): n

Command action

   e   extended

   p   primary partition (1-4)

e

Partition number (1-4): 1

First cylinder (1-6527, default 1):

Using default value 1

Last cylinder or +size or +sizeM or +sizeK (1-6527, default 6527):

Using default value 6527

Command (m for help): n

Command action

   l   logical (5 or over)

   p   primary partition (1-4)

l

First cylinder (1-6527, default 1):

Using default value 1

Last cylinder or +size or +sizeM or +sizeK (1-6527, default 6527):

Using default value 6527

 

Command (m for help): w

The partition table has been altered!

 

Calling ioctl() to re-read partition table.

Syncing disks.

 

3.DRBD的部署(两台机器都这么做)

yum -y install gcc  gcc-c++  kernel*  安装好后重启一下,免得后面出错

#wget http://oss.linbit.com/drbd/8.4/drbd-8.4.3.tar.gz

# tar zxvf drbd-8.4.3.tar.gz

#cd drbd-8.4.3

#./configure --prefix=/ --with-km

#make

#make install

4. 加载安装DRDB模块

#modprobe drbd

机器重启后DRBD模块消失,需要写入/etc/rc.local开机自动执行

通过lsmod来检查是否已经执行成功

[root@dbm128 drbd-8.4.3]# lsmod | grep drbd

drbd                  327968  0

5.编辑配置文件(两台配置一样dbm128dbm129

如果后面有问题要把LINUX防火墙关闭,或者把端口增加过去

[root@dbm128 drbd-8.4.3]# cat /etc/drbd.conf

global {usage-count yes;}

common{syncer{rate 200M;}}

resource r0{

         protocol C;

         net{

         cram-hmac-alg sha1;

         shared-secret "FooFunFactory";

         }

         disk{

         on-io-error detach;

         fencing resource-only;

         }

         startup{

         wfc-timeout 120;

         degr-wfc-timeout 120;

         }

         device /dev/drbd1;

         on dbm128{

         disk /dev/sdb5;

         address 172.16.0.128:7789;

         meta-disk internal;

         }

         on dbm129{

        disk /dev/sdb5;

        address 172.16.0.129:7789;

        meta-disk internal;

        }

}

 

6.启动DRBD(两台操作)

可通过如下命令启动DRBD

[root@dbm128 drbd-8.4.3]# drbdadm create-md all

[root@dbm128 drbd-8.4.3]# /etc/init.d/drbd start

[root@dbm128 drbd-8.4.3]# drbdadm -- --overwrite-data-of-peer primary all (这一步只在主服务器上做)

在主机上查看DRBD状态

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

version: 8.4.3 (api:1/proto:86-101)

GIT-hash: 89a294209144b68adb3ee85a73221f964d3ee515 build by root@dbm128, 2014-07-18 23:01:15

 1: cs:SyncSource ro:Primary/Secondary ds:UpToDate/Inconsistent C r---n-

    ns:45789492 nr:0 dw:0 dr:45794304 al:0 bm:2794 lo:0 pe:3 ua:5 ap:0 ep:1 wo:f oos:6639292

         [================>...] sync'ed: 87.4% (6480/51196)M

         finish: 0:06:46 speed: 16,336 (18,732) K/sec

以上数据正在同步。下面同步完成

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

version: 8.4.3 (api:1/proto:86-101)

GIT-hash: 89a294209144b68adb3ee85a73221f964d3ee515 build by root@dbm128, 2014-07-18 23:01:15

 

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

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

在从机上查看DRBD状态

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

version: 8.4.3 (api:1/proto:86-101)

GIT-hash: 89a294209144b68adb3ee85a73221f964d3ee515 build by root@dbm129, 2014-07-18 23:14:34

 

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

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

在执行过程中,如果出现上述信息,代表操作成功

7.格式化DRBD文件系统(只在主机上做

[root@dbm128 ~]# mkfs.ext3 /dev/drbd1

注意开机不要自启动DRBD服务,因为要通过heartbeat来启动DRBD

8.挂载DRBD分区

[root@dbm128 ~]# mkdir /data

[root@dbm128 ~]# mount /dev/drbd1 /data

[root@dbm128 ~]# df -h

Filesystem            Size  Used Avail Use% Mounted on

/dev/mapper/VolGroup00-LogVol00

                       92G  4.9G   82G   6% /

/dev/sda1              99M   35M   59M  38% /boot

tmpfs                 501M     0  501M   0% /dev/shm

/dev/drbd1             50G  180M   47G   1% /data

后面就把mysql数据目录放在/data

 

9.安装MYSQL数据库(四台都要安装)

安装所需要系统库相关库文件

[root@dbm128 ~]#yum -y install gcc gcc-c++ gcc-g77 autoconf automake zlib* fiex* libxml* ncurses-devel libmcrypt* libtool-ltdl-devel*  cmake

[root@dbm128 ~]# groupadd mysql

[root@dbm128 ~]# useradd -r -g mysql mysql

[root@dbm128 ~]# tar zxvf mysql-5.5.25.tar.gz

[root@dbm128 ~]# cd mysql-5.5.25

[root@dbm128 mysql-5.5.25]#  cmake -DCMAKE_INSTALL_PREFIX=/opt/mysql/ -DMYSQL_DATADIR=/opt/mysql/data -DMYSQL_UNIX_ADDR=/opt/mysql/data/mysqld.sock -DWITH_INNOBASE_STORAGE_ENGINE=1  -DENABLED_LOCAL_INFILE=1 -DMYSQL_TCP_PORT=3306 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_UNIX_ADDR=/opt/mysql/data/mysql.sock -DMYSQL_USER=mysql  -DWITH_DEBUG=0

[root@dbm128 mysql-5.5.25]# make && make install

赋予相关的权限

[root@dbm128 mysql]# chown -R mysql:mysql /opt/mysql

[root@dbm128 mysql]#  /opt/mysql/scripts/mysql_install_db --user=mysql --basedir=/opt/mysql   --datadir=/opt/mysql/data

mysql配置文件

[root@dbm128 mysql]# cp /opt/mysql/support-files/mysql.server /etc/init.d/mysql

[root@dbm128 mysql]# cp /opt/mysql/support-files/my-large.cnf /etc/my.cnf

[root@dbm128 mysql]# chmod +x /etc/init.d/mysql

dbm128上操作就可以,因为会同步过去所以dbm129不用操作

[root@dbm128 mysql]# cp -r /opt/mysql/data /data/mysql

[root@dbm128 mysql]# chown -R mysql:mysql /data/mysql

 

[root@dbm128 mysql]# vi /etc/init.d/mysql   (两台要修改dbm128,dbm129)

找到datadir=/opt/mysql/data 修改成datadir=/data/mysql

 

[root@dbm128 mysql]#/etc/init.d/mysql start 启动(dbm129不用去启动到时做heartbeat采用自动启动)

[root@dbm128 mysql]# /opt/mysql/bin/mysqladmin -u root password '123456' (三台都操作,dbm129不用操作已经同步过去了)

 

10.安装heartbeat(两台机器都安装)

要先安装DRBD,然后才能安装hearbeat

安装命令如下:

[root@dbm128 ~]# yum -y install heartbeat heartbeat-devel  heartbeat-stonith heartbeat-pils

接下来配置文件

[root@dbm128 ~]# cp /usr/share/doc/heartbeat-2.1.3/ha.cf /etc/ha.d/ha.cf

[root@dbm128 ~]# cp /usr/share/doc/heartbeat-2.1.3/haresources /etc/ha.d/haresources

[root@dbm128 ~]# cp /usr/share/doc/heartbeat-2.1.3/authkeys /etc/ha.d/authkeys

[root@dbm128 ~]# chmod 600 /etc/ha.d/authkeys

修改ha.cf

[root@dbm128 ~]# cat /etc/ha.d/ha.cf

debugfile /var/log/ha-debug

logfile       /var/log/ha-log

logfacility local0

 

keepalive 1

deadtime 15

warntime 5

initdead 30

 

ucast eth0 172.16.0.129 (修改对方的节点IP)

ping_group group 172.16.0.128 172.16.0.129

auto_failback on (从节点修改为off)

 

node dbm128

node dbm129

respawn hacluster /usr/lib64/heartbeat/ipfail

respawn hacluster /usr/lib64/heartbeat/dopd

 

apiauth ipfail gid=haclient uid=hacluster

apiauth dopd gid=haclient uid=hacluster

增加到后面cat  /etc/ha.d/authkeys

auth 1

1 sha1 HA_JACKBILLOW

增加到后面 cat /etc/ha.d/haresources

dbm128  drbddisk::r0 Filesystem::/dev/drbd1::/data mysql  IPaddr::172.16.0.233/24/eth0

上面dbm128主机,如果dbm129修改本主机名。

启动heartbeat服务:

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

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

配置heartbeat 在启动级别3中自动启动:

[root@dbm128 ~]chkconfig --level 35 heartbeat on

[root@dbm129 ~]chkconfig --level 35 heartbeat on

观察heartbeat 启动日志是否正常

[root@dbm128 ~]# tail -f /var/log/ha-log

IPaddr[4782]:  2014/07/21_16:48:15 INFO:  Success

heartbeat[4147]: 2014/07/21_16:48:57 info: dbm129 wants to go standby [foreign]

heartbeat[4147]: 2014/07/21_16:48:57 info: standby: acquire [foreign] resources from dbm129

heartbeat[5029]: 2014/07/21_16:48:57 info: acquire local HA resources (standby).

ResourceManager[5042]:        2014/07/21_16:48:57 info: Acquiring resource group: dbm128 drbddisk::r0 Filesystem::/dev/drbd1::/data mysql IPaddr::172.16.0.233/24/eth0

Filesystem[5083]:    2014/07/21_16:48:58 INFO:  Running OK

IPaddr[5158]:  2014/07/21_16:48:58 INFO:  Running OK

heartbeat[5029]: 2014/07/21_16:48:58 info: local HA resource acquisition completed (standby).

heartbeat[4147]: 2014/07/21_16:48:58 info: Standby resource acquisition done [foreign].

heartbeat[4147]: 2014/07/21_16:48:58 info: remote resource transition completed.

测试一下,看172.16.0.233VIP,在两台主机之间漂移。

11.主从复制的配置

修改dbs130,dbs131主的my.cnfserver-id=1,每个同步服务都必须设定一个唯一的编号。

Master(这里为dbm128机器)上增加一个用于复制的账号:

mysql>GRANT REPLICATION SLAVE ON *.*  TO 'repl'@'172.16.0.%'  IDENTIFIED BY '123456';

mysql>FLUSH REPLICATION;

mysql> show master status;

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

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

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

| mysql-bin.000018 |      107 |              |                  |

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

1 row in set (0.00 sec)

设置Slave 主机

修改my.cnfserver-id,内容如下:

dbs130主机

server-id=3

dbs130主机

server-id=4

开启MasterSlave的同步

Slave上执行如下命令

mysql>CHANGE MASTER TO MASTER_HOST='172.16.0.233',

         ->MASTER_USER='repl',

         ->MASTER_PASSWORD='123456',

         ->MASTER_LOG_FILE='mysql-bin.000018',

         ->MASTER_LOG_POS=107;

 之后执行

 mysql>slave start;

mysql> show slave status\G

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

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 172.16.0.233

                  Master_User: repl

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000018

          Read_Master_Log_Pos: 107

               Relay_Log_File: dbs130-relay-bin.000007

                Relay_Log_Pos: 253

        Relay_Master_Log_File: mysql-bin.000018

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Replicate_Do_DB:

          Replicate_Ignore_DB:

           Replicate_Do_Table:

       Replicate_Ignore_Table:

      Replicate_Wild_Do_Table:

  Replicate_Wild_Ignore_Table:

                   Last_Errno: 0

                   Last_Error:

                 Skip_Counter: 0

          Exec_Master_Log_Pos: 107

              Relay_Log_Space: 556

              Until_Condition: None

               Until_Log_File:

                Until_Log_Pos: 0

           Master_SSL_Allowed: No

           Master_SSL_CA_File:

           Master_SSL_CA_Path:

              Master_SSL_Cert:

            Master_SSL_Cipher:

               Master_SSL_Key:

        Seconds_Behind_Master: 0

Master_SSL_Verify_Server_Cert: No

                Last_IO_Errno: 0

                Last_IO_Error:

               Last_SQL_Errno: 0

               Last_SQL_Error:

  Replicate_Ignore_Server_Ids:

             Master_Server_Id: 1

1 row in set (0.00 sec)

在其他几台slave机器上也执行上面的命令

 


本文转自 jxzhfei  51CTO博客,原文链接:http://blog.51cto.com/jxzhfei/1440849


相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
MySQL索引策略与查询性能调优实战
在实际应用中,需要根据具体的业务需求和查询模式,综合运用索引策略和查询性能调优方法,不断地测试和优化,以提高MySQL数据库的查询性能。
368 66
Mysql高可用架构方案
本文阐述了Mysql高可用架构方案,介绍了 主从模式,MHA模式,MMM模式,MGR模式 方案的实现方式,没有哪个方案是完美的,开发人员在选择何种方案应用到项目中也没有标准答案,合适的才是最好的。
413 3
Mysql高可用架构方案
云数据库:从零到一,构建高可用MySQL集群
在互联网时代,数据成为企业核心资产,传统单机数据库难以满足高并发、高可用需求。云数据库通过弹性扩展、分布式架构等优势解决了这些问题,但也面临数据安全和性能优化挑战。本文介绍了如何从零开始构建高可用MySQL集群,涵盖选择云服务提供商、创建实例、配置高可用架构、数据备份恢复及性能优化等内容,并通过电商平台案例展示了具体应用。
大数据-88 Spark 集群 案例学习 Spark Scala 案例 SuperWordCount 计算结果数据写入MySQL
大数据-88 Spark 集群 案例学习 Spark Scala 案例 SuperWordCount 计算结果数据写入MySQL
74 3
大数据-140 - ClickHouse 集群 表引擎详解5 - MergeTree CollapsingMergeTree 与其他数据源 HDFS MySQL
大数据-140 - ClickHouse 集群 表引擎详解5 - MergeTree CollapsingMergeTree 与其他数据源 HDFS MySQL
87 0
数据库优化:MySQL索引策略与查询性能调优实战
【10月更文挑战第27天】本文深入探讨了MySQL的索引策略和查询性能调优技巧。通过介绍B-Tree索引、哈希索引和全文索引等不同类型,以及如何创建和维护索引,结合实战案例分析查询执行计划,帮助读者掌握提升查询性能的方法。定期优化索引和调整查询语句是提高数据库性能的关键。
696 1
MySQL与Redis协同作战:优化百万数据查询的实战经验
【10月更文挑战第13天】 在处理大规模数据集时,传统的关系型数据库如MySQL可能会遇到性能瓶颈。为了提升数据处理的效率,我们可以结合使用MySQL和Redis,利用两者的优势来优化数据查询。本文将分享一次实战经验,探讨如何通过MySQL与Redis的协同工作来优化百万级数据统计。
253 5
MySQL最左前缀优化原则:深入解析与实战应用
【10月更文挑战第12天】在数据库架构设计与优化中,索引的使用是提升查询性能的关键手段之一。其中,MySQL的最左前缀优化原则(Leftmost Prefix Principle)是复合索引(Composite Index)应用中的核心策略。作为资深架构师,深入理解并掌握这一原则,对于平衡数据库性能与维护成本至关重要。本文将详细解读最左前缀优化原则的功能特点、业务场景、优缺点、底层原理,并通过Java示例展示其实现方式。
226 1
数据库优化:MySQL索引策略与查询性能调优实战
【10月更文挑战第26天】数据库作为现代应用系统的核心组件,其性能优化至关重要。本文主要探讨MySQL的索引策略与查询性能调优。通过合理创建索引(如B-Tree、复合索引)和优化查询语句(如使用EXPLAIN、优化分页查询),可以显著提升数据库的响应速度和稳定性。实践中还需定期审查慢查询日志,持续优化性能。
492 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等