實驗環境:node
因爲新公司的服務器所有是Debian,之前接觸所有是Centos,過程當中碰見了不少坑,特此記錄mysql
192.168.3.143:ha.cluster.master1nginx
192.168.3.148:ha.cluster.slave1sql
1、安裝mysqlvim
1.一、第一個坑,使用apt-get安裝的mysql-5.5,服務啓動正常,鏈接正常,可是服務中止不了,嘗試過幾種中止方法api
1.1.一、/etc/init.d/mysql stopbash
1.1.二、service mysql stop服務器
1.1.三、kill -9 pkill -9ssh
所有沒法中止,錯誤日誌顯示正常退出,可是進程依然在,可使用客戶端工具鏈接到服務器,花了一個上午依然沒有解決,因而嘗試編譯安裝mysql解決問題。。。。。。有哪位大仙遇到過相似問題,歡迎留言告知,謝謝socket
1.二、編譯步驟和CentOS沒什麼差異,只不過依賴貌似不太同樣,下面記錄編譯過程
1.2.一、編譯安裝
root@ha:/usr/local/src/mysql-5.5.38# apt-get install cmake g++ libncurses5-dev root@ha:/usr/local/src/mysql-5.5.38# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ -DMYSQL_UNIX_ADDR=/space/mysql/data/mysql.sock \ -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci \ -DWITH_EXTRA_CHARSETS:STRING=utf8,gbk -DWITH_MYISAM_STORAGE_ENGINE=1 \ -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1-DWITH_READLINE=1 \ -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=/space/mysql/data -DMYSQL_USER=mysql -DMYSQL_TCP_PORT=3306 root@ha:/usr/local/src/mysql-5.5.38# make && make install
1.2.二、準備配置文件等
root@ha:/usr/local/mysql/support-files# cp my-large.cnf /etc/my.cnf root@ha:/usr/local/mysql/support-files# cp mysql.server /etc/init.d/mysqld root@ha:/usr/local/mysql/support-files# chmod +x /etc/init.d/mysqld root@ha:/usr/local/mysql/support-files# useradd mysql root@ha:/usr/local/mysql# chmod +x scripts/mysql_install_db root@ha:/usr/local/mysql# ./scripts/mysql_install_db --datadir=/space/mysql/data --basedir=/usr/local/mysql --user=mysql
1.2.三、第二個坑(服務啓動不了,提示找不到mysql.service)
在Centos中,將mysql.server拷貝到init.d下,執行chkconfig就能夠了,Debian中要作下面的操做,不然服務啓動不了
root@ha:/usr/local/mysql# update-rc.d mysqld defaults
至此,mysql安裝完成,經測試,啓動中止終於正常了,哭了。。
2、配置drbd
2.一、安裝
2.1.一、
root@ha:/usr/local/mysql# apt-get update root@ha:/usr/local/mysql# apt-get -y upgrade root@ha:/usr/local/mysql# apt-get install drbd-utils
2.1.二、查看系統是否正確加載了drbd模塊
root@ha:/usr/local/mysql/support-files# lsmod |grep drbd drbd 318526 4 lru_cache 13032 1 drbd libcrc32c 12426 1 drbd
2.二、配置兩臺節點的ssh互信和hosts
2.2.一、master1節點
root@ha:/usr/local/mysql# ssh-keygen -t rsa -P root@ha:/usr/local/mysql# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.3.148 root@ha:/usr/local/mysql/support-files# cat /etc/hosts 127.0.0.1 localhost ha.cluster.master1 192.168.2.56 ha.cluster.master1 192.168.3.143 ha.cluster.master1 192.168.3.148 ha.cluster.slave1
2.2.二、slave1節點
root@ha:~# ssh-keygen -t rsa -P root@ha:~# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.3.148 root@ha:~# cat /etc/hosts 127.0.0.1 localhost ha.cluster.slave1 192.168.2.56 ha.cluster.slave1 192.168.3.143 ha.cluster.master1 192.168.3.148 ha.cluster.slave1
注意:debian若是須要改本機host名稱,還須要同時更改/etc/hostname才能生效
drbd添加磁盤就不記錄了,正常操做,格式化
2.三、配置drbd資源(兩個節點配置文件同樣,配置完畢拷貝到另外一個節點上)
root@ha:/etc/drbd.d# vim global_common.conf global { usage-count no; } common { net { cram-hmac-alg "sha1"; shared-secret "mydrbd1fa2jg8"; } syncer { rate 200M; } } resource r0 { on ha.cluster.master1 { device /dev/drbd0; disk /dev/sdb1; address 192.168.3.143:7789; meta-disk internal; } on ha.cluster.slave1 { device /dev/drbd0; disk /dev/sdb1; address 192.168.3.148:7789; meta-disk internal; } } root@ha:/etc/drbd.d# scp global_common.conf ha.cluster.slave1:/etc/drbd.d/
2.四、在兩個節點上建立定義的drbd資源
root@ha:/etc/drbd.d# drbdadm create-md r0 --== Thank you for participating in the global usage survey ==-- The server's response is: Writing meta data... initializing activity log NOT initialized bitmap New drbd meta data block successfully created. root@ha:~# drbdadm create-md r0 --== Thank you for participating in the global usage survey ==-- The server's response is: Writing meta data... initializing activity log NOT initialized bitmap New drbd meta data block successfully created.
2.五、啓動drbd服務並查看狀態
root@ha:~# /etc/init.d/drbd start version: 8.4.3 (api:1/proto:86-101) srcversion: 1A9F77B1CA5FF92235C2213 0: cs:Connected ro:Secondary/Secondary ds:Inconsistent/Inconsistent C r----- ns:21176 nr:90328 dw:91024 dr:27909 al:7 bm:10 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0 root@ha:/etc/drbd.d# /etc/init.d/drbd start version: 8.4.3 (api:1/proto:86-101) srcversion: 1A9F77B1CA5FF92235C2213 0: cs:Connected ro:Secondary/Secondary ds:Inconsistent/Inconsistent C r----- ns:21176 nr:90328 dw:91024 dr:27909 al:7 bm:10 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0
如今兩臺服務器得drbd都處於從狀態,而且數據不是同步的
2.六、在master1節點手動提高爲主節點並掛載drbd分區
root@ha:/etc/drbd.d# drbdadm primary r0 root@ha:/etc/drbd.d# mount /dev/drbd0 /space/mysql/data/ root@ha:/etc/drbd.d# cat /proc/drbd version: 8.4.3 (api:1/proto:86-101) srcversion: 1A9F77B1CA5FF92235C2213 0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r----- ns:44724 nr:696 dw:45420 dr:10531 al:16 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0
2.七、從新安裝mysql庫使其運行在drbd分區上
2.7.一、建立測試表並提高從節點爲主查看數據是否存在
root@ha:/usr/local/mysql# ./scripts/mysql_install_db --datadir=/space/mysql/data --basedir=/usr/local/mysql --user=mysql root@ha:/usr/local/mysql# chmod -R mysql.mysql /space/mysql/data root@ha:/usr/local/mysql# service mysqld start root@ha:/usr/local/mysql# mysql -uroot Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.5.38-log Source distribution Copyright (c) 2000, 2018, 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> mysql> create database jumpserver; mysql> create table user(id int primary key auto_increment, name varchar(30), description varchar(500)); mysql> insert into jumpserver.user (name,description)value('gouyacai','administrator'); mysql> select * from jumpserver.user; +----+---------------+---------------+ | id | name | description | +----+---------------+---------------+ | 1 | gouyacai | administrator | +----+---------------+---------------+ 2 rows in set (0.01 sec) mysql>
2.7.二、手動提高slave爲主
一、主操做
root@ha:/usr/local/mysql# service mysqld stop root@ha:/usr/local/mysql# umount /space/mysql/data root@ha:/usr/local/mysql# drbdadm secondary r0 root@ha:/usr/local/mysql# cat /proc/drbd version: 8.4.3 (api:1/proto:86-101) srcversion: 1A9F77B1CA5FF92235C2213 0: cs:Connected ro:Secondary/Secondary ds:UpToDate/UpToDate C r----- ns:45208 nr:696 dw:45904 dr:12615 al:16 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0
二、從操做
root@ha:~# drbdadm primary r0 root@ha:~# mount /dev/drbd0 /space/mysql/data/ root@ha:~# service mysqld start
2.7.三、登陸從上的mysql查看數據
mysql> select * from jumpserver.user; +----+---------------+---------------+ | id | name | description | +----+---------------+---------------+ | 1 | gouyacai | administrator | +----+---------------+---------------+ 2 rows in set (0.00 sec) mysql>
能夠看出,從上的drbd數據已經同步過來。至此,drbd配置完成。
3、配置HeartBeat實現故障自動切換
3.一、配置認證文件
root@ha:/etc/ha.d# cat authkeys # Authentication file. Must be mode 600 auth 3 3 md5 ha-cluster-memcache root@ha:/etc/ha.d# chmod 600 authkeys
3.二、配置時間同步
master和從都要配置
*/5 * * * * /sbin/ntpdate 192.168.2.57 &> /dev/null
3.三、heartbeat主配置文件
一、master
root@ha:/etc/ha.d# vim ha.cf logfacility local0 keepalive 2 deadtime 5 warntime 10 initdead 10 udpport 694 ucast eth0 192.168.3.148 # 使用單播方式,地址是對方節點的ip用做心跳檢測 # ping 192.168.2.1 # 當對方節點ping不通時,就ping這個ip auto_failback off use_logd yes node ha.cluster.master1 node ha.cluster.slave1
二、slave
root@ha:/etc/ha.d# vim ha.cf logfacility local0 keepalive 2 deadtime 5 warntime 10 initdead 10 udpport 694 ucast eth0 192.168.3.143 auto_failback off use_logd yes node ha.cluster.master1 node ha.cluster.slave1
3.四、配置資源
一、主
root@ha:/etc/ha.d# vim haresources ha.cluster.master1 IPaddr::192.168.3.151/16/eth0:0 drbddisk::r0 Filesystem::/dev/drbd0::/space/mysql/data::ext3 mysqld
二、從
root@ha:/etc/ha.d# cat haresources #ha.cluster.master1 192.168.3.151/16/eth0:0 memcache ha.cluster.master1 IPaddr::192.168.3.151/16/eth0:0 drbddisk::r0 Filesystem::/dev/drbd0::/space/mysql/data::ext3 mysqld
注:該文件內IPaddr,Filesystem等腳本存放路徑在/etc/ha.d/resource.d/下,也能夠在該目錄下存放其餘服務啓動腳本,如memcached,nginx等。將相同腳本名稱添加到/etc/ha.d/haresources內容中,從而跟隨heartbeat啓動而啓動該腳本。
IPaddr::192.168.3.151/16/eth0:0:用IPaddr腳本配置浮動VIP
drbddisk::r0:用drbddisk腳本實現DRBD主從節點資源組的掛載和卸載
Filesystem::/dev/drbd0::data::ext4:用Filesystem腳本實現磁盤掛載和卸載
3.五、取消mysqld和drbd的自動啓動
由於主heartbeat啓動的時候會掛載drbd文件系統以及啓動mysql,切換的時候會將主上的mysql中止並卸載文件系統,從上會掛載文件系統,並啓動mysql。
一、主
root@ha:~# sysv-rc-conf --level 2345 mysqld off root@ha:~# sysv-rc-conf --level 2345 drbd off
二、從
root@ha:~# sysv-rc-conf --level 2345 mysqld off root@ha:~# sysv-rc-conf --level 2345 drbd off
3.六、測試
重啓master和slave兩臺節點,看是否heartbeat會自動掛載drbd設備並啓動mysql
root@ha:~# ps -ef |grep heartbeat root 924 1 0 03:53 ? 00:00:00 heartbeat: master control process root 930 924 0 03:53 ? 00:00:00 heartbeat: FIFO reader root 931 924 0 03:53 ? 00:00:00 heartbeat: write: ucast eth0 root 932 924 0 03:53 ? 00:00:00 heartbeat: read: ucast eth0 root 2251 967 0 03:57 pts/0 00:00:00 grep heartbeat root@ha:~# root@ha:~# cat /proc/drbd version: 8.4.3 (api:1/proto:86-101) srcversion: 1A9F77B1CA5FF92235C2213 0: cs:Connected ro:Secondary/Secondary ds:Inconsistent/Inconsistent C r----- ns:24868 nr:0 dw:292 dr:31269 al:7 bm:4 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0 root@ha:~# ifconfig eth0 Link encap:Ethernet HWaddr 00:0c:29:be:10:c3 inet addr:192.168.3.143 Bcast:192.168.255.255 Mask:255.255.0.0 inet6 addr: fe80::20c:29ff:febe:10c3/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:4341 errors:0 dropped:5 overruns:0 frame:0 TX packets:18732 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:688318 (672.1 KiB) TX bytes:26807817 (25.5 MiB) 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:65536 Metric:1 RX packets:155 errors:0 dropped:0 overruns:0 frame:0 TX packets:155 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:32655 (31.8 KiB) TX bytes:32655 (31.8 KiB) root@ha:~# ps -ef |grep mysql root 2276 967 0 03:58 pts/0 00:00:00 grep mysql
能夠看到,master上drbd設備沒有掛載,vip沒有漂移上來,mysql未啓動
下面查看heartbeat日誌
Aug 14 03:29:46 ha /usr/lib/ocf/resource.d//heartbeat/IPaddr(IPaddr_192.168.3.151): [1566]: INFO: Resource is stopped Aug 14 03:29:46 ha ResourceManager(default)[1511]: [1581]: info: Running /etc/ha.d/resource.d/IPaddr 192.168.3.151/16/eth0:0 start Aug 14 03:29:46 ha IPaddr(IPaddr_192.168.3.151)[1606]: [1637]: INFO: Using calculated netmask for 192.168.3.151: 255.255.0.0 Aug 14 03:29:46 ha IPaddr(IPaddr_192.168.3.151)[1606]: [1659]: INFO: eval ifconfig eth0:0 192.168.3.151 netmask 255.255.0.0 broadcast 192.168.255.255 Aug 14 03:29:46 ha /usr/lib/ocf/resource.d//heartbeat/IPaddr(IPaddr_192.168.3.151): [1681]: INFO: Success Aug 14 03:29:46 ha ResourceManager(default)[1511]: [1710]: info: Running /etc/ha.d/resource.d/drbddisk r0 start Aug 14 03:29:46 ha kernel: [ 24.395989] block drbd0: role( Secondary -> Primary ) Aug 14 03:29:46 ha kernel: [ 24.396555] block drbd0: new current UUID FD9C41E5A4B2DC47:A077794C3E2A99EA:1F567BC15BEB6FB0:1F557BC15BEB6FB1 Aug 14 03:29:46 ha /usr/lib/ocf/resource.d//heartbeat/Filesystem(Filesystem_/dev/d: [1770]: INFO: Resource is stopped Aug 14 03:29:46 ha ResourceManager(default)[1511]: [1785]: info: Running /etc/ha.d/resource.d/Filesystem /dev/drbd0 /space/mysql/data ext3 start Aug 14 03:29:46 ha Filesystem(Filesystem_/dev/drbd0)[1792]: [1823]: INFO: Running start for /dev/drbd0 on /space/mysql/data Aug 14 03:29:46 ha kernel: [ 24.519309] EXT4-fs (drbd0): mounting ext3 file system using the ext4 subsystem Aug 14 03:29:46 ha /usr/lib/ocf/resource.d//heartbeat/Filesystem(Filesystem_/dev/d: [1842]: INFO: Success Aug 14 03:29:46 ha kernel: [ 24.539684] EXT4-fs (drbd0): mounted filesystem with ordered data mode. Opts: (null) Aug 14 03:29:46 ha ResourceManager(default)[1511]: [1872]: info: Running /etc/init.d/mysqld start Aug 14 03:29:48 ha ResourceManager(default)[1511]: [2207]: ERROR: Return code 1 from /etc/init.d/mysqld Aug 14 03:29:48 ha ResourceManager(default)[1511]: [2209]: CRIT: Giving up resources due to failure of mysqld Aug 14 03:29:48 ha ResourceManager(default)[1511]: [2211]: info: Releasing resource group: ha.cluster.master1 IPaddr::192.168.3.151/16/eth0:0 drbddisk::r0 Filesystem::/dev/drbd0::/space/mysql/data::ext3 mysqld Aug 14 03:29:48 ha ResourceManager(default)[1511]: [2222]: info: Running /etc/init.d/mysqld stop Aug 14 03:29:48 ha ResourceManager(default)[1511]: [2247]: info: Running /etc/ha.d/resource.d/Filesystem /dev/drbd0 /space/mysql/data ext3 stop Aug 14 03:29:48 ha Filesystem(Filesystem_/dev/drbd0)[2254]: [2285]: INFO: Running stop for /dev/drbd0 on /space/mysql/data Aug 14 03:29:48 ha Filesystem(Filesystem_/dev/drbd0)[2254]: [2300]: INFO: Trying to unmount /space/mysql/data Aug 14 03:29:48 ha Filesystem(Filesystem_/dev/drbd0)[2254]: [2308]: INFO: unmounted /space/mysql/data successfully Aug 14 03:29:48 ha /usr/lib/ocf/resource.d//heartbeat/Filesystem(Filesystem_/dev/d: [2315]: INFO: Success Aug 14 03:29:48 ha ResourceManager(default)[1511]: [2330]: info: Running /etc/ha.d/resource.d/drbddisk r0 stop Aug 14 03:29:48 ha kernel: [ 26.836087] block drbd0: role( Primary -> Secondary ) Aug 14 03:29:48 ha kernel: [ 26.836873] block drbd0: bitmap WRITE of 2 pages took 1 jiffies Aug 14 03:29:48 ha kernel: [ 26.836878] block drbd0: 248 KB (62 bits) marked out-of-sync by on disk bit-map. Aug 14 03:29:48 ha ResourceManager(default)[1511]: [2350]: info: Running /etc/ha.d/resource.d/IPaddr 192.168.3.151/16/eth0:0 stop Aug 14 03:29:48 ha IPaddr(IPaddr_192.168.3.151)[2375]: [2391]: INFO: ifconfig eth0:0 down Aug 14 03:29:48 ha /usr/lib/ocf/resource.d//heartbeat/IPaddr(IPaddr_192.168.3.151): [2395]: INFO: Success
能夠看到上面的heartbeat日誌顯示啓動mysql時發生了錯誤,致使drbd分區umont,vip釋放等操做。因而手動啓動mysql發現錯誤
180814 3:46:08 [ERROR] /usr/local/mysql/bin/mysqld: Can't create/write to file '/var/run/mysql/mysql.pid' (Errcode: 2) 180814 3:46:08 [ERROR] Can't start server: can't create PID file: No such file or directory 180814 03:46:08 mysqld_safe mysqld from pid file /var/run/mysql/mysql.pid ended
排查發現/var/run/下面沒有mysql目錄,因而手動建立並賦予了mysql屬主,重啓服務器測試,問題依舊。這裏不明緣由,有知道的大仙麻煩留言告知。。
解決辦法:將my.cnf中的pid-file變量設置到/space/mysql/data/mysql.pid中解決
一、下面查看master的服務狀態
Aug 14 03:54:07 ha ip-request-resp(default)[1521]: [1533]: received ip-request-resp IPaddr::192.168.3.151/16/eth0:0 OK yes Aug 14 03:54:07 ha ResourceManager(default)[1534]: [1545]: info: Acquiring resource group: ha.cluster.master1 IPaddr::192.168.3.151/16/eth0:0 drbddisk::r0 Filesystem::/dev/drbd0::/space/mysql/data::ext3 mysqld Aug 14 03:54:07 ha /usr/lib/ocf/resource.d//heartbeat/IPaddr(IPaddr_192.168.3.151): [1589]: INFO: Resource is stopped Aug 14 03:54:07 ha ResourceManager(default)[1534]: [1604]: info: Running /etc/ha.d/resource.d/IPaddr 192.168.3.151/16/eth0:0 start Aug 14 03:54:07 ha IPaddr(IPaddr_192.168.3.151)[1629]: [1660]: INFO: Using calculated netmask for 192.168.3.151: 255.255.0.0 Aug 14 03:54:07 ha IPaddr(IPaddr_192.168.3.151)[1629]: [1682]: INFO: eval ifconfig eth0:0 192.168.3.151 netmask 255.255.0.0 broadcast 192.168.255.255 Aug 14 03:54:07 ha /usr/lib/ocf/resource.d//heartbeat/IPaddr(IPaddr_192.168.3.151): [1709]: INFO: Success Aug 14 03:54:07 ha ResourceManager(default)[1534]: [1738]: info: Running /etc/ha.d/resource.d/drbddisk r0 start Aug 14 03:54:07 ha kernel: [ 42.027571] block drbd0: role( Secondary -> Primary ) Aug 14 03:54:07 ha heartbeat: [924]: info: remote resource transition completed. Aug 14 03:54:07 ha /usr/lib/ocf/resource.d//heartbeat/Filesystem(Filesystem_/dev/d: [1798]: INFO: Resource is stopped Aug 14 03:54:07 ha ResourceManager(default)[1534]: [1813]: info: Running /etc/ha.d/resource.d/Filesystem /dev/drbd0 /space/mysql/data ext3 start Aug 14 03:54:07 ha Filesystem(Filesystem_/dev/drbd0)[1820]: [1851]: INFO: Running start for /dev/drbd0 on /space/mysql/data Aug 14 03:54:07 ha kernel: [ 42.402139] EXT4-fs (drbd0): mounting ext3 file system using the ext4 subsystem Aug 14 03:54:07 ha kernel: [ 42.424843] EXT4-fs (drbd0): mounted filesystem with ordered data mode. Opts: (null) Aug 14 03:54:07 ha /usr/lib/ocf/resource.d//heartbeat/Filesystem(Filesystem_/dev/d: [1874]: INFO: Success Aug 14 03:54:07 ha ResourceManager(default)[1534]: [1904]: info: Running /etc/init.d/mysqld start Aug 14 03:55:01 ha CRON[2248]: (root) CMD (/sbin/ntpdate 192.168.2.57 &> /dev/null) Aug 14 04:00:01 ha CRON[2278]: (root) CMD (/sbin/ntpdate 192.168.2.57 &> /dev/null) root@ha:/space/mysql/data# ifconfig eth0 Link encap:Ethernet HWaddr 00:0c:29:be:10:c3 inet addr:192.168.3.143 Bcast:192.168.255.255 Mask:255.255.0.0 inet6 addr: fe80::20c:29ff:febe:10c3/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:17311 errors:0 dropped:12 overruns:0 frame:0 TX packets:20789 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:2799246 (2.6 MiB) TX bytes:28640153 (27.3 MiB) eth0:0 Link encap:Ethernet HWaddr 00:0c:29:be:10:c3 inet addr:192.168.3.151 Bcast:192.168.255.255 Mask:255.255.0.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:65536 Metric:1 RX packets:161 errors:0 dropped:0 overruns:0 frame:0 TX packets:161 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:32967 (32.1 KiB) TX bytes:32967 (32.1 KiB) root@ha:/space/mysql/data# cat /proc/drbd version: 8.4.3 (api:1/proto:86-101) srcversion: 1A9F77B1CA5FF92235C2213 0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r----- ns:25020 nr:0 dw:444 dr:31537 al:7 bm:4 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0 root@ha:/space/mysql/data# ps -ef |grep mysq; root 1914 1 0 03:54 ? 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/space/mysql/data --pid-file=/space/mysql/data/mysql.pid mysql 2214 1914 0 03:54 ? 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/space/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/space/mysql/data/ha.cluster.master1.err --pid-file=/space/mysql/data/mysql.pid --socket=/space/mysql/data/mysql.sock --port=3306 root 2425 967 0 04:14 pts/0 00:00:00 grep mysq root@ha:/space/mysql/data# mysql -uroot Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.38-log Source distribution Copyright (c) 2000, 2018, 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> mysql> mysql> select * from jumpserver.user -> ; +----+---------------+---------------+ | id | name | description | +----+---------------+---------------+ | 1 | gouyacai | administrator | | 2 | zhangjinsongt | admin | +----+---------------+---------------+ 2 rows in set (0.03 sec)
二、slave的狀態
root@ha:~# ifconfig eth0 Link encap:Ethernet HWaddr 00:0c:29:38:b1:90 inet addr:192.168.3.148 Bcast:192.168.255.255 Mask:255.255.0.0 inet6 addr: fe80::20c:29ff:fe38:b190/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:34696 errors:0 dropped:15 overruns:0 frame:0 TX packets:2673 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:29799737 (28.4 MiB) TX bytes:434321 (424.1 KiB) 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:65536 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) root@ha:~# cat /proc/drbd version: 8.4.3 (api:1/proto:86-101) srcversion: 1A9F77B1CA5FF92235C2213 0: cs:Connected ro:Secondary/Primary ds:UpToDate/UpToDate C r----- ns:0 nr:25128 dw:25128 dr:0 al:0 bm:4 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0 root@ha:~# ps -ef |grep mysql root 1086 994 0 04:15 pts/0 00:00:00 grep mysql
以上狀態能夠確認一切正常,下面關掉master上的heartbeat服務,查看vip和mysql是否會切換到slave上面
一、主操做
root@ha:~# service heartbeat stop root@ha:~# cat /proc/drbd version: 8.4.3 (api:1/proto:86-101) srcversion: 1A9F77B1CA5FF92235C2213 0: cs:Connected ro:Secondary/Primary ds:UpToDate/UpToDate C r----- ns:25312 nr:300 dw:1036 dr:31553 al:7 bm:4 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0 root@ha:~# ifconfig eth0 Link encap:Ethernet HWaddr 00:0c:29:be:10:c3 inet addr:192.168.3.143 Bcast:192.168.255.255 Mask:255.255.0.0 inet6 addr: fe80::20c:29ff:febe:10c3/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:21995 errors:0 dropped:16 overruns:0 frame:0 TX packets:21930 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:3770763 (3.5 MiB) TX bytes:29091655 (27.7 MiB) 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:65536 Metric:1 RX packets:163 errors:0 dropped:0 overruns:0 frame:0 TX packets:163 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:33071 (32.2 KiB) TX bytes:33071 (32.2 KiB) root@ha:~# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda1 49G 2.1G 44G 5% / udev 10M 0 10M 0% /dev tmpfs 98M 4.6M 93M 5% /run tmpfs 244M 0 244M 0% /dev/shm tmpfs 5.0M 4.0K 5.0M 1% /run/lock tmpfs 244M 0 244M 0% /sys/fs/cgroup root@ha:~# ps -ef |grep mysql root 2770 2728 0 04:18 pts/0 00:00:00 grep mysql
能夠看到,master中止了heartbeat以後,drbd分區已經umount,vip已經釋放,mysql服務自動關閉,而且drbd狀態已經變成了Secondary
二、下面是slave的狀態
root@ha:~# ifconfig eth0 Link encap:Ethernet HWaddr 00:0c:29:38:b1:90 inet addr:192.168.3.148 Bcast:192.168.255.255 Mask:255.255.0.0 inet6 addr: fe80::20c:29ff:fe38:b190/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:39219 errors:0 dropped:16 overruns:0 frame:0 TX packets:3537 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:30556311 (29.1 MiB) TX bytes:861986 (841.7 KiB) eth0:0 Link encap:Ethernet HWaddr 00:0c:29:38:b1:90 inet addr:192.168.3.151 Bcast:192.168.255.255 Mask:255.255.0.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:65536 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) root@ha:~# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda1 49G 2.1G 44G 5% / udev 10M 0 10M 0% /dev tmpfs 98M 4.6M 93M 5% /run tmpfs 244M 0 244M 0% /dev/shm tmpfs 5.0M 0 5.0M 0% /run/lock tmpfs 244M 0 244M 0% /sys/fs/cgroup /dev/drbd0 20G 75M 19G 1% /space/mysql/data root@ha:~# cat /proc/drbd version: 8.4.3 (api:1/proto:86-101) srcversion: 1A9F77B1CA5FF92235C2213 0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r----- ns:300 nr:25312 dw:25612 dr:6693 al:7 bm:4 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0 root@ha:~# ps -ef |grep mysql root 1519 1 0 04:17 ? 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/space/mysql/data --pid-file=/space/mysql/data/mysql.pid mysql 1819 1519 0 04:17 ? 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/space/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/space/mysql/data/ha.cluster.slave1.err --pid-file=/space/mysql/data/mysql.pid --socket=/space/mysql/data/mysql.sock --port=3306 root 1861 994 0 04:20 pts/0 00:00:00 grep mysql
在看一下heartbeat的日誌
Aug 14 04:17:01 ha CRON[1088]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly) Aug 14 04:17:26 ha kernel: [ 1439.804089] block drbd0: peer( Primary -> Secondary ) Aug 14 04:17:26 ha heartbeat: [918]: info: Received shutdown notice from 'ha.cluster.master1'. Aug 14 04:17:26 ha heartbeat: [918]: info: Resources being acquired from ha.cluster.master1. Aug 14 04:17:26 ha heartbeat: [918]: debug: StartNextRemoteRscReq(): child count 1 Aug 14 04:17:26 ha heartbeat: [1090]: info: acquire local HA resources (standby). Aug 14 04:17:26 ha heartbeat: [1090]: info: local HA resource acquisition completed (standby). Aug 14 04:17:26 ha heartbeat: [918]: info: Standby resource acquisition done [all]. Aug 14 04:17:26 ha heartbeat: [918]: debug: StartNextRemoteRscReq(): child count 1 Aug 14 04:17:26 ha heartbeat: [1091]: info: No local resources [/usr/share/heartbeat/ResourceManager listkeys ha.cluster.slave1] to acquire. Aug 14 04:17:26 ha heartbeat: [1116]: debug: notify_world: setting SIGCHLD Handler to SIG_DFL Aug 14 04:17:26 ha harc(default)[1116]: [1122]: info: Running /etc/ha.d//rc.d/status status Aug 14 04:17:27 ha mach_down(default)[1127]: [1147]: info: Taking over resource group IPaddr::192.168.3.151/16/eth0:0 Aug 14 04:17:27 ha ResourceManager(default)[1148]: [1159]: info: Acquiring resource group: ha.cluster.master1 IPaddr::192.168.3.151/16/eth0:0 drbddisk::r0 Filesystem::/dev/drbd0::/space/mysql/data::ext3 mysqld Aug 14 04:17:27 ha /usr/lib/ocf/resource.d//heartbeat/IPaddr(IPaddr_192.168.3.151): [1203]: INFO: Resource is stopped Aug 14 04:17:27 ha ResourceManager(default)[1148]: [1218]: info: Running /etc/ha.d/resource.d/IPaddr 192.168.3.151/16/eth0:0 start Aug 14 04:17:27 ha IPaddr(IPaddr_192.168.3.151)[1243]: [1274]: INFO: Using calculated netmask for 192.168.3.151: 255.255.0.0 Aug 14 04:17:27 ha IPaddr(IPaddr_192.168.3.151)[1243]: [1296]: INFO: eval ifconfig eth0:0 192.168.3.151 netmask 255.255.0.0 broadcast 192.168.255.255 Aug 14 04:17:27 ha /usr/lib/ocf/resource.d//heartbeat/IPaddr(IPaddr_192.168.3.151): [1318]: INFO: Success Aug 14 04:17:27 ha ResourceManager(default)[1148]: [1347]: info: Running /etc/ha.d/resource.d/drbddisk r0 start Aug 14 04:17:27 ha kernel: [ 1440.194942] block drbd0: role( Secondary -> Primary ) Aug 14 04:17:27 ha /usr/lib/ocf/resource.d//heartbeat/Filesystem(Filesystem_/dev/d: [1407]: INFO: Resource is stopped Aug 14 04:17:27 ha ResourceManager(default)[1148]: [1422]: info: Running /etc/ha.d/resource.d/Filesystem /dev/drbd0 /space/mysql/data ext3 start Aug 14 04:17:27 ha Filesystem(Filesystem_/dev/drbd0)[1429]: [1460]: INFO: Running start for /dev/drbd0 on /space/mysql/data Aug 14 04:17:27 ha kernel: [ 1440.308138] EXT4-fs (drbd0): mounting ext3 file system using the ext4 subsystem Aug 14 04:17:27 ha kernel: [ 1440.327117] EXT4-fs (drbd0): mounted filesystem with ordered data mode. Opts: (null) Aug 14 04:17:27 ha /usr/lib/ocf/resource.d//heartbeat/Filesystem(Filesystem_/dev/d: [1479]: INFO: Success Aug 14 04:17:27 ha ResourceManager(default)[1148]: [1509]: info: Running /etc/init.d/mysqld start Aug 14 04:17:30 ha mach_down(default)[1127]: [1843]: info: /usr/share/heartbeat/mach_down: nice_failback: foreign resources acquired Aug 14 04:17:30 ha mach_down(default)[1127]: [1848]: info: mach_down takeover complete for node ha.cluster.master1. Aug 14 04:17:30 ha heartbeat: [918]: info: mach_down takeover complete. Aug 14 04:17:34 ha heartbeat: [918]: WARN: node ha.cluster.master1: is dead Aug 14 04:17:34 ha heartbeat: [918]: info: Dead node ha.cluster.master1 gave up resources. Aug 14 04:17:34 ha heartbeat: [918]: info: Link ha.cluster.master1:eth0 dead. Aug 14 04:20:01 ha CRON[1851]: (root) CMD (/sbin/ntpdate 192.168.2.57 &> /dev/null) root@ha:~# mysql -uroot Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.38-log Source distribution Copyright (c) 2000, 2018, 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> select * from jumpserver.user; +----+---------------+---------------+ | id | name | description | +----+---------------+---------------+ | 1 | gouyacai | administrator | | 2 | zhangjinsongt | admin | +----+---------------+---------------+ 2 rows in set (0.03 sec) mysql>
切換成功
至此,大功告成。