讀寫分離好處是提升數據庫的高可用,水平擴展性能:php
至於原理,你們能夠自行百度,也能夠參考下這篇文章:
mysql讀寫分離原理介紹
http://blog.csdn.net/soar_awa...
下面,開始數據庫配置讀寫分離。html
192.168.92.128
192.168.99.1
配置master庫mysql
vim /etc/my.cnf bind-address = 192.168.92.128 #your master ip server-id = 1 #在master-slave架構中,每臺機器節點都須要有惟一的server-id log_bin = /var/log/mysql/mysql-bin.log #開啓binlog
重啓:service mysql restartsql
建立主從同步用戶:docker
$ mysql -u root -p Password: ##建立slave2用戶,並指定該用戶只能在主機192.168.99.1(slave)上登陸。 mysql> GRANT ALL PRIVILEGES ON *.* TO 'slave2'@'%' IDENTIFIED BY '111111' WITH GRANT OPTION; Query OK, 0 rows affected (0.00 sec)
配置slave庫數據庫
bind-address = 192.168.99.1 #your slave ip,不綁定的話能夠不配 server-id = 2 #master-slave結構中,惟一的server-id log_bin = /var/log/mysql/mysql-bin.log #開啓binlog
slave上創建連接vim
$ mysql -u root -p Password: mysql> STOP SLAVE; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> CHANGE MASTER TO -> MASTER_HOST='192.168.92.128', -> MASTER_USER='slave2', -> MASTER_PASSWORD='111111', -> MASTER_LOG_FILE='mysql-bin.000018', -> MASTER_LOG_POS=372; Query OK, 0 rows affected, 2 warnings (0.01 sec) mysql> START SLAVE; Query OK, 0 rows affected (0.00 sec)
MASTER_LOG_FILE='mysql-bin.000018'與MASTER_LOG_POS=372的值,是從上面的 SHOW MASTER STATUS 獲得的。segmentfault
正常狀況下,在slave上執行`SHOW PROCESSLIST`以下:
Id User Host db Command Time State Info ------ ----------- --------------- ------ ------- ------ --------------------------------------------------------------------------- ------------------ 4 root localhost:51955 test Query 0 (NULL) SHOW PROCESSLIST 5 root localhost:51956 (NULL) Sleep 1396 (NULL) 15 system user (NULL) Connect 735 Waiting for master to send event (NULL) 16 system user (NULL) Connect -28385 Slave has read all relay log; waiting for the slave I/O thread to update it (NULL)
正常狀況下,在slave上執行SHOW SLAVE STATUS
以下:centos
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 Master_SSL_Verify_Server_Cert Last_IO_Errno Last_IO_Error Last_SQL_Errno Last_SQL_Error Replicate_Ignore_Server_Ids Master_Server_Id -------------------------------- -------------- ----------- ----------- ------------- ---------------- ------------------- -------------------------------- ------------- --------------------- ---------------- ----------------- --------------- ------------------- ------------------ ---------------------- ----------------------- --------------------------- ---------- ---------- ------------ ------------------- --------------- --------------- -------------- ------------- ------------------ ------------------ ------------------ --------------- ----------------- -------------- --------------------- ----------------------------- ------------- ------------- -------------- -------------- --------------------------- ------------------ Waiting for master to send event 192.168.92.128 slave2 3306 60 mysql-bin.000018 2341 XB-201703192016-relay-bin.000002 2222 mysql-bin.000018 Yes Yes 0 0 2341 2388 None 0 No 0 No 0 0 1 如下兩個參數很重要,只有都爲YES的時候才說明配置無誤。 Slave_IO_Running: Yes Slave_SQL_Running: Yes 若是Slave_IO_Running爲connecting,檢查:A.slave連接時,經過master建立的賬號是否能夠連上master。B.網絡是否通暢。
正常狀況下,在master上執行SHOW PROCESSLIST
以下:網絡
Id User Host db Command Time State Info ------ ------ ------------------ ------ ----------- ------ --------------------------------------------------------------------- ------------------ 3 root 192.168.92.1:52056 mysql Query 0 (NULL) SHOW PROCESSLIST 5 slave2 192.168.92.1:52060 (NULL) Binlog Dump 1066 Master has sent all binlog to slave; waiting for binlog to be updated (NULL)
tips:因爲我本機上安了vmwaer和docker等等...經過cmd-ipconfig得到本機的局域網ip時搞錯了好幾回,當這種狀況出現時,總結了一個比較好的方法,見得到本機局域網ip
lz固然是配置成功啦,小試一下:
master上INSERT INTO test
.linkdb
(id
, value
) VALUES ('4', '3'); slave刷新表更新成功。刪除、修改一樣OK,收工!