1. 程式人生 > >Mysql叢集的HA原理及配置指南之主備模式(一)

Mysql叢集的HA原理及配置指南之主備模式(一)

最近手頭上專案很多,所以部落格這邊耽誤了2個月實在抱歉。

先說說最近搭專案的感受吧:

給一家做網際網路金融的集團公司上私有云,方案用的都是主流的HA高可用方案,沒有什麼特別的,第一期是十臺伺服器(7臺計算節點),現在部署了線上服務的虛擬機器差不多接近60臺,總體來說比較穩定,遇到過幾個問題基本上都是對面的運維配交換機的小問題(其實也不能說小,但是部署除錯完之後那邊IDC的網路硬體我們根本碰不到)。

方案的最大分歧實在mysql資料庫節點的存放問題,我比較傾向於將mysql搭建在3臺控制節點上做主備方案,但是最終定下來的方案是放在3臺虛擬機器裡面,到現在我還是不認可的。在我平時的開發及除錯環境的時候,發現大量的connectio會導致mysql排隊過多的情況,這還是有ssd的情況下。我也理解mysql搭在虛擬機器的原因,不就是成本嘛,這個大家都懂(現在的架構師動不動就拿成本來做藉口),但是玩過私有云的都知道,基本上最重要的就是做資料永久儲存的資料庫,rabbitmq down了只不過服務停了,修復的成本並不高,但是資料庫一旦down了那就是整個環境要不要重新搭的問題了。雖然有三臺虛擬機器做主備,但虛擬機器畢竟是虛擬機器,虛擬機器之間的通訊本身相對物理機就有效能的損耗,反正我還是希望看到這篇文章的博友之後在給人家設計私有云方案的時候多多重視Mysql的高可用,別臺相信虛擬機器,更別太相信那些開源的所謂的很穩定的HA元件。

上述就是我寫今天這篇mysql主備配置文章的原因。

正文:

1.mysql資料備份,為什麼要做備份:

Survive Failures - Replication gives you an up-to-the-moment copy of the master database, on a server that's ready to take over when the inevitable crash happens. It is typically much faster to restore service using a slave than restoring from a backup.

Scale Reads - Many modern applications have a write-once, read-many access pattern. For example, a blog post will be inserted once and read many times. You can use replication to spread the much higher SELECT load over manyread-slaves. The smaller write load (INSERTs, UPDATEs, DELETES) still has to run on the master.

Distribute Data - A master can replicate to slaves anywhere on the globe. You can use slaves to improve network latency serving customers. You can also use a distant replicated slave as a Business Continuity Plan in case the entire location housing the master server is offline.

Run Intensive Reports - Some queries take a huge amount of processor, memory, or disk to execute. Dedicated read slaves can distribute the impact of heavy reports.

總結來說就是資料庫的高可用,master可以與多個slave進行資料的同步,但這裡和別的方案不同的是,它可以做到slave連線slave,而不是所有的slave全部掛在master上面。

2.Start:

我這裡用的mysql是percona發行版,至於為什麼用這個,我只能說我用過來感覺這個比較穩定一點,相對mariadb更高階一點,大家當然也可以用別的,反正都是支援的。

(Percona發行版的下載地址 :https://www.percona.com/downloads/)

先講講資料庫的master-slave主備吧,原理這裡就不細說了,大家看下面的圖也能明白:


接下去就是進資料庫配置了,先是master節點:

master ~ $ mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.29 MySQL Community Server (GPL)

Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> CREATE USER 'replicator'@'%' IDENTIFIED BY 'TileLowNilWife';
Query OK, 0 rows affected (0.00 sec)

mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'replicator'@'%';
Query OK, 0 rows affected (0.00 sec)
                
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql>

After this step is completed:
  1. replicator can log into MySQL on the master from the slave with password TileLowNilWife
第一步就是對剛搭的master資料庫建立slave使用者,讓slave節點可以通過特定的使用者進行連線。

In order for this server to be a replication master, it needs log_bin enabled, and a server_id that is not the default (0).

Set those values by editing /etc/my.cnf:

接下去這一步比較重要,因為slave是通過mater的log進行分析從而同步資料的,所以我們需要將這個引數在/etc/my.cnf開啟:
[mysqld]
log_bin = mysql-bin
server_id = 10
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
......some content not shown
然後再重啟資料庫:
master ~ $ sudo service mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]

進mysql檢查剛才的配置是否成功:
master ~ $ mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.29-log MySQL Community Server (GPL)

Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SHOW variables LIKE 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 10    |
+---------------+-------+
1 row in set (0.01 sec)

mysql> SHOW variables LIKE 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin       | ON    |
+---------------+-------+
1 row in set (0.00 sec)

mysql>
如果之前資料庫中有資料,需要對其備份一下:
mysql> exit
master ~ $ mysqldump -u root --single-transaction --all-databases --master-data=1 > /tmp/master_backup.sql 
-- Warning: Skipping the data of table mysql.event. Specify the --events option explicitly.
master ~ $ 

將備份的sql資料傳到slave節點上:
master ~ $ scp /tmp/master_backup.sql slave.example.com:/tmp/
The authenticity of host 'slave.example.com (10.242.58.189)' can't be established.
RSA key fingerprint is 0f:47:42:f4:71:51:4c:a3:70:94:db:83:03:4c:d2:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'slave.example.com,10.242.58.189' (RSA) to the list of known hosts.
[email protected]'s password: (input your password)
master_backup.sql                           100%  501KB 501.3KB/s   00:00
master ~ $ 

可以看到備份的資料產生了一個log file,slave即是根據該log file進行資料同步的:
master ~ $ egrep "^CHANGE MASTER" /tmp/master_backup.sql
CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=107;
master ~ $ 

然後就是在slave上進行配置:

第一步是修改/etc/my.cnf:

slave ~ $ sudoedit /etc/my.cnf

[mysqld]
server_id = 20
log_bin = mysql-bin
log_slave_updates = 1
relay_log = mysql-relay-bin
read_only = 1
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

開啟一些配置項,再進行資料庫重啟:
slave ~ $ sudo service mysqld restart
Stopping mysqld:                                           [  OK  ]
Starting mysqld:                                           [  OK  ]

接下去進mysql檢查剛才的配置是否生效:
slave ~ $ mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.29-log MySQL Community Server (GPL)

Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SHOW variables WHERE Variable_Name IN ("server_id", "log_bin", "relay_log", "log_slave_updates", "read_only");
+-------------------+-----------------+
| Variable_name     | Value           |
+-------------------+-----------------+
| log_bin           | ON              |
| log_slave_updates | ON              |
| read_only         | ON              |
| relay_log         | mysql-relay-bin |
| server_id         | 20              |
+-------------------+-----------------+
5 rows in set (0.00 sec)

mysql>

匯入master上之前的資料:
mysql> source /tmp/master_backup.sql
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 508 rows affected (0.02 sec)
Records: 508  Duplicates: 0  Warnings: 0

Configure the slave with the network name of the master and the username and password for the replicator account.

This information, combined with the log file and position in the backup file, are all the slave needs to establish replication with the master.

這一步很關鍵,slave通過剛才master上建的使用者進行對master連線,產生一個備份對映關係:
mysql> CHANGE MASTER TO MASTER_HOST='master.example.com', 
MASTER_USER='replicator', 
MASTER_PASSWORD='TileLowNilWife';
Query OK, 0 rows affected (0.05 sec)

mysql>
成功後開啟slave模式:
mysql> SLAVE START;
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: master.example.com
                  Master_User: replicator
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 960
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 1106
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 960
              Relay_Log_Space: 1262
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 10
1 row in set (0.00 sec)

mysql>

到這一步master -> slave的配置基本完成了,我們接下去可以test一下:

在master上插入資料:

master ~ $ mysql -u root
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.5.29-log MySQL Community Server (GPL)

Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> INSERT INTO important.stuff SET details='Replication is running';
Query OK, 1 row affected (0.04 sec)

mysql>

在slave上進行檢視:
mysql> SELECT * FROM important.stuff;
+----+------------------------+---------------------+
| id | details                | happened            |
+----+------------------------+---------------------+
|  1 | Replication is running | 2013-03-12 19:29:33 |
+----+------------------------+---------------------+
1 row in set (0.00 sec)

mysql> 

如果資料出來了,表示最最基本的mysql主備完成了,那些大叢集的mysql備份方案都大同小異,原理就是這樣沒什麼去吧,希望對博友認識mysql以及高可用有幫助,謝謝。



相關推薦

Mysql叢集HA原理配置指南模式

最近手頭上專案很多,所以部落格這邊耽誤了2個月實在抱歉。 先說說最近搭專案的感受吧: 給一家做網際網路金融的集團公司上私有云,方案用的都是主流的HA高可用方案,沒有什麼特別的,第一期是十臺伺服器(7臺計算節點),現在部署了線上服務的虛擬機器差不多接近60臺,總體來說比較穩定

工廠三兄弟抽象工廠模式

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

工廠三兄弟工廠方法模式

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!        

Java設計模式觀察者模式

觀察者模式:Observer           觀察者模式主要用於1:N的通知,當一個物件(目標物件subject或Observable)的狀態變化時,他需要及時告知一系列物件(觀察者Observer),令他們做出相應           通知觀察者的方式有兩種,分

工廠三兄弟簡單工廠模式

也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!                工廠模式是最常用的一類建立型設計模式,通常我們所說的工廠模式是指工廠方法模式,它也是使用頻率最高的工廠模式。本章將要學習的簡單工廠模式是工廠方法模式的“小弟”

工廠三兄弟工廠方法模式:日誌記錄器的設計

簡單工廠模式雖然簡單,但存在一個很嚴重的問題。當系統中需要引入新產品時,由於靜態工廠方法通過所傳入引數的不同來建立不同的產品,這必定要修改工廠類的原始碼,將違背“開閉原則”,如何實現增加新產品而不影響已有程式碼?工廠方法模式應運而生,本文將介紹第二種工廠模式——

工廠方法模式-Factory Method Pattern 工廠三兄弟工廠方法模式:日誌記錄器的設計

簡單工廠模式雖然簡單,但存在一個很嚴重的問題。當系統中需要引入新產品時,由於靜態工廠方法通過所傳入引數的不同來建立不同的產品,這必定要修改工廠類的原始碼,將違背“開閉原則”,如何實現增加新產品而不影

Java設計模式結構型模式

在解決了物件的建立問題之後,物件的組成以及物件之間的依賴關係就成了開發人員關注的焦點,因為如何設計物件的結構、繼承和依賴關係會影響到後續程式的維護性、程式碼的健壯性、耦合性等。 一、介面卡模式(Adapter) 介面卡模式是指“將一個類的介面轉換成客戶希

設計模式單例模式

這篇我們學習的是單例模式,相信很多朋友都或多或少使用過這個模式。很多設計模式的入門,都把單例模式作為第一個的,但是因為我們是跟著書本學習,所以放在了第五個裡面。那麼,你使用過的單例模式是怎麼樣的呢?懶漢式?餓漢式?雙重校驗?靜態? 先來看下定義,單例模式(Singleton Pattern):用來建立獨一無二

設計模式模板方法模式

學習了前面的朋友都知道,目前為止,我們的議題都是繞著封裝轉;我們已經封裝了物件建立、方法呼叫、複雜介面、鴨子、比薩...那接下來呢? 我們將要深入封裝演算法塊、好讓子類可以在任何時候都可以將自己掛接進運算裡。我們甚至會在這裡學到一個受好萊塢影響而啟發的設計原則。 喝點咖啡或茶飲 有些人喜歡喝咖啡,沒有咖啡感覺

C#可擴展編程MEF學習筆記:MEF簡介簡單的Demo

com ring this exec hosting code .cn 引用 展開 在文章開始之前,首先簡單介紹一下什麽是MEF,MEF,全稱Managed Extensibility Framework(托管可擴展框架)。單從名字我們不難發現:MEF是專門致力於解決擴展性

Linux7配置team聚合鏈模式

Linux7配置team聚合鏈 centos7、redhat7使用teaming實現聚合鏈路,能夠提升網絡卡繫結之後的網路吞吐效能,並且提供網絡卡故障後切換網絡卡處理的能力 team是基於小型核心驅動實現聚合鏈路,在使用者層提供teamd命令實現鏈路管理 teamd可以實現以

SpringBoot簡明教程Web檢視層:WebJars靜態資源對映規則

SpringBoot簡明教程之檢視層(一):靜態資源對映規則及WebJars的使用 文章目錄 SpringBoot簡明教程之檢視層(一):靜態資源對映規則及WebJars的使用 專案建立 靜態資源對映規則 靜態資源對映

複習JavaScript基本語法——三種引入方式load、write事件

JavaScript是什麼 JavaScript一種直譯式指令碼語言,是一種動態型別、弱型別、基於原型的語言,內建支援型別。它的直譯器被稱為JavaScript引擎,為瀏覽器的一部分,廣泛用於客戶端的指令碼語言。 JavaScript的作用 JavaScript

MySQL查詢效能優化

為什麼查詢速度會慢 通常來說,查詢的生命週期大致可以按照順序來看:從客戶端,到伺服器,然後在伺服器上進行解析,生成執行計劃,執行,並返回結果給客戶端。其中“執行”可以認為是整個生命週期中最重要的階段,這其中包括了大量為了檢索資料到儲存引擎的呼叫以及呼叫後的資料處理,包括排序、分組等。 在完成這些任務的時候

設計模式(一):單例模式 JVM類載入機制 JDK原始碼學習筆記——Enum列舉使用及原理 Java併發:雙重檢驗鎖定DCL Java併發:Java記憶體模型 Java併發:Java記憶體模型 Java併發:雙重檢驗鎖定DCL JDK原始碼學習筆記——Enum列舉使用及原理

單例模式是一種常用的軟體設計模式,其定義是單例物件的類只能允許一個例項存在。 單例模式一般體現在類宣告中,單例的類負責建立自己的物件,同時確保只有單個物件被建立。這個類提供了一種訪問其唯一的物件的方式,可以直接訪問,不需要例項化該類的物件。 適用場合: 需要頻繁的進行建立和銷燬的物件; 建立物

MySQL基礎命令列操作

                                          &

mysql基礎篇多表查詢

1、前面的 select * from emp limit 5; 2、Null的用法 0和null是不一樣的,null表示空值,沒有值,0表示一個確定的值 其中null不能參與如下運算:<&g

RabbitMQ指南四:路由Routing和直連交換機Direct Exchange

on() call basic play logs ued void emit 依賴 原文:RabbitMQ指南之四:路由(Routing)和直連交換機(Direct Exchange)  在上一章中,我們構建了一個簡單的日誌系統,我們可以把消息廣播給很多的消費者。在本章中

python框架 Tornado 學習筆記

tornado pythontornado 一個簡單的服務器的例子:首先,我們需要安裝 tornado ,安裝比較簡單: pip install tornado 測試安裝是否成功,可以打開python 終端,輸入: import tornado.https