MySQL-主從復制



前言

前篇說了作為運維在數據庫塊最起碼要會兩大技能,今天來說說第二技能--主從復制

隨着業務的增長,一台數據庫服務器以滿足不了需求了,負載過重,這時候就需要減壓,實現負載均衡讀寫分離,一主一從或一主多從

主服務器只管寫,從服務器管讀,從而提高效率減輕壓力。

主從復制分類:

主從同步:當用戶寫數據主服務器必須和從服務器同步一致了才告訴用戶寫入成功,等待時間太長

主從異步:只要用戶訪問寫數據主服務器寫入立馬返回給用戶成功

主從半步同步:當用戶訪問寫數據主服務器寫入並同步其中一個從服務器就返回給用戶成功

備注:通常都是使用的主從異步,根據環境需求來選擇,想要數據更安全選擇半步同步

主從復制注意事項

注意:selinux策略、防火牆

1、開啟二進制日志

log_bin

2、設置二進制記錄格式為ROW(推薦)

3、設置唯一server-id

server_id=#

4、設置datadir中日志名稱(可選)

log-basename=master

5、創建有復制權限的用戶賬號

GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'HOST' IDENTIFIED BY 'replpass';

6、如果要啟用級聯復制,需要在從服務器啟用

log_bin
log_slave_updates

7、限制從服務器為只讀

read_only=ON

8、禁止主機名解析

skip_name_resolve = on

9、高可用從服務器要加上

relay_log_purge=0 #不清除中繼日志

10、如何保證主從復制的事務安全(根據需求添加)

1)在master節點啟用參數:

sync_binlog=1
# 每次寫后立即同步二進制日志到磁盤,性能差

2)如果用到的為InnoDB存儲引擎:

innodb_flush_log_at_trx_commit=1 
# 每次事務提交立即同步日志寫磁盤
innodb_support_xa=ON 
# 默認值,分布式事務MariaDB10.3.0廢除
sync_master_info=# 
# #次事件后master.info同步到磁盤

3)在slave節點啟用參數:

sync_relay_log=# 
# #次寫后同步relay log到磁盤
sync_relay_log_info=#
# #次事務后同步relay-log.info到磁盤

主從復制原理

如圖

clipboard

備注:

主從同步有延遲,為什么?因為它時是單線程傳送日志

實戰-實現主從復制+高可用

准備工作

准備4台主機67、17、37、57,分別充當角色為管理主機、主服務器、2個從服務器

安裝包:和主服務器同版本的mysql數據庫包、和高可用安裝包

mha4mysql-manager

mha4mysql-node

比如主服務器已經運行了1年了,發現滿足不了需求了,需要搭建從服務器,我們先從搭建從服務器開始

備注:首先確保主服務器開啟了二進制日志

確認二進制文件已啟動

MariaDB [(none)]> show variables like '%log_bin%'

確認server-id

MariaDB [(none)]> show variables like 'server%';

主服務器設置

1、創建可用於復制的賬號

grant replication slave on *.* to repluser@'192.168.43.%' identified by 'centos';

2、完全備份數據(用於在從服務器上來還原)

mysqldump -pcentos -A -F --single-transaction --master-data=1 >/data/all-`date +%F`.sql

備注:備份時的某表的狀態

image

為了下面的實驗測試在這里我們備份玩再增加一條記錄

insert hellodb.students (name,age)values('gaoda001',20);

image

3、傳送到從服務器37、57上

scp…

37、57(從)服務器設置

1、配置文件
vim /etc/my.cnf

1)開啟二進制日志

log-bin=mysql-bin

2)設置server-id(要和主服務id區分開)

server-id=2

備注:57設置為3 只要他們不相同就可

3)從服務器設置為只讀

read_only=on

4)禁止主機名解析

skip_name_resolve = on

5)數據和索引分開存放

innodb_file_per_table = on 
# 10版本以上默認開啟

6)重啟或啟動服務

備注:以防萬一可以查看下最重要的兩項開啟了沒有

mysql -e "show variables like 'log%'"
mysql -e "show variables like 'server%'"

2、實現復制

1)進入數據庫查找同步代碼

MariaDB [(none)]> help change
MariaDB [(none)]> help change master to 

image

2)根據情況編輯上圖上的信息

注:起始位置可以在完全備份文件里查看

兩種方式:

① 先還有完全備份數據再拿以上信息編輯下,在數據庫中執行

② 直接把上面的信息添加到完全備份文件里

下面用第二種方式來操作如圖

image

3)37主機還原並查看

mysql < all-2018-08-08.sql

image

備注:以還原到備份時的狀態

           主服務器備份完有用戶新加數據對吧,下面開啟主從復制看看能不能復制過來最新的數據

4)查看復制狀態

MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.43.17
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000005  
          Read_Master_Log_Pos: 385       #從主服務器讀取到的位置
               Relay_Log_File: centos7_05-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000005
             Slave_IO_Running: No       #表示還沒開啟
            Slave_SQL_Running: No       #表示還沒開啟
              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: 385
              Relay_Log_Space: 256
              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: NULL   ##注意這個 表示同步時間差
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: 0
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
                   Using_Gtid: No
                  Gtid_IO_Pos: 
      Replicate_Do_Domain_Ids: 
  Replicate_Ignore_Domain_Ids: 
                Parallel_Mode: conservative
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: 
1 row in set (0.03 sec)

5)開啟主從復制並查看狀態

start slave;
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.43.17
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000005
          Read_Master_Log_Pos: 608
               Relay_Log_File: centos7_05-relay-bin.000003
                Relay_Log_Pos: 778
        Relay_Master_Log_File: mysql-bin.000005
             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: 608
              Relay_Log_Space: 1092
              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: 1
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
                   Using_Gtid: No
                  Gtid_IO_Pos: 
      Replicate_Do_Domain_Ids: 
  Replicate_Ignore_Domain_Ids: 
                Parallel_Mode: conservative
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
1 row in set (0.00 sec)

6)查看表確認有沒有從主服務器同步最新的數據

image

7)57(從服務器同樣的設置)

備注:目前現實了異步同步復制,下面來看半同步設置

半同步設置

主服務器設置

1、首先在主服務器上安裝插件

INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';

備注:

show plugins; ##查看當前系統中的插件列表

UNINSTALL PLUGIN rpl_semi_sync_master;##卸載插件

2、查看插件狀態

SHOW GLOBAL VARIABLES LIKE '%semi%';

image

3、開啟

set global rpl_semi_sync_master_enabled=on;

備注:最好寫在配置文件中

image

4、查看插件變量狀態

SHOW GLOBAL STATUS LIKE '%semi%';

image

備注:這里記錄有幾個半同步主機

從服務器設置

1、安裝插件

INSTALL PLUGIN rpl_semi_sync_slave SONAME  'semisync_slave.so';

2、啟動插件

set global rpl_semi_sync_slave_enabled=on;

備注:同樣建議寫在配置文件中

image

3、從服務器查看改插件是否開始工作

SHOW GLOBAL STATUS LIKE '%semi%';

image

為什么沒有開始工作?

因為是先開啟的主從復制再安裝的次插件

所以這種情況下,先停止從服務器的主從復制功能

1)停止:

stop slave;

2)再次開啟主從復制

start slave;

3)再去查看

image

備注:現在就啟用了半同步功能,下面開始搭建高可用,實現主服務器宕機自動提升從服務器當主

MHA高性能

備注:四台主機之間必須是ssh基於key驗證登陸,所以要先實現ssh互相鏈接

步驟:

ssh-keygen

cd .ssh

ssh-copy-id 192.168.43.67

把.ssh目錄考到其他主機

1、在主服務器上創建管理者賬號

grant all on *.* to mhauser@'192.168.43.%' identified by 'centos';

2、在67管理主機上安裝以下包

mha4mysql-manager 
mha4mysql-node

3、在17、37、57安裝如下包

mha4mysql-node

4、在管理主機創建管理節點

mkdir /etc/mastermha/
cd /etc/mastermha/
vim app1.cnf

根據上面創建的信息填寫

[server default]
user=mhauser
password=centos
manager_workdir=/app/mastermha/app1/
manager_log=/app/mastermha/app1/manager.log
remote_workdir=/app/mastermha/app1/
ssh_user=root
repl_user=repluser
repl_password=centos
ping_interval=1
##以上是全局設置

##以下是針對某一組集群設置
[server1]
hostname=192.168.43.17
candidate_master=1
[server2]
hostname=192.168.43.37
candidate_master=1
[server3]
hostname=192.168.43.57
candidate_master=1

備注:

candidate_master=1:是說主服務器宕機了帶有這項的主機有機會當主

5、測試

1)ssh協議

/usr/bin/masterha_check_ssh --conf=/etc/mastermha/app1.cnf


2、檢查復制

masterha_check_repl --conf=/etc/mastermha/app1.cnf

3、啟動(默認前台執行)

masterha_manager --conf=/etc/mastermha/app1.cnf

image

備注:如果你是編譯安裝在/etc/mastermha/app1.cnf配置文件中指定日志路徑,最好主從服務器數據庫設置的位置都是一致的。

image


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM