Mysql/Mariadb主從複製

来源:https://www.cnblogs.com/kuiyajia/archive/2019/11/12/11843722.html
-Advertisement-
Play Games

概念 什麼是·Mysql/Mariadb主從複製? Mysql/Mariadb主從複製:當Master(主)資料庫發生變化的時候,變化實時會同步到slave(從)資料庫中; 類似於:Samba共用文件(C/S)、NFS網路文件共用(C/S),當服務端(Server)發生變化時,客戶端(client) ...


概念

什麼是·Mysql/Mariadb主從複製?    

Mysql/Mariadb主從複製:當Master(主)資料庫發生變化的時候,變化實時會同步到slave(從)資料庫中;
類似於:Samba共用文件(C/S)、NFS網路文件共用(C/S),當服務端(Server)發生變化時,客戶端(client)數據內容會根據服務端進行改變;

好處

  • 水平擴展資料庫的負載能力,後備資料庫,主資料庫伺服器故障後,可切換到從資料庫繼續工作;
  • 容錯、高可用,從資料庫可用來做備份、數據統計等工作,這樣不影響主資料庫的性能;
  • 數據分佈;
  • 數據備份;

實現原理

在master機器上,主從同步事件會被寫到特殊的log文件中(binary-log);

主從同步事件有3種形式:statement、row、mixed。

 statement:會將對資料庫操作的sql語句寫入到binlog中。
 row:會將每一條數據的變化寫入到binlog中。
 mixed:statement與row的混合。Mysql決定什麼時候寫statement格式的,什麼時候寫row格式的binlog。

整體上來說,複製有3個步驟:

  • master將改變記錄到二進位日誌(binary log)中(這些記錄叫做二進位日誌事件,binary log events);
  • slave將master的binary log events拷貝到它的中繼日誌(relay log);
  • slave重做中繼日誌中的事件,將改變反映它自己的數據。

下麵這章圖可以詳細解釋其原理:

主從複製過程

說的簡單一些就是:

當對Master資料庫不管做了增、刪、改還是創建了資料庫、表等操作時,Slave就會快速的接受這些數據以及對象的操作,從而實現主從數據複製,保證數據的一致性。  

實戰

我記得我學PHP開發的時候,教員經常說的一句話就是:學習半小時,實戰一分鐘;
好了,接下來到我們實戰的時刻了,認真聽講喲!!! 

環境介紹

系統環境:系統基本上都差不多,一般多數都是Linux平臺和Windows平臺比較多,不管什麼樣的系統環境對這次實戰的操作都影響不大,我在這裡使用的是Docker虛擬出來的CentOS操作系統,當然您可以選用Ubuntu、RedHat以及Windows系統,這些都不會影響到大的操作;

我這裡使用的系統版本:

[root@master /]# cat /etc/redhat-release
CentOS Linux release 8.0.1905 (Core) 

這裡會用到兩台伺服器:其中一臺MasterIP172.18.0.2,另外一個slaveIP172.18.0.3
資料庫版本:(我這裡使用的Mariadb,你可以使用mysql資料庫)

[root@master /]# mysql --version
mysql  Ver 15.1 Distrib 10.3.11-MariaDB, for Linux (x86_64) using readline 5.1

配置Master資料庫

  1.更改Master配置文件
    找到下麵文件:

mysql資料庫:/etc/mysql/mysql,conf.d/mysqld.cnf
mariadb資料庫:/etc/my.cnf.d/mariadb.cnf

註意:我這裡是使用yum進行安裝的所以預設配置文件是在/etc下麵,建議在修改上面兩個文件時要先將配置文件進行備份 

修改以下配置:  

bind-address=172.18.0.2		\\指定Master地址
server-id = 1					\\指定唯一的serverid		部分版本沒有需手動寫入
log_bin = /var/log/mariadb/mariadb-bin.log				\\開啟binlog			部分版本沒有需手動寫入

註意:log_bin這個欄位需根據實際情況來定,需找到資料庫的日誌文件,預設實在 /var/log

   2.重新啟動資料庫

[root@master my.cnf.d]# systemctl restart mariadb		\\centos7、centos8、ubuntu重新啟動方式
[root@master my.cnf.d]# server  mariadb  restart		\\centos6及以下版本使用這個重新啟動方式

  mysql重新啟動: 

[root@master my.cnf.d]# systemctl restart mysqld		\\centos7、centos8、ubuntu重新啟動方式
[root@master my.cnf.d]# server  mysqld  restart		\\centos6及以下版本使用這個重新啟動方式

  3.初始化資料庫

[root@master my.cnf.d]# mysql_secure_installation 

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 					//這裡敲回車
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y						//這裡是設置root密碼,可不進行設置
New password: 					//新密碼
Re-enter new password: 			//舊密碼
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!
[root@master my.cnf.d]# 

  4.創建主從同步的用戶 

 [root@master ~]# mysql -u root -p				\\登陸資料庫
Enter password: 			\\輸入root密碼
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 18
Server version: 10.3.11-MariaDB-log MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> GRANT REPLICATION SLAVE on *.* to 'slave'@'%' IDENTIFIED BY 'redhat';		
\\創建用戶,並設置相應的許可權
	\\此處%表示允許從任何地方(除本地外)使用此賬號進行登陸使用,在正式環境建議具體到某台主機IP
Query OK, 0 rows affected (0.000 sec)			\\表示sql語句執行成功

  5.更新Slave用戶許可權 

 MariaDB [(none)]> flush privileges;		\\每次修改用戶許可權,都要使用這個sql語句進行更新
Query OK, 0 rows affected (0.000 sec)

  6.導出資料庫中所有數據(此步驟取決於slave的許可權)

[root@master ~]# mysqldump -u root -p --all-databases --master-data > mariadb.bat.sql
	--all-databases		\\此參數表示備份所有資料庫
	--master-data		\\此參數表示將二進位的信息寫入到輸出文件中,在這裡是寫入到備份的sql文件中
Enter password:

  7.查看MASTERr REPLICATION LOG位置

 MariaDB [(none)]> show master status;
+--------------------+----------+--------------+------------------+
| File               | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| mariadb-bin.000002 |     1974 |              |                  |
+--------------------+----------+--------------+------------------+
1 row in set (0.000 sec)

配置Slave資料庫

  1.更改Slave配置文件

   文件位置與Master位置一致

mysql資料庫:/etc/mysql/mysql,conf.d/mysqld.cnf
mariadb資料庫:/etc/my.cnf.d/mariadb.cnf

   註意:我這裡是使用yum進行安裝的所以預設配置文件是在/etc下麵,建議在修改上面兩個文件時要先將配置文件進行備份

  修改以下配置:

bind-address=172.18.0.3		\\指定Master地址
server-id = 2					\\指定唯一的serverid		部分版本沒有需手動寫入
log_bin = /var/log/mariadb/mariadb-bin.log				\\開啟binlog			部分版本沒有需手動寫入

  註意:log_bin這個欄位需根據實際情況來定,需找到資料庫的日誌文件,預設實在 /var/log

  2.重新啟動資料庫

[root@master my.cnf.d]# systemctl restart mariadb		\\centos7、centos8、ubuntu重新啟動方式
[root@master my.cnf.d]# server  mariadb  restart		\\centos6及以下版本使用這個重新啟動方式

  mysql重新啟動:

[root@master my.cnf.d]# systemctl restart mysqld		\\centos7、centos8、ubuntu重新啟動方式
[root@master my.cnf.d]# server  mysqld  restart		\\centos6及以下版本使用這個重新啟動方式

  3.初始化資料庫

[root@master my.cnf.d]# mysql_secure_installation 

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 					//這裡敲回車
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y						//這裡是設置root密碼,可不進行設置
New password: 					//新密碼
Re-enter new password: 			//舊密碼
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!
[root@master my.cnf.d]# 

  4.從Master將資料庫備份複製到slave伺服器

[root@slave my.cnf.d]# scp [email protected]:/opt/mariadb.bat.sql /opt/
[email protected]'s password: 
mariadb.bat.sql                   

  5.將備份數據恢復到slave資料庫

[root@slave my.cnf.d]# mysql -u root -p < /opt/mariadb.bat.sql 
 Enter password: 

  6.使slave與master建立連接

[root@slave my.cnf.d]# mysql -u root -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@slave my.cnf.d]# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 22
Server version: 10.3.11-MariaDB-log MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> stop slave;
Query OK, 0 rows affected, 1 warning (0.000 sec)

MariaDB [(none)]> CHANGE MASTER TO
    -> MASTER_HOST = '172.18.0.2',		\\指定Master資料庫地址
    -> MASTER_USER = 'slave',			\\指定主從複製用戶名
    -> MASTER_PASSWORD = 'redhat',			\\指定主從複製用戶密碼
    -> MASTER_LOG_FILE = 'mariadb-bin.000002',		\\指定Master資料庫的binlog文件名
    -> MASTER_LOG_POS=1974;			
Query OK, 0 rows affected (0.290 sec)

MariaDB [(none)]> start slave;			\\開啟複製功能
Query OK, 0 rows affected (0.019 sec)

MariaDB [(none)]>

  註意:lMASTER_LOG_FILE='mariadb-bin.000002與MASTER_LOG_POS=1974的值,是從上面的 SHOW MASTER STATUS 得到的。

  到這裡已經可以做到主從複製了下麵讓我們測試一下吧

驗證資料庫是否同步

測試方法很簡單,只需要在主資料庫上面創建資料庫或者增加一條記錄就可以測試是否主從複製配置成功

MariaDB [(none)]> show  databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.018 sec)

MariaDB [(none)]> create database a;			\\在主資料庫創建a資料庫
Query OK, 1 row affected (0.063 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| a                  |
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.000 sec)

  下麵我們來看看從資料庫上面有沒有a這個資料庫吧

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| a                  |
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.075 sec)

  我們會發現已經有了a這個資料庫


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • ⽹絡時間服務和chrony 實驗練習: 1. 準備實驗環境: 2. 時間同步(centos6) 3. ntp軟體實現時間同步(centos6) centos6上預設安裝了ntp軟體包(包括客戶端和伺服器端),但是ntp同步需要⼀定時間才能完全同步時間的,⽽chrony同步時間⽐ntp快。centos ...
  • 故事背景:我們公司是做新零售的,需要對發佈的每台機器進行文件的同步更新,所以我這裡做了一個小小的調研 技術調研:linux之間同步文件有兩種方式rsync與scp。 sync和scp在文件夾均不存在時,執行時間相差不大,但是文件夾存在的情況下差異很大。原因是scp是複製:若mas2文件不存在則新建, ...
  • 環境:centos7 nginx1.16.1(源碼安裝) 一、下載nginx源碼包 地址:http://nginx.org/en/download.html Mainline version(主線版本)Stable version(穩定版本)Legacy versions(傳統老版本) 下載穩定版: ...
  • xcrun: error 在終端輸入 git clone *****後,提示: xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing*****,解決方法,直接在終端輸入以下 ...
  • 括弧的種類 小括弧,圓括弧 ( ) 中括弧,方括弧 [ ] 大括弧、花括弧 { } 一、單小括弧 () 1.另開命令組——小括弧中的命令將會新開啟一個子shell獨立順序運行,所以括弧中的變數不能夠被腳本餘下的部分使用。括弧中多個命令之間用分號隔開,最後一個命令不需要分號,各命令和括弧之間無空格。 ...
  • chrony軟體使用說明 chrony簡介 chrony是一個開源的自由軟體,它能保持系統時鐘與時間伺服器(ntp)同步,讓時間保持精確。 它由兩個程式組成:chrongd和chronyc。 chronyd是一個後臺運行的守護進程,用於調整內核運行的系統時鐘和時間伺服器同步。 它確定電腦增減時間的 ...
  • Flink-測試用的fake溫度感測器 Flink中,測試時,會用到自定義的source。 下為一例。。 該例使用溫度感測器的格式演示fake日誌數據源。 代碼用Scala寫的。 感測器... 感測器 - 樣例類 SensorReads.scala: ​x 1 package sr 2 ​ 3 /* ...
  • 整理記錄關於使用cmd安裝mysql的過程 1.配置環境變數 1) 電腦->屬性->高級系統設置->環境變數 2)先添加變數 變數名:MYSQL_HOME 變數值:D:\mysql-8.0.13-winx64\bin (mysql存放地址) 3)然後配置Path系統變數 在系統變數里,找到Path ...
一周排行
    -Advertisement-
    Play Games
  • 基於.NET Framework 4.8 開發的深度學習模型部署測試平臺,提供了YOLO框架的主流系列模型,包括YOLOv8~v9,以及其系列下的Det、Seg、Pose、Obb、Cls等應用場景,同時支持圖像與視頻檢測。模型部署引擎使用的是OpenVINO™、TensorRT、ONNX runti... ...
  • 十年沉澱,重啟開發之路 十年前,我沉浸在開發的海洋中,每日與代碼為伍,與演算法共舞。那時的我,滿懷激情,對技術的追求近乎狂熱。然而,隨著歲月的流逝,生活的忙碌逐漸占據了我的大部分時間,讓我無暇顧及技術的沉澱與積累。 十年間,我經歷了職業生涯的起伏和變遷。從初出茅廬的菜鳥到逐漸嶄露頭角的開發者,我見證了 ...
  • C# 是一種簡單、現代、面向對象和類型安全的編程語言。.NET 是由 Microsoft 創建的開發平臺,平臺包含了語言規範、工具、運行,支持開發各種應用,如Web、移動、桌面等。.NET框架有多個實現,如.NET Framework、.NET Core(及後續的.NET 5+版本),以及社區版本M... ...
  • 前言 本文介紹瞭如何使用三菱提供的MX Component插件實現對三菱PLC軟元件數據的讀寫,記錄了使用電腦模擬,模擬PLC,直至完成測試的詳細流程,並重點介紹了在這個過程中的易錯點,供參考。 用到的軟體: 1. PLC開發編程環境GX Works2,GX Works2下載鏈接 https:// ...
  • 前言 整理這個官方翻譯的系列,原因是網上大部分的 tomcat 版本比較舊,此版本為 v11 最新的版本。 開源項目 從零手寫實現 tomcat minicat 別稱【嗅虎】心有猛虎,輕嗅薔薇。 系列文章 web server apache tomcat11-01-官方文檔入門介紹 web serv ...
  • 1、jQuery介紹 jQuery是什麼 jQuery是一個快速、簡潔的JavaScript框架,是繼Prototype之後又一個優秀的JavaScript代碼庫(或JavaScript框架)。jQuery設計的宗旨是“write Less,Do More”,即倡導寫更少的代碼,做更多的事情。它封裝 ...
  • 前言 之前的文章把js引擎(aardio封裝庫) 微軟開源的js引擎(ChakraCore))寫好了,這篇文章整點js代碼來測一下bug。測試網站:https://fanyi.youdao.com/index.html#/ 逆向思路 逆向思路可以看有道翻譯js逆向(MD5加密,AES加密)附完整源碼 ...
  • 引言 現代的操作系統(Windows,Linux,Mac OS)等都可以同時打開多個軟體(任務),這些軟體在我們的感知上是同時運行的,例如我們可以一邊瀏覽網頁,一邊聽音樂。而CPU執行代碼同一時間只能執行一條,但即使我們的電腦是單核CPU也可以同時運行多個任務,如下圖所示,這是因為我們的 CPU 的 ...
  • 掌握使用Python進行文本英文統計的基本方法,並瞭解如何進一步優化和擴展這些方法,以應對更複雜的文本分析任務。 ...
  • 背景 Redis多數據源常見的場景: 分區數據處理:當數據量增長時,單個Redis實例可能無法處理所有的數據。通過使用多個Redis數據源,可以將數據分區存儲在不同的實例中,使得數據處理更加高效。 多租戶應用程式:對於多租戶應用程式,每個租戶可以擁有自己的Redis數據源,以確保數據隔離和安全性。 ...