数据库
首页 > 数据库> > MySQL复制从库建立-xtracebackup方式

MySQL复制从库建立-xtracebackup方式

作者:互联网

Percona XtraBackup工具提供了一种在系统运行时执行MySQL数据热备份的方法。

Percona XtraBackup在事务系统上执行联机非阻塞,紧密压缩,高度安全的完整备份,因此在计划的维护时段内,应用程序仍保持完全可用。

环境准备:

master:192.168.0.106:3306 
slave:192.168.0.105:3306

datadir: /data/mysql/mysql3306/data

cnfpath: /data/mysql/mysql3306/my3306.cnf

XtraBackup 安装,两台机器都安装:

下载地址:https://www.percona.com/downloads/Percona-XtraBackup-LATEST/

TheMaster$ wget https://www.percona.com/downloads/Percona-XtraBackup-LATEST/Percona-XtraBackup-8.0-7/binary/redhat/7/x86_64/percona-xtrabackup-80-8.0.7-1.el7.x86_64.rpm
TheMaster$ yum install -y percona-xtrabackup-80-8.0.7-1.el7.x86_64.rpm

1、在Master制作一个备份

TheMaster$ mkdir xtrabackup_backupfiles
TheMaster$ xtrabackup --defaults-file=/data/mysql/mysql3306/my3306.cnf -S /tmp/mysql3306.sock -uroot -p'' --backup --target-dir=./xtrabackup_backupfiles
...
completed OK!

为了让快照是一致的:需要prepare备份数据:

TheMaster$ xtrabackup -S /tmp/mysql3306.sock -uroot -p --prepare --target-dir=./xtrabackup_backupfiles/
...
completed OK!

undo log目录需要处理下,默认会解压到xtrabackup_backupfiles目录下,在my3306.cnf中定义在/data/mysql/mysql3306/data/undolog/下,所以:

TheMaster$ cd xtrabackup_backupfiles && mkdir undolog && mv undo_* undolog/

2、复制备份数据到Slave上

使用rsync或scp将数据从Master复制到Slave。如果直接将数据同步到slave的数据目录,建议在这之前先停止mysqld。

TheMaster$ rsync -avpP -e ssh ./xtrabackup_backupfiles/ 192.168.0.105:/data/mysql/mysql3306/data/

复制数据前,您可以备份原始或先前安装的MySQL datadir(注意:在移动mysqld的内容或将快照移动到其datadir之前,请确保mysqld已关闭。):

TheSlave$ mv /data/mysql/mysql3306/data /data/mysql/mysql3306/data_bak

复制数据后,确保MySQL有权限访问

TheSlave$ chown -R mysql:mysql /data/mysql/mysql3306/data

3、创建Master上配置复制账号

TheMaster|mysql> GRANT REPLICATION SLAVE ON *.*  TO 'repl'@'192.168.0.%' IDENTIFIED BY 'repl';

测试连接

TheSlave$ mysql --host=192.168.0.106 --user=repl --password=repl
mysql> SHOW GRANTS;

4、配置Slave上的MySQL服务器

首先将my3306.cnf从TheMaster复制到TheSlave:

TheSlave$ scp root@192.168.0.106:/data/mysql/mysql3306/my3306.cnf /data/mysql/mysql3306/
TheSlave$ chown -R mysql:mysql /data/mysql/mysql3306/data/my3306.cnf

修改TheSlave上的my3306.cnf:

server-id=1053306  # 建议ip最后一位+端口号

启动TheSlave 上的 mysqld

TheSlave$ /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql3306/my3306.cnf &

观察error.log是否有错误

TheSlave$ more /data/mysql/mysql3306/data/error.log 

5、启动复制

TheSlave$ cat /data/mysql/mysql3306/data/xtrabackup_binlog_info
mysql-bin.000012        195     e8f74dde-ed8e-11e9-8ebb-000c29f5c092:1-3

执行CHANGE MASTER语句在MySQL命令行:

TheSlave|mysql> CHANGE MASTER TO
                MASTER_HOST='192.168.0.106',
                MASTER_USER='repl',
                MASTER_PASSWORD='repl',
                MASTER_AUTO_POSITION=1;

开始复制:

TheSlave|mysql> START SLAVE;

6、检查

TheSlave|mysql> SHOW SLAVE STATUS \G
         ...
         Slave_IO_Running: Yes
         Slave_SQL_Running: Yes
         ...
         Seconds_Behind_Master: 0
         ...

看到IO和SQL线程处于运行中 (Yes)表示复制正常。

root@localhost 22:42:41 [(none)]> show slave status\G
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    16
Current database: *** NONE ***

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.106
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000016
          Read_Master_Log_Pos: 383
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 557
        Relay_Master_Log_File: mysql-bin.000016
             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: 383
              Relay_Log_Space: 769
              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: 1063306
                  Master_UUID: e8f74dde-ed8e-11e9-8ebb-000c29f5c092
             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: e8f74dde-ed8e-11e9-8ebb-000c29f5c092:4
            Executed_Gtid_Set: e8f74dde-ed8e-11e9-8ebb-000c29f5c092:1-4
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
       Master_public_key_path: 
        Get_master_public_key: 0
            Network_Namespace: 
1 row in set (0.00 sec)

 

标签:MySQL,xtracebackup,mysql3306,Master,mysql,从库,data,TheSlave,xtrabackup
来源: https://www.cnblogs.com/augustuss/p/14136691.html