主頁 > 作業系統 > mysql主從

mysql主從

2022-08-03 10:51:46 作業系統

mysql主從


目錄
  • mysql主從
    • 1.主從簡介
      • 1.1 主從作用
      • 1.2 主從形式
    • 2.主從復制原理
    • 3.主從復制配置
      • 3.1 mysql安裝
      • 3.2 mysql主從配置
        • 3.2.1 確保從資料庫與主資料庫里的資料一樣
        • 3.2.2 在主資料庫里創建一個同步賬號授權給從資料庫使用
        • 3.2.3 配置主資料庫
        • 3.2.4 配置從資料庫
        • 測驗驗證
    • 4. GTID主從
      • 4.1 GTID概念介紹
      • 4.2 GTID作業原理
      • 4.3 GTID主從配置

1.主從簡介

用一臺資料庫存放資料,若此資料庫服務器宕機了導致資料丟失怎么辦?
業務量大了,資料多了,訪問的人多了,一臺資料庫無法保證服務質量了怎么辦?

1.1 主從作用

防范出現問題,用于故障的切換
讀寫分離,方便進行查詢
備份,反正意外丟失資料

1.2 主從形式

  1. 一主一從(一個主資料庫一個輔助資料庫)
  2. 主主復制(兩臺資料庫同步)
  3. 一主多從---擴展系統讀取的性能,因為讀是在從庫讀取的
  4. 多主一從---5.7開始支持
  5. 聯級復制

2.主從復制原理

主從復制步驟:
主庫將所有的寫操作記錄到binlog日志中并生成一個log dump執行緒,將binlog日志傳給從庫的I/O執行緒
從庫生成兩個執行緒,一個I/O執行緒,一個SQL執行緒
I/O執行緒去請求主庫的binlog,并將得到的binlog日志寫到relay log(中繼日志) 檔案中
SQL執行緒,會讀取relay log檔案中的日志,并決議成具體操作,來實作主從的操作一致,達到最終資料一致的目的

3.主從復制配置

主從復制配置步驟:

  1. 確保從資料庫與主資料庫里的資料一樣
  2. 在主資料庫里創建一個同步賬號授權給從資料庫使用
  3. 配置主資料庫(修改組態檔)
  4. 配置從資料庫(修改組態檔)
    需求:
    搭建兩臺MySQL服務器,一臺作為主服務器,一臺作為從服務器,主服務器進行寫操作,從服務器進行讀操作
    環境說明:
資料庫角色 IP 應用與系統版本 有無資料
主資料庫 192.168.222.250 centos8/redhat8 mysql-5.7 有資料
從資料庫 192.168.222.251 centos8/redhat8 mysql-5.7 無資料

3.1 mysql安裝

分別在主從兩臺服務器上安裝mysql-5.7版本,此處略過安裝步驟,若有疑問請參考《mysql進階》

3.2 mysql主從配置

3.2.1 確保從資料庫與主資料庫里的資料一樣

為確保從資料庫與主資料庫里的資料一樣,先全備主資料庫并還原到從資料庫中

先查看主庫有哪些庫:
[root@master ~]# mysql -uroot -plnh123 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| lnh                |
| mysql              |
| performance_schema |
| sys                |
| tushanbu           |
+--------------------+
再查看從庫有哪些庫:
[root@slave ~]# mysql -uroot -plnh123 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
全備主庫:
//全備主庫時需要另開一個終端,給資料庫加上讀鎖,避免在備份期間有其他人在寫入導致資料不一致
[root@master ~]# mysql -uroot -plnh123 
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 8
Server version: 5.7.38 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> flush tables with read lock;
Query OK, 0 rows affected (0.05 sec)
//此鎖表的終端必須在備份完成以后才能退出
因為主庫已經鎖住,我們可以再開一個主庫的終端進行操作
[root@master ~]# mysqldump -uroot -plnh123 --all-databases > /opt/all-$(date '+%Y%m%d%H%M%S').sql  //全量備份主庫
mysqldump: [Warning] Using a password on the command line interface can be insecure
[root@master ~]# ll /opt/
total 864
-rw-r--r--. 1 root  root  878669 Aug  2 00:42 all-20220802004207.sql
drwxr-xr-x. 7 mysql mysql   4096 Jul 27 22:04 xbz
[root@master ~]# scp /opt/all-20220802004207.sql [email protected]:/opt/
The authenticity of host '192.168.222.251 (192.168.222.251)' can't be established.
ECDSA key fingerprint is SHA256:y11UDaNXs3AnvVUnZQfAim2VHAplF09YOvQp2NemHyk.
Are you sure you want to continue connecting (yes/no/[fingerprint])? y
Please type 'yes', 'no' or the fingerprint: yes
Warning: Permanently added '192.168.222.251' (ECDSA) to the list of known hosts.
[email protected]'s password: 
Permission denied, please try again.
[email protected]'s password: 
all-20220802004207.sql                           100%  858KB  91.7MB/s   00:00    
//將主庫的備份資料傳送到從庫里面去
mysql> quit
Bye
//在主里面退出鎖定
在從庫里面:
[root@slave ~]# ll /opt/
total 864
-rw-r--r--. 1 root  root  878669 Aug  2 00:45 all-20220802004207.sql
drwxr-xr-x. 5 mysql mysql   4096 Aug  1 23:45 xbz
//可以查看到有資料備份的檔案
[root@slave ~]# mysql -uroot -plnh123 < /opt/all-20220802004207.sql 
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@slave ~]# mysql -uroot -plnh123 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| lnh                |
| mysql              |
| performance_schema |
| sys                |
| tushanbu           |
+--------------------+
//將主庫里面的資料成功全部備份到從庫里面實作了主庫和從庫里面的資料一致

3.2.2 在主資料庫里創建一個同步賬號授權給從資料庫使用

[root@master ~]# mysql -uroot -plnh123
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 10
Server version: 5.7.38 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> grant replication slave on *.* to 'repl'@'192.168.222.251' identified by 'repl123';    
Query OK, 0 rows affected, 1 warning (0.01 sec)
//這里是授權從庫的主機ip
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> quit
Bye

3.2.3 配置主資料庫

[root@master ~]# vim /etc/my.cnf 
[root@master ~]# cat /etc/my.cnf 
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/xbz
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/xbz/mysql.pid
user = mysql
skip-name-resolve
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION


log-bin=mysql_bin  //啟用binlog日志
server-id = 10     //資料庫服務器唯一識別符號,主庫的server-id值必須比從庫的大   //添加這兩行 10的目的是因為后面可能會有主庫什么的
[root@master ~]# systemctl restart mysqld.service //重啟服務
[root@master ~]# ss -antl    //查看埠
State    Recv-Q   Send-Q     Local Address:Port      Peer Address:Port   Process   
LISTEN   0        128              0.0.0.0:22             0.0.0.0:*                
LISTEN   0        128                    *:80                   *:*                
LISTEN   0        128                 [::]:22                [::]:*                
LISTEN   0        80                     *:3306                 *:*                
[root@master ~]# mysql -uroot -plnh123
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 2
Server version: 5.7.38-log MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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 master status;    //查看狀態
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql_bin.000001 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

3.2.4 配置從資料庫

[root@slave ~]# vim /etc/my.cnf 
[root@slave ~]# cat /etc/my.cnf 
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/xbz
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/xbz/mysql.pid
user = mysql
skip-name-resolve
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

server-id = 20  //設定從庫的唯一識別符號,從庫的server-id值必須大于主庫的該值  
relay-log = myrelay //啟用中繼日志relay-log
[root@slave ~]# systemctl restart mysqld.service //重啟服務
[root@slave ~]# ss -antl   //查看埠
State    Recv-Q   Send-Q     Local Address:Port      Peer Address:Port   Process   
LISTEN   0        128              0.0.0.0:22             0.0.0.0:*                
LISTEN   0        128                 [::]:22                [::]:*                
LISTEN   0        80                     *:3306                 *:*                
[root@slave ~]# mysql -uroot -plnh123
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 2
Server version: 5.7.38 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> change master to    //配置并啟動主從復制
    -> master_host='192.168.222.250',
    -> master_user='repl',
    -> master_password='repl123',
    -> master_log_file='mysql_bin.000001',
    -> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

mysql> start slave;    //開啟
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.222.250
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_bin.000001
          Read_Master_Log_Pos: 603
               Relay_Log_File: myrelay.000002
                Relay_Log_Pos: 769
        Relay_Master_Log_File: mysql_bin.000001
             Slave_IO_Running: Yes  //這里必須是yes(這里如果沒有yes那么就是找不到指定東西)
            Slave_SQL_Running: Yes  //這里必須是yes(這里如果沒有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: 603
              Relay_Log_Space: 968
              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: 10
                  Master_UUID: 09b3989b-0d96-11ed-b83a-000c2905f428
             Master_Info_File: /opt/xbz/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)

測驗驗證

在主服務器的lnh庫的xbz表中插入資料:

mysql> use lnh;
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 table xbz(id int not null,name varchar(20),age int);
Query OK, 0 rows affected (0.03 sec)

mysql> desc xbz;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
| age   | int(11)     | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> insert into xbz values (1,'dad',20),(2,'tom',22),(3,'jerry',12);
Query OK, 3 rows affected (0.05 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from xbz;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | dad   |   20 |
|  2 | tom   |   22 |
|  3 | jerry |   12 |
+----+-------+------+
3 rows in set (0.00 sec)

在從資料庫中查看資料是否同步:

[root@slave ~]# mysql -uroot -plnh123 
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.38 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> use lnh;
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> select * from xbz;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | dad   |   20 |
|  2 | tom   |   22 |
|  3 | jerry |   12 |
+----+-------+------+
3 rows in set (0.00 sec)
//測驗成功

4. GTID主從

4.1 GTID概念介紹

GTID即全域事務ID (global transaction identifier), 其保證為每一個在主上提交的事務在復制集群中可以生成一個唯一的ID,GTID最初由google實作,官方MySQL在5.6才加入該功能,mysql主從結構在一主一從情況下對于GTID來說就沒有優勢了,而對于2臺主以上的結構優勢例外明顯,可以在資料不丟失的情況下切換新主,使用GTID需要注意: 在構建主從復制之前,在一臺將成為主的實體上進行一些操作(如資料清理等),通過GTID復制,這些在主從成立之前的操作也會被復制到從服務器上,引起復制失敗,也就是說通過GTID復制都是從最先開始的事務日志開始,即使這些操作在復制之前執行,比如在server1上執行一些drop、delete的清理操作,接著在server2上執行change的操作,會使得server2也進行server1的清理操作,
GTID實際上是由UUID+TID (即transactionId)組成的,其中UUID(即server_uuid) 產生于auto.conf檔案(cat /data/mysql/data/auto.cnf),是一個MySQL實體的唯一標識,TID代表了該實體上已經提交的事務數量,并且隨著事務提交單調遞增,所以GTID能夠保證每個MySQL實體事務的執行(不會重復執行同一個事務,并且會補全沒有執行的事務),GTID在一組復制中,全域唯一.


如圖:server1(master)宕機了,這個時候server2(slave)已經同步到了server1的全部資料,而server3(slave)只是同步到了一半,這時要是把Server2提升為主,Server3變成Server2的從,這時在Server3上執行change的時候需要做一些計算,這個問題在5.6的GTID出現后,就顯得非常的簡單,由于同一事務的GTID在所有節點上的值一致,那么根據Server3當前停止點的GTID就能定位到Server2上的GTID,甚至由于MASTER_AUTO_POSITION功能的出現,我們都不需要知道GTID的具體值,直接使用CHANGE MASTER TO MASTER_HOST='xxx', MASTER_AUTO_POSITION命令就可以直接完成failover的作業,
====== GTID和Binlog的關系 ======

GTID在binlog中的結構:

GTID event 結構:

Previous_gtid_log_event
Previous_gtid_log_event 在每個binlog 頭部都會有每次binlog rotate的時候存盤在binlog頭部Previous-GTIDs在binlog中只會存盤在這臺機器上執行過的所有binlog,不包括手動設定gtid_purged值,換句話說,如果你手動set global gtid_purged=xx; 那么xx是不會記錄在Previous_gtid_log_event中的,

GTID和Binlog之間的關系是怎么對應的呢? 如何才能找到GTID=? 對應的binlog檔案呢?
假設有3個binlog: bin.001,bin.002,bin.003,
bin.001 : Previous-GTIDs=empty; binlog_event有: 1-40
bin.002 : Previous-GTIDs=1-40; binlog_event有: 41-80
bin.003 : Previous-GTIDs=1-80; binlog_event有: 81-120
假設現在我們要找GTID=$A,那么MySQL的掃描順序為:
從最后一個binlog開始掃描(即: bin.003)
bin.003的Previous-GTIDs=1-80,如果$A=100 > Previous-GTIDs,那么肯定在bin.003中
bin.003的Previous-GTIDs=1-80,如果$A=79 包含在Previous-GTIDs中,那么繼續對比上一個binlog檔案 bin.002,然后再回圈前面2個步驟,直到找到為止.

GTID相關引數:

引數 comment
gtid_executed 執行過的所有GTID
gtid_purged 丟棄掉的GTID
gtid_mode GTID模式
gtid_next session級別的變數,下一個gtid
gtid_owned 正在運行的GTID
enforce_gtid_consistency 保證GTID安全的引數

開啟GTID的必備條件:
gtid_mode=on (必選)
enforce-gtid-consistency=1 (必選)
log_bin=mysql-bin (可選) #高可用切換,最好開啟該功能
log-slave-updates=1 (可選) #高可用切換,最好打開該功能

4.2 GTID作業原理

master更新資料時,會在事務前產生GTID,一同記錄到binlog日志中,
slave端的i/o 執行緒將變更的binlog,寫入到本地的relay log中,
sql執行緒從relay log中獲取GTID,然后對比slave端的binlog是否有記錄,
如果有記錄,說明該GTID的事務已經執行,slave會忽略,
如果沒有記錄,slave就會從relay log中執行該GTID的事務,并記錄到binlog,
在決議程序中會判斷是否有主鍵,如果沒有就用二級索引,如果沒有就用全部掃描,

4.3 GTID主從配置

環境說明:

資料庫角色 IP 應用與系統版本
主資料庫 192.168.222.250 centos8/redhat8 mysql-5.7
從資料庫 192.168.222.251 centos8/redhat8 mysql-5.7

我這里是直接進行yum/dnf下載mysql的
詳細操作可以看我的《mysql基礎里面》
做之前要確保主從庫的里面資料一樣
主庫配置,vim /etc/my.cnf,添加以下配置,重啟mysql,

[root@master ~]# vim /etc/my.cnf   //添加下面這幾行
log-bin=mysql_bin
server-id=10
gtid_mode=on
enforce-gtid-consistency=true
log-slave-updates=on
[root@master ~]# systemctl restart mysqld.service  //重啟服務

關閉防火墻

[root@master ~]# systemctl stop firewalld.service 
[root@master ~]# systemctl disable firewalld.service 
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@master ~]# vim /etc/selinux/config 
SELINUX=disabled
[root@master ~]# setenforce 0

從庫配置,vim /etc/my.cnf, 添加以下配置,重啟mysql,

[root@slave ~]# vim /etc/my.cnf   //添加下面這幾行
server-id=20
relay-log=myrelay
gtid_mode=on
enforce-gtid-consistency=true
log-slave-updates=on
read_only=on
master-info-repository=TABLE
relay-log-info-repository=TABLE
[root@slave ~]# systemctl restart mysqld.service //重啟服務

關閉防火墻

[root@slave ~]# systemctl stop firewalld.service 
[root@slave ~]# systemctl disable firewalld.service 
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@slave ~]# vim /etc/selinux/config 
SELINUX=disabled
[root@slave ~]# setenforce 0

主庫授權復制用戶,

[root@master ~]# mysql -uroot -plnh123
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 3
Server version: 5.7.39-log MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)

mysql> grant replication slave on *.* to 'repl'@'%' identified by 'repl123!';
Query OK, 0 rows affected, 1 warning (0.01 sec)

從庫設定要同步的主庫資訊,并開啟同步,

[root@slave ~]# mysql -uroot -p'lnh123'
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.39 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> change master to master_host='192.168.222.250',master_port=3306,master_user='repl',master_password='repl123!',master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.222.250
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_bin.000001
          Read_Master_Log_Pos: 3044
               Relay_Log_File: myrelay.000002
                Relay_Log_Pos: 3257
        Relay_Master_Log_File: mysql_bin.000001
             Slave_IO_Running: Yes  //這里必須是yes
            Slave_SQL_Running: Yes  //這里必須是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: 3044
              Relay_Log_Space: 3456
              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: 10
                  Master_UUID: 316e0fd9-1208-11ed-9c1a-000c2905f428
             Master_Info_File: mysql.slave_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: 316e0fd9-1208-11ed-9c1a-000c2905f428:1-13
            Executed_Gtid_Set: 316e0fd9-1208-11ed-9c1a-000c2905f428:1-13
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

配置完之后,通過查看slave的狀態,可以看是否配置成功,同時可以在主庫進行一些操作,提交一些事務(insert,update),之后資料就會自動同步到從庫,
在主庫里面新建xbz庫,和創建表,插入一些資料

[root@master ~]# mysql -uroot -plnh123
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 8
Server version: 5.7.39-log MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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 |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)
mysql> create database xbz;
Query OK, 1 row affected (0.01 sec)

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

mysql> use xbz;
Database changed
mysql> create table hai(id int not null,name varchar(10),age int);
Query OK, 0 rows affected (0.01 sec)
mysql> insert into hai values(1,'xb',20),(2,'hb',21);
Query OK, 2 rows affected (0.03 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from hai;
+----+------+------+
| id | name | age  |
+----+------+------+
|  1 | xb   |   20 |
|  2 | hb   |   21 |
+----+------+------+
2 rows in set (0.00 sec)

在從庫里面查看

[root@slave ~]# mysql -uroot -p'lnh123'
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.39 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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 |
| mysql              |
| performance_schema |
| sys                |
| xbz                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use xbz;
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> select * from hai;
+----+------+------+
| id | name | age  |
+----+------+------+
|  1 | xb   |   20 |
|  2 | hb   |   21 |
+----+------+------+
2 rows in set (0.02 sec)

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/500714.html

標籤:Linux

上一篇:RAR Extractor - ZIP Unarchiver for Mac(rar壓縮解壓工具)

下一篇:Topaz Gigapixel AI for Mac(圖片無損放大軟體)

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • CA和證書

    1、在 CentOS7 中使用 gpg 創建 RSA 非對稱密鑰對 gpg --gen-key #Centos上生成公鑰/密鑰對(存放在家目錄.gnupg/) 2、將 CentOS7 匯出的公鑰,拷貝到 CentOS8 中,在 CentOS8 中使用 CentOS7 的公鑰加密一個檔案 gpg -a ......

    uj5u.com 2020-09-10 00:09:53 more
  • Kubernetes K8S之資源控制器Job和CronJob詳解

    Kubernetes的資源控制器Job和CronJob詳解與示例 ......

    uj5u.com 2020-09-10 00:10:45 more
  • VMware下安裝CentOS

    VMware下安裝CentOS 一、軟硬體準備 1 Centos鏡像準備 1.1 CentOS鏡像下載地址 下載地址 1.2 CentOS鏡像下載程序 點擊下載地址進入如下圖的網站,選擇需要下載的版本,這里選擇的是Centos8,點擊如圖所示。 決定選擇Centos8后,選擇想要的鏡像源進行下載,此 ......

    uj5u.com 2020-09-10 00:12:10 more
  • 如何使用Grep命令查找多個字串

    如何使用Grep 命令查找多個字串 大家好,我是良許! 今天向大家介紹一個非常有用的技巧,那就是使用 grep 命令查找多個字串。 簡單介紹一下,grep 命令可以理解為是一個功能強大的命令列工具,可以用它在一個或多個輸入檔案中搜索與正則運算式相匹配的文本,然后再將每個匹配的文本用標準輸出的格式 ......

    uj5u.com 2020-09-10 00:12:28 more
  • git配置http代理

    git配置http代理 經常遇到克隆 github 慢的問題,這里記錄一下幾種配置 git 代理的方法,解決 clone github 過慢。 目錄 git配置代理 git單獨配置github代理 git配置全域代理 配置終端環境變數 git配置代理 主要使用 git config 命令 git單獨 ......

    uj5u.com 2020-09-10 00:12:33 more
  • Linux npm install 裝包時提示Error EACCES permission denied解

    npm install 裝包時提示Error EACCES permission denied解決辦法 ......

    uj5u.com 2020-09-10 00:12:53 more
  • Centos 7下安裝nginx,使用yum install nginx,提示沒有可用的軟體包

    Centos 7下安裝nginx,使用yum install nginx,提示沒有可用的軟體包。 18 (flaskApi) [root@67 flaskDemo]# yum -y install nginx 19 已加載插件:fastestmirror, langpacks 20 Loading ......

    uj5u.com 2020-09-10 00:13:13 more
  • Linux查看服務器暴力破解ssh IP

    在公網的服務器上經常遇到別人爆破你服務器的22埠,用來挖礦或者干其他嘿嘿嘿的事情~ 這種情況下正確的做法是: 修改默認ssh的22埠 使用設定密鑰登錄或者白名單ip登錄 建議服務器密碼為復雜密碼 創建普通用戶登錄服務器(root權限過大) 建立堡壘機,實作統一管理服務器 統計爆破IP [root ......

    uj5u.com 2020-09-10 00:13:17 more
  • CentOS 7系統常見快捷鍵操作方式

    Linux系統中一些常見的快捷方式,可有效提高操作效率,在某些時刻也能避免操作失誤帶來的問題。 ......

    uj5u.com 2020-09-10 00:13:31 more
  • CentOS 7作業系統目錄結構介紹

    作業系統存在著大量的資料檔案資訊,相應檔案資訊會存在于系統相應目錄中,為了更好的管理資料資訊,會將系統進行一些目錄規劃,不同目錄存放不同的資源。 ......

    uj5u.com 2020-09-10 00:13:35 more
最新发布
  • vim的常用命令

    Vim的6種基本模式 1. 普通模式在普通模式中,用的編輯器命令,比如移動游標,洗掉文本等等。這也是Vim啟動后的默認模式。這正好和許多新用戶期待的操作方式相反(大多數編輯器默認模式為插入模式)。 2. 插入模式在這個模式中,大多數按鍵都會向文本緩沖中插入文本。大多數新用戶希望文本編輯器編輯程序中一 ......

    uj5u.com 2023-04-20 08:43:21 more
  • vim的常用命令

    Vim的6種基本模式 1. 普通模式在普通模式中,用的編輯器命令,比如移動游標,洗掉文本等等。這也是Vim啟動后的默認模式。這正好和許多新用戶期待的操作方式相反(大多數編輯器默認模式為插入模式)。 2. 插入模式在這個模式中,大多數按鍵都會向文本緩沖中插入文本。大多數新用戶希望文本編輯器編輯程序中一 ......

    uj5u.com 2023-04-20 08:42:36 more
  • docker學習

    ###Docker概述 真實專案部署環境可能非常復雜,傳統發布專案一個只需要一個jar包,運行環境需要單獨部署。而通過Docker可將jar包和相關環境(如jdk,redis,Hadoop...)等打包到docker鏡像里,將鏡像發布到Docker倉庫,部署時下載發布的鏡像,直接運行發布的鏡像即可。 ......

    uj5u.com 2023-04-19 09:26:53 more
  • 設定Windows主機的瀏覽器為wls2的默認瀏覽器

    這里以Chrome為例。 1. 準備作業 wsl是可以使用Windows主機上安裝的exe程式,出于安全考慮,默認情況下改功能是無法使用。要使用的話,終端需要以管理員權限啟動。 我這里以Windows Terminal為例,介紹如何默認使用管理員權限打開終端,具體操作如下圖所示: 2. 操作 wsl ......

    uj5u.com 2023-04-19 09:25:49 more
  • docker學習

    ###Docker概述 真實專案部署環境可能非常復雜,傳統發布專案一個只需要一個jar包,運行環境需要單獨部署。而通過Docker可將jar包和相關環境(如jdk,redis,Hadoop...)等打包到docker鏡像里,將鏡像發布到Docker倉庫,部署時下載發布的鏡像,直接運行發布的鏡像即可。 ......

    uj5u.com 2023-04-19 09:19:04 more
  • Linux學習筆記

    IP地址和主機名 IP地址 ifconfig可以用來查詢本機的IP地址,如果不能使用,可以通過install net-tools安裝。 Centos系統下ens33表示主網卡;inet后表示IP地址;lo表示本地回環網卡; 127.0.0.1表示代指本機;0.0.0.0可以用于代指本機,同時在放行設 ......

    uj5u.com 2023-04-18 06:52:01 more
  • 解決linux系統的kdump服務無法啟動的問題

    問題:專案麒麟系統服務器的kdump服務無法啟動,沒有相關日志無法定位問題。 1、查看服務狀態是關閉的,重啟系統也無法啟動 systemctl status kdump 2、修改grub引數,修改“crashkernel”為“512M(有的機器數值太大太小都會導致報錯,建議從128M開始試,或者加個 ......

    uj5u.com 2023-04-12 09:59:50 more
  • 解決linux系統的kdump服務無法啟動的問題

    問題:專案麒麟系統服務器的kdump服務無法啟動,沒有相關日志無法定位問題。 1、查看服務狀態是關閉的,重啟系統也無法啟動 systemctl status kdump 2、修改grub引數,修改“crashkernel”為“512M(有的機器數值太大太小都會導致報錯,建議從128M開始試,或者加個 ......

    uj5u.com 2023-04-12 09:59:01 more
  • 你是不是暴露了?

    作者:袁首京 原創文章,轉載時請保留此宣告,并給出原文連接。 如果您是計算機相關從業人員,那么應該經歷不止一次網路安全專項檢查了,你肯定是收到過資訊系統技術檢測報告,要求你加強風險監測,確保你提供的系統服務堅實可靠了。 沒檢測到問題還好,檢測到問題的話,有些處理起來還是挺麻煩的,尤其是線上正在運行的 ......

    uj5u.com 2023-04-05 16:52:56 more
  • 細節拉滿,80 張圖帶你一步一步推演 slab 記憶體池的設計與實作

    1. 前文回顧 在之前的幾篇記憶體管理系列文章中,筆者帶大家從宏觀角度完整地梳理了一遍 Linux 記憶體分配的整個鏈路,本文的主題依然是記憶體分配,這一次我們會從微觀的角度來探秘一下 Linux 內核中用于零散小記憶體塊分配的記憶體池 —— slab 分配器。 在本小節中,筆者還是按照以往的風格先帶大家簡單 ......

    uj5u.com 2023-04-05 16:44:11 more