大纲

一、环境准备

二、拓扑准备

三、安装与配置heartbrat

四、配置NFS服务

五、安装与配置mysql

六、crm配置资源


一、环境准备

1.操作系统

  • CentOS 5.5 X86_64 位系统

2.软件环境

  • heartbeat-2.1.4-11.el5

  • heartbeat-gui-2.1.4-11.el5

  • mysql-5.5.33(注:这里用的mysql版本是通用二进制包,直接解压就能用的,若有博友对mysql安装有问题,请参考:http://freeloda.blog.51cto.com/2033581/1252067)

3.高可用集群使用条件

(1).节点名称必须跟 uname -n 命令的执行结果一致

node1:

[root@node1 src]# uname -n 
node1.test.com
[root@node1 src]# vim /etc/hosts
# Do not remove the following line, or various programs 
# that require network functionality will fail.  
127.0.0.1               localhost.localdomain localhost  
::1             localhost6.localdomain6 localhost6  
192.168.18.201  node1.test.com  node1  
192.168.18.202  node2.test.com  node2

node2:

[root@node2 ~]# uname -n 
node2.test.com  
[root@node2 ~]# vim /etc/hosts
# Do not remove the following line, or various programs 
# that require network functionality will fail.  
127.0.0.1               localhost.localdomain localhost  
::1             localhost6.localdomain6 localhost6  
192.168.18.201  node1.test.com  node1  
192.168.18.202  node2.test.com  node2

(2).各节点之间ssh互信

node1: 
ssh-keygen  -t rsa -f ~/.ssh/id_rsa  -P ''   
ssh-copy-id -i .ssh/id_rsa.pub root@node2.test.com
node2:
ssh-keygen  -t rsa -f ~/.ssh/id_rsa  -P ''  
ssh-copy-id -i .ssh/id_rsa.pub root@node2.test.com

(3).各节点之间时间同步

node1:
[root@node1 ~]# ntpdate 202.120.2.101
node2:
[root@node2 ~]# ntpdate 202.120.2.101

4.增加EPEL yum源

node1,node2:

[root@node src]# wget http://download.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
[root@node src]# rpm -ivh epel-release-5-4.noarch.rpm
warning: epel-release-5-4.noarch.rpm: Header V3 DSA signature: NOKEY, key ID 217521f6
Preparing...                ########################################### [100%]
1:epel-release           ########################################### [100%]
[root@node src]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
[root@node src]# yum list

5.关闭防火墙与SELinux

node1,node2:
[root@node ~]# service iptables stop
[root@node ~]# vim /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#       enforcing - SELinux security policy is enforced.
#       permissive - SELinux prints warnings instead of enforcing.
#       disabled - SELinux is fully disabled.
SELINUX=disabled
# SELINUXTYPE= type of policy in use. Possible values are:
#       targeted - Only targeted network daemons are protected.
#       strict - Full SELinux protection.
SELINUXTYPE=targeted

二、拓扑准备


HA MySQL

说明:拓扑与上几篇博客一致,这里就不详细说明了。

三、安装与配置heartbrat

1.heartbeat v2安装与配置

(1).安装heartbrat

node1:

[root@node ~]# yum -y install heartbeat*

node2:

[root@node ~]# yum -y install heartbeat*

(2).配置heartbeat

说明:默认安装好heartbeat没有配置文件,但是样例文件

root@node1 ~]# cd /usr/share/doc/heartbeat-2.1.4/ 
[root@node1 heartbeat-2.1.4]# ls  
apphbd.cf  COPYING           faqntips.txt         HardwareGuide.html  hb_report.txt       README             rsync.txt  
authkeys   COPYING.LGPL      GettingStarted.html  HardwareGuide.txt   heartbeat_api.html  Requirements.html  startstop  
AUTHORS    DirectoryMap.txt  GettingStarted.txt   haresources         heartbeat_api.txt   Requirements.txt  
ChangeLog  faqntips.html     ha.cf                hb_report.html      logd.cf             rsync.html

注:这里我们只需要两个文件,ha.cf 与 authkeys

[root@node1 ha.d]# dd if=/dev/random bs=512count=1| openssl md5 #生成密钥随机数
0+1records in
0+1records out
128bytes (128B) copied, 0.000214seconds, 598kB/s
a4d20b0dd3d5e35e0f87ce4266d1dd64
[root@node1 ~]# vim /etc/ha.d/authkeys
auth 1 
1 md5 a4d20b0dd3d5e35e0f87ce4266d1dd64
[root@node1 ha.d]# chmod 600 authkeys  #修改密钥文件的权限为600
[root@node1 ha.d]# vim ha.cf
主要修改两处(其它都可以默认):
(1).修改心跳信息的传播方式(这里是组播)
mcast eth0 225.100.100.100 694 1 0
(2).配置集群中的节点数
node    node1.test.com
node    node2.test.com
(3).启用crm
crm on

2.复制以上两个配置文件到node2上

[root@node1 ha.d]# scp authkeys ha.cf  node2:/etc/ha.d/

3.启动节点

[root@node1 ha.d]# ssh node2 "service heartbeat start"
Strting High-Availability services:
[确定]
logd is already stopped
[root@node1 ha.d]# service heartbeat start      
Starting High-Availability services:
2013/08/07_17:19:22 INFO:  Resource is stopped
[确定]

4.查看一下端口

node1:

[root@node1 ~]# netstat -ntulp 
Active Internet connections (only servers)  
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name 
tcp        0      0 0.0.0.0:616                 0.0.0.0:*                   LISTEN      2554/rpc.statd    
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      2515/portmap      
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      2850/cupsd        
tcp        0      0 0.0.0.0:5560                0.0.0.0:*                   LISTEN      3170/mgmtd        
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      2940/sendmail: acce   
tcp        0      0 127.0.0.1:6010              0.0.0.0:*                   LISTEN      17808/0           
tcp        0      0 :::22                       :::*                        LISTEN      2837/sshd         
tcp        0      0 ::1:6010                    :::*                        LISTEN      17808/0           
udp        0      0 0.0.0.0:50185               0.0.0.0:*                               2925/heartbeat: wri   
udp        0      0 0.0.0.0:47376               0.0.0.0:*                               3069/avahi-daemon:
udp        0      0 0.0.0.0:694                 0.0.0.0:*                               2925/heartbeat: wri   
udp        0      0 0.0.0.0:610                 0.0.0.0:*                               2554/rpc.statd    
udp        0      0 0.0.0.0:613                 0.0.0.0:*                               2554/rpc.statd    
udp        0      0 0.0.0.0:5353                0.0.0.0:*                               3069/avahi-daemon:
udp        0      0 0.0.0.0:111                 0.0.0.0:*                               2515/portmap      
udp        0      0 0.0.0.0:631                 0.0.0.0:*                               2850/cupsd        
udp        0      0 :::5353                     :::*                                    3069/avahi-daemon:
udp        0      0 :::46332                    :::*                                    3069/avahi-daemon:

node2:

[root@node1 ~]# netstat -ntulp 
Active Internet connections (only servers)  
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name 
tcp        0      0 0.0.0.0:616                 0.0.0.0:*                   LISTEN      2554/rpc.statd    
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      2515/portmap      
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      2850/cupsd        
tcp        0      0 0.0.0.0:5560                0.0.0.0:*                   LISTEN      3170/mgmtd        
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      2940/sendmail: acce   
tcp        0      0 127.0.0.1:6010              0.0.0.0:*                   LISTEN      17808/0           
tcp        0      0 :::22                       :::*                        LISTEN      2837/sshd         
tcp        0      0 ::1:6010                    :::*                        LISTEN      17808/0           
udp        0      0 0.0.0.0:50185               0.0.0.0:*                               2925/heartbeat: wri   
udp        0      0 0.0.0.0:47376               0.0.0.0:*                               3069/avahi-daemon:
udp        0      0 0.0.0.0:694                 0.0.0.0:*                               2925/heartbeat: wri   
udp        0      0 0.0.0.0:610                 0.0.0.0:*                               2554/rpc.statd    
udp        0      0 0.0.0.0:613                 0.0.0.0:*                               2554/rpc.statd    
udp        0      0 0.0.0.0:5353                0.0.0.0:*                               3069/avahi-daemon:
udp        0      0 0.0.0.0:111                 0.0.0.0:*                               2515/portmap      
udp        0      0 0.0.0.0:631                 0.0.0.0:*                               2850/cupsd        
udp        0      0 :::5353                     :::*                                    3069/avahi-daemon:
udp        0      0 :::46332                    :::*                                    3069/avahi-daemon:

注:上面端口号可以看出,heartbeat已经正常启动。

5.查看一下集群状态

m1

注:两个节点都在线,资源暂时还没有配置。

6.测试一下hb_gui图形配置界面

[root@node1 ~]# hb_gui & 
[1] 20377

m2

四、配置NFS服务

1.创建LVM逻辑卷(用来存放mysql数据文件)

[root@nfs ~]# pvcreate /dev/sdb #创建物理卷 
[root@nfs ~]# vgcreate myvg /dev/sdb #创建卷组  
[root@nfs ~]# lvcreate -L 10G -n mydata myvg #创建逻辑卷  
[root@nfs ~]# mke2fs -j /dev/myvg/mydata #格式化逻辑卷  
[root@nfs ~]# lvs #查看逻辑卷  
[root@nfs ~]# mkdir /mydata #创建挂载目录  
[root@nfs ~]# mount /dev/myvg/mydata /mydata/ #挂载  
[root@nfs ~]# cd /mydata/ #进入挂载目录  
[root@nfs ~]# mkdir data #创建数据目录

2.创建mysql的用户与mysql组

node1,node2,nfs:(三个节点都要创建相同的用户与组)

(1).创建mysql组  

[root@nfs ~]# groupadd -g 3306 mysql

(2).创建mysql用户  

[root@nfs ~]# useradd -u 3306 -g mysql -s /sbin/nologin -M mysql

(3).查看

[root@nfs ~]# id mysql 
uid=3306(mysql) gid=3306(mysql) groups=3306(mysql)

说明:在node1与node2也要创建相同的用户和组

3.修改数据目录的用户与组

[root@nfs ~]# chow -R mysql.mysql /mydata/data/
[root@nfs ~]# ll /mydata/ 
总计 12  
drwxr-xr-x 6 mysql mysql 4096 08-12 13:40 data

4.修改nfs配置文件

[root@nfs ~]# vim /etc/exports
/mydata              192.168.18.0/24(no_root_squash,rw,async)

5.重新输出一下nfs

[root@nfs ~]# exportfs -arv 
exporting 192.168.18.0/24:/mydata

6.查看输出的nfs共享存储

[root@nfs ~]# showmount -e 192.168.18.208 
Export list for 192.168.18.208:  
/mydata 192.168.18.0/24

7.测试挂载

node1:

[root@node1 ~]# mkdir /mydata
[root@node1 ~]# mount -t nfs 192.168.18.208:/mydata/ /mydata/
[root@node1 ~]# ll /mydata/ 
total 12  
drwxr-xr-x 6 mysql mysql 4096 Aug 12 13:40 data

node2:

[root@node2~]# mkdir /mydata
[root@node2 ~]# mount -t nfs 192.168.18.208:/mydata/ /mydata/
[root@node2 ~]# ll /mydata/ 
total 12  
drwxr-xr-x 6 mysql mysql 4096 Aug 12 13:50 data

五、安装与配置mysql

node1:

1.解压并链接mysql

[root@node1 ~]# tar -zxvf mysql-5.5.33-linux2.6-x86_64.tar.gz -C /usr/local/ #直接解压到/usr/local目录下
[root@node1 ~]# ln -sv mysql-5.5.33-linux2.6-x86_64 mysql #设置一个软链接
[root@node1 ~]# cd /usr/local/mysql 
[root@node1 mysql]# ll  
total 204  
drwxr-xr-x  2 root mysql   4096 Aug 11 14:45 bin  
-rw-r--r--  1 root mysql  17987 Jul 15 20:01 COPYING  
drwxr-xr-x  3 root mysql   4096 Aug 12 13:11 data  
drwxr-xr-x  2 root mysql   4096 Aug 11 14:45 docs  
drwxr-xr-x  3 root mysql   4096 Aug 11 14:45 include  
-rw-r--r--  1 root mysql 134493 Jul 15 20:01 INSTALL-BINARY  
drwxr-xr-x  3 root mysql   4096 Aug 11 15:27 lib  
drwxr-xr-x  4 root mysql   4096 Aug 11 14:45 man  
drwxr-xr-x 10 root mysql   4096 Aug 11 14:45 mysql-test  
-rw-r--r--  1 root mysql   2496 Jul 15 20:01 README  
drwxr-xr-x  2 root mysql   4096 Aug 11 14:45 scripts  
drwxr-xr-x 27 root mysql   4096 Aug 11 14:45 share  
drwxr-xr-x  4 root mysql   4096 Aug 11 14:45 sql-bench  
drwxr-xr-x  3 root mysql   4096 Aug 11 14:45 support-files

2.初始化mysql

[root@node1 ~]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/mydata/data/ --user=mysql

3.提供mysql配置文件

[root@node1 ~]# cp /usr/local/mysql/support-files/my-large.cnf  /etc/my.cnf
[root@node1 ~]# vim /etc/my.cnf
datadir = /mydata/my.cnf #指定数据目录 
innodb_file_per_table = 1 #innodb表单独表空间

4.提供mysql启动脚本

[root@node1 ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@node1 ~]# chmod +x /etc/init.d/mysqld

5.启动mysql

[root@node1 mysql]# service mysqld start 
Starting MySQL...                                          [  OK  ]

6.查看数据目录

[root@node1 ~]# mount 
/dev/sda2 on / type ext3 (rw)  
proc on /proc type proc (rw)  
sysfs on /sys type sysfs (rw)  
devpts on /dev/pts type devpts (rw,gid=5,mode=620)  
/dev/sda3 on /data type ext3 (rw)  
/dev/sda1 on /boot type ext3 (rw)  
tmpfs on /dev/shm type tmpfs (rw)  
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)  
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)  
192.168.18.208:/mydata/ on /mydata type nfs (rw,addr=192.168.18.208)  
[root@node1 ~]# cd /mydata/data/  
[root@node1 data]# ll  
total 28784  
-rw-rw---- 1 mysql mysql 18874368 Aug 12 13:40 ibdata1  
-rw-rw---- 1 mysql mysql  5242880 Aug 12 14:27 ib_logfile0  
-rw-rw---- 1 mysql mysql  5242880 Aug 12 08:05 ib_logfile1  
-rw-r--r-- 1 root  root      4721 Aug 12 07:55 my.cnf  
drwx------ 2 mysql mysql     4096 Aug 12 08:07 mydb  
drwx------ 2 mysql root      4096 Aug 12 07:39 mysql  
-rw-rw---- 1 mysql mysql      126 Aug 12 13:40 mysql-bin.000001  
-rw-rw---- 1 mysql mysql       19 Aug 12 13:18 mysql-bin.index  
-rw-r----- 1 mysql root     18748 Aug 12 14:28 node1.test.com.err  
-rw-rw---- 1 mysql mysql        6 Aug 12 14:27 node1.test.com.pid  
drwx------ 2 mysql mysql     4096 Aug 12 07:39 performance_schema  
drwx------ 2 mysql root      4096 Aug 12 07:39 test

7.登录测试一下

[root@node1 data]# mysql 
Welcome to the MySQL monitor.  Commands end with ; or \g.  
Your MySQL connection id is 1  
Server version: 5.5.33-log MySQL Community Server (GPL)
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 |  
| mydb               |  
| mysql              |  
| performance_schema |  
| test               |  
+--------------------+  
5 rows in set (0.02 sec)

注:node1节点mysql配置完成,下面我们来配置node2

node2:

1.卸载数据文件目录

[root@node1 data]# service mysqld stop 
Shutting down MySQL.                                       [  OK  ]  
[root@node1 data]# cd  
[root@node1 ~]# umount /mydata/  
[root@node1 ~]# mount  
/dev/sda2 on / type ext3 (rw)  
proc on /proc type proc (rw)  
sysfs on /sys type sysfs (rw)  
devpts on /dev/pts type devpts (rw,gid=5,mode=620)  
/dev/sda3 on /data type ext3 (rw)  
/dev/sda1 on /boot type ext3 (rw)  
tmpfs on /dev/shm type tmpfs (rw)  
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)  
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)

2.挂载数据目录到node2上

[root@node2 ~]# mount  
/dev/sda2 on / type ext3 (rw)  
proc on /proc type proc (rw)  
sysfs on /sys type sysfs (rw)  
devpts on /dev/pts type devpts (rw,gid=5,mode=620)  
/dev/sda3 on /data type ext3 (rw)  
/dev/sda1 on /boot type ext3 (rw)  
tmpfs on /dev/shm type tmpfs (rw)  
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)  
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)  
[root@node2 ~]# mount -t nfs 192.168.18.208:/mydata/ /mydata/  
[root@node2 ~]# mount  
/dev/sda2 on / type ext3 (rw)  
proc on /proc type proc (rw)  
sysfs on /sys type sysfs (rw)  
devpts on /dev/pts type devpts (rw,gid=5,mode=620)  
/dev/sda3 on /data type ext3 (rw)  
/dev/sda1 on /boot type ext3 (rw)  
tmpfs on /dev/shm type tmpfs (rw)  
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)  
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)  
192.168.18.208:/mydata/ on /mydata type nfs (rw,addr=192.168.18.208)  
[root@node2 ~]# cd /mydata/data/  
[root@node2 data]# ll  
total 28780  
-rw-rw---- 1 mysql mysql 18874368 Aug 12 14:30 ibdata1  
-rw-rw---- 1 mysql mysql  5242880 Aug 12 14:30 ib_logfile0  
-rw-rw---- 1 mysql mysql  5242880 Aug 12 08:05 ib_logfile1  
-rw-r--r-- 1 root  root      4721 Aug 12 07:55 my.cnf  
drwx------ 2 mysql mysql     4096 Aug 12 08:07 mydb  
drwx------ 2 mysql root      4096 Aug 12 07:39 mysql  
-rw-rw---- 1 mysql mysql      126 Aug 12 13:40 mysql-bin.000001  
-rw-rw---- 1 mysql mysql       19 Aug 12 13:18 mysql-bin.index  
-rw-r----- 1 mysql root     19162 Aug 12 14:30 node1.test.com.err  
-rw-r----- 1 mysql root      4442 Aug 12 13:40 node2.test.com.err  
drwx------ 2 mysql mysql     4096 Aug 12 07:39 performance_schema  
drwx------ 2 mysql root      4096 Aug 12 07:39 test

3.将配置文件与启动脚本复制到node2上

[root@node1 ~]# scp /etc/my.cnf node2:/etc/ 
my.cnf                                                                                 100% 4721     4.6KB/s   00:00  
[root@node1 ~]# scp /etc/init.d/mysqld node2:/etc/init.d/  
mysqld                                                                                 100%   11KB  10.6KB/s   00:00

4.启动mysql

[root@node2 data]# service mysqld start 
Starting MySQL..                                           [  OK  ]  
[root@node2 data]# ll  
total 28784  
-rw-rw---- 1 mysql mysql 18874368 Aug 12 14:30 ibdata1  
-rw-rw---- 1 mysql mysql  5242880 Aug 12 14:33 ib_logfile0  
-rw-rw---- 1 mysql mysql  5242880 Aug 12 08:05 ib_logfile1  
-rw-r--r-- 1 root  root      4721 Aug 12 07:55 my.cnf  
drwx------ 2 mysql mysql     4096 Aug 12 08:07 mydb  
drwx------ 2 mysql root      4096 Aug 12 07:39 mysql  
-rw-rw---- 1 mysql mysql      126 Aug 12 13:40 mysql-bin.000001  
-rw-rw---- 1 mysql mysql       19 Aug 12 13:18 mysql-bin.index  
-rw-r----- 1 mysql root     19162 Aug 12 14:30 node1.test.com.err  
-rw-r----- 1 mysql root      5738 Aug 12 14:33 node2.test.com.err  
-rw-rw---- 1 mysql mysql        5 Aug 12 14:33 node2.test.com.pid  
drwx------ 2 mysql mysql     4096 Aug 12 07:39 performance_schema  
drwx------ 2 mysql root      4096 Aug 12 07:39 test

5.登录并查看

[root@node2 data]# mysql 
Welcome to the MySQL monitor.  Commands end with ; or \g.  
Your MySQL connection id is 1  
Server version: 5.5.33-log MySQL Community Server (GPL)
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 |  
| mydb               |  
| mysql              |  
| performance_schema |  
| test               |  
+--------------------+  
5 rows in set (0.01 sec)

6.停止mysql并卸载数据目录

[root@node2 data]# service mysqld stop 
Shutting down MySQL.                                       [  OK  ]  
[root@node2 data]# cd  
[root@node2 ~]# umount /mydata/  
[root@node2 ~]# mount  
/dev/sda2 on / type ext3 (rw)  
proc on /proc type proc (rw)  
sysfs on /sys type sysfs (rw)  
devpts on /dev/pts type devpts (rw,gid=5,mode=620)  
/dev/sda3 on /data type ext3 (rw)  
/dev/sda1 on /boot type ext3 (rw)  
tmpfs on /dev/shm type tmpfs (rw)  
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)  
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)

注:到这里mysql配置全部完成,下面我们来具体配置一下,高可用的mysql集群

六、crm配置资源

1.新建组资源

m3

2.给组资源增加ID名

m4

3.增加vip资源

m5

4.增加共享存储资源

m6

5.增加mysql服务资源

m7

6.资源增加完成

m8

7.启动全部资源

m9

8.查看一下端口号

[root@node2 ~]# netstat -ntulp 
Active Internet connections (only servers)  
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name 
tcp        0      0 0.0.0.0:610                 0.0.0.0:*                   LISTEN      2548/rpc.statd    
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      8507/mysqld       
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      2509/portmap      
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      2842/cupsd        
tcp        0      0 0.0.0.0:36600               0.0.0.0:*                   LISTEN      -                 
tcp        0      0 0.0.0.0:5560                0.0.0.0:*                   LISTEN      3157/mgmtd        
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      2933/sendmail: acce   
tcp        0      0 127.0.0.1:6011              0.0.0.0:*                   LISTEN      6706/1            
tcp        0      0 :::22                       :::*                        LISTEN      2829/sshd         
tcp        0      0 ::1:6011                    :::*                        LISTEN      6706/1            
udp        0      0 0.0.0.0:40975               0.0.0.0:*                               2918/heartbeat: wri   
udp        0      0 0.0.0.0:33809               0.0.0.0:*                               -                 
udp        0      0 0.0.0.0:57110               0.0.0.0:*                               3062/avahi-daemon:
udp        0      0 0.0.0.0:694                 0.0.0.0:*                               2918/heartbeat: wri   
udp        0      0 0.0.0.0:604                 0.0.0.0:*                               2548/rpc.statd    
udp        0      0 0.0.0.0:607                 0.0.0.0:*                               2548/rpc.statd    
udp        0      0 0.0.0.0:5353                0.0.0.0:*                               3062/avahi-daemon:
udp        0      0 0.0.0.0:111                 0.0.0.0:*                               2509/portmap      
udp        0      0 0.0.0.0:631                 0.0.0.0:*                               2842/cupsd        
udp        0      0 :::46858                    :::*                                    3062/avahi-daemon:
udp        0      0 :::5353                     :::*                                    3062/avahi-daemon:

9.查看一下接口IP

[root@node2 ~]# ifconfig  
eth0      Link encap:Ethernet  HWaddr 00:0C:29:EA:CE:79
          inet addr:192.168.18.202  Bcast:192.168.18.255  Mask:255.255.255.0  
          inet6 addr: fe80::20c:29ff:feea:ce79/64 Scope:Link  
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1  
          RX packets:133635 errors:0 dropped:0 overruns:0 frame:0  
          TX packets:46597 errors:0 dropped:0 overruns:0 carrier:0  
          collisions:0 txqueuelen:1000   
          RX bytes:46103367 (43.9 MiB)  TX bytes:9677373 (9.2 MiB)
eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:EA:CE:79
          inet addr:192.168.18.200  Bcast:192.168.18.255  Mask:255.255.255.0  
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0  
          inet6 addr: ::1/128 Scope:Host  
          UP LOOPBACK RUNNING  MTU:16436  Metric:1  
          RX packets:287 errors:0 dropped:0 overruns:0 frame:0  
          TX packets:287 errors:0 dropped:0 overruns:0 carrier:0  
          collisions:0 txqueuelen:0   
          RX bytes:27967 (27.3 KiB)  TX bytes:27967 (27.3 KiB)

10.查看一下资源状态

m10

说明:从图中可以看出,所有资源全部运行在node2上,现在我们就去访问一下mysql服务

11.node2上测试连接mysql

[root@node2 ~]# mysql 
Welcome to the MySQL monitor.  Commands end with ; or \g.  
Your MySQL connection id is 2  
Server version: 5.5.33-log MySQL Community Server (GPL)
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 |  
| mydb               |  
| mysql              |  
| performance_schema |  
| test               |  
+--------------------+  
5 rows in set (0.03 sec)

12.mysql授权

mysql> grant all on *.* to root@"192.168.18.%" identified by "123456"; 
mysql> flush privileges;

13.测试一下集群效果,直接访问192.168.18.200

[root@nfs ~]# /usr/local/mysql/bin/mysql -uroot -p123456 -h 192.168.18.200 
Welcome to the MySQL monitor.  Commands end with ; or \g.  
Your MySQL connection id is 3  
Server version: 5.5.33-log MySQL Community Server (GPL)
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 |  
| mydb               |  
| mysql              |  
| performance_schema |  
| test               |  
+--------------------+  
5 rows in set (0.01 sec)

14.模拟故障

(1).将node2节点,设置为standby,大家可以看,所有资源全部切换到node1上

m11

(2).node1上查看一下端口

[root@node1 ~]# netstat -ntulp 
Active Internet connections (only servers)  
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name 
tcp        0      0 0.0.0.0:616                 0.0.0.0:*                   LISTEN      2554/rpc.statd    
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      22825/mysqld      
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      2515/portmap      
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      2850/cupsd        
tcp        0      0 0.0.0.0:5560                0.0.0.0:*                   LISTEN      3170/mgmtd        
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      2940/sendmail: acce   
tcp        0      0 0.0.0.0:58330               0.0.0.0:*                   LISTEN      -                 
tcp        0      0 127.0.0.1:6011              0.0.0.0:*                   LISTEN      20469/1           
tcp        0      0 :::22                       :::*                        LISTEN      2837/sshd         
tcp        0      0 ::1:6011                    :::*                        LISTEN      20469/1           
udp        0      0 0.0.0.0:50185               0.0.0.0:*                               2925/heartbeat: wri   
udp        0      0 0.0.0.0:47376               0.0.0.0:*                               3069/avahi-daemon:
udp        0      0 0.0.0.0:694                 0.0.0.0:*                               2925/heartbeat: wri   
udp        0      0 0.0.0.0:41302               0.0.0.0:*                               -                 
udp        0      0 0.0.0.0:610                 0.0.0.0:*                               2554/rpc.statd    
udp        0      0 0.0.0.0:613                 0.0.0.0:*                               2554/rpc.statd    
udp        0      0 0.0.0.0:5353                0.0.0.0:*                               3069/avahi-daemon:
udp        0      0 0.0.0.0:111                 0.0.0.0:*                               2515/portmap      
udp        0      0 0.0.0.0:631                 0.0.0.0:*                               2850/cupsd        
udp        0      0 :::5353                     :::*                                    3069/avahi-daemon:
udp        0      0 :::46332                    :::*                                    3069/avahi-daemon:

(3).node1上查看一下IP

[root@node1 ~]# ifconfig  
eth0      Link encap:Ethernet  HWaddr 00:0C:29:23:76:4D
          inet addr:192.168.18.201  Bcast:192.168.18.255  Mask:255.255.255.0  
          inet6 addr: fe80::20c:29ff:fe23:764d/64 Scope:Link  
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1  
          RX packets:285092 errors:0 dropped:0 overruns:0 frame:0  
          TX packets:319282 errors:0 dropped:0 overruns:0 carrier:0  
          collisions:0 txqueuelen:1000   
          RX bytes:155185716 (147.9 MiB)  TX bytes:297534308 (283.7 MiB)
eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:23:76:4D
          inet addr:192.168.18.200  Bcast:192.168.18.255  Mask:255.255.255.0  
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0  
          inet6 addr: ::1/128 Scope:Host  
          UP LOOPBACK RUNNING  MTU:16436  Metric:1  
          RX packets:63015 errors:0 dropped:0 overruns:0 frame:0  
          TX packets:63015 errors:0 dropped:0 overruns:0 carrier:0  
          collisions:0 txqueuelen:0   
          RX bytes:46067195 (43.9 MiB)  TX bytes:46067195 (43.9 MiB)
(4).node1上测试访问一下mysql
[root@node1 ~]# mysql 
Welcome to the MySQL monitor.  Commands end with ; or \g.  
Your MySQL connection id is 1  
Server version: 5.5.33-log MySQL Community Server (GPL)
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 |  
| mydb               |  
| mysql              |  
| performance_schema |  
| test               |  
+--------------------+  
5 rows in set (0.02 sec)

(5).远程测试连接一下mysql

[root@nfs ~]# /usr/local/mysql/bin/mysql -uroot -p123456 -h 192.168.18.200 
Welcome to the MySQL monitor.  Commands end with ; or \g.  
Your MySQL connection id is 2  
Server version: 5.5.33-log MySQL Community Server (GPL)
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 |  
| mydb               |  
| mysql              |  
| performance_schema |  
| test               |  
+--------------------+  
5 rows in set (0.01 sec)

注:高可用的mysql集群,演示全部完成,最后粘一下cib配置文件,供大家参考!

15.cib.xml 集群资源信息库

[root@node1 ~]# cat /var/lib/heartbeat/crm/cib.xml 
 <cib generated="true" admin_epoch="0" have_quorum="true" ignore_dtd="false" num_peers="2" cib_feature_revision="2.0" crm_feature_set="2.0" ccm_transition="2" dc_uuid="4a0c6580-3e21-4523-a3bb-a921b3f487c0" epoch="217" num_updates="1" cib-last-written="Mon Aug 12 16:03:08 2013">  
   <configuration>  
     <crm_config>  
       <cluster_property_set id="cib-bootstrap-options">  
         <attributes>  
           <nvpair id="cib-bootstrap-options-dc-version" name="dc-version" value="2.1.4-node: aa909246edb386137b986c5773344b98c6969999"/>  
           <nvpair id="cib-bootstrap-options-last-lrm-refresh" name="last-lrm-refresh" value="1376291880"/>  
           <nvpair id="cib-bootstrap-options-default-resource-stickiness" name="default-resource-stickiness" value="100"/>  
         </attributes>  
       </cluster_property_set>  
     </crm_config>  
     <nodes>  
       <node uname="node2.test.com" type="normal" id="4a0c6580-3e21-4523-a3bb-a921b3f487c0">  
         <instance_attributes id="nodes-4a0c6580-3e21-4523-a3bb-a921b3f487c0">  
           <attributes>  
             <nvpair name="standby" id="standby-4a0c6580-3e21-4523-a3bb-a921b3f487c0" value="on"/>  
           </attributes>  
         </instance_attributes>  
       </node>  
       <node id="d1437638-90d9-4bbb-84fc-c897d8f8d6a2" uname="node1.test.com" type="normal">  
         <instance_attributes id="nodes-d1437638-90d9-4bbb-84fc-c897d8f8d6a2">  
           <attributes>  
             <nvpair id="standby-d1437638-90d9-4bbb-84fc-c897d8f8d6a2" name="standby" value="off"/>  
           </attributes>  
         </instance_attributes>  
       </node>  
     </nodes>  
     <resources>  
       <group id="Mysql Service">  
         <meta_attributes id="Mysql Service_meta_attrs">  
           <attributes>  
             <nvpair id="Mysql Service_metaattr_target_role" name="target_role" value="started"/>  
             <nvpair id="Mysql Service_metaattr_ordered" name="ordered" value="true"/>  
             <nvpair id="Mysql Service_metaattr_collocated" name="collocated" value="true"/>  
           </attributes>  
         </meta_attributes>  
         <primitive id="vip" class="ocf" type="IPaddr" provider="heartbeat">  
           <instance_attributes id="vip_instance_attrs">  
             <attributes>  
               <nvpair id="880a5e57-0d7b-448d-adb2-27191987f2bd" name="ip" value="192.168.18.200"/>  
               <nvpair id="9ede4882-ab99-4450-9828-b89bc8d43df4" name="nic" value="eth0"/>  
               <nvpair id="9179225c-a7f3-4190-8f7b-4d6012498e59" name="cidr_netmask" value="24"/>  
             </attributes>  
           </instance_attributes>  
         </primitive>  
         <primitive id="nfs" class="ocf" type="Filesystem" provider="heartbeat">  
           <instance_attributes id="nfs_instance_attrs">  
             <attributes>  
               <nvpair id="0615970e-633d-41a6-acd7-d6823c73d701" name="device" value="192.168.18.208:/mydata"/>  
               <nvpair id="4e2146f8-3890-40a8-8340-6e2787979a60" name="directory" value="/mydata"/>  
               <nvpair id="b7a1d528-a43f-4a46-bee9-fcb7b7b40ee6" name="fstype" value="nfs"/>  
             </attributes>  
           </instance_attributes>  
           <meta_attributes id="nfs_meta_attrs">  
             <attributes/>  
           </meta_attributes>  
         </primitive>  
         <primitive id="mysql" class="lsb" type="mysqld" provider="heartbeat">  
           <meta_attributes id="mysql_meta_attrs">  
             <attributes/>  
           </meta_attributes>  
         </primitive>  
       </group>  
     </resources>  
     <constraints/>  
   </configuration>  
 </cib>

补充说明:在下一篇博文中我们会重点讲解,corosync的详细使用!^_^……