Mysql-5.7.20主從複製測試[20180110]

来源:https://www.cnblogs.com/also-brook/archive/2018/01/11/8267045.html
-Advertisement-
Play Games

前言 Mysql 5.7.20測試主從複製 環境 主庫 192.168.1.59 t-xi-sonar01 從庫 192.168.1.51 t-xi-orc01 設定主機host文件 主庫 [root@t-xi-sonar01 ~]# cat /etc/hosts 127.0.0.1 localho ...


前言     Mysql 5.7.20測試主從複製   環境     主庫 192.168.1.59  t-xi-sonar01     從庫 192.168.1.51  t-xi-orc01   設定主機host文件   
主庫
[root@t-xi-sonar01 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.59 t-xi-sonar01
192.168.1.51 t-xi-orc01
從庫
[root@t-xi-orc01 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.51 t-xi-orc01
192.168.1.59 t-xi-sonar01
  Mysql資料庫配置     【Master-Server】  
[root@t-xi-sonar01 ~]# service mysqld stop
 Stopping mysqld:                                           [  OK  ]
[root@t-xi-sonar01 ~]# vim /etc/my.cnf
#Server ID,一般設置成IP地址的最後一位,如下測試就按後兩位
server_id=59
#開啟log bin,名字最好有意義用來區分
log-bin=dev-bin
#需要進行複製的資料庫,可以指定資料庫
#binlog-do-db=DB_master
#不需要備份的資料庫,可以設置多個資料庫,一般不會同步mysql這個庫
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
#為每個session 分配的記憶體,在事務過程中用來存儲二進位日誌的緩存
binlog_cache_size=1m
#二進位日誌自動刪除/過期的天數。預設值為0,表示不自動刪除。
expire_logs_days=7
# 跳過主從複製中遇到的所有錯誤或指定類型的錯誤,避免slave端複製中斷。
# 如:1062錯誤是指一些主鍵重覆,1032錯誤是因為主從資料庫數據不一致
slave_skip_errors=1062
[root@t-xi-sonar01 ~]# service mysqld start
Starting mysqld:                                           [  OK  ]
[root@t-xi-sonar01 ~]#
 
mysql 5.7.20登陸報錯解決
  ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
mysql -u root
update user set authentication_string=PASSWORD("****") where User='root';
flush privileges;
 
主庫創建同步賬戶
service mysqld start
mysql> mysql -u root -p
mysql> CREATE USER 'replication'@'192.168.1.51' IDENTIFIED BY 'slave';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'replication'@'192.168.1.51';
mysql> flush privileges;
 
主庫鎖定後備份將資料同步到從庫
mysql>use sonar
mysql>FLUSH TABLES WITH READ LOCK;
mysqldump -u root -p --databases sonar > sonar.sql
scp sonar.sql @192.168.1.51:/root
mysql> unlock tables;
[master-server]
mysql> show master status ;
+----------------+----------+--------------+---------------------------------------------+-------------------+
| File           | Position | Binlog_Do_DB | Binlog_Ignore_DB                            | Executed_Gtid_Set |
+----------------+----------+--------------+---------------------------------------------+-------------------+
| dev-bin.000004 |      783 |              | mysql,information_schema,performance_schema |                   |
+----------------+----------+--------------+---------------------------------------------+-------------------+

 


    【Slave-Server】          
 service mysqld stop
vim /etc/my.cnf
#add slave-server
server_id=51
#binlog-ignore-db=mydql
#binlog-ignore-db=information_schema
#binlog-ignore-db=performance_schema
#log-bin=dev-slave-bin
binlog_cache_size=1M
binlog_format=mixed
expire_logs_days=7
slave_skip_errors=1062
relay_log=dev-relay-bin
#log_slave_updates=1
read_only=1
 
service mysqld start
將主庫備份導入從庫
mysql>source /root/sonar.sql
添加鏈接到主庫同步複製的賬戶
mysql>CHANGE MASTER TO MASTER_HOST='192.168.1.59', MASTER_USER='replication', MASTER_PASSWORD='slave', MASTER_LOG_FILE='dev-bin.000001', MASTER_LOG_POS=0;
    
MASTER_LOG_FILE:指定log bin日誌文件名稱
MASTER_LOG_POS :指定同步複製log分區號,可以從0開始。
 
查看slave狀態
show slave status \G
    
Slave_IO_State #從站的當前狀態
Slave_IO_Running: Yes #讀取主程式二進位日誌的I/O線程是否正在運行
Slave_SQL_Running: Yes #執行讀取主伺服器中二進位日誌事件的SQL線程是否正在運行。與I/O線程一樣
Seconds_Behind_Master #是否為0,0就是已經同步了
啟動slave
start slave;
mysql>     show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.59
                  Master_User: replication
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: dev-bin.000004
          Read_Master_Log_Pos: 1255
               Relay_Log_File: dev-relay-bin.000005
                Relay_Log_Pos: 1464
        Relay_Master_Log_File: dev-bin.000004
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
  Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 1255
              Relay_Log_Space: 1878
              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: 59
                  Master_UUID: d6901902-ea28-11e7-b859-000c29255261
             Master_Info_File: /var/lib/mysql/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)

 

    

 

      【Master-Server】         
創建table和database測試
     
在sonar下建立測試表
mysql> use sonar;   
mysql> create table slave_t(
    -> id int(10) not null, name varchar(20)
    -> )
    -> ;
Query OK, 0 rows affected (1.57 sec)
 
mysql> insert into slave_t values(1,'name01');
Query OK, 1 row affected (0.33 sec)
  
創建slave_db測試資料庫
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Database changed
mysql> create database slave_db;
Query OK, 1 row affected (0.17 sec)

 

      【Slave-Server】     
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| slave_db           |
| sonar              |
| sys                |
+--------------------+
6 rows in set (0.00 sec)
 
mysql> use sonar;
Database changed
mysql> select * from slave_t;
+----+--------+
| id | name   |
+----+--------+
|  1 | name01 |
+----+--------+
1 row in set (0.00 sec)

主庫上的table和database已同步複製過來

 

 

  【命令參考】
查看主庫master狀態
mysql> show master status;
+----------------+----------+--------------+---------------------------------------------+-------------------+
| File           | Position | Binlog_Do_DB | Binlog_Ignore_DB                            | Executed_Gtid_Set |
+----------------+----------+--------------+---------------------------------------------+-------------------+
| dev-bin.000004 |     1426 |              | mysql,information_schema,performance_schema |                   |
+----------------+----------+--------------+---------------------------------------------+-------------------+
1 row in set (0.00 sec)
 
查看從庫主機列表
mysql> show slave hosts;
+-----------+------+------+-----------+--------------------------------------+
| Server_id | Host | Port | Master_id | Slave_UUID                           |
+-----------+------+------+-----------+--------------------------------------+
|        51 |      | 3306 |        59 | 86fff1d0-f62d-11e7-834d-000c29477dac |
+-----------+------+------+-----------+--------------------------------------+
1 row in set (0.00 sec)
 
查看bin log文件列表
mysql> show binary logs;
+----------------+-----------+
| Log_name       | File_size |
+----------------+-----------+
| dev-bin.000001 |       177 |
| dev-bin.000002 |       177 |
| dev-bin.000003 |       177 |
| dev-bin.000004 |      1426 |
+----------------+-----------+
4 rows in set (0.00 sec)
 
查看bin log文件的內容
mysql> show binlog events;
+----------------+-----+----------------+-----------+-------------+---------------------------------------+
| Log_name       | Pos | Event_type     | Server_id | End_log_pos | Info                                  |
+----------------+-----+----------------+-----------+-------------+---------------------------------------+
| dev-bin.000001 |   4 | Format_desc    |        59 |         123 | Server ver: 5.7.20-log, Binlog ver: 4 |
| dev-bin.000001 | 123 | Previous_gtids |        59 |         154 |                                       |
| dev-bin.000001 | 154 | Stop           |        59 |         177 |                                       |
+----------------+-----+----------------+-----------+-------------+---------------------------------------+
3 rows in set (0.00 sec)
 
mysql> show binlog events in 'dev-bin.000004';
+----------------+------+----------------+-----------+-------------+---------------------------------------------------------------------------------------------------------------------------------+
| Log_name       | Pos  | Event_type     | Server_id | End_log_pos | Info                                                                                                                            |
+----------------+------+----------------+-----------+-------------+---------------------------------------------------------------------------------------------------------------------------------+
| dev-bin.000004 |    4 | Format_desc    |        59 |         123 | Server ver: 5.7.20-log, Binlog ver: 4                                                                                           |
| dev-bin.000004 |  123 | Previous_gtids |        59 |         154 |                                                                                                                                 |
| dev-bin.000004 |  154 | Anonymous_Gtid |        59 |         219 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                            |
| dev-bin.000004 |  219 | Query          |        59 |         417 | CREATE USER 'replication'@'192.168.1.51' IDENTIFIED WITH 'mysql_native_password' AS '*51125B3597BEE0FC43E0BCBFEE002EF8641B44CF' |
| dev-bin.000004 |  417 | Anonymous_Gtid |        59 |         482 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                            |
| dev-bin.000004 |  482 | Query          |        59 |         631 | GRANT REPLICATION SLAVE ON *.* TO 'replication'@'192.168.1.51'                                                                  |
| dev-bin.000004 |  631 | Anonymous_Gtid |        59 |         696 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                            |
| dev-bin.000004 |  696 | Query          |        59 |         783 | flush privileges                                                                                                                |
| dev-bin.000004 |  783 | Anonymous_Gtid |        59 |         848 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                            |
| dev-bin.000004 |  848 | Query          |        59 |         985 | use `sonar`; create table slave_t(
id int(10) not null, name varchar(20)
)                                                      |
| dev-bin.000004 |  985 | Anonymous_Gtid |        59 |        1050 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                            |
| dev-bin.000004 | 1050 | Query          |        59 |        1123 | BEGIN                                                                                                                           |
| dev-bin.000004 | 1123 | Table_map      |        59 |        1177 | table_id: 326 (sonar.slave_t)                                                                                                   |
| dev-bin.000004 | 1177 | Write_rows     |        59 |        1224 | table_id: 326 flags: STMT_END_F                                                                                                 |
| dev-bin.000004 | 1224 | Xid            |        59 |        1255 | COMMIT /* xid=178006 */                                                                                                         |
| dev-bin.000004 | 1255 | Anonymous_Gtid |        59 |        1320 | SET @@SESSION.GTID_NEXT= 'ANONYMOUS'                                                                                            |
| dev-bin.000004 | 1320 | Query          |        59 |        1426 | create database slave_db                                                                                                        |
+----------------+------+----------------+-----------+-------------+---------------------------------------------------------------------------------------------------------------------------------+
17 rows in set (0.13 sec)
 
查看當前資料庫線程列表
【master-server】
mysql> SHOW PROCESSLIST \G
*************************** 1. row ***************************
     Id: 199
   User: root
   Host: localhost
     db: mysql
Command: Query
   Time: 0
  State: starting
   Info: SHOW PROCESSLIST
*************************** 2. row ***************************
     Id: 201
   User: replication
   Host: t-xi-orc01:41452
     db: NULL
Command: Binlog Dump
   Time: 1450
  State: Master has sent all binlog to slave; waiting for more updates
   Info: NULL
*************************** 3. row ***************************
     Id: 203
   User: sonar
   Host: localhost:57162
     db: sonar
Command: Sleep
   Time: 434
  State:
   Info: NULL
*************************** 4. row ***************************
     Id: 204
   User: sonar
   Host: localhost:57358
     db: sonar
Command: Sleep
   Time: 123
  State:
   Info: NULL
*************************** 5. row ***************************
     Id: 205
   User: sonar
   Host: localhost:57524
     db: sonar
Command: Sleep
   Time: 2
  State:
   Info: NULL
*************************** 6. row ***************************
     Id: 206
   User: sonar
   Host: localhost:57720
     db: sonar
Command: Sleep
   Time: 3
  State:
   Info: NULL
6 rows in set (0.00 sec)
 
【slave-server】
mysql> SHOW PROCESSLIST  \G
*************************** 1. row ***************************
     Id: 4
   User: root
   Host: localhost
     db: sonar
Command: Query
   Time: 0
  State: starting
   Info: SHOW PROCESSLIST
*************************** 2. row ***************************
     Id: 5
   User: system user
   Host:
     db: NULL
Command: Connect
   Time: 1445
  State: Waiting for master to send event
   Info: NULL
*************************** 3. row ***************************
     Id: 6
   User: system user
   Host:
     db: NULL
Command: Connect
   Time: 26717
  State: Slave has read all relay log; waiting for more updates
   Info: NULL
3 rows in set (0.00 sec)
 
 
 
從庫啟動複製
mysql> START SLAVE;
從庫停止複製
mysql> STOP SLAVE;

 

  參考: https://segmentfault.com/a/1190000010867488 https://www.jianshu.com/p/208667156faf  
您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 連接資料庫: 輸入資料庫密碼即可登陸。 查看mysql版本信息: 查看當前時間: 實現Windows與Ubuntu虛擬機之間的文件互傳問題。 其實很簡單就是需要安裝一個VMware tools即可。 打開文件管理就有一個VM的tar包,沒有的話就需要多掛一個cd驅動器。 右鍵,設置, 我有兩個cd驅 ...
  • (scott賬戶) SELECT * FROM scott.dept;--4SELECT * FROM scott.emp;--14 /**笛卡爾積內連接(等值連接)外連接(非等值連接)自連接*/ --笛卡爾積--當查詢數據時沒有使用連接條件,會查出所有關聯數據--4*14=56SELECT * F ...
  • 今天新部署Cloudera Manager Hadoop(CDH)集群 發現祖國江山一片紅,所有服務都報時鐘偏差 1、查看各伺服器,時鐘是否正確 發現並無問題 2、查看CDH主機配置————主機時鐘偏差閾值 尼瑪,這個預設值警告是2s,嚴重是10s,仔細查看界面後面的描述如下: 這是主機運行狀況測試 ...
  • .Net MongoDB LBS地理位置定位 開發過程,實現商家按距離排序 前言: 在使用美團點外賣,看電影,找好吃的時候,經常會註意到軟體有一個按距離排序,找離我最近的商家,心中有一些疑問,.Net+Sql Server是如何做到這一需求的?發現使用關係型資料庫Sql Server實現這一需求是有 ...
  • 一 前言 以往公司開發的地理信息系統數據都是存儲在oracle中,然而在地圖數據的分析和計算中一直存在不小的問題,所以今次我們嘗試使用PostgreSQL+PostGIS來解決這一問題。 本篇筆記的目的就是記錄在研究這一開發路徑期間的“學習的內容”、“遇到的問題和解決過程”以及“簡單的實踐內容”以備 ...
  • --scott用戶不能使用,使用system登錄--修改用戶scott賬戶解鎖ALTER USER SCOTT ACCOUNT UNLOCK;--重新設置密碼identified被識別的alter user scott identified by tiger; --選擇 所有欄位 scott用戶部門 ...
  • Kettle使用心得彙總系列一 一、kettle實現的功能: kettle可以實現從不同數據源(excel、資料庫、文本文件等)獲取數據,然後將數據進行整合、轉換處理,可以再將數據輸出到指定的位置(excel、資料庫、文本文件)等; 通過kettle處理大量數據非常方便,如果window伺服器或者L ...
  • 概述: 閃回技術是Oracle強大資料庫備份恢復機制的一部分,在資料庫發生邏輯錯誤的時候,閃回技術能提供快速且最小損失的恢復(多數閃回功能都能在資料庫聯機狀態下完成)。需要註意的是,閃回技術旨在快速恢復邏輯錯誤,對於物理損壞或是介質丟失的錯誤,閃回技術就回天乏術了,還是得藉助於Oracle一些高級的 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...