Mysql學習之XtraBackup(4)-------使用XtraBackup搭建Mysql主從

-------配置Master-Slave(基於位點)

 環境介紹:
        服務器:RHEL 6.8
        Master_Port:3306
        Slave_Port:3307
        Mysql:5.7.19-log
        Xtrabackup:xtrabackup version 2.4.15

Master部分:
1.Master設置複製用戶,授權
mysql>grant replication slave on *.* to 'repl'@'%' identified by '111111' ;
Query OK, 0 rows affected, 1 warning (0.37 sec)

2.Master設置參數,開啓binlog
log-bin=/u01/mysql/log/mysql-bin
server-id=10241
log_slave_updates=1
#binlog-do-db=test
#binlog-do-db=testrecovery
(binlog-do-db可以指定哪些庫啓用binlog日誌記錄,本次實驗我沒用這個)

3.使用Xtrabackup備份

[root@rhel6 data]# xtrabackup --defaults-file=/etc/my.cnf --backup -uroot -pxxx --target-dir=/u01/mysqlbackup/all-20190731bak

4.prepare備份集
       (prepare只需要指定備份集路徑即可,網上很多例子中的賬號、密碼、參數文件等參數理論上都無需指定)

[root@rhel6 data]#xtrabackup --prepare --target-dir=/u01/mysqlbackup/all-20190731bak/

Slave部分:
1.停服務、備份data、還原備份集

[root@rhel6 data]#mysqladmin -uroot -xxx --socket=/tmp/mysql3307.sock shutdown
[root@rhel6 data]#mv data data_bak
[root@rhel6 data]#cp -R /u01/mysqlbackup/all-20190731bak data
[root@rhel6 data]#chown -R mysql:mysql data/

2.修改參數文件,起服務
server-id=10242
log-bin=/u01/mysql3307/log/mysql3307-bin
#replicate-do-db=test
#replicate-do-db=testrecovery
log_slave_updates=1
(replicate-do-db可以指定哪些庫啓用binlog複製,本次實驗我也沒用這個)

[root@rhel6 data]#mysqld_safe --defaults-file=/etc/my3307.cnf &

3.從xtrabackup_binlog_info中獲取master_log_file和master_pos

[root@rhel6 all-20190732bak]# cat xtrabackup_binlog_info 
mysql-bin.000007	1372	5f95521a-b073-11e9-9591-000c296ee978:1-7,
7842c967-0958-11e9-9f6c-000c296ee978:1-36,
eb495b42-b06a-11e9-af85-000c296ee978:1-3,
fad44c35-affe-11e9-b2fc-000c296ee978:1-2

4. 配置複製相關信息

[root@rhel6 ~]# mysql -uroot -pxxx --host=127.0.0.1 --port=3307
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.19-log MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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> select @@port;
+--------+
| @@port |
+--------+
|   3307 |
+--------+
1 row in set (0.00 sec)

mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.18 sec)

mysql> show warnings;
+-------+------+-----------------------------------------------------------+
| Level | Code | Message                                                   |
+-------+------+-----------------------------------------------------------+
| Note  | 3084 | Replication thread(s) for channel '' are already stopped. |
+-------+------+-----------------------------------------------------------+
1 row in set (0.02 sec)

mysql> change master to master_host='192.169.10.241', master_user='repl', master_password='111111',master_port=3306,master_log_file='mysql-bin.000007',master_log_pos=314;
Query OK, 0 rows affected, 2 warnings (0.26 sec)

mysql> show warnings;
+-------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message                                                                                                                                                                                                                                                                              |
+-------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Note  | 1759 | Sending passwords in plain text without SSL/TLS is extremely insecure.                                                                                                                                                                                                               |
| Note  | 1760 | Storing MySQL user name or password information in the master info repository is not secure and is therefore not recommended. Please consider using the USER and PASSWORD connection options for START SLAVE; see the 'START SLAVE Syntax' in the MySQL Manual for more information. |
+-------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

5.起slave進程,查看同步狀態

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

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.169.10.241
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000007
          Read_Master_Log_Pos: 579
               Relay_Log_File: rhel6-relay-bin.000002
                Relay_Log_Pos: 585
        Relay_Master_Log_File: mysql-bin.000007
             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: 579
              Relay_Log_Space: 792
              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: 10241
                  Master_UUID: 5f95521a-b073-11e9-9591-000c296ee978
             Master_Info_File: /u01/mysql3307/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: 5f95521a-b073-11e9-9591-000c296ee978:4
            Executed_Gtid_Set: 5f95521a-b073-11e9-9591-000c296ee978:1-4,
7842c967-0958-11e9-9f6c-000c296ee978:1-36,
eb495b42-b06a-11e9-af85-000c296ee978:1-3,
fad44c35-affe-11e9-b2fc-000c296ee978:1-2,
fdf1c8db-9abf-11e9-bd3c-000c296ee978:1-16
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

6.測試同步
       略

-------配置Master-Slave(基於GTID)

環境介紹:
        服務器:RHEL 6.8
        Master_Port:3306
        Slave_Port:3307
        Mysql:5.7.19-log
        Xtrabackup:xtrabackup version 2.4.15

Master部分:
1.Master設置複製用戶,授權
2.Master設置參數,開啓binlog

log-bin=/u01/mysql/log/mysql-bin
server-id=10241
gtid_mode=on
enforce-gtid-consistency=on

3.使用Xtrabackup備份
4.prepare備份集

Slave部分
1.停服務、備份data、還原備份集
2.修改參數文件,起服務

server-id=10242
log-bin=/u01/mysql3307/log/mysql3307-bin
gtid_mode=on
enforce-gtid-consistency=on
log_slave_updates=1

3.從xtrabackup_binlog_info中獲取Executed_Gtid_Set

[root@rhel6 mysql3307]# cat ./data/xtrabackup_binlog_info 
mysql-bin.000007	314	5f95521a-b073-11e9-9591-000c296ee978:1-3,
7842c967-0958-11e9-9f6c-000c296ee978:1-36,
eb495b42-b06a-11e9-af85-000c296ee978:1-3,
fad44c35-affe-11e9-b2fc-000c296ee978:1-2

4.配置複製相關信息
1)查看slave的Executed_Gtid_Set,如果不爲空的話,需要執行reset master進行清理

mysql> show master status\G
*************************** 1. row ***************************
             File: mysql3307-bin.000002
         Position: 154
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 5f95521a-b073-11e9-9591-000c296ee978:1-3,
7842c967-0958-11e9-9f6c-000c296ee978:1-36,
eb495b42-b06a-11e9-af85-000c296ee978:1-3,
fad44c35-affe-11e9-b2fc-000c296ee978:1-2
1 row in set (0.00 sec)

mysql> reset master;
Query OK, 0 rows affected (0.02 sec)

2) 執行GTID_PURGED

mysql> set session sql_log_bin=0;
Query OK, 0 rows affected (0.01 sec)

mysql> set global gtid_purged='5f95521a-b073-11e9-9591-000c296ee978:1-3,7842c967-0958-11e9-9f6c-000c296ee978:1-36,eb495b42-b06a-11e9-af85-000c296ee978:1-3,fad44c35-affe-11e9-b2fc-000c296ee978:1-2';
Query OK, 0 rows affected (0.01 sec)

mysql> show global variables like '%gtid_purged%';
+---------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Variable_name | Value                                                                                                                                                                   |
+---------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| gtid_purged   | 5f95521a-b073-11e9-9591-000c296ee978:1-3,
7842c967-0958-11e9-9f6c-000c296ee978:1-36,
eb495b42-b06a-11e9-af85-000c296ee978:1-3,
fad44c35-affe-11e9-b2fc-000c296ee978:1-2 |
+---------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

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

3) 配置複製信息

mysql> change master to master_host='192.169.10.241',master_port=3306,master_user='repl',master_password='111111',master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

5.起slave進程,查看同步狀態

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.169.10.241
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000007
          Read_Master_Log_Pos: 2432
               Relay_Log_File: rhel6-relay-bin.000002
                Relay_Log_Pos: 2485
        Relay_Master_Log_File: mysql-bin.000007
             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: 2432
              Relay_Log_Space: 2692
              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: 10241
                  Master_UUID: 5f95521a-b073-11e9-9591-000c296ee978
             Master_Info_File: /u01/mysql3307/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: 5f95521a-b073-11e9-9591-000c296ee978:4-11
            Executed_Gtid_Set: 5f95521a-b073-11e9-9591-000c296ee978:1-11,
7842c967-0958-11e9-9f6c-000c296ee978:1-36,
eb495b42-b06a-11e9-af85-000c296ee978:1-3,
fad44c35-affe-11e9-b2fc-000c296ee978:1-2
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

6.測試同步
       略


 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章