Mysql 資料庫相信大家已經投入了生産使用。很多人都将他和 PHP 內建在 Apache 中,為WebSite 服務。的确,他們在WebSite 中的應用比較多,而且PhpMyAdmin 又是一個PHP+Mysql 的最好應用例子。
那麼Mysql 能不能實作兩個系統之間通過TCP/IP去複制資料庫?能不能實作實時複制呢?也就是說能不能實作同步(Synchronization)的問題。先概括介紹一下Mysql 的Replication Database功能。
複制(Replication)類似于拷貝資料庫到另一台伺服器上,但它是通過定義Master 和Slave的關系去實時地保證兩個資料庫的完全同步。這個功能在Mysql的3.23版中開始出現。
下面大家一起來測試一下Mysql的Replication 功能。
作者的平台是:
Master:Mysql 3.23.53-log on FreeBSD 4.7 Release IP:192.168.10.100
Slave: Mysql 3.23.56-log on FreeBSD 4.8 Stable IP:192.168.10.200
1、Master 機器設定權限,賦予Slave Relication 權利,并打包要同步的資料庫結構。
MasterBSD# pwd
/usr/local/mysql/bin
MasterBSD#./mysql –u root –p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or /g.
Your MySQL connection id is 2 to server version: 3.23.53-log
Type 'help;' or '/h' for help. Type '/c' to clear the buffer.
mysql> GRANT FILE ON *.* TO [email protected] IDENTIFIED BY ‘repplication’;
(賦予192.168.10.200也就是Slave 機器有File權限)
然後打包要複制的資料庫
MasterBSD# cd var
MasterBSD# tar czvf repdatabase.tar.gz repdatabase
這樣,我們的到一個repdatabase資料庫的打封包件repdatabase.tar.gz
2設定主伺服器Master的my.cnf,啟動Mysql服務
MasterBSD# vi /etc/my.cnf
在[mysqld]添加或修改以下的
[mysqld]
log-bin
server-id=1
sql-bin-update-same
binlog-do-db= repdatabase
針對repdatabase 庫做replication 功能
然後把Master主伺服器的Mysql重新開機。
MasterBSD# /usr/local/mysql/bin/mysqladmin –u root –p shutdown
MasterBSD# /usr/local/mysql/bin/safe_mysqld --user=mysql &
3、建立Slave資料庫
剛才我們在Master中打包了repdatabase.tar.gz,它的作用就是要在Slave恢複成一樣的資料庫。先把Master 的repdatabase.tar.gz檔案傳到Slave機器中去。然後
SlaveBSD# tar zxvf repdatabase.tar.gz -C /usr/local/mysql/var/
4、修改Slave伺服器的my.cnf
SlaveBSD# vi /etc/my.cnf
在[mysqld]添加或修改以下的
master-host=192.168.10.100
master-user=replication
master-password=replication
master-port=3306
server-id=2
master-connect-retry=60
replicate-do-db=reldatabase [要更新的資料庫]
log-slave-updates
5、重新開機動Slave的slave start。
SlaveBSD# /usr/local/mysql/bin/mysqladmin –u root –p shutdown
SlaveBSD# /usr/local/mysql/bin/safe_mysqld --user=mysql &
6、測試
先檢測兩個Mysql資料庫中的repdatabase是否正常。
正常情況應該是Master和Slave 中的Mysql 都有相同的repdatabase 資料庫,并且裡面的資料都一樣。
然後我們測試replication 功能是否起用。
在Master中的repdatabas資料庫添加一筆資料:
MasterBSD# /usr/local/mysql/bin/mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or /g.
Your MySQL connection id is 12 to server version: 3.23.53-log
Type 'help;' or '/h' for help. Type '/c' to clear the buffer.
mysql> use repdatabase;
Database changed
mysql> INSERT INTO `rep_table` ( `name` , `num` , `selectd ` ) VALUES ('test1', '4321', 'Y');
Query OK, 1 row affected (0.00 sec)
mysql>
在Slave的資料庫中應該也會同樣有這樣一條資料
SlaveBSD# /usr/local/mysql/bin/mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or /g.
Your MySQL connection id is 17 to server version: 3.23.56-log
Type 'help;' or '/h' for help. Type '/c' to clear the buffer.
mysql> select * from repdatabase.rep_table;
+--------+-----------+------------+
| name | num | selectd |
+--------+-----------+------------+
| aaa | 44444 | N |
| ddd | 111112222 | N |
| insert | 1234 | N |
| test | 12345 | N |
| test1 | 4321 | Y | 這一行就是Master插入的時候Slave 同步得回來的資料。
+--------+-----------+------------+
5 rows in set (0.01 sec)
mysql>
到此,我們的兩個資料庫replication 功能實驗成功。
7、互為replication
在Mysql 的文檔資料中也指出了,一台Mysql機器同樣可以作為Master也可以作為Slave的,也可以互相replication資料。
8、應用
replication可以用在那方面呢?我的想法是,一台Mysql生産機器在提供繁忙的SQL查詢,比如說是股市的查詢,那它僅僅是作為查詢而已。那麼我們就可以通過兩台機器,提供查詢的機器為Slave,那麼資料錄入的機器是Master,通過雙網卡去進行,
這樣的好處是顯而易見,Slave作為大量SQL查詢的伺服器,繁忙、任務大!而且對着Internet,破壞可能性也大!是以,引入Mysql的replication我們可以友善、安全地管理資料庫!