Mysql主從配置和跳過事務
Mysql主從配置和跳過事務
一、介紹:
大型網站中數據層還是原來那種傳統的數據架構,或者只是淡淡靠一臺服務器來扛,如此多的數據庫連接操作,數據必然會崩潰,數據丟失的話,可想而知後果不堪設想。所以我們想到很多解決方法·:一方面采用優秀的代碼框架,進行代碼的優化,采用優秀的數據緩存技術如:redis,如果資金豐厚的話,必然會想到架設服務器群,來分擔主數據庫的壓力。然後重點到了今天介紹的:利用MySQL主從配置,實現讀寫分離,減輕數據庫壓力。這種方式,在如今很多網站裏都有使用,也不是什麽新鮮事情,今天總結一下,方便大家學習參考一下。
原理:主服務器(Master)負責網站NonQuery操作,從服務器負責
拓撲圖:
Master---cml5:192.168.5.103
Slave---cml2:192.168.5.102
Slave---cml6:192.168.5.106
二、配置:
1、首先備份主(master)主機數據庫(全備),然後倒入到從(slave)主機上:
[root@cml5 ~]# mysqldump -uroot -predhat--single-transaction -R --triggers -E --master-data --flush-logs--all-databases > cml.sql mysqldump:[Warning] Using a password on the command line interface can be insecure. [root@cml5 mydata]# scp cml.sql cml2:/data/ cml.sql 100% 1617 1.6KB/s 00:00 [root@cml2 data]# mysql -uroot -predhat< cml.sql mysql: [Warning] Using a password on thecommand line interface can be insecure.
2、編輯master主機的my.cnf配置文件:
[root@cml5 mydata]# cat /etc/my.cnf [mysqld] datadir=/usr/local/mysql/mydata socket=/usr/local/mysql/mysql.sock log_bin=/usr/local/mysql/mydata/mysql-bin server-id=1
##備註:server-id 服務器唯一標識,log_bin 啟動MySQL二進制日誌
3、主庫創建同步用戶:
mysql> grant all on *.* to ‘rsync‘@‘%‘identified by ‘redhat‘; Query OK, 0 rows affected, 1 warning (0.00sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
4、查看主庫上數據節點:
5、修改slave上的my.cnf配置文件:
[root@cml2 mydata]# cat /etc/my.cnf [mysqld] datadir=/usr/local/mysql/mydata socket=/usr/local/mysql/mysql.sock log_bin=/usr/local/mysql/mydata/mysql-bin server-id=2 relay_log=relay-log
6、slave褲上面同步:
mysql> CHANGE MASTER TOMASTER_HOST=‘192.168.5.103‘,MASTER_USER=‘rsync‘,MASTER_PASSWORD=‘redhat‘,MASTER_LOG_FILE=‘mysql-bin.000011‘,MASTER_LOG_POS=587; Query OK, 0 rows affected, 2 warnings (0.00sec) mysql> start slave; Query OK, 0 rows affected (0.01 sec)
7、查看狀態兩個線程必須都是YES:
mysql> show slave status\G;
*************************** 1. row***************************
Slave_IO_State: Connecting tomaster
Master_Host: 192.168.5.103
Master_User: rsync
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000011
Read_Master_Log_Pos: 587
Relay_Log_File: relay-log.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000011
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
8、連接一臺空的DB,測試數據是否同步:
server-id=3 relay_log=relay-log
###測試:
mysql> CHANGE MASTER TOMASTER_HOST=‘192.168.5.103‘,MASTER_USER=‘rsync‘,MASTER_PASSWORD=‘redhat‘,MASTER_LOG_FILE=‘mysql-bin.000011‘,MASTER_LOG_POS=587; Query OK, 0 rows affected, 2 warnings (0.01sec)mysql> start slave;
##因為之前已經創建了一張表,所以寫入肯定會出錯:
mysql> show slave status\G;
*************************** 1. row***************************
Slave_IO_State: Connecting tomaster
Master_Host: 192.168.5.103
Master_User: rsync
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000011
Read_Master_Log_Pos: 587
Relay_Log_File: relay-log.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000011
Slave_IO_Running: Yes
Slave_SQL_Running: no
##線上可以直接跳過這個錯誤,但是這個事務的數據久找不到了:
mysql>stop slave; Query OK, 0 rows affected (0.00 sec) mysql> SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1 ; ##跳過一個事務,多個就改變數字。 Query OK, 0 rows affected (0.00 sec) mysql> start slave; Query OK, 0 rows affected (0.00 sec)
##重設置slave:
mysql> stop slave; Query OK, 0 rows affected (0.00sec) mysql> reset slave; Query OK, 0 rows affected (0.00sec)
本文出自 “第一個legehappy51cto博客” 博客,請務必保留此出處http://legehappy.blog.51cto.com/13251607/1981409
Mysql主從配置和跳過事務