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

编译安装MySQL实现corosync+pacemaker+drbd+mysql高可用

程序员文章站 2022-03-28 10:34:14
...

编译安装MySQL实现corosync+pacemaker+drbd+mysql高可用 实验拓扑图: 实验节点:节点一: IP : 172.16.249.248 host: node1.stu11.com node1 节点二: IP : 172.16.249.122 host: node3.stu11.com node3 650) this.width=650;" src="http://www.68idc.cn/

编译安装MySQL实现corosync+pacemaker+drbd+mysql高可用


实验拓扑图:

实验节点:节点一:IP172.16.249.248 host: node1.stu11.com node1

节点二:IP172.16.249.122 host: node3.stu11.com node3



编译安装MySQL实现corosync+pacemaker+drbd+mysql高可用



节点二的配置前准备:

##########下载安装drbd,以及依赖的包########
[root@node3 ~ ]# rpm -ivh drbd84-utils-8.9.1-1.el6.elrepo.x86_64.rpm kmod-drbd84-8.4.5-504.1.el6.x86_64.rpm     
[root@node3 ~ ]# yum install corosync pacemaker -y ####安装corosync和pacemaker
[root@node3 .ssh ]#fdisk /dev/sda       ####创建用作drbd的磁盘,我这里使用分区模拟了
Command (m for help): n
Command action e  extended p  
primary partition (1-4) p
Partition number (1-4): 3
First cylinder (7859-15665, default 7859):
Using default value 7859
Last cylinder,
+cylinders or +size{K,M,G} (7859-15665, default 15665): +2G ##2G同步快一点,实际大小看生产需要
Command (m for help): w
[root@node3 .ssh ]#partx -a /dev/sda

节点一配置:

[root@node1 ~ ]# rpm -ivh drbd84-utils-8.9.1-1.el6.elrepo.x86_64.rpm kmod-drbd84-8.4.5-504.1.e
[root@node1 ~ ]# yum install corosync pacemaker -y
[root@node1 ~ ]#fdisk /dev/sda
Command (m forhelp): n
Command action e  extended  p  
primary partition (1-4) p
Partition number(1-4): 3
First cylinder(7859-15665, default 7859):
Using default value7859
Last cylinder,
+cylinders or +size{K,M,G} (7859-15665, default 15665): +2G 
Command (m forhelp): w
[root@node1 ~ ]#partx -a /dev/sda

##############################编辑corosync配置文件##############################

[root@node1 corosync]# cp corosync.conf.example corosync.conf
[root@node1 corosync]# vim corosync.conf    ##仅显示的是在使用的部分
  2 compatibility: whitetank
  3
  4 totem {
  5     version: 2
 10     secauth: on
 12     threads: 0
 17     interface {
 19         ringnumber: 0
 25         bindnetaddr: 172.16.0.0     
 39         mcastaddr: 239.22.11.123  ##指定组播地址
 44         mcastport: 5405
 49         ttl: 1
 50     }
 51 }
 53 logging {
 57     fileline: off
 60     to_stderr: no
 63     to_logfile: yes
 64    logfile: /var/log/cluster/corosync.log
 66     to_syslog: yes
 68     debug: off
 72     timestamp: on
 73    logger_subsys {
 74        subsys: AMF
 75        debug: off
 76     }
 77 }
 78 service {         ####将pacemaker以模块方式和corosync结合工作
 79    ver: 0
 80    name: pacemaker
 81 }

##########################################生成密钥########################################

[root@node1 yum.repos.d ]# corosync-keygen
Writing corosync key
to /etc/corosync/authkey.
[root@node1 drbd.d]# ls
global_common.conf  global_common.conf.bak  mydisk.res
[root@node1 corosync]# ll
total 24
-r-------- 1 root
root  128 Jan 17 09:38 authkey    ###保证权限是600或400
-rw-r--r-- 1 root
root 2701 Jan 17 09:38 corosync.conf
[root@node1 corosync]# scp corosync.conf authkey node3:/etc/corosync/
corosync.conf                   100% 2701     2.6KB/s   00:00   
authkey                         100%  128     0.1KB/s  00:00 
[root@node3 corosync ]# ll    //查看各节点密钥文件权限都是600,默认是不需要修改的。
total 24
-r-------- 1 root
root  128 Jan 17 09:38 authkey

############################配置各节点DRBD配置文件配置##############################################

[root@node1 drbd.d]# vim global_common.conf
  5 global {
  6    usage-count no;
  7     #minor-count dialog-refresh disable-ip-verification
  8 }
 10 common {
 11    handlers {
 17         pri-on-incon-degr "/usr/lib/drbd/notify-pri-on-incon-degr.sh;/usr/lib/drbd/notify-emergency-reboot.sh; echo     b > /proc/sysrq-trigger ; reboot-f";
 18         pri-lost-after-sb "/usr/lib/drbd/notify-pri-lost-after-sb.sh;/usr/lib/drbd/notify-emergency-reboot.sh; echo     b > /proc/sysrq-trigger ; reboot-f";
 19         local-io-error "/usr/lib/drbd/notify-io-error.sh;/usr/lib/drbd/notify-emergency-shutdown.sh; echo o > /proc    /sysrq-trigger ; halt -f";
 25     }
 26
 27    startup {
 28        # wfc-timeout degr-wfc-timeout outdated-wfc-timeout wait-after-sb
 29     }
 30
 31    options {
 32        # cpu-mask on-no-data-accessible
 33     }
 35     disk {
 36          on-io-error detach; ###磁盘故障直接拆除
 41     }
 42
 43    net {
 44         protocol C; ###最稳妥的协议
 45         cram-hmac-alg "sha1";
 46         shared-secret "test";
 54     }
 56 syncer {
 57    rate 100M;
 58
 59 }
 60 }    //一定要注意最后一个引号,是全局段的。


##################################配值DRBD资源文件##################################################

 [root@node1 drbd.d ]#vim mydisk.res 
  1 resource mydisk {
  2    on node1.stu11.com {
  3        device /dev/drbd0;
  4        disk /dev/sda3;
  5        address 172.16.249.248:7789;
  6        meta-disk internal;
  7        }
  8        on node3.stu11.com {
  9        device /dev/drbd0;
 10        disk /dev/sda3;
 11        address 172.16.249.122:7789;
 12        meta-disk internal;
 13        }
 14 }

#################################同步两个节点的配置文件###########################################

[root@node1 drbd.d]# scp global_common.conf mydisk.res node3:/etc/drbd.d/
global_common.conf                    100% 2101     2.1KB/s   00:00   
mydisk.res                            100%  257     0.3KB/s   00:00

##################################各节点资源初始化#################################################

[root@node1 corosync]# drbdadm create-md mydisk
initializing
activity log
NOT initializing
bitmap
Writing meta data...
New drbd meta data
block successfully created.
[root@node3 drbd.d]# drbdadm create-md mydisk
initializing
activity log
NOT initializing
bitmap
Writing meta data...
New drbd meta data
block successfully created.
[root@node3 corosync]# service drbd start
Starting DRBD
resources:

##########################################启动各节点DRBD###########################################

[root@node1 corosync]# service drbd start
Starting DRBD
resources:

编译安装MySQL实现corosync+pacemaker+drbd+mysql高可用

[root@node1 corosync ]# drbd-overview //查看节点状态

0:mydisk/0 Connected Secondary/Secondary Inconsistent/Inconsistent

##########################提升一个节点为主节点:并且查看数据在同步######################################

[root@node1 corosync]# drbdadm primary --force mydisk
[root@node1 corosync]# drbd-overview
 0:mydisk/0 
SyncSource Primary/Secondary UpToDate/Inconsistent
[>...................]
sync'ed:  7.1% (1959028/2103412)K

编译安装MySQL实现corosync+pacemaker+drbd+mysql高可用

##################################对DRBD格式化####################################################

[root@node1 corosync ]# mke2fs -t ext4 /dev/drbd0 //对主节点格式化

编译安装MySQL实现corosync+pacemaker+drbd+mysql高可用

#####################################挂载创建测试文件############################################

[root@node1 corosync ]# mount /dev/drbd0 /mnt/  
[root@node1 corosync]# cd /mnt/
[root@node1 mnt ]#ls
lost+found
[root@node1 mnt ]#touch test.txt
[root@node1 mnt ]#ls
lost+found  test.txt

##################### ##降为从节点把另一个节点升为主节点挂载查看测试文件########################

[root@node1 ~ ]# umount /mnt/     
[root@node1 ~ ]#drbd
drbdadm        drbdmeta       drbd-overview  drbdsetup     
[root@node1 ~ ]#drbdadm secondary mydisk
[root@node1 ~ ]#drbd-overview
 0:mydisk/0 
Connected Secondary/Secondary UpToDate/UpToDate

#####################节点二升级主节点,查看测试文件#################################

编译安装MySQL实现corosync+pacemaker+drbd+mysql高可用

至此:两个节点的DRBD已经建成同步完成。开始布置两个节点的mysql

######################准备各节点的mysql############################################# 准备编译环境及安装:

[root@node3 /]# yum groupinstall "Development Tools" -y
[root@node3 /]# yum install "Server Platform Developmnet"
[root@node3 ~ ]# yum install cmake -y     //mariadb是使用cmake的编译安装的
[root@node3 ~ ]# tar xf mariadb-10.0.13.tar.gz -C /usr/local/
[root@node3 local ]#groupadd -g 306 mysql
[root@node3 local ]#useradd -u 306 -g 306 mysql
[root@node3 local ]#id mysql
uid=306(mysql) gid=306(mysql) groups=306(mysql)
[root@node3 local ]#mkdir /mydata
[root@node3 ~ ]#umount /mnt/     
[root@node3 local ]#chown -R mysql:mysql /mydata
[root@node3 ~ ]#mount /dev/drbd0 /mydata     
[root@node3 mariadb-10.0.13 ]#cmake .
            -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \##指定安装路径
            -DMYSQL_DATADIR=/mydata \                ##指定库位置          
            -DWITH_INNOBASE_STORAGE_ENGINE=1 \       ##指定存储引擎          
            -DWITH_ARCHIVE_STORAGE_ENGINE=1\                      
            -DWITH_BLACKHOLE_STORAGE_ENGINE=1\          
            -DWITH_READLINE=1\                                
            -DWITH_SSL=system\                      ##定制SSL功能          
            -DWITH_ZLIB=system\                     ##定制ZLIB功能          
            -DWITH_LIBWRAP=0\           
            -DMYSQL_UNIX_ADDR=/tmp/mysql.sock\      ##sock位置          
            -DDEFAULT_CHARSET=utf8 \                 ##默认字符集         
            -DDEFAULT_COLLATION=utf8_general_ci  
#make 
#make之后
Linking CXX
executable my_safe_process
[100%] Built target
my_safe_process
[root@node3 mysql ]#ls /mydata/
aria_log.00000001  aria_log_control  ibdata1 
ib_logfile0  ib_logfile1  mysql performance_schema  test
[root@node3support-files ]# cp my-large.cnf /etc/mysql/my.cnf
[root@node3support-files ]# cd /etc/mysql
[root@node3 mysql ]#ls
my.cnf
[root@node3 mysql ]#vim my.cnf  #####在mysqld添加如下
innodb_file_per_table = on
datadir = /mydata
[root@node3 mysql ]#
service mysqld start
Starting
MySQL...                                         
[  OK  ]
[root@node3 mysql ]# mysql   //登陆已经正常了
Welcome to the MySQL
monitor.  Commands end with ; or \g.
Your MySQL
connection id is 4
Server version: 5.5.5-10.0.13-MariaDB-log Source
distribution
Copyright (c) 2000,
2013, Oracle and/or its affiliates. All rights reserved.

编译安装MySQL实现corosync+pacemaker+drbd+mysql高可用

注意:同样的步骤部署第二个节点mysql但是,不要初始化。因为共享磁盘已经初始化过了。

########################下面,就可以使用crmsh配置资源了。###########################

下面,就可以使用crmsh配置资源了。

首先:保证我们两个节点的服务不会开机自动启动

编译安装MySQL实现corosync+pacemaker+drbd+mysql高可用

编译安装MySQL实现corosync+pacemaker+drbd+mysql高可用

##################启动两个节点的corosync##########################################################

[root@node3 ~ ]#service corosync start
Starting Corosync
Cluster Engine (corosync):              [  OK  ]
[root@node3 ~ ]# service mysqld stop    ##停掉我们使用的所有资源
Stopping
mysqld:                                          [  OK  ]
[root@node3 ~ ]#umount /mydata/
[root@node3 ~ ]#drbd-overview
 0:mydisk/0 Connected Primary/Secondary UpToDate/UpToDate
[root@node3 ~ ]#drbdadm secondary mydisk
[root@node3 ~ ]#drbd-overview
 0:mydisk/0 Connected Secondary/Secondary UpToDate/UpToDate
[root@node3 ~ ]#service drbd stop
Stopping all DRBD
resources: .

编译安装MySQL实现corosync+pacemaker+drbd+mysql高可用

安装crmsh ##安装并且定义

[root@node3 ~ ]# yum install crmsh-2.1-1.6.x86_64.rpm
[root@node3 ~ ]# crm
crm(live)# configure
#####################第一步:定义前提属性#############################
crm(live)configure# property stonith-enabled=false            //禁用stonish
crm(live)configure# property no-quorum-policy=ignore    //定义无票策略
#####################第二步:定义DRBD主资源###########################
crm(live)configure#
primitive mydrbd ocf:linbit:drbd params drbd_resource="mydisk" op
monitor role=Master timeout=20 interval=10 op monitor role=Slave timeout=20
interval=10 op start timeout=240 op stop timeout=100
####################第三步:定义DRBD主从资源##########################
crm(live)configure#ms ms_mydrbd mydrbd meta master-max="1" master-node-max="1"
clone-max="2" clone-node-max="1" notify="true"
####################第四步:定义文件系统资源##########################
crm(live)configure#primitive myfs ocf:heartbeat:Filesystem params device=/dev/drbd0
directory=/mydata fstype="ext4" op monitor timeout=40 interval=20 op start timeout=60 op stop timeout=60
####################第五步:定义协同约束##############################
crm(live)configure#colocation mydrbd-master_with_myfs inf: ms_mydrbd:Master myfs
crm(live)configure#
####################第六步:定义顺序约束##############################
crm(live)configure#order ms_mydrbd-master_before_myfs inf: ms_mydrbd:promote myfs:start
#################第七步:定义流动IP###################################
crm(live)configure#primitive myvip ocf:heartbeat:IPaddr params ip=172.16.11.11 op monitor
timeout=20 interval=10 op start timeout=20 op stop timeout=20
###################第八步:定义mysql服务资源############################
crm(live)configure#primitive myservice lsb:mysqld op monitor timeout=20 interval=20
crm(live)configure#
###################第九步:定义他们为一个组资源########################
crm(live)configure#group mygroup myvip myfs ms_mydrbd:Master myservice
####################第十步:定义他们的协同约束#########################
crm(live)configure#order myfs_before_myservice inf: myfs:start myservice:start
crm(live)configure# commit     ##提交
interval in monitor_Master must be unique
crm(live)configure#show     ##查看我们定义的所有资源

编译安装MySQL实现corosync+pacemaker+drbd+mysql高可用

切换测试结果:

编译安装MySQL实现corosync+pacemaker+drbd+mysql高可用

编译安装MySQL实现corosync+pacemaker+drbd+mysql高可用


至此高可用全部完成!