本文是參考了網上的一些資料,本身親自實踐的,經歷了一次失敗,從新安裝了一次虛擬機rhel5,在找linbet的時候,不少老大們的連接都失效了,最後本身找到最新的連接(文章裏有),感謝那些分享經驗的朋友們!!node
試驗環境:mysql
vmware 7.1.1sql
三臺虛擬機:數據庫
操做系統 IP地址 部署應用 備註 主機名centos
centos5 192.168.1.146/eth0 nfs服務端 api
rhel5 192.168.1.144 /eth0 mysql 1.1.1.1 /eth1 rhel5A(主)服務器
rhel5 192.168.1.145/eth0 mysql 1.1.1.2 /eth1 rhel5B (備)socket
1、nfs服務器搭建:ide
1.查詢系統安裝包 nfs-utils nfs-utils-lib portmap
[root@Centos ~]# rpm -qa | grep nfs
nfs-utils-lib-1.0.8-7.6.el5
nfs-utils-1.0.9-44.el5
[root@Centos ~]# rpm -qa | grep portmap
portmap-4.0-65.2.2.1
2.檢查系統服務
[root@Centos ~]# chkconfig nfs on
[root@Centos ~]# mkdir /nfsdata
[root@Centos ~]# mkdir /nfsdata/mysql_data
3.修改配置文件
[root@Centos ~]# vi /etc/exports
/nfsdata
192.168.1.0/24(rw,sync,no_root_squash)
[root@Centos ~]# exportfs -rv
[root@Centos ~]# service nfs start
4.檢查輸出
[root@Centos ~]# showmount -e 192.168.1.146
Export list for 192.168.1.146:
/nfsdata 192.168.1.0/24
2、安裝mysql (主備相同)測試
[root@rhel5A ~]# mkdir /data
[root@rhel5A ~]# mount -t nfs 192.168.1.146:/nfsdata /data
[root@rhel5A ~]# df –h
文件系統
容量
已用 可用 已用% 掛載點
/dev/sda3
18G 2.2G 15G 14% /
/dev/sda1
122M 11M 105M 10% /boot
tmpfs
327M 0 327M 0% /dev/shm
192.168.1.146:/nfsdata
18G 3.6G 13G 22% /data
[root@rhel5A ~]# groupadd mysql
[root@rhel5A ~]# useradd -g mysql -s /sbin/nologin -M mysql
[root@rhel5A ~]# cd /tmp
[root@rhel5A tmp]# tar -zxvf mysql-5.1.49.tar.gz
[root@rhel5A tmp]# cd mysql-5.1.49/
[root@rhel5A mysql-5.1.49]# autoreconf --force --install
[root@rhel5A mysql-5.1.49]# libtoolize --automake --force
[root@rhel5A mysql-5.1.49]# automake --force --add-missing
[root@rhel5A mysql-5.1.49]# ./configure --prefix=/usr/local/mysql --with-mysqld-user=mysql --with-unix-socket-path=/usr/local/mysql/tmp/mysql.sock --with-charset=utf8 --with-extra-charsets=all --enable-assembler --enable-thread-safe-client –without-innodb
須要innodb的要加 --with-plugins=innobase innodb能夠支持4G內存,可是copy my-innodb-heavy-4G.cnf
[root@rhel5A mysql-5.1.49]# make
[root@rhel5A mysql-5.1.49]# make install
[root@rhel5A mysql-5.1.49]# cp support-files/my-large.cnf /etc/my.cnf
[root@rhel5A mysql-5.1.49]# chown -R mysql.mysql /usr/local/mysql/
[root@rhel5A mysql-5.1.49]# vi /etc/my.cnf
更改:
skip-locking 爲 skip-external-locking
log-error = /usr/local/mysql/var/rhel5A.err
[root@rhel5A mysql-5.1.49]# /usr/local/mysql/bin/mysql_install_db --basedir=/usr/local/mysql/ --datadir=/data/mysql_data --user=mysql --pid-file=/usr/local/mysql/tmp/mysql.pid
[root@rhel5A mysql-5.1.49]# cp support-files/mysql.server /etc/init.d/mysqld
[root@rhel5A mysql-5.1.49]# chmod 755 /etc/init.d/mysqld
[root@rhel5A mysql-5.1.49]# vi /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/data/mysql_data
pid_file=/usr/local/mysql/tmp/mysql.pid
server_pid_file=/usr/local/mysql/tmp/rhel5A.pid
[root@rhel5A mysql-5.1.49]# mkdir /usr/local/mysql/var
[root@rhel5A mysql-5.1.49]# chown -R mysql.mysql /usr/local/mysql/
[root@rhel5A mysql-5.1.49]# service mysqld start
這樣就能夠把日誌文件和pid文件分離到本機
[root@rhel5A mysql-5.1.49]# ln -s /usr/local/mysql/bin/mysql /usr/bin/
[root@rhel5A mysql-5.1.49]# ln -s /usr/local/mysql/bin/mysqladmin /usr/bin/
3、安裝heartbeat
主備基本安裝相同,惟一不一樣的是ha.cf裏面 ucast eth1 1.1.1.2 這裏的IP指向對方IP
[root@rhel5A mysql-5.1.49]# cd /tmp
[root@rhel5A tmp]# tar -zxvf libnet.tar.tar
[root@rhel5A tmp]# cd libnet
[root@rhel5A libnet]# ./configure
[root@rhel5A libnet]# make && make install
[root@rhel5A libnet]# cd ..
[root@rhel5A tmp]# tar -zxvf heartbeat-2.1.3.tar.gz
[root@rhel5A tmp]# cd heartbeat-2.1.3
[root@rhel5A heartbeat-2.1.3]# groupadd haclient
[root@rhel5A heartbeat-2.1.3]# useradd -g haclient hacluster
[root@rhel5A heartbeat-2.1.3]# ./ConfigureMe configure
[root@rhel5A heartbeat-2.1.3]# make && make install
[root@rhel5A heartbeat-2.1.3]# cd /etc/ha.d/
[root@rhel5A ha.d]# vi ha.cf
debugfile /var/log/ha-debug
debugfile /var/log/ha-debug
logfile /var/log/ha-log
keepalive 10
deadtime 60
warntime 20
initdead 60
udpport 694
auto_failback on
node rhel5A
node rhel5B
ucast eth1 1.1.1.2
ping_group group1 192.168.1.144 192.168.1.146
respawn hacluster /usr/lib/heartbeat/ipfail
apiauth ipfail gid=haclient uid=hacluster
hopfudge
其中:
1.x的基本配置信息以下:
引用
# more /etc/ha.d/ha.cf
#發送keepalive包的間隔時間
keepalive 2
#定義節點的失效時間
deadtime 30
#定義heartbeat服務啓動後,等待外圍其餘設備(如網卡啓動等)的等待時間
initdead 30
#使用udp端口694 進行心跳監測
udpport 694
#定義心跳
bcast eth0 eth1 # Linux
#定義是否使用auto_failback功能
auto_failback off
#定義集羣的節點
node hatest3
node hatest4
#使用heartbeat提供的日誌服務,若use_logd設置爲yes後,下面的三個選項會失效
use_logd yes
#logfile /var/log/ha_log/ha-log.log
#logfacility local7
#debugfile /var/log/ha_log/ha-debug.log
#設定一個監控網關,用於判斷心跳是否正常
ping 192.168.228.153
deadping 5
Ucast eth1 1.1.1.2 指的是鏈接心跳的網卡
eth1,對方IP 1.1.1.2
#指定和heartbeat一塊兒啓動、關閉的進程
respawn hacluster /usr/local/lib64/heartbeat/ipfail
apiauth ipfail gid=haclient uid=hacluster
該配置文件是比較簡單和清晰的,在不適用
crm yes的狀況下,即爲1.x style格式。
四、設定
haresources文件
這個是1.x使用的集羣資源文件,每行一個組,第一列定義的是優先級服務器,也就是默認資源運行在該服務器上:
[root@rhel5A ha.d]# vi haresources
rhel5A IPaddr::192.168.1.200/24/eth0 Filesystem::192.168.1.146:/nfsdata::/data::nfs mysqld
這裏有四個信息:第一個是優先服務器節點 rhel5A ;第二個是服務虛擬IP地址;第三個是文件系統;第四個是維護的服務腳本
這裏heartbeat會自主維護文件系統和服務腳本,隨heartbeat啓動而啓動
[root@rhel5A ha.d]# vi authkeys
auth 1
1 crc
這裏使用第一種驗證方式
[root@rhel5A ha.d]# chmod 600 authkeys
[root@rhel5A ha.d]# cp /etc/init.d/mysqld /etc/ha.d/resource.d/
[root@rhel5A ha.d]# vi /etc/hosts
1.1.1.1
rhel5A
1.1.1.2
rhel5B
[root@rhel5A ha.d]# umount /data/
[root@rhel5A ha.d]# chkconfig --add heartbeat
[root@rhel5A ha.d]# chkconfig heartbeat on
兩臺都要添加
4、測試
1.停掉兩臺服務器的mysql,卸載nfs
2.service heartbeat start啓動兩臺heartbeat
3.ifconfig查看虛擬IP是否提起來,在主服務器上創建個數據庫
4.斷掉主服務器網線,測試備機是否啓動服務