corosync+pacemaker+RA實現mysql高可用

操作目的:

基於Corosync和NFS服務器實現MySQL的高可用

規劃:


準備工作:根據規劃圖,設置三臺主機的主機名和地址
(這裏不再給出具體過程)

一、配置nfs服務器

  1. #fdisk /dev/sda  
  2. 創建一個大小爲20G 類型爲8e的分區 本文爲/dev/sda5  
  3.  
  4. #partprobe /dev/sda  
  5. #pvcreate /dev/sda5  
  6. #vgcreate myvg /dev/sda5  
  7. #lvcreate -L 10G -n mydata myvg  
  8. #mke2fs -j -L MYDATA /dev/myvg/mydata   
  9. #mkdir /mydata  
  10. #vim /etc/fstab  
  11. 添加  
  12. LABEL=MYDATA            /mydata                 ext3    defaults        0 0  
  13. #mount -a  
  14.  
  15. 添加用戶  
  16. # groupadd -g 306 -r mysql  
  17. # useradd -g mysql -r -u 306 -s /sbin/nologin mysql  
  18. # id mysql  
  19. uid=306(mysql) gid=306(mysql) groups=306(mysql) context=root:system_r:unconfined_t:SystemLow-SystemHigh  
  20.  
  21. 讓mysql用戶對/mydata目錄有讀寫的權限  
  22. # chown -R mysql:mysql /mydata/  
  23.  
  24. 通過nfs共享/mydata  
  25. #vim /etc/exports  
  26. /mydata         172.16.220.11(rw,no_root_squash) 172.16.220.12(rw,no_root_squash)  
  27.  
  28. #service nfs start  
  29. # showmount -e localhost  
  30. Export list for localhost:
    /mydata 172.16.220.12,172.16.220.11
  31.  
  32.  

二、配置mysql的高可用

1  配置node1 node2的時間同步和ssh互聯

  1. node1:  
  2. #hwclock -s  
  3. #ssh-keygen -i rsa  
  4. #vim /etc/hosts  
  5. 172.16.220.11 node1  
  6. 172.16.220.12 node2  
  7. # ssh-copy-id -i .ssh/id_rsa.pub root@node2  
  8. # ssh node2 'ifconfig'  
  9.  
  10. node2:  
  11. #hwclock -s  
  12. #ssh-keygen -i rsa  
  13. #vim /etc/hosts  
  14. 172.16.220.11 node1  
  15. 172.16.220.12 node2  
  16. # ssh-copy-id -i .ssh/id_rsa.pub root@node1  
  17. # ssh node1 'ifconfig'  
  18.  

 

2  添加用戶

  1. node1 node2:  
  2. # groupadd -r -g 306 mysql  
  3. # useradd -g mysql -u 306 -r mysql  
  4. # mkdir /mydata  
  5. # mount -t nfs 172.16.220.22:/mydata /mydata/  
  6. # ls /mydata/  
  7. lost+found  
  8.  
  9. 驗證mysql用戶是否對/mydata目錄有寫的權限  
  10. # su - mysql  
  11. su: warning: cannot change directory to /home/mysql: No such file or directory  
  12. -bash-3.2$ cd /mydata  
  13. -bash-3.2$ mkdir data   //創建這個目錄,作爲mysql的數據目錄
  14. -bash-3.2$ ls  
  15. data  lost+found  
  16.  

 

3  安裝mysql

  1. node1 :  
  2. 安裝 mysql-5.5.22-linux2.6-i686.tar.gz  
  3.  
  4. # tar xf mysql-5.5.22-linux2.6-i686.tar.gz -C /usr/local/  
  5. # cd /usr/local/  
  6. # ln -sv mysql-5.5.22-linux2.6-i686 mysql  
  7. #cd mysql  
  8.  
  9. 初始化mysql       
  10. #chown -R mysql:mysql .  
  11. # scripts/mysql_install_db --user=mysql --datadir=/mydata/data/  
  12.  
  13.  
  14. 提供配置文件  
  15. # cp /usr/local/mysql/support-files/my-large.cnf /etc/my.cnf  
  16. #vim /etc/my.cnf  
  17. [mysqld]中修改  
  18. thread_concurrency = 2 
  19. datadir = /mydata/data  
  20.  
  21. 提供腳本  
  22. # cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld  
  23. # chkconfig --add mysqld  
  24. # service mysqld start  
  25. Starting MySQL........                                     [  OK  ]  
  26. # /usr/local/mysql/bin/mysql   連上mysql  
  27.  
  28.    
  29.  
  30. 停止服務  
  31. #service mysqld stop  
  32. # chkconfig mysqld off  
  33. # chkconfig --list mysqld      //保證都是off  
  34.  
  35. node2:
  36. 安裝 mysql-5.5.22-linux2.6-i686.tar.gz  
  37.  
  38. # tar xf mysql-5.5.22-linux2.6-i686.tar.gz -C /usr/local/  
  39. # cd /usr/local/  
  40. # ln -sv mysql-5.5.22-linux2.6-i686 mysql  
  41. #cd mysql  
  42. #chown -R root:mysql   
  43. # cp /usr/local/mysql/support-files/my-large.cnf /etc/my.cnf  
  44. #vim /etc/my.cnf  
  45. [mysqld]中修改  
  46. thread_concurrency = 2 
  47. datadir = /mydata/data  
  48. # cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld  
  49. # chkconfig --add mysqld  
  50. # service mysqld start  
  51. Starting MySQL........                                     [  OK  ]  
  52. # /usr/local/mysql/bin/mysql   連上mysql
  53. #service mysqld stop  
  54. # chkconfig mysqld off  
  55. # chkconfig --list mysqld      //保證都是off  
  56.  
  57. 到此兩個mysql服務器可以實現nfs的共享存儲了  
  58.  

 

4  添加集羣服務

準備的包:
 

  1. corosync-1.2.7-1.1.el5.i386.rpm                                  
  2. corosynclib-1.2.7-1.1.el5.i386.rpm    
  3.  
  4. pacemaker-1.1.5-1.1.el5.i386.rpm  
  5. pacemaker-libs-1.1.5-1.1.el5.i386.rpm  
  6.        
  7. heartbeat-3.0.3-2.3.el5.i386.rpm         
  8. heartbeat-libs-3.0.3-2.3.el5.i386.rpm   
  9.  
  10. cluster-glue-1.0.6-1.6.el5.i386.rpm         
  11. cluster-glue-libs-1.0.6-1.6.el5.i386.rpm                              
  12.  
  13. perl-TimeDate-1.16-5.el5.noarch.rpm  
  14. libesmtp-1.0.4-5.el5.i386.rpm           
  15. resource-agents-1.0.4-1.1.el5.i386.rpm  
  16.  

下載到node1 node2 的/root目錄下

4.1安裝corosync

  1. node1 node2:  
  2. #yum -y --nogpgcheck localinstall *.rpm  

 

4.2 corosync的配置文件

  1. node1:  
  2. # cd /etc/corosync/corosync.conf.example /etc/corosync/corosync.conf  
  3. #vim /etc/corosync.conf  
  4. 修改內容如下:  
  5. secauth: on  
  6. bindnetaddr: 172.16.0.0  
  7. mcastaddr: 226.99.1.1  
  8.  to_logfile: yes  
  9.  to_syslog: no  
  10.  
  11. 接着編輯添加如下內容:  
  12. service {  
  13.         ver: 0  
  14.         name: pacemaker  
  15.         use_mgmtd: yes  
  16. }  
  17.  
  18. aisexec {  
  19.         user: root  
  20.         group: root  
  21. }  
  22.  
  23. 生成節點間通信時用到的認證密鑰文件:  
  24. # corosync-keygen        //會自動的在/etc/corosync目錄下生成一個key文件,是二進制格式的,權限爲400  
  25.  
  26. 將corosync和authkey複製至node2:  
  27. # scp -p authkey corosync.conf node2:/etc/corosync/  
  28.  
  29. 分別爲兩個節點創建corosync生成的日誌所在的目錄:  
  30. # mkdir  /var/log/cluster  
  31. # ssh node2 'mkdir  /var/log/cluster'  
  32.  
  33. 啓動服務  
  34. # service corosync start  
  35. # ssh node2 '/etc/init.d/corosync start'  
  36.  
  37. 查看工作狀態  
  38. # crm status 
  39. ============  
  40. Last updated: Tue Aug 21 09:07:33 2012  
  41. Stack: openais  
  42. Current DC: node1 - partition with quorum  
  43. Version: 1.1.5-1.1.el5-01e86afaaa6d4a8c4836f68df80ababd6ca3902f  
  44. 2 Nodes configured, 2 expected votes  
  45. 0 Resources configured. 
  46. ============  
  47.  
  48. Online: [ node1 node2 ]  
  49.  

 

三、配置集羣資源

node1:(集羣資源的配置,在一個節點上完成即可)

1 禁用stonith
# crm configure property stonith-enabled=false

2 修改忽略quorum不能滿足的集羣狀態檢查:
# crm configure property no-quorum-policy=ignore

3 爲資源指定默認黏性值:
# crm configure rsc_defaults resource-stickiness=100

4 配置資源(資源的添加,在哪個節點上都可以)

1)添加訪問mysql的Ip
# crm configure primitive myip ocf:heartbeat:IPaddr params ip=172.16.220.21

2)添加掛載目錄的資源
 

  1. 現在node1 node2上卸載掛載的目錄  
  2. #umount /mydata           
  3.  
  4. #crm  
  5. crm(live)configure# primitive mynfs ocf:heartbeat:Filesystem params device="172.16.220.22:/mydata" directory="/mydata" fstype="nfs" op start timeout=60s op stop timeout=60s 
  6.  
  7. 資源的工作狀態  
  8. # crm status 
  9. ============  
  10. Last updated: Tue Aug 21 09:46:30 2012  
  11. Stack: openais  
  12. Current DC: node1 - partition with quorum  
  13. Version: 1.1.5-1.1.el5-01e86afaaa6d4a8c4836f68df80ababd6ca3902f  
  14. 2 Nodes configured, 2 expected votes  
  15. 2 Resources configured. 
  16. ============  
  17.  
  18. Online: [ node1 node2 ]  
  19.  
  20.  myip (ocf::heartbeat:IPaddr): Started node1  
  21.  mynfs (ocf::heartbeat:Filesystem): Started node2  
  22.  
  23. 此時可以測試:  
  24. mynfs資源在node2上,查看即可  
  25. # ls /mydata/data  
  26. ibdata1      ib_logfile1  mysql-bin.000001  mysql-bin.000003  node1.err  performance_schema  
  27. ib_logfile0  mysql        mysql-bin.000002  mysql-bin.index   node2.err  test  
  28. 有數據文件了,此時在node1上是沒有/data目錄的  
  29.  
  30. 讓node2變爲被動節點  
  31. #crm node standby  
  32. # crm status 
  33. ============  
  34. Last updated: Tue Aug 21 09:51:52 2012  
  35. Stack: openais  
  36. Current DC: node1 - partition with quorum  
  37. Version: 1.1.5-1.1.el5-01e86afaaa6d4a8c4836f68df80ababd6ca3902f  
  38. 2 Nodes configured, 2 expected votes  
  39. 2 Resources configured. 
  40. ============  
  41.  
  42. Node node2: standby  
  43. Online: [ node1 ]  
  44.  
  45.  myip (ocf::heartbeat:IPaddr): Started node1  
  46.  mynfs (ocf::heartbeat:Filesystem): Started node1  
  47. 此時node1上查看  
  48. #ls /mydata/data  
  49. ibdata1      ib_logfile1  mysql-bin.000001  mysql-bin.000003  node1.err  performance_schema  
  50. ib_logfile0  mysql        mysql-bin.000002  mysql-bin.index   node2.err  test  
  51.  

 

3)添加mysqld服務

  1. (mysqld 一定要和myip mynfs在一起,並且mysqld要晚於mynfs和myip)  
  2. # crm   
  3. crm(live)# configure  
  4. crm(live)configure# primitive mysqld lsb:mysqld  
  5. 定義排列約束  
  6. crm(live)configure# colocation mysqld_with_mynfs_with_myip inf: mysqld mynfs myip  
  7.  
  8. 定義順序約束  
  9. crm(live)configure# order mysqld_after_myip mandatory: myip mysqld   
  10. crm(live)configure# order mysqld_after_mynfs mandatory: mynfs mysqld:start  
  11. 定義完順序之後,查看xml  
  12. crm(live)configure# show xml 要確保是order是如下內容  
  13. <rsc_order first="mynfs" id="mysqld_after_mynfs" score="INFINITY" then="mysqld" then-action="start"/> 
  14. <rsc_order first="myip" id="mysqld_after_myip" score="INFINITY" then="mysqld"/> 
  15.  
  16. 提交配置  
  17. crm(live)configure# commit  
  18.  
  19. #crm status  
  20. Online: [ node1 node2 ]  
  21.  
  22.  myip (ocf::heartbeat:IPaddr): Started node1  
  23.  mynfs (ocf::heartbeat:Filesystem): Started node1  
  24.  mysqld (lsb:mysqld): Started node1  
  25. 此時可以測試以下,mysqld在node1,就在node1上測試  
  26. #/usr/local/mysql/bin/mysql  
  27. mysql> show databases;  
  28. +--------------------+  
  29. | Database           |  
  30. +--------------------+  
  31. | information_schema |  
  32. | mysql              |  
  33. | performance_schema |  
  34. | test               |  
  35. +--------------------+           證明資源生效了  
  36.  

 

 建立一個遠程連接的用戶
 

  1. mysql> grant all on *.* to root@'%' identified by 'redhat';  
  2. mysql> flush privileges;  

 

遠程連接172.16.220.21的mysql服務如圖

 

 

 


操作過程中遇到的問題
 

  1. 1 添加mynfs資源時  
  2. Failed actions:  
  3.     mynfs_start_0 (node=node1call=5rc=5status=complete): not installed  
  4.     mynfs_start_0 (node=node2call=4rc=5status=complete): not installed  
  5. 第一次使用  
  6. # crm configure primitive mynfs ocf:heartbeat:Filesystem params device="172.16.220.22/mydata" directory="/mydata" fstype="nfs" op start timeout=60s op stop timeout=60s 
  7. 來添加的,出現瞭如上錯誤  
  8.  
  9. 第二次使用  
  10. #crm  
  11. crm(live)configure# primitive mynfs ocf:heartbeat:Filesystem params device="172.16.220.22:/mydata" directory="/mydata" fstype="nfs" op start timeout=60s op stop timeout=60s 
  12.  
  13. 工作正常了 詭異啊  
  14. 2 添加進了mgmtd服務,但是5560服務還是沒有啓動。解決一下  
  15.  
  16. 3 可以遠程連接所在節點的mysql,但是不能通過vip連接,怎麼原因?  
  17. 解決:地址衝突,其他主機佔用了這個vip(一定要保證vip,虛擬ip沒有被其他主機佔用)  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章