MySQL搭建主从复制--使用mysqldump备份进行恢复(二)

1.mysqldump备份

[root@localhost ~]# mysqldump -S /tmp/mysql.sock -A --master-data=2 --single-transaction  -R --triggers >/data/backup/full.sql

2.在备机进行source

source /data/backup/full.sql

mysql> select @@server_id;
+-------------+
| @@server_id |
+-------------+
|           8 |
+-------------+
1 row in set (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| gtid               |
| mysql              |
| performance_schema |
| sys                |
| test               |
| world              |
+--------------------+
7 rows in set (0.00 sec)

3.在备份文件搜索change master to,找到备份事件的position号和binlog文件

image

4.查看主库的binlog目前的position号(该步骤只是测试是否准确)

mysql> show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000018 |      649 |              |                  | 09dd0d62-8b86-11ec-bd6f-000c29245cff:1-9 |
+------------------+----------+--------------+------------------+------------------------------------------+

5.在主库中创建一个测试表,模拟白天备份期间有业务

mysql> use test;
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 t3(id int);
Query OK, 0 rows affected (0.01 sec)

6.在主库中创建复制用户repl

grant replication slave on . to repl@'IP' identified by '123'

7.在备机执行change maste to并启动主从复制关系

执行help change master to查看帮助

 help change master to
 


从帮助中获取change master to语句进行修改

image

8.查看复制关系是否正常

mysql> CHANGE MASTER TO
    ->   MASTER_HOST='192.168.1.38',
    ->   MASTER_USER='repl',
    ->   MASTER_PASSWORD='123',
    ->   MASTER_PORT=3306,
    ->   MASTER_LOG_FILE='mysql-bin.000018',
    ->   MASTER_LOG_POS=649,
    ->   MASTER_CONNECT_RETRY=10;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> 
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.1.38
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 10
              Master_Log_File: mysql-bin.000018
          Read_Master_Log_Pos: 811
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 482
        Relay_Master_Log_File: mysql-bin.000018
             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: 811
              Relay_Log_Space: 693
              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: 6
                  Master_UUID: 09dd0d62-8b86-11ec-bd6f-000c29245cff
             Master_Info_File: /data/3308/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: 09dd0d62-8b86-11ec-bd6f-000c29245cff:10
            Executed_Gtid_Set: 09dd0d62-8b86-11ec-bd6f-000c29245cff:1-10
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

ERROR: 
No query specified

9.主库插入数据测试

## mysql3308(从库)

![image](https://img2022.cnblogs.com/blog/2191731/202202/2191731-20220219200348710-1656470028.png)

## mysql3307(主库)

![image](https://img2022.cnblogs.com/blog/2191731/202202/2191731-20220219200611633-666914518.png)



## 插入数据


![image](https://img2022.cnblogs.com/blog/2191731/202202/2191731-20220219200555107-1083854458.png)


## 在mysql3308上查看是否数据有复制过来

![image](https://img2022.cnblogs.com/blog/2191731/202202/2191731-20220219200640977-102009315.png)



如果执行help change master to 报错如下

mysql> help contents;

Nothing found

Please check if 'help tables' are loaded.

可以执行下面的语句

mysql -uxxxx -pxxxx mysql < /soft/mysql/share/fill_help_tables.sql

posted @ 2022-02-19 20:11  南大仙  阅读(301)  评论(0)    收藏  举报