Mysql主從配置和跳過事務

來源:互聯網
上載者:User

標籤:mysql主從配置和跳過事務

Mysql主從配置和跳過事務

一、介紹:

大型網站中資料層還是原來那種傳統的資料架構,或者只是淡淡靠一台伺服器來扛,如此多的資料庫連接操作,資料必然會崩潰,資料丟失的話,可想而知後果不堪設想。所以我們想到很多解決方案·:一方面採用優秀的代碼架構,進行代碼的最佳化,採用優秀的資料緩衝技術如:redis,如果資金豐厚的話,必然會想到架設伺服器群,來分擔主要資料庫的壓力。然後重點到了今天介紹的:利用MySQL主從配置,實現讀寫分離,減輕資料庫壓力。這種方式,在如今很多網站裡都有使用,也不是什麼新鮮事情,今天總結一下,方便大家學習參考一下。

原理:主伺服器(Master)負責網站NonQuery操作,從伺服器負責Query操作,使用者可以根據網站功能模特性塊固定訪問Slave伺服器,或者自己寫個池或隊列,自由為請求分配從伺服器串連。主從伺服器利用MySQL的二進位記錄檔,實現資料同步。二進位日誌由主伺服器產生,從伺服器響應擷取同步資料庫。

拓撲圖:

650) this.width=650;" src="https://s2.51cto.com/oss/201711/13/f73c83a77fcd7496fafb9cf0a86829a4.png" title="mysql主從.png" width="700" height="565" border="0" hspace="0" vspace="0" style="width:700px;height:565px;" alt="f73c83a77fcd7496fafb9cf0a86829a4.png" />

Master---cml5:192.168.5.103

Slave---cml2:192.168.5.102

Slave---cml6:192.168.5.106

二、配置:

1、首先備份主(master)主機資料庫(全備),然後倒入到從(slave)主機上:

[[email protected] ~]# mysqldump -uroot -predhat--single-transaction -R --triggers -E --master-data --flush-logs--all-databases > cml.sqlmysqldump:[Warning] Using a password on the command line interface can be insecure.[[email protected] mydata]# scp cml.sql cml2:/data/cml.sql                                                         100% 1617     1.6KB/s   00:00[[email protected] data]# mysql -uroot -predhat< cml.sqlmysql: [Warning] Using a password on thecommand line interface can be insecure.

 

2、編輯master主機的my.cnf設定檔:

[[email protected] mydata]# cat /etc/my.cnf[mysqld]datadir=/usr/local/mysql/mydatasocket=/usr/local/mysql/mysql.socklog_bin=/usr/local/mysql/mydata/mysql-binserver-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、查看主庫上資料節點:

650) this.width=650;" src="https://s1.51cto.com/oss/201711/13/83d4694eb09433d85ab536f0d52d5758.png" title="mysql主從2.png" alt="83d4694eb09433d85ab536f0d52d5758.png" />

 

5、修改slave上的my.cnf設定檔:

[[email protected] mydata]# cat /etc/my.cnf[mysqld]datadir=/usr/local/mysql/mydatasocket=/usr/local/mysql/mysql.socklog_bin=/usr/local/mysql/mydata/mysql-binserver-id=2relay_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=3relay_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/1981408

Mysql主從配置和跳過事務

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.