DRBD Mysql
方案
測試環境:rhel5.5,drbd 8.0.7,mysql5.0.51-rc-log
2
、檢查主機上面有沒有linux的內核源代碼,若是沒有,須要找到相對應版本的源代碼包安裝上去。
3
、開始安裝drbd:
1)
解壓:tar -zxvf drbd-8.0.7.tar.gz
2)
進入drbd源碼目錄,根據kernel源碼位置來編譯drbd:
make KDIR=/usr/src/kernel/ (
若是沒有更改過內核,能夠直接運行make,編譯程序會到/lib/module裏面去本身根據相關配置尋找到kernel源碼)
make install
若是沒有報錯,應該基本install好了,檢查是否生成了相應的文件:/etc/drbd.conf ; /etc/init.d/drbd ; 以及./drbd/drbd.ko
同時系統應該至少多瞭如下兩個命令:drbdadm和drbdsetup
不要刪除此源碼目錄,後面還會用到裏面的./scripts/drbd.conf 和 ./drbd/drbd.ko
4
、如今能夠加載安裝drbd模塊了
insmod drbd.ko
或者 modprobe drbd
經過lsmod檢查是否已經成功
#lsmod |grep drbd
若是有,則表示成功了
5
、更改drbd配置文件:
/etc/drbd.conf
[root@mysql1 ha.d]# cat /etc/drbd.conf
…
on mysql1 {
device /dev/drbd0;
disk /dev/i2o/hda9;
address 10.0.65.45:8888;
flexible-meta-disk internal;
}
on mysql2 {
device /dev/drbd0;
disk /dev/sde2;
address 10.0.65.106:8888;
meta-disk internal;
}
…
6
、primary node設置:
1)
建立matadata:
#drbdadm create-md all
2)
啓動drbd:
#/etc/init.d/drbd start
3)
設置爲主節點:
#drbdadm
— –overwrite-data-of-peer primary all
4)
在新設備上面建立文件系統
#mkfs.ext3 /dev/drbd0
5)
將文件系統mount上
#mkdir /drbddata
#mount /dev/drbd0 /drbddata
7
、secondary node設置:
1)
建立matadata:
#drbdadm create-md all
2)
啓動drbd:
#/etc/init.d/drbd start
注:這裏不要建立文件系統(由於這些信息都會從主節點同步過來的)。
8
、primary和secondary節點都配置完而且都啓動後,開始檢查配置是否成功
1)
檢查進程:
a) primary :
[root@mysql1 /]# ps -auxf |grep drbd
Warning: bad syntax, perhaps a bogus
‘-’? See /usr/share/doc/procps-3.2.3/FAQ
root 5454 0.0 0.0 3744 672 pts/0 S+ 17:36 0:00 \_ grep drbd
root 5389 0.6 0.0 0 0 ? S 17:16 0:07 [drbd0_worker]
root 5403 1.1 0.0 0 0 ? S 17:16 0:14 [drbd0_receiver]
root 5448 0.3 0.0 0 0 ? S 17:35 0:00 [drbd0_asender]
b) secondary:
root@mysql2:/>ps -auxf |grep drbd
Warning: bad syntax, perhaps a bogus
‘-’? See /usr/share/doc/procps-3.2.3/FAQ
root 5272 0.0 0.0 4752 640 pts/1 S+ 16:27 0:00 \_ grep drbd
root 5168 0.0 0.0 0 0 ? S 16:07 0:00 [drbd0_worker]
root 5182 2.3 0.0 0 0 ? S 16:07 0:29 [drbd0_receiver]
root 5270 1.9 0.0 0 0 ? S 16:25 0:03 [drbd0_asender]
能夠看到兩個節點的進程都起來了,每一個drbd設備會有三個進程:drbd0_worker是drbd0的主要進程,drbd0_asender是primary上drbd0的數據發送進程,drbd0_receiver是secondary上drbd0的數據接收進程。
2)
查看/dev/drbd文件的輸出:
[root@mysql1 /]# cat /proc/drbd
version: 8.0.7 (api:86/proto:86)
GIT-hash: cf14288833afe95db396075f8530a5960d29e498 build by root@mysql1, 2008-03-01 17:02:58
0: cs:SyncSource st:Primary/Secondary ds:UpToDate/Inconsistent C r
—
ns:6575584 nr:36 dw:289636 dr:6298278 al:142 bm:515 lo:1 pe:227 ua:30 ap:0
[====>...............] sync
’ed: 21.8% (9336/11932)M
finish: 0:52:36 speed: 3,024 (4,176) K/sec
resync: used:1/31 hits:454805 misses:514 starving:0 dirty:0 changed:514
act_log: used:0/257 hits:72258 misses:154 starving:0 dirty:12 changed:142
root@mysql2:/>
cat /proc/drbd
version: 8.0.7 (api:86/proto:86)
GIT-hash: cf14288833afe95db396075f8530a5960d29e498 build by root@mysql2, 2008-02-29 21:21:46
0: cs:SyncTarget st:Secondary/Primary ds:Inconsistent/UpToDate C r
—
ns:36 nr:5550548 dw:5550552 dr:89 al:2 bm:326 lo:2 pe:97 ua:1 ap:0
[==>.................] sync
’ed: 13.5% (10330/11932)M
finish: 0:28:37 speed: 6,148 (4,248) K/sec
resync: used:2/31 hits:391148 misses:451 starving:0 dirty:0 changed:451
act_log: used:0/257 hits:7 misses:2 starving:0 dirty:0 changed:2
輸出文件上面最開始是drbd的版本信息,而後就是數據同步的一些狀態信息,從mysql的文檔上介紹了每個狀態的意思以下:
cs
— connection state
st
— node state (local/remote)
ld
— local data consistency
ds
— data consistency
ns
— network send
nr
— network receive
dw
— disk write
dr
— disk read
pe
— pending (waiting for ack)
ua
— unack’d (still need to send ack)
al
— access log write count
3)
更進一步驗證數據是否同步正確了:
a)
在mysql1上將該文件系統umount,而後執行drbdadm secondary all,改爲secondary模式
[root@mysql1 /]# umount /drbddata
[root@mysql1 /]# drbdadm secondary all
b)
在原mysql2 執行drbdadm primary all 改爲primary模式,再mount文件系統
root@mysql2:/>drbdadm primary all
root@mysql2:/>mount /dev/drbd0 /drbddata
檢查以前在mysql1下寫入的文件是否已經徹底同步到mysql2下面,經驗證,數據已經同步好了
4)
最後連同mysql一塊兒手工測試一次切換:
a)
主節點mysql1下關閉mysql,釋放資源,將資源切換成secondary模式:
[root@mysql1 ha.d]# mysqladmin -u root shutdown
[root@mysql1 ha.d]# umount /drbddata
[root@mysql1 ha.d]# drbdadm secondary all
b)
次節點mysql2下獲取資源,mount上,切換成primary模式,並啓動mysql:
root@mysql2:/root/mysql-5.0.51a> drbdadm primary all
root@mysql2:/root/mysql-5.0.51a> mount /dev/drbd0 /drbddata
root@mysql2:/usr/local/mysql/bin> ./mysqld_safe
–user=mysql &
[1] 27900
root@mysql2:/usr/local/mysql/bin>
Starting mysqld daemon with databases from /drbddata/mysqldata
root@mysql2:/usr/local/mysql/bin>tail -f /drbddata/mysqldata/mysql2.err
080303 13:53:25 mysqld started
080303 13:53:26 InnoDB: Started; log sequence number 0 43656
080303 13:53:26 [Note] /usr/local/mysql/libexec/mysqld: ready for connections.
Version:
‘5.0.51a-log’ socket: ‘/usr/local/mysql/sock/mysql.sock’ port: 3306 Source distribution
c)
登入數據庫中檢查數據,這裏我同時在新的主節點上面測試了寫,以便在後面切換回主節點後檢查數據是否也正常:
root@mysql2:/usr/local/mysql/bin>mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1 to server version: 5.0.51a-log
Type
‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.
mysql> show databases;
+
——————–+
| Database |
+
——————–+
| information_schema |
| mysql |
| test |
+
——————–+
3 rows in set (0.03 sec)
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+
—————-+
| Tables_in_test |
+
—————-+
| t1 |
| t2 |
| t3 |
+
—————-+
3 rows in set (0.00 sec)
mysql> create table t4(id int);
Query OK, 0 rows affected (0.07 sec)
mysql> exit
Bye
root@mysql2:/usr/local/mysql/bin>
d)
再經過以前相同的切換步驟切換回各自原來的模式,啓動主節點的mysql,並檢查數據:
…
[root@mysql1 ha.d]# drbdadm primary all
[root@mysql1 ha.d]# mount /dev/drbd0 /drbddata
[root@mysql1 ha.d]# mysqld_safe
–user=mysql &
[1] 8451
[root@mysql1 ha.d]# Starting mysqld daemon with databases from /drbddata/mysqldata
[root@mysql1 ha.d]#
[root@mysql1 ha.d]#
[root@mysql1 ha.d]#
[root@mysql1 ha.d]#
[root@mysql1 ha.d]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1 to server version: 5.0.51a-log
Type
‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+
—————-+
| Tables_in_test |
+
—————-+
| t1 |
| t2 |
| t3 |
| t4 |
+
—————-+
4 rows in set (0.00 sec)
mysql> insert into t4 values(111);
Query OK, 1 row affected (0.01 sec)
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from t4;
+
——+
| id |
+
——+
| 111 |
+
——+
1 row in set (0.00 sec)
幾個注意點:
1
、除了primary節點的文件系統是手工建立的以外,其餘的全部secondary節點的文件系統都是經過同步完成的,兩邊的設備大小好像並不要求徹底同樣的大小,在我測試的過程當中,兩邊用於drbd設備的分區大小相差很大,但好像並無出現錯誤,從drbd相關文檔上看到這個並不會致使問題。可是若是主節點比備節點大,並且用的空間也超出了備節點空間大小會有問題,具體會怎樣尚未測試到。
2
、在mount drbd的設備以前,該節點必須已經設置爲primary模式,並且若是一邊沒有umount,另外一邊是沒法mount上的,直接mount會報以下錯誤:
root@mysql2:/>mount /dev/drbd0 /drbddata
mount: block device /dev/drbd0 is write-protected, mounting read-only
也就是說,在同步的過程當中,只有一個節點的數據可見,也就是不能經過咱們人直接簡單的查看數據文件等方式之間驗證數據是否正常。不過看文檔說好像能夠經過手動設置一些信息來查看是否正常