使用mysqldump搭建主从库

文章目录

  • my.cnf参数前提条件
      • 参数配置情况
  • 使用mysqldump备份
    • 备份
    • 参数说明
    • 备份文件传给从库
  • 恢复数据
    • 报错-1
    • 报错-1: 解决办法
    • 报错-2
    • 报错-2: 解决办法
  • 配置同步
    • 主库创建同步用户
    • 备库配置同步
    • 开启slave
    • 错误-3
    • 错误-3:解决办法
  • 验证同步
    • 主库更新数据
    • 从库检查数据

my.cnf参数前提条件

搭建一套最简单、最简易的mysql主从架构环境,至少my.cnf参数文件应具有以下参数配置:

参数 说明
server_id 1033306 区分每个数据库的唯一ID,可由IP+Port组成
gtid_mode on gtid 功能需要开启
enforce_gtid_consistency on gtid的另一个功能需要开启
biglog_format row binlog功能需要开启

参数配置情况

最最简易版的参数

  • 以下为主库配置文件:
[root@pg01 backups]# more /etc/my.cnf 
[mysqld]
basedir         = /usr/local/mysql
datadir         = /data/mysql/data
port            = 3306
secure_file_priv= ''
log-bin         = binlog
server-id       = 1033306
gtid_mode       = on
enforce_gtid_consistency = on
  • 以下为从库配置文件:
[root@pg01 backups]# more /etc/my.cnf 
[mysqld]
basedir         = /usr/local/mysql
datadir         = /data/mysql/data
port            = 3306
secure_file_priv= ''
log-bin         = binlog
server-id       = 1043306
gtid_mode       = on
enforce_gtid_consistency = on

使用mysqldump备份

备份

[root@pg01 backups]# time mysqldump --master-data=2 --single-transaction -p -A >mysqldump_`date +%Y%m%d`.sql
Enter password: 
Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database. If you don't want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events. 

real    0m2.764s
user    0m0.034s
sys     0m0.027s

参数说明

time 可显示备份过程所用时间

--master-data=2 将产生对应的binlog日志位置信息,在进行增量备份时,可从此记录的log位置之后进行备份。查看备份文件内容时将会发现以下部分的内容:

-- CHANGE MASTER TO MASTER_LOG_FILE='binlog.000005', MASTER_LOG_POS=424; 

备份文件传给从库

scp mysqldump_20200318.sql  192.168.56.104:/date/mysql/backups

恢复数据

恢复数据时,命令就简单很多了

time mysql -p </data/mysql/backups/mysqldump_20200318.sql

恢复完数据后一定要登陆到数据库刷以下内存:

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

报错-1

在从库上恢复数据时候,遇到了ERROR 1840

[root@pg02 backups]# time mysql -p 
Enter password: 
ERROR 1840 (HY000) at line 24: @@GLOBAL.GTID_PURGED can only be set when @@GLOBAL.GTID_EXECUTED is empty.

报错-1: 解决办法

登陆从库数据库内,重制master

[root@pg02 backups]# mysql -uroot -pmysql

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

报错-2

如果恢复完数据之后,没有执行flush privileges命令,那么此后在启动slave的时候将会获得以下报错信息:

Last_SQL_Error: Error 'Operation CREATE USER failed for 'repl'@'%'' on query. Default database: ''. Query: 'CREATE USER 'repl'@'%' IDENTIFIED WITH 'mysql_native_password' AS '*A424E797037BF97C19A2E88CF7891C5C2038C039''

看上去说不能创建user。

报错-2: 解决办法

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

配置同步

主库创建同步用户


mysql> create user 'repl'@'%'  identified  by 'repl'; 
Query OK, 0 rows affected (0.01 sec)

mysql> grant replication slave on *.* to 'repl'@'%' ;
Query OK, 0 rows affected (0.00 sec)

备库配置同步

change master to master_host='192.168.56.103' , 
master_port=3306 , 
master_user='repl' , 
master_password='repl' , 
master_auto_position=1;

开启slave

start slave;

查看slave 进程状态是否正常,看到是以下的内容为 正常:

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.56.103
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: binlog.000005
          Read_Master_Log_Pos: 1700
               Relay_Log_File: pg02-relay-bin.000003
                Relay_Log_Pos: 445
        Relay_Master_Log_File: binlog.000005
             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: 1700
              Relay_Log_Space: 1495
              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: 103
                  Master_UUID: 3a38867c-6423-11ea-9918-080027a9c49a
             Master_Info_File: /data/mysql/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: 3a38867c-6423-11ea-9918-080027a9c49a:5-7
            Executed_Gtid_Set: 3a38867c-6423-11ea-9918-080027a9c49a:1-7,
a67c95bf-f374-11ea-bc88-0800271dc433:1
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

错误-3

如果在查看状态的时候发现以下报错,无法连接到master,则说明你的服务器防火墙没有关闭

mysql> show slave status\G

                Last_IO_Error: error connecting to master '[email protected]:3306' - retry-time: 60  retries: 1

错误-3:解决办法

查看防火墙状态(此为linux 6 的检查方式)

[root@pg02 backups]# service iptables status
Table: filter
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
2    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           
3    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
4    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22 
5    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         
1    REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         

发现防火墙为启动状态,关闭防火墙:

[root@pg02 backups]# service iptables stop
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]

验证同步

主库更新数据

mysql> select * from dong.t1;
+------+-------+----------+------+
| id   | name  | city     | age  |
+------+-------+----------+------+
|    6 | dd    | qhd      |   11 |
+------+-------+----------+------+
11 rows in set (0.00 sec)

mysql> update dong.t1 set name='cbr500' where city='qhd';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from dong.t1;
+------+--------+----------+------+
| id   | name   | city     | age  |
+------+--------+----------+------+
|    6 | cbr500 | qhd      |   11 |
+------+--------+----------+------+

从库检查数据

mysql> update dong.t1 set name=‘cbr500’ where city=‘qhd’;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from dong.t1;
±-----±-------±---------±-----+
| id | name | city | age |
±-----±-------±---------±-----+
| 6 | cbr500 | qhd | 11 |
±-----±-------±---------±-----+

你可能感兴趣的:(MySQL,mysql)