MySQL 5.7主從復制實戰篇


               MySQL 5.7主從復制實戰篇

                                     作者:尹正傑

版權聲明:原創作品,謝絕轉載!否則將追究法律責任。

 

 

 

一.安裝MySQL數據庫並啟動

1>.在MySQL官方下載相應的安裝包(https://dev.mysql.com/downloads/mysql/)

2>.解壓MySQL壓縮文件至安裝目錄

[root@node106.yinzhengjie.org.cn ~]# ll
total 629752
-rw-r--r-- 1 root root 644862820 Jun 10 15:18 mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# tar -zxf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz -C /yinzhengjie/softwares/
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# ln -s /yinzhengjie/softwares/mysql-5.7.25-linux-glibc2.12-x86_64 /yinzhengjie/softwares/mysql
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# mkdir /yinzhengjie/softwares/mysql/data
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/mysql
lrwxrwxrwx 1 root root 58 Jun 10 16:58 /yinzhengjie/softwares/mysql -> /yinzhengjie/softwares/mysql-5.7.25-linux-glibc2.12-x86_64
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/mysql/
total 36
drwxr-xr-x  2 root root   4096 Jun 10 16:57 bin
-rw-r--r--  1 7161 31415 17987 Dec 21 18:39 COPYING
drwxr-xr-x  2 root root      6 Jun 10 16:58 data
drwxr-xr-x  2 root root     55 Jun 10 16:57 docs
drwxr-xr-x  3 root root   4096 Jun 10 16:57 include
drwxr-xr-x  5 root root    230 Jun 10 16:57 lib
drwxr-xr-x  4 root root     30 Jun 10 16:57 man
-rw-r--r--  1 7161 31415  2478 Dec 21 18:39 README
drwxr-xr-x 28 root root   4096 Jun 10 16:57 share
drwxr-xr-x  2 root root     90 Jun 10 16:57 support-files
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# vi ~/.bash_profile 
[root@node106.yinzhengjie.org.cn ~]#
[root@node106.yinzhengjie.org.cn ~]# tail -2 ~/.bash_profile 
PATH=$PATH:$HOME/bin:/yinzhengjie/softwares/mysql/bin/
export PATH
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# source ~/.bash_profile 
[root@node106.yinzhengjie.org.cn ~]#

3>.創建mysql用戶,並將解壓的MySQL程序文件授權給mysql用戶。

[root@node106.yinzhengjie.org.cn ~]# useradd -s /sbin/nologin mysql
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# id mysql
uid=1001(mysql) gid=1001(mysql) groups=1001(mysql)
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# chown mysql:mysql -R /yinzhengjie/softwares/mysql-5.7.25-linux-glibc2.12-x86_64
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# chown mysql:mysql -R /yinzhengjie/softwares/mysql
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# ll -d /yinzhengjie/softwares/mysql
lrwxrwxrwx 1 mysql mysql 58 Jun 10 16:58 /yinzhengjie/softwares/mysql -> /yinzhengjie/softwares/mysql-5.7.25-linux-glibc2.12-x86_64
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# ll -d /yinzhengjie/softwares/mysql-5.7.25-linux-glibc2.12-x86_64/
drwxr-xr-x 10 mysql mysql 141 Jun 10 16:58 /yinzhengjie/softwares/mysql-5.7.25-linux-glibc2.12-x86_64/
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]#

4>.初始化MySQL數據庫

[root@node107.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/mysql/data/
total 0
[root@node107.yinzhengjie.org.cn ~]#
[root@node106.yinzhengjie.org.cn ~]# mysqld --initialize --user=mysql --basedir=/yinzhengjie/softwares/mysql --datadir=/yinzhengjie/softwares/mysql/data &
[1] 18800
[root@node106.yinzhengjie.org.cn ~]# 2019-06-10T09:16:38.425725Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-06-10T09:16:39.122750Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-06-10T09:16:39.235538Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-06-10T09:16:39.304637Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 74227047-8b60-11e9-8cba-000c29985293.
2019-06-10T09:16:39.306459Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-06-10T09:16:39.312634Z 1 [Note] A temporary password is generated for root@localhost: .wIyMa&xf8BL      #注意,這里標紅的為MySQL服務器的臨時密碼,建議大家先保存一下,一會登陸數據庫時要用到,如果你忘記了臨時密碼重復該步驟即可。

[1]+  Done                    mysqld --initialize --user=mysql --basedir=/yinzhengjie/softwares/mysql --datadir=/yinzhengjie/softwares/mysql/data
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/mysql/data/                        #很顯然,一旦我們初始化成功后,我們就會發現該目錄下是有初始化數據生成的
total 110636
-rw-r----- 1 mysql mysql       56 Jun 10 17:16 auto.cnf
-rw-r----- 1 mysql mysql      419 Jun 10 17:16 ib_buffer_pool
-rw-r----- 1 mysql mysql 12582912 Jun 10 17:16 ibdata1
-rw-r----- 1 mysql mysql 50331648 Jun 10 17:16 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 Jun 10 17:16 ib_logfile1
drwxr-x--- 2 mysql mysql     4096 Jun 10 17:16 mysql
drwxr-x--- 2 mysql mysql     8192 Jun 10 17:16 performance_schema
drwxr-x--- 2 mysql mysql     8192 Jun 10 17:16 sys
-rw-r----- 1 mysql mysql      177 Jun 10 17:16 yinzhengjie-mysql-bin.000001
-rw-r----- 1 mysql mysql       31 Jun 10 17:16 yinzhengjie-mysql-bin.index
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# 

5>.修改MySQL的配置文件

[root@node106.yinzhengjie.org.cn ~]# cat /etc/my.cnf
[mysqld]
basedir=/yinzhengjie/softwares/mysql/              
datadir=/yinzhengjie/softwares/mysql/data/
log-bin=yinzhengjie-mysql-bin
server-id=106
[root@node106.yinzhengjie.org.cn ~]# 

配置參數說明如下:
basedir
  該參數指定了安裝 MySQL 的安裝路徑,填寫全路徑可以解決相對路徑所造成的問題。

datadir   該參數指定了 MySQL 的數據庫文件放在什么路徑下。數據庫文件即我們常說的 MySQL data 文件。
log
-bin   該參數只要配置就表示開啟了MySQL的bin log日志功能,注意改參數的值是我們自定義的,我們自定義的值將作為bin log的名稱的前綴信息喲,我們可以使用MySQL命令"show variables like '%log_bin%';"查看咱們的配置。
server
-id   該參數可以指定數據庫服務器的唯一標識。在同一個復制組下的所有實例的server_id都必須是唯一的,而且取值必須是正整數,取值范圍是1~(232)−1

6>.啟動MySQL服務

[root@node106.yinzhengjie.org.cn ~]# cp /yinzhengjie/softwares/mysql/support-files/mysql.server /etc/init.d/        #拷貝MySQL的啟動腳本到系統自啟動路徑下
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# /etc/init.d/mysql.server start
Starting MySQL.Logging to '/yinzhengjie/softwares/mysql/data/node106.yinzhengjie.org.cn.err'.
 SUCCESS! 
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# /etc/init.d/mysql.server status
 SUCCESS! MySQL running (18997)
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# ps -ef | grep mysql
root      18861      1  0 17:20 pts/0    00:00:00 /bin/sh /yinzhengjie/softwares/mysql//bin/mysqld_safe --datadir=/yinzhengjie/softwares/mysql/data/ --pid-file=/yinzhengjie/softwares/mysql/data//node106.yinzhengjie.org.cn.pid
mysql     18997  18861  4 17:20 pts/0    00:00:00 /yinzhengjie/softwares/mysql/bin/mysqld --basedir=/yinzhengjie/softwares/mysql/ --datadir=/yinzhengjie/softwares/mysql/data --plugin-dir=/yinzhengjie/softwares/mysql//lib/plugin --user=mysql --log-error=node106.yinzhengjie.org.cn.err --pid-file=/yinzhengjie/softwares/mysql/data//node106.yinzhengjie.org.cn.pid
root      19047  18609  0 17:20 pts/0    00:00:00 grep --color=auto mysql
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# 

7>.登錄MySQL數據庫並初始化管理員密碼

[root@node106.yinzhengjie.org.cn ~]# mysql -u root -p
Enter password:                                 #注意,這里使用的密碼是我們在第4步驟初始化數據生成的臨時密碼。
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.25-log

Copyright (c) 2000, 2019, 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> 
mysql> alter user user() identified by 'yinzhengjie';          #由於臨時密碼不方便記憶,我這里改成比較好記的字符串。
Query OK, 0 rows affected (0.01 sec)

mysql> 
mysql> quit
Bye
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# mysql -u root -pyinzhengjie    #我們使用剛剛修改后的密碼登陸數據庫
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.25-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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%';                  #查看當前服務器的server_id +----------------+-------+
| Variable_name | Value |
+----------------+-------+
| server_id    | 106 |
| server_id_bits | 32 |
+----------------+-------+
2 rows in set (0.01 sec)

mysql>
mysql> quit
Bye
[root@node106.yinzhengjie.org.cn ~]# 

8>.在另一外一台服務器上重復上述操作(我們實驗環境用node107.yinzhengjie.org.cn作備用的MySQL數據庫)

 

二. MySQL復制概述

1>.MySQL復制介紹

  (1)MySQL復制允許將主實例(master)上的數據同步到一個或多個從實例(slave)上,默認情況下復制是異步進行的,從庫也不需要一直連接主庫來同步數據。
  (
2)MySQL復制的數據粒度可以是主實例上所有的數據庫,也可以是指定的一個或多個數據庫,也可以是一個數據庫里的指定的表。
  (
3)MySQL復制帶來的優勢在於:       擴展能力:       通過復制可以將MySQL的性分到一個或多個slave上。這要求所有的寫操作和修改操作都必須在Master上完成,而讀操作可以被分配到一個或多個salve上。將讀寫分離到不同服務執行之后,MySQL的讀寫性能得到提升。       數據庫備份:         由於從實例時同步主實例的數據,所以可以將備份作業部署到從庫。       數據分析和報表:         同樣,一些數據分析和報表的實現可以在從實例執行,以減少對主庫的性能影響。       容災能力:         可以在物理距離較遠的另一個數據建立slave,保證在主實例所在地區遭遇災難時,在另一個數據中心能快速恢復。

2>.MySQL復制有兩種方法

  (1)傳統方式
      基於主庫的bin-log將日志事件和事件位置復制到從庫,從庫再加以應用來達到主從同步的目的。
  
(2)Gtid方式       global transaction identitifiers 是基於事物來復制數據,因此也就不依賴日志文件,同時又能更好的保證主從庫數據一致性。

3>.MySQL復制有多種類型

1)異步復制
    一個主庫,一個或多個從庫,數據異步同步到從庫。

(2)同步復制
    在MySQL cluster中特有的復制方式。

(3)半同步復制
    在異步復制的基礎上,確保任何一個主庫上的事物在提交之前至少有一個從庫已經收到該事物並日志記錄下來。

(4)延遲復制
    在異步復制的基礎上,人為設定主庫和從庫的數據同步延遲時間,即保證數據延遲至少是這個參數。

4>.MySQL的復制原理

 如上圖所示,MySQL復制的原理大致總結如下:
    (1)在Slave 服務器上執行sart slave命令開啟主從復制開關,開始進行主從復制。
    (2)此時,Slave服務器的IO線程會通過在master上已經授權的復制用戶權限請求連接master服務器,並請求從執行binlog日志文件的指定位置(日志文件名和位置就是在配置主從復制服務時執行change master命令指定的)之后開始發送binlog日志內容
    (3)Master服務器接收到來自Slave服務器的IO線程的請求后,二進制轉儲IO線程會根據Slave服務器的IO線程請求的信息分批讀取指定binlog日志文件指定位置之后的binlog日志信息,然后返回給Slave端的IO線程。返回的信息中除了binlog日志內容外,還有在master服務器端記錄的新的binlog文件名稱,以及在新的binlog中的下一個指定更新位置。
    (4)當Slave服務器的IO線程獲取到Master服務器上IO線程發送的日志內容、日志文件及位置點后,會將binlog日志內容依次寫到Slave端自身的Relay Log(即中繼日志)文件(MySQL-relay-bin.xxx)的最末端,並將新的binlog文件名和位置記錄到master-info文件中,以便下一次讀取master端新binlog日志時能告訴Master服務器從新binlog日志的指定文件及位置開始讀取新的binlog日志內容
    (5)Slave服務器端的SQL線程會實時檢測本地Relay Log 中IO線程新增的日志內容,然后及時把Relay LOG 文件中的內容解析成sql語句,並在自身Slave服務器上按解析SQL語句的位置順序執行應用這樣sql語句,並在relay-log.info中記錄當前應用中繼日志的文件名和位置點

5>.MySQL復制有三種核心格式

復制的工作原理是數據庫修改記錄到bin log日志並傳遞到slave,然后slave在本地還原的過程。而時間記錄到bin log的格式會有所不同。

基於語句的復制(statement based replication):
  基於主庫將SQL語句寫入到bin log中完成復制。

基於行數據的復制(row based replication):
  基於主庫將每一行數據變化的信息作為時間寫入到bin log中完成日志。默認就是基於行級別的復制,因為它相對語句復制邏輯更為嚴謹。

混合復制(mixed based replication):
  上述兩者的結合。默認情況下優先使用基於語句的復制,只有當部分語句如果基於語句復制不完全的情況下才會自動切換為基於行數據的復制。
[root@node106.yinzhengjie.org.cn ~]# mysql -uroot -pyinzhengjie
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.25-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> 
mysql> SHOW VARIABLES LIKE '%BINLOG_FORMAT%';        #很顯然,默認就是基於行復制的
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW   |
+---------------+-------+
1 row in set (0.00 sec)

mysql> 
mysql> SET binlog_format='STATEMENT';            #我們可以修改基於語句復制
Query OK, 0 rows affected (0.00 sec)

mysql> 
mysql> SHOW VARIABLES LIKE '%BINLOG_FORMAT%';
+---------------+-----------+
| Variable_name | Value     |
+---------------+-----------+
| binlog_format | STATEMENT |
+---------------+-----------+
1 row in set (0.00 sec)

mysql> 
mysql> quit
Bye
[root@node106.yinzhengjie.org.cn ~]# 
修改默認的復制模式格式案例(推薦大家使用默認的基於ROW的復制方式)

 

三.配置MySQL基於bin-log主從同步

1>.編輯my.cnf配置文件

[root@node106.yinzhengjie.org.cn ~]# cat /etc/my.cnf
[mysqld]
basedir=/yinzhengjie/softwares/mysql/
datadir=/yinzhengjie/softwares/mysql/data/
log-bin=yinzhengjie-mysql-bin
server-id=106
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# cat /etc/my.cnf      #主庫
[root@node107.yinzhengjie.org.cn ~]# cat /etc/my.cnf
[mysqld]
basedir=/yinzhengjie/softwares/mysql/
datadir=/yinzhengjie/softwares/mysql/data/
log-bin=yinzhengjie-mysql-bin
server-id=107
[root@node107.yinzhengjie.org.cn ~]# 
[root@node107.yinzhengjie.org.cn ~]# cat /etc/my.cnf      #從庫

2>.在主庫創建一個專門用來復制的數據庫用戶,這樣所有從庫都可以用這個用戶來連接主庫,也可以確保這個用戶只有復制的權限 

[root@node106.yinzhengjie.org.cn ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.25-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> 
mysql> CREATE USER 'copy'@'172.30.1.10%' IDENTIFIED BY 'yinzhengjie';
Query OK, 0 rows affected (0.01 sec)

mysql> 
mysql> GRANT REPLICATION SLAVE ON *.* TO 'copy'@'172.30.1.10%';
Query OK, 0 rows affected (0.00 sec)

mysql> 
mysql> quit
Bye
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# 
[root@node107.yinzhengjie.org.cn ~]# mysql -u copy -pyinzhengjie -P 3306 -h node106.yinzhengjie.org.cn
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 20
Server version: 5.7.25-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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 DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.00 sec)

mysql> 
mysql> QUIT
Bye
[root@node107.yinzhengjie.org.cn ~]# 
[root@node107.yinzhengjie.org.cn ~]# 
[root@node107.yinzhengjie.org.cn ~]# mysql -u copy -pyinzhengjie -P 3306 -h node106.yinzhengjie.org.cn        #從slave數據庫驗證遠程連接主庫是否正常,一定要檢查

3>.獲取主庫的日志信息並生成主庫數據鏡像

mysql> FLUSH TABLES WITH READ LOCK;                  #對主庫上所有表加鎖,停止修改,即在從庫復制的過程中主庫不能執行UPDATA,DELETE,INSERT語句!
Query OK, 0 rows affected (0.00 sec)

mysql> 
mysql> SHOW MASTER STATUS;                           #獲取主庫的日志信息,file表示當前日志文件名稱,position表示當前日志的位置
+------------------------------+----------+--------------+------------------+-------------------+
| File                         | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------------------+----------+--------------+------------------+-------------------+
| yinzhengjie-mysql-bin.000002 |     4095 |              |                  |                   |
+------------------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql> 
[root@node106.yinzhengjie.org.cn ~]#  mysqldump --all-databases --master-data -u root -pyinzhengjie -P 3306 > yinzhengjie-master.db       #另開一個終端生成鏡像,在生成完成之前不要釋放鎖。
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# ll
total 776
-rw-r--r-- 1 root root 791962 Jun 10 19:24 yinzhengjie-master.db
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# mysqldump --all-databases --master-data -u root -pyinzhengjie -P 3306 > yinzhengjie-master.db       #另開一個終端生成鏡像,在生成完成之前不要釋放鎖。
mysql>
mysql> UNLOCK TABLES;                #主庫數據生成鏡像完畢后,我們需要把主庫的鎖釋放掉,需要注意的是,在上鎖這一段期間,我們無法對數據庫進行寫操作,比如UPDATA,DELETE,INSERT。
Query OK, 0 rows affected (0.00 sec)

mysql> 

4>.將主庫的鏡像拷貝當從庫中,讓從庫應用主庫鏡像

[root@node106.yinzhengjie.org.cn ~]# ll
total 776
-rw-r--r-- 1 root root 791962 Jun 10 19:24 yinzhengjie-master.db
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# scp yinzhengjie-master.db node107.yinzhengjie.org.cn:~
The authenticity of host 'node107.yinzhengjie.org.cn (172.30.1.107)' can't be established.
ECDSA key fingerprint is SHA256:BkN6bKO2q5zMgvremE/3rOIsCaq9eTPudgfU0lhbqGo.
ECDSA key fingerprint is MD5:75:bd:cb:be:35:f8:45:a2:ea:74:bc:aa:29:74:4d:0d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'node107.yinzhengjie.org.cn,172.30.1.107' (ECDSA) to the list of known hosts.
root@node107.yinzhengjie.org.cn's password: 
yinzhengjie-master.db                                                                                                                                                                                  100%  773KB  60.7MB/s   00:00    
[root@node106.yinzhengjie.org.cn ~]# 
[root@node106.yinzhengjie.org.cn ~]# scp yinzhengjie-master.db node107.yinzhengjie.org.cn:~
[root@node107.yinzhengjie.org.cn ~]# pwd
/root
[root@node107.yinzhengjie.org.cn ~]# 
[root@node107.yinzhengjie.org.cn ~]# ll
total 776
-rw-r--r-- 1 root root 791962 Jun 10 19:37 yinzhengjie-master.db
[root@node107.yinzhengjie.org.cn ~]# 
[root@node107.yinzhengjie.org.cn ~]# mysql -uroot -pyinzhengjie
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.25-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> source yinzhengjie-master.db;
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected, 1 warning (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 1 row affected (0.00 sec)

Database changed
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 41 rows affected (0.00 sec)
Records: 41  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 699 rows affected (0.01 sec)
Records: 699  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 1413 rows affected (0.01 sec)
Records: 1413  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 643 rows affected (0.08 sec)
Records: 643  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 7 rows affected (0.00 sec)
Records: 7  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 6 rows affected (0.00 sec)
Records: 6  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected, 1 warning (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> 
mysql> source yinzhengjie-master.db;              #在從庫應用主庫的數據鏡像

5>.在從庫上建立復制關系,即從庫指定主庫的日志信息和鏈接信息

mysql> CHANGE MASTER TO
    -> MASTER_HOST='node106.yinzhengjie.org.cn',
    -> MASTER_PORT=3306,
    -> MASTER_USER='copy',
    -> MASTER_PASSWORD='yinzhengjie',
    -> MASTER_LOG_FILE='yinzhengjie-mysql-bin.000002',
    -> MASTER_LOG_POS=4095;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql>

6>.從庫啟動復制進程

mysql> START SLAVE;
Query OK, 0 rows affected (0.00 sec)

mysql> 
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: node106.yinzhengjie.org.cn
                  Master_User: copy
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: yinzhengjie-mysql-bin.000002
          Read_Master_Log_Pos: 4095
               Relay_Log_File: node107-relay-bin.000002
                Relay_Log_Pos: 332
        Relay_Master_Log_File: yinzhengjie-mysql-bin.000002
             Slave_IO_Running: Yes        #觀察IO進程是否為yes,如果為yes說明正常,如果長時間處於"Connecting"狀態就得檢查你的從庫指定的主庫的鏈接信息是否正確
            Slave_SQL_Running: Yes        #觀察SQL進程是否為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: 4095
              Relay_Log_Space: 541
              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            #該參數表示從庫和主庫有多少秒的延遲,咱們可以理解為再過多少秒數據和主庫保持一致,如果為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: 106
                  Master_UUID: 74227047-8b60-11e9-8cba-000c29985293
             Master_Info_File: /yinzhengjie/softwares/mysql/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           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
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

mysql> 

 

四.創建cdh數據庫並驗證從庫是否有數據

1>.在主庫中創建cdh數據庫

[root@node106.yinzhengjie.org.cn ~]# mysql -uroot -pyinzhengjie
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 5.7.25-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> 
mysql> CREATE DATABASE cdh DEFAULT CHARACTER SET = utf8;
Query OK, 1 row affected (0.00 sec)

mysql> 
mysql> GRANT ALL PRIVILEGES ON cdh.* TO 'cdh'@'172.30.1.10%' IDENTIFIED BY 'yinzhengjie' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> 
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| cdh                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> 
mysql> QUIT
Bye
[root@node106.yinzhengjie.org.cn ~]# 

2>.在從庫的服務器觀察數據是否同步

[root@node107.yinzhengjie.org.cn ~]# mysql -uroot -pyinzhengjie
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.7.25-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> 
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| cdh                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> QUIT
Bye
[root@node107.yinzhengjie.org.cn ~]# 

 3>.測試完成后,記得要刪除cdh庫,否則我們在安裝CM時會提示cdh庫已經存在的報錯

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| cdh                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> 
mysql> drop database cdh;
Query OK, 0 rows affected (0.00 sec)

mysql> 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> 

 

 

 


免責聲明!

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



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