MySQL 5.7主從複製實戰篇node
做者:尹正傑mysql
版權聲明:原創做品,謝絕轉載!不然將追究法律責任。linux
一.安裝MySQL數據庫並啓動sql
1>.在MySQL官方下載相應的安裝包(https://dev.mysql.com/downloads/mysql/)數據庫
2>.解壓MySQL壓縮文件至安裝目錄bash
[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的配置文件ide
[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 ~]#
三.配置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@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 ~]#
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 ~]#
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 ~]#
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@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>
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>