MySQL mysqldump搭建主从复制

实验环境


此次实验的环境如下

  • MySQL 5.7.30

  • Centos 7.4 

  • 操作系统账号:mysql

  • 数据库复制账号:repl

  • 复制格式:基于行的复制

IP地址主从关系复制账号复制格式
192.168.179.102主库replRow-Based
192.168.179.103从库replRow-Based

这节我们的内容为MySQL的复制,MySQL复制有两种形式

  • 基于二进制日志文件位置
  • 基于GTID

这节为第一种基于二进制日志文件位置

 

1. 开启二进制日志功能


无论是使用哪种方式我们都需要启用二进制日志功能

如果未开启则需要在my.cnf文件中加入如下参数,需要重启数据库生效

主库

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
server-id=1
log-bin=/var/lib/mysql/mysql-bin
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

从库

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
server-id=2
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

 我们需要保证server-id不一样,从库使用了read_only参数确保无其他写入,之后重启数据库。

 

2. 查看UUID是否一致


需要注意的是如果从库是由主库克隆而来,这时的uuid是一样的,这样也会报错

该文件位于daadir的auto.cnf文件中

vim /var/lib/mysql/auto.cnf 

如果一样可删除该文件后重新启动数据库即可,这时会生成一个新的文件

 

3. 建立复制账号


接下来我们建立一个独立的用于复制的账号

主库和从库

mysql> set global validate_password_policy=0;
mysql> set global validate_password_length=1;
mysql> grant replication slave on *.* to 'repl'@'192.168.179.%' identified by 'repl';
mysql> flush privileges;

 

4. 备份主库


我们通过mysqldump备份主库的文件

主库192.168.179.102

#在还未主从同步的时候创建表来测试,看看主从同步之后该表会不会在从库上面
mysql> create database luleidb;
Query OK, 1 row affected (0.01 sec)

mysql> use luleidb;
Database changed
mysql> create table test(id int,name varchar(10));
Query OK, 0 rows affected (0.02 sec)


mysql> insert into test values(1,'lulei');
Query OK, 1 row affected (0.01 sec)

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

[root@localhost ~]# mysqldump -S /var/lib/mysql/mysql.sock -uroot -p --all-databases  --single-transaction --master-data=2 --set-gtid-purged=off   --triggers --events --routines> /tmp/dumpmaster.sql
Enter password: 
[root@localhost ~]# ll /tmp/dumpmaster.sql 
-rw-r--r-- 1 root root 851421 Jun  2 20:41 /tmp/dumpmaster.sql




mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |      779 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
[root@localhost ~]# head -n 60 /tmp/dumpmaster.sql 
-- MySQL dump 10.13  Distrib 5.7.30, for Linux (x86_64)
--
-- Host: localhost    Database: 
-- ------------------------------------------------------
-- Server version	5.7.30-log

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Position to start replication or point-in-time recovery from
--

-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=779;

--
-- Current Database: `luleidb`
--

CREATE DATABASE /*!32312 IF NOT EXISTS*/ `luleidb` /*!40100 DEFAULT CHARACTER SET latin1 */;

USE `luleidb`;



#可以看到mysqldump当中使用--master-data=2的时候,-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=779;会被注释掉,在pos点779之前已经创建了测试的luleidb库和test表,这部分数据需要使用sqldump导出至从库还原

 

5. 文件传输


接下来将主库的dump文件传到备份,之后更改备库的文件权限

主库 :192.168.179.102

[root@localhost ~]# scp /tmp/dumpmaster.sql  root@192.168.179.103:/tmp/
root@192.168.179.103's password: 
dumpmaster.sql                                                                             100%  831KB  16.8MB/s   00:00 

从库:192.168.179.103

[root@localhost ~]# chown mysql:mysql /tmp/dumpmaster.sql 

 

6. 备库导入数据


接下来我们将备份的数据导入到备份

[root@localhost ~]# mysql -S /var/lib/mysql/mysql.sock  -uroot -p </tmp/dumpmaster.sql
Enter password: 

 

7.开始同步


接下来我们开启同步首先我们查看dumpmaster.sql文件中master的信息

-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=779;

之后使用如下命令开启同步 

mysql> change master to
    -> master_host='192.168.179.102',master_port=3306,master_user='repl',master_password='repl',master_log_file='mysql-bin.000002',master_log_pos=779;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

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

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.179.102
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 779
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000002
             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: 779
              Relay_Log_Space: 531
              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: 1
                  Master_UUID: 18f5da07-a096-11ea-8c70-000c290e1abf
             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)

主要关注如下几点

  • Slave_IO_Running需要为YES
  • Slave_SQL_Running需要为YES
  • Seconds_Behind_Master需要为0

查看主从同步之前创建的库和表是否存在,下面的结果就是为什么要使用--master-data的好处(在pos点之前数据通过mysqldump导出的dumpmaster.sql进行恢复,之后的数据恢复需要从库从指定pos点来同步主库) 

#可以看到数据都在
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| luleidb            |
| mysql              |
| mytest             |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

mysql> use luleidb;
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> show tables;
+-------------------+
| Tables_in_luleidb |
+-------------------+
| test              |
+-------------------+
1 row in set (0.00 sec)

mysql> select * from test;
+------+-------+
| id   | name  |
+------+-------+
|    1 | lulei |
+------+-------+
1 row in set (0.00 sec)

 

8. 重启和重置复制


使用如下命令关闭重启

mysql>stop slave;
mysql>start slave;

我们可以独立的重启IO进程或者SQL进程

mysql>stop slave sql_thread;
mysql>stop slave io_thread;

mysql>start slave io_thread;
mysql>start slave sql_thread;

使用如下命令重置复制

mysql>reset slave all;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值