概述
在做web應用系統(tǒng)中,如果數(shù)據(jù)庫出現(xiàn)了性能瓶頸,而你又是使用的MySQL數(shù)據(jù)庫,那么就可以考慮采用數(shù)據(jù)庫集群的方式來實現(xiàn)查詢負載了。因為一般來講一個系統(tǒng)中數(shù)據(jù)庫的查詢操作比更新操作要多的多,因此通過多臺查詢服務器將數(shù)據(jù)庫的查詢分擔到不同的查詢數(shù)據(jù)庫從而提高數(shù)據(jù)庫的查詢效率。
MySQL數(shù)據(jù)庫支持數(shù)據(jù)庫的主從復制功能,使用主數(shù)據(jù)庫進行數(shù)據(jù)的插入、刪除與更新操作,而從數(shù)據(jù)庫則專門用來進行數(shù)據(jù)庫查詢,從數(shù)據(jù)庫還可以建立多臺,通過負載均衡來實現(xiàn)查詢,這樣就可以將更新操作與查詢操作分離到不同的數(shù)據(jù)庫上,從而提高查詢的效率,降低系統(tǒng)的負載。
配置
一、主數(shù)據(jù)庫配置
修改mysql的配置文件(對于windows就是MySQL安裝目錄下的my.ini文件,對于linux通常就
是 /etc/my.cnf文件),在配置文件中加入(或修改)
server-id = 1
log-bin=mysql-bin
binlog-do-db=dbname --要同步的數(shù)據(jù)庫名稱
binlog-ignore-db=dbname --不要同步的數(shù)據(jù)庫名稱
注:MySQL進行主從復制是通過二進制的日志文件來進行,所以我們必須開啟MySQL的日志功能,即我們上面的log-bin,同時每一臺數(shù)據(jù)庫服務器都需要指定一個唯一的server-id,通常主數(shù)據(jù)庫服務器我們指定為1。
給主數(shù)據(jù)庫授予一個可以進行復制的用戶,如下:
grant replication slave on *.* to repuser@'host' identified by '123456';
replication slave——MySQL數(shù)據(jù)庫中表示復制的權限名稱
repuser——從數(shù)據(jù)庫服務器登陸到主數(shù)據(jù)庫服務器時用到的用戶名稱
host——從數(shù)據(jù)庫IP地址
123456——登陸密碼
如果上述權限設置后,無法同步,可將其改為
grant select,insert,update,delete,replication slave on *.* to repuser@'10.20.20.167' identified by '123456';
配置完上述步驟后,就可以啟動主數(shù)據(jù)庫了。
二、從數(shù)據(jù)庫配置
修改配置文件
server-id=2
log-bin=mysql-bin --在從服務器上啟動日志記錄,不是必須,但是官方建議
master-host=主機 --主數(shù)據(jù)庫服務器的IP地址
master-user=用戶名 --執(zhí)行復制的用戶名稱,就是grant的用戶,即repuser
master-password=密碼 --復制用戶的密碼,就是grant的用戶密碼,即123456
master-port=端口 --主數(shù)據(jù)庫服務器的端口,默認是3306
replicate-do-db=dbname --需要同步的數(shù)據(jù)庫
#replicate-ignore-db=dbname --不需要同步的數(shù)據(jù)庫
現(xiàn)在可以重啟從數(shù)據(jù)庫,啟動后,還需要啟動復制線程
slave start
查看復制線程的狀態(tài)
show slave status;
我們還可以在從數(shù)據(jù)庫服務器上動態(tài)的改變對主數(shù)據(jù)庫的配置信息,通過如下命令來進行:
CHANGE MASTER TO MASTER_HOST='主數(shù)據(jù)庫服務器的IP地址',MASTER_PORT=3306,MASTER_USER='主數(shù)據(jù)庫上的復制賬號',MASTER_PASSWORD='密碼';
三、啟動與監(jiān)控
1、監(jiān)控主數(shù)據(jù)庫服務器
show master status; --運行于主數(shù)據(jù)庫
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 | 370558 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
其中File是表示日志記錄的文件,而Position則是表示當前日志在文件中的位置,這個也是從數(shù)據(jù)庫服務器上執(zhí)行復制操作必須的標識,后面的兩個字段分別表示要記錄的數(shù)據(jù)庫名稱和不需要記錄的數(shù)據(jù)庫名稱,我們也可以在配置文件中進行配置。
2、監(jiān)控從數(shù)據(jù)庫服務器
show slave status; --運行于從數(shù)據(jù)庫
+----------------------------------+---------------+-------------+-------------+---------------+------------------+---------------------+-----------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+
| Slave_IO_State | Master_Host | Master_User | Master_Port | Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master |
+----------------------------------+---------------+-------------+-------------+---------------+------------------+---------------------+-----------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+
| Waiting for master to send event | 10.20.20.194 | repuser | 3306 | 60 | mysql-bin.000003 | 370558 | WEB2-relay-bin.000206 | 12251 | mysql-bin.000003 | Yes | Yes | | | | | | | 0 | | 0 | 370558 | 12251 | None |