1. 程式人生 > >MySQL5.6 (5.7)資料庫主從(Master/Slave)同步安裝與配置詳解

MySQL5.6 (5.7)資料庫主從(Master/Slave)同步安裝與配置詳解

安裝環境

作業系統 :CentOS 6.5 
資料庫版本:MySQL 5.6.27
主機A192.168.1.1 (Master)
主機B:192.168.1.2 (Slave)

這裡強調的資料庫的版本,是因為MySQL在5.6之前和之後的安裝方式是不一樣的。
本人在進行配置的時候,也遇到了這個坑,這裡提前說明,希望大家不要採坑。

基本環境配置

然後可以在兩臺機子之間進行 ping操作,確保兩臺機器之間能夠相同。

Master的配置

在Linux環境下MySQL的配置檔案的位置是在 /etc/my.cnf ,在該檔案下指定Master的配置如下:

log-bin=mysql-bin
server-id
=2 binlog-ignore-db=information_schema binlog-ignore-db=cluster binlog-ignore-db=mysql binlog-do-db=ufind_db

這裡的server-id用於標識唯一的資料庫,這裡設定為2,在設定從庫的時候就需要設定為其他值。

binlog-ignore-db:表示同步的時候ignore的資料庫
binlog-do-db:指定需要同步的資料庫

完整配置截圖如下:

這裡寫圖片描述

1、然後重啟mysql:service mysqld restart

2、進入mysql:[[email protected]

_221_4_centos ~]# mysql -u root -p 回車,輸入mysql密碼進入。

3、 賦予從庫許可權帳號,允許使用者在主庫上讀取日誌,賦予192.168.1.2也就是Slave機器有File許可權,只賦予Slave機器有File許可權還不行,還要給它REPLICATION SLAVE的許可權才可以。

在Master資料庫命令列中輸入:

 >GRANT FILE ON *.* TO 'root'@'192.168.1.2' IDENTIFIED BY 'mysql password';

 >GRANT REPLICATION SLAVE ON *.* TO 'root'
@'192.168.1.2' IDENTIFIED BY 'mysql password'; >FLUSH PRIVILEGES

這裡使用的仍是 root 使用者作為同步的時候使用到的使用者,可以自己設定。

4、重啟mysql,登入mysql,顯示主庫資訊

mysql> show master status;
mysql> show master status;
+------------------+----------+--------------+----------------------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB                 | Executed_Gtid_Set |
+------------------+----------+--------------+----------------------------------+-------------------+
| mysql-bin.000004 |    28125 | ufind_db     | information_schema,cluster,mysql |                   |
+------------------+----------+--------------+----------------------------------+-------------------+
1 row in set (0.00 sec)

mysql> 

這裡寫圖片描述

這裡的 File 、Position 是在配置Salve的時候要使用到的,Binlog_Do_DB表示要同步的資料庫,Binlog_Ignore_DB 表示Ignore的資料庫,這些都是在配置的時候進行指定的。

另外:如果執行這個步驟始終為Empty set(0.00 sec),那說明前面的my.cnf沒配置對。

Slave的配置

1、從庫的配置,首先也是修改配置檔案:/etc/my.cnf 如下:

log-bin=mysql-bin
server-id=3
binlog-ignore-db=information_schema
binlog-ignore-db=cluster
binlog-ignore-db=mysql
replicate-do-db=ufind_db
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
slave-net-timeout=60

這裡寫圖片描述

2、這裡可以看到,在MySQL5.6之後的版本中沒有指定:

master-host=192.168.1.1 #Master的主機IP
master-user=root
master-password=mysql password #Master的MySQL密碼


3、這也是在網上很多搜尋的配置過程,他們也都指定了資料庫的版本,但是並沒有說出來新版本的配置這種方式是不適用的。

4、如果,你在MySQL5.6和之後的版本中配置從庫的時候,設定到了上邊的內容,即指定了master-host、master-user等資訊的話,重啟MySQL的時候就回報錯,錯誤資訊如下:

[root@VM_128_194_centos bin]# service mysqld restart
Shutting down MySQL... SUCCESS! 
Starting MySQL... ERROR! The server quit without updating PID file (/data/mysqldb/VM_128_194_centos.pid).
[root@VM_128_194_centos bin]# 

此時,檢視資料庫的報錯資訊(資料庫的目錄, /data/mysqldb/VM_128_194_centos.err ),可以看到:

2016-05-06 13:12:04 13345 [Note] InnoDB: Waiting for purge to start
2016-05-06 13:12:04 13345 [Note] InnoDB: 5.6.27 started; log sequence number 2850211
2016-05-06 13:12:04 13345 [ERROR] /data/home/server/mysql-5.6.27/bin/mysqld: unknown variable 'master-host=192.168.1.1'
2016-05-06 13:12:04 13345 [ERROR] Aborting



可以看出master-host 被檢測數是一個未知的變數,因此會出現錯誤。

5、在5.6以及後續版本的配置如下:

修改完/etc/my.cnf 檔案之後,重啟一下MySQL(service mysqld restart)

進入Slave mysql控制檯,執行:

這裡寫圖片描述


mysql> stop slave;  #關閉Slave
mysql> change master to master_host='192.168.1.1',master_user='root',master_password='123456',master_log_file='mysql-bin.000004', master_log_pos=28125;

mysql> start slave;  #開啟Slave


在這裡指定Master的資訊,master_log_file是在配置Master的時候的File選項, master_log_pos是在配置Master的Position 選項,這裡要進行對應。

然後可以通過mysql> show slave status; 檢視配置的資訊:

mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.167.1.1
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 28125
               Relay_Log_File: VM_128_194_centos-relay-bin.000004
                Relay_Log_Pos: 26111
        Relay_Master_Log_File: mysql-bin.000004
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: ufind_db
          Replicate_Ignore_DB: mysql
           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: 28125
              Relay_Log_Space: 26296
              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: 2
                  Master_UUID: 8ac3066a-9680-11e5-a2ec-5254007529fd
             Master_Info_File: /data/mysqldb/master.info
                    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
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
1 row in set (0.00 sec)

ERROR: 
No query specified

mysql> 

可以看到,已經配置成功。

新增需要同步的從庫Slave

由於種種原因,測試的時候使用test庫,這裡我按照上述的方式,修改Master的my.cnf的配置檔案,新增同步的資料庫test,重啟MySQL,執行Master的:show master status如下:

這裡寫圖片描述

相應的,要修改Slave從庫的資訊在my.cnf 增加 replicate-do-db=test,重啟Mysql,根據上述的show master status,在Slave從庫中執行下邊的內容:


>stop slave
>change master to master_host='192.168.1.1',master_user='root',master_password='123456',master_log_file='mysql-bin.000005', master_log_pos=120;
>start slave


然後使用:show slave status;

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.1
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000005
          Read_Master_Log_Pos: 1422
               Relay_Log_File: VM_128_194_centos-relay-bin.000004
                Relay_Log_Pos: 283
        Relay_Master_Log_File: mysql-bin.000005
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: ufind_db,test
          Replicate_Ignore_DB: mysql
           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: 1422
              Relay_Log_Space: 468
              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: 2
                  Master_UUID: 8ac3066a-9680-11e5-a2ec-5254007529fd
             Master_Info_File: /data/mysqldb/master.info
                    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
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
1 row in set (0.00 sec)

ERROR: 
No query specified

mysql> 

已經新增加了test。

真正的測試

在主庫中新增資料庫表,user,觀察從庫變化如下:

建立資料庫的時候:

這裡寫圖片描述

新增資料的時候:

這裡寫圖片描述

刪除Master資料庫表的時候:
這裡寫圖片描述

配置過程,到此為止,希望能夠幫助大家,如有疑問 歡迎留言。

親測5.7配置成功


相關推薦

MySQL5.6 (5.7)資料庫主從Master/Slave同步安裝配置

安裝環境 作業系統 :CentOS 6.5 資料庫版本:MySQL 5.6.27 主機A:192.168.1.1 (Master) 主機B:192.168.1.2 (Slave) 這裡強調的資料庫的版本,是因為MySQL在5.6之前和之後的安裝方式是不一樣的。 本人

MySQL5.6 數據庫主從Master/Slave同步安裝配置

inux bind 主從配置 希望 master 強調 數據庫主從 ria 配置文件 目錄(?)[+] 安裝環境 操作系統 :CentOS 6.5 數據庫版本:MySQL 5.6.27 主機A:192.168.1.1 (Master) 主機B:192.168.

MySQL5.6 資料庫主從Master/Slave同步安裝配置

安裝環境 作業系統 :CentOS 6.5 資料庫版本:MySQL 5.6.27 主機A:192.168.1.1 (Master) 主機B:192.168.1.2 (Slave) 這裡強調的資料庫的版本,是因為MySQL在5.6之前和之後的安裝方式是不

本地Mysql5.7主從Master/Slave安裝,my.ini檔案配置

找到Master mysql的位置 C:\Program Files\MySQL\MySQL Server 5.7 複製資料夾到你所需要的目錄,我是在D:\Mysql separation\MySQL

MySQL 8.0主從Master-Slave配置

MySQL 主從複製的方式有多種,本文主要演示基於基於日誌(binlog)的主從複製方式。 MySQL 主從複製(也稱 A/B 複製) 的原理 Master將資料改變記錄到二進位制日誌(binary log)中,也就是配置檔案log-bin指定的檔案,

mysql master/slave複製原理及配置

1 複製概述       Mysql內建的複製功能是構建大型,高效能應用程式的基礎。將Mysql的資料分佈到多個系統上去,這種分佈的機制,是通過將Mysql的某一臺主機的資料複製到其它主機(slaves)上,並重新執行一遍來實現的。複製過程中一個伺服器充當主伺服器,而一個或

Linux中Nginx安裝配置(CentOS-6.5:nginx-1.5.0)

1 Nginx簡介Nginx ("engine x") 是一個高效能的 HTTP 和 反向代理 伺服器,也是一個 IMAP/POP3/SMTP 代理伺服器。 Nginx 是由 Igor Sysoev 為俄羅斯訪問量第二的 Rambler.ru 站點開發的,第一個公開版本0

CentOS 7下Samba服務安裝配置

chown grep -E add -s login mkdir entos passwd 密碼 centos7.5系統下進行 yum -y install samba samba-client systemctl start smb nmbps -ef | grep -E

win7-MySQL資料庫安裝配置

目錄 一、概述 一、概述   MySQL版本:5.7.17   客戶端工具:NavicatforMySQL  二、MySQL安裝  安裝條件:   如果Windows Server 2003 在安裝.net framework4.0安裝過程中報錯: net framework 4.0安裝時提

win10MySQL資料庫安裝配置

一、概述   MySQL版本:5.6.21   客戶端工具:NavicatforMySQL  二、MySQL安裝  安裝條件:   如果Windows Server 2003 在安裝.net framework4.0安裝過程中報錯: net framework 4

Mysql 5.7.1.0 實現主從複製master-slave

開始前 請 保證兩臺主機的 防火牆、防毒軟體 不會阻止兩臺主機的正常通訊 。下面通過6步操作實現主從複製!

MySQL5.6/ 5.7 SSL配置

專題一:mysql5.7上開啟並配置ssl[[email protected] bin]# ./mysql_ssl_rsa_setup --datadir=/data/mysql_data1/ --user=mysqlGenerating a 2048 bit RS

Linux --- CentOS 7 搭建MySQL5.6資料庫伺服器配置

Centos7將預設資料庫mysql替換成了Mariadb,如果想繼續使用mysql 需要解除安裝Mariadb 再安裝mysql; 1、解除安裝 MariaDB 檢視已安裝的程式 rpm -qa |grep -i mariadb 解除

mysql-5.7.18版本二進制包安裝-自定義安裝路徑

mysql linux lnmp mysql-5.7.18版本(二進制包安裝)-自定義安裝路徑安裝路徑:/application/mysql-5.7.181.前期準備mysql依賴libaioyum install -y libaio創建用戶mysql,以該用戶的身份執行mysqluseradd

Linux下一臺服務器Redis主從復制master-slave配置

conf mas linux -a src fig 客戶 數據操作 ima 主從概念 ?個master可以擁有多個slave,?個slave?可以擁有多個slave,如此下去,形成了強?的多級服務器集群架構 master用來寫數據,slave用來讀數據,經統計:網站的讀寫

實例1: 如何將CentOS 6.5從命令行id:3切換到圖形界面(id:5)

修改 保持 oot 強制 htm html 令行 退出 實例 實例1: 如何將CentOS 6.5從命令行(id:3)切換到圖形界面(id:5) 1.CentOS 系統要求,至少512MB.2.使用VI 編輯命令修改文件 /etc/inittab 文件的 id:3:init

Redis實現主從複製Master&Slave

         由於前段時間公司專案比較趕,一直抽不出時間寫部落格,今天偷空寫一篇吧。前面給大家講解了單機版redis的基本操作,現在繼續給大家講解一下Redis的進階部分,主從複製和讀寫分離。 一、Master&Slave是什麼?          也就是我們所

5.6~5.15 雙目運算子指標運算

返回目錄5.7 + -對於涉及指標的算術運算,標準中有比較嚴格的限制條件:參與運算的指標必須指向陣列物件中的元素,或者指向陣列物件最後一個元素的下一個元素(這是STL中能夠使用distance(v.begin(),v.end())計算元素個數的前提條件)。(注:對於指向普通物

CentOS安裝Python3.6.5的流程指導多坑

為了給centos伺服器的pyspark升級成python3.6的,安裝anaconda之後輸入python已經自動使用了anaconda自帶的python版本,但是如果嘗試用 ln -s /root/anaconda3/lib/python3.6 /usr/bin/pyt

redis基礎簡介- 主從複製master & slave

簡介 redis主從複製配置和使用都非常的簡單。通過主從複製可以允許多個 slave 擁有和 master 相同的資料庫副本。 redis主從複製的特點: master可以擁有多個slave 多個 slave 除了可以連線同一個master之外,還可以連