一、基本環境介紹及基本環境配置
節點1: node1.hulala.com 192.168.1.35 centos6.5_64 : node2.hulala.com 192.168.1.36 centos6.5_64 新增8G新硬碟
vvip # 192.168.1.39168.1.39
vip # #設定hosts解析:
vim /etc/sysconfig/network HOSTNAME=node1.hulala.com
<br/>
vim /etc/hosts 192.168.1.35 node1.hulala.com node1 192.168.1.36 node2.hulala.com node2
<br/>
ntpdate cn.pool.ntp.org
<br/>二:配置ssh互信
service iptables stop chkconfig iptables off cat /etc/sysconfig/selinux SELINUX=disabled
[root@node1~]#ssh-keygen -t rsa -b 1024 [root@node1~]#ssh-copy-id root@192.168.1.36 [root@node2~]#ssh-keygen -t rsa -b 1024 [root@node2~]#ssh-copy-id root@192.168.1.35
[root@node1~]#wget -c http://www.php.cn/ [root@node1~]#wget -c http://www.php.cn/ [root@node1~]#rpm -ivh *.rpm
設定檔<br/>:/etc/drbd.d/dbcluster. res
[root@node1~]#sha1sum /etc/drbd.conf 8a6c5f3c21b84c66049456d34b4c4980468bcfb3 /etc/drbd.conf
after-sb-0pri : “Split Brain”發生時且沒有資料變更,兩節點之間正常連接<br/>after -sb-1pri : 如果有資料變更,則放棄輔設備資料,並且從主設備同步<br/>rr-conflict: 假如前面的設定不能應用,並且drbd系統有角色衝突的話,系統自動斷開節點間連接<br/>META-DISK: Meta data保存在同一個磁碟(sdb1)<br/>ON [root@node1~]# vim /etc/drbd.d/dbcluster.res
resource dbcluster {
protocol C;
net {
cram-hmac-alg sha1;
shared-secret "8a6c5f3c21b84c66049456d34b4c4980468bcfb3";
after-sb-0pri discard-zero-changes;
after-sb-1pri discard-secondary;
after-sb-2pri disconnect;
rr-conflict disconnect;
}
device /dev/drbd0;
disk /dev/sdb1;
meta-disk internal;
on node1.hulala.com {
address 192.168.1.35:7789;
}
on node2.hulala.com {
address 192.168.1.36:7789;
}
}
[root@node1~]#scp /etc/drbd.d/dbcluster.res root@192.168.1.36:/etc/drbd.d/
[#root@node1~]fdisk /dev/sdb
– 首先確保drbd <br/>module
#已經載入查看是否載入:<br/>[root@node1~drbd]#drbdadm create-md dbcluster
# lsmod | grep drbd
<br/>
# modprobe drbd # lsmod | grep drbd drbd 317261 0 libcrc32c 1246 1 drbd
<br/>
[root@node1 drbd]# drbdadm up dbcluster [root@node2 drbd]# drbdadm up dbcluster
開始同步:<br/>
僅在主節點操作(這裡為node1)<br/>
[root@node2 drbd]# /etc/init.d/drbd status GIT-hash: 7ad5f850d711223713d6dcadc3dd48860321070c build by dag@Build64R6, 2016-10-23 08:16:10 m:res cs ro ds p mounted fstype 0:dbcluster Connected Secondary/Secondary Inconsistent/Inconsistent C
<br/>#
[root@node1 drbd]# drbdadm — –overwrite-data-of-peer primary dbcluster
cs (connection state): 網路連線狀態<br/>ro (roles): 節點的角色(本節點的角色首先顯示)
ds ( disk states):硬碟的狀態複製協定: A, B or C(本配置是C)<br/>看到drbd狀態為」cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate」即表示同步結束.<br/>也可以這樣查看drbd狀態:<br/><br/>
<br/>
[root@centos193 drbd]# drbd-overview 0:dbcluster/0 Connected Secondary/Primary UpToDate/UpToDate C r—–
#註:沒必要在輔節點(Node2)做同樣的操作,因為DRBD會處理原始磁碟資料的同步.<br/>另外,我們也不需要將這個DRBD系統掛載到任何一台機器(當然安裝<br/> MySQL
的時候需要暫時掛載來安裝MySQL),因為叢集管理軟體會處理.還有要確保複製的檔案系統僅僅掛載在Active的主伺服器上.<br/>四:mysql的安裝<br/>MySQL的安裝也可以參考博文《MySQL之-CentOS6.5 編譯安裝MySQL5.6.16》
1,在node1和node2節點安裝mysql:<br/>
[root@node1 drbd]# mkfs -t ext4 /dev/drbd0 mke2fs 1.41.12 (17-May-2010) Filesystem label= OS type: Linux Block size=4096 (log=2) ……. 180 days, whichever comes first. Use tune2fs -c or -i to override.
<br/>
yum install mysql* -y
<br/>
[root@node1~]# service mysql stop Shutting down MySQL. [ OK ]
<br/>
[root@host1 /]# mkdir -p /mysql/data [root@host1 /]# chown -R mysql:mysql /mysql
在[mysqld]下新增新的資料存放路徑<br/>
[root@node1 ~]# mount /dev/drbd0 /mysql/
6.將預設的資料路徑下的所有檔案和目錄cp到新的目錄下(node2不用操作)<br/> <br/>
datadir=/mysql/data
<br/>
[root@host1 mysql]#cd /var/lib/mysql [root@host1 mysql]#cp -R * /mysql/data/
<br/>
[root@host1 mysql]# chown -R mysql:mysql /mysql
<br/>
[root@host1 mysql]# mysql
<br/>#
[root@node1 ~]# umount /var/lib/mysql_drbd [root@node1 ~]# drbdadm secondary dbcluster
<br/>
[root@node2 ~]# drbdadm primary dbcluster [root@node2 ~]# mount /dev/drbd0 /mysql/
<br/>
[root@node1 ~]# scp node2:/etc/my.cnf /etc/my.cnf [root@node2 ~]# chown mysql /etc/my.cnf [root@node2 ~]# chmod 644 /etc/my.cnf
<br/>
[root@node2 ~]# mysql
<br/>安裝Pacemaker必須依賴:
[root@node2~]# umount /var/lib/mysql_drbd [root@node2~]# drbdadm secondary dbcluster [root@node2~]# drbd-overview 0:dbcluster/0 Connected Secondary/Secondary UpToDate/UpToDate C r—– [root@node2~]#
<br/>
[root@node1~]#yum -y install automake autoconf libtool-ltdl-devel pkgconfig python glib2-devel libxml2-devel libxslt-devel python-devel gcc-c++ bzip2-devel gnutls-devel pam-devel libqb-devel
<br/>
[root@node1~]yum -y install clusterlib-devel corosynclib-devel
<br/>
[root@node1~]yum -y install pacemaker
crmsh安装:<br/>
[root@node1~]wget http://www.php.cn/:/ha-clustering:/Stable/CentOS_CentOS-6/network:ha-clustering:Stable.repo [root@node1~]yum -y install crmsh
1,配置corosync<br/>Corosync Key<br/>– 生成节点间安全通信的key:<br/>
[root@node1~]# corosync-keygen
– 将authkey拷贝到node2节点(保持authkey的权限为400): [root@node~]# scp /etc/corosync/authkey node2:/etc/corosync/ 2,[root@node1~]# cp /etc/corosync/corosync.conf.example /etc/corosync/corosync.conf
编辑/etc/corosync/corosync.conf:<br/>
# Please read the corosync.conf.5 manual page compatibility: whitetank aisexec { user: root group: root } totem { version: 2 secauth: off threads: 0 interface { ringnumber: 0 bindnetaddr: 192.168.1.0 mcastaddr: 226.94.1.1 mcastport: 4000 ttl: 1 } } logging { fileline: off to_stderr: no to_logfile: yes to_syslog: yes logfile: /var/log/cluster/corosync.log debug: off timestamp: on logger_subsys { subsys: AMF debug: off } } amf { mode: disabled }
– 创建并编辑/etc/corosync/service.d/pcmk,添加”pacemaker”服务<br/>
[root@node1~]# cat /etc/corosync/service.d/pcmk service { # Load the Pacemaker Cluster Resource Manager name: pacemaker ver: 1 }
将上面两个配置文件拷贝到另一节点<br/>
[root@node1]# scp /etc/corosync/corosync.conf node2:/etc/corosync/corosync.conf [root@node1]# scp /etc/corosync/service.d/pcmk node2:/etc/corosync/service.d/pcmk
3,启动corosync和Pacemaker<br/> 分别在两个节点上启动corosync并检查.<br/>
[root@node1]# /etc/init.d/corosync start Starting Corosync Cluster Engine (corosync): [ OK ] [root@node1~]# corosync-cfgtool -s Printing ring status. Local node ID -1123964736 RING ID 0 id = 192.168.1.189 status = ring 0 active with no faults [root@node2]# /etc/init.d/corosync start Starting Corosync Cluster Engine (corosync): [ OK ]
– 在两节点上分别启动Pacemaker:<br/>
[root@node1~]# /etc/init.d/pacemaker start Starting Pacemaker Cluster Manager: [ OK ] [root@node2~]# /etc/init.d/pacemaker start Starting Pacemaker Cluster Manager:
<br/>配置资源及约束 <br/>配置默认属性<br/>查看已存在的配置:
[root@node1 ~]# crm configure property stonith-enabled=false [root@node1 ~]# crm_verify -L
禁止STONITH错误:<br/>
[root@node1 ~]# crm configure property stonith-enabled=false [root@node1 ~]# crm_verify -L
让集群忽略Quorum:<br/>
[root@node1~]# crm configure property no-quorum-policy=ignore
防止资源在恢复之后移动:<br/>
[root@node1~]# crm configure rsc_defaults resource-stickiness=100
设置操作的默认超时:<br/>
[root@node1~]# crm configure property default-action-timeout="180s"
设置默认的启动失败是否为致命的:
[root@node1~]# crm configure property start-failure-is-fatal="false"
配置DRBD资源<br/>– 配置之前先停止DRBD:<br/>
[root@node1~]# /etc/init.d/drbd stop [root@node2~]# /etc/init.d/drbd stop
– 配置DRBD资源:<br/>
[root@node1~]# crm configure crm(live)configure# primitive p_drbd_mysql ocf:linbit:drbd params drbd_resource="dbcluster" op monitor interval="15s" op start timeout="240s" op stop timeout="100s"
– 配置DRBD资源主从关系(定义只有一个Master节点):<br/>
crm(live)configure# ms ms_drbd_mysql p_drbd_mysql meta master-max="1" master-node-max="1" clone-max="2" clone-node-max="1" notify="true"
– 配置文件系统资源,定义挂载点(mount point):<br/>
crm(live)configure# primitive p_fs_mysql ocf:heartbeat:Filesystem params device="/dev/drbd0" directory="/var/lib/mysql_drbd/" fstype="ext4"
配置VIP资源<br/>
crm(live)configure# primitive p_ip_mysql ocf:heartbeat:IPaddr2 params ip="192.168.1.39" cidr_netmask="24" op monitor interval="30s"
配置MySQL资源<br/>
crm(live)configure# primitive p_mysql lsb:mysql op monitor interval="20s" timeout="30s" op start interval="0" timeout="180s" op stop interval="0" timeout="240s"
通过”组”确保DRBD,MySQL和VIP是在同一个节点(Master)并且确定资源的启动/停止顺序.<br/>
启动: p_fs_mysql–>p_ip_mysql->p_mysql 停止: p_mysql–>p_ip_mysql–>p_fs_mysql
crm(live)configure# group g_mysql p_fs_mysql p_ip_mysql p_mysql
组group_mysql永远只在Master节点:<br/>
crm(live)configure# colocation c_mysql_on_drbd inf: g_mysql ms_drbd_mysql:Master
MySQL的启动永远是在DRBD Master之后:<br/>
crm(live)configure# order o_drbd_before_mysql inf: ms_drbd_mysql:promote g_mysql:start
配置检查和提交<br/>
crm(live)configure# verify crm(live)configure# commit crm(live)configure# quit
查看集群状态和failover测试<br/>状态查看:<br/>
[root@node1 mysql]# crm_mon -1r
Failover测试:<br/>将Node1设置为Standby状态<br/>
[root@node1 ~]# crm node standby
过几分钟查看集群状态(若切换成功,则看到如下状态):<br/>
[root@node1 ~]# crm status
将Node1恢复online状态:<br/>
[root@node1 mysql]# crm node online [root@node1 mysql]# crm status
以上是MySQL之-MySQL高可用實作的詳細介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!