首页 > 技术文章 > The Twelfth week

zpkf 2021-12-01 17:57 原文

当你知道你不再是你的时候,那才是真正的你。

1、主从复制及主主复制的实现

#主节点

[16:13:43 root@zpp-master ~]#dnf -y install mariadb-server

[16:13:43 root@zpp-master ~]#cat /etc/my.cnf

[mysqld]

server-id=128

log-bin

log_slave_update

[16:15:34 root@zpp-master ~]#mysql

MariaDB [(none)]> select version();

+---------------------+

| version()           |

+---------------------+

| 10.5.13-MariaDB-log |

+---------------------+

1 row in set (0.000 sec)

 

MariaDB [(none)]> show master logs;

+----------------------+-----------+

| Log_name             | File_size |

+----------------------+-----------+

| zpp-slave-bin.000001 |       332 |

| zpp-slave-bin.000002 |       332 |

+----------------------+-----------+

MariaDB [(none)]> grant replication slave on *.* to zpp@'192.168.33.%' identified by 'lucklyzpp';

从节点配置

从节点

[16:13:47 root@lcuklyzpp ~]#cat /etc/my.cnf

!includedir /etc/my.cnf.d

[mysqld]

server-id=129

[16:14:35 root@lcuklyzpp ~]#mysql

MariaDB [(none)]> CHANGE MASTER TO

    ->  MASTER_HOST='192.168.33.128',

    ->  MASTER_USER='zpp',

    ->  MASTER_PASSWORD='lucklyzpp',

    ->  MASTER_PORT=3306,

    ->  MASTER_LOG_FILE='zpp-slave-bin.000002',

    ->  MASTER_LOG_POS=332;

Query OK, 0 rows affected (0.004 sec)

 

MariaDB [(none)]> start slave;

MariaDB [(none)]> show slave status\G

*************************** 1. row ***************************

                Slave_IO_State: Waiting for master to send event

                   Master_Host: 192.168.33.128

                   Master_User: zpp

                   Master_Port: 3306

                 Connect_Retry: 60

               Master_Log_File: zpp-slave-bin.000002

           Read_Master_Log_Pos: 531

                Relay_Log_File: lcuklyzpp-relay-bin.000002

                 Relay_Log_Pos: 758

         Relay_Master_Log_File: zpp-slave-bin.000002

              Slave_IO_Running: Yes

             Slave_SQL_Running: Yes

配置主主实现过程

主主复制的配置步骤:

 (1) 各节点使用一个惟一server_id

 (2) 都启动binary logrelay log

 (3) 创建拥有复制权限的用户账号

 (4) 定义自动增长id字段的数值范围各为奇偶

 (5) 均把对方指定为主节点,并启动复制线程

范例:实现两个节点的主主复制模型

#在第一个master节点上实现
[16:47:03 root@zpp-master ~]#cat /etc/my.cnf
[mysqld]
server-id=128
log-bin
auto_increment_offset=1
auto_increment_increment=2
log_slave_update
MariaDB [(none)]> select version();
+---------------------+
| version()           |
+---------------------+
| 10.5.13-MariaDB-log |
MariaDB [(none)]> show master logs;
+------------------------+-----------+
| Log_name               | File_size |
+------------------------+-----------+
| zpp-master1-bin.000001 |       334 |
+------------------------+-----------+
1 row in set (0.001 sec)

MariaDB [(none)]> grant replication slave on *.* to zpp@'192.168.33.%' identified by 'lucklyzpp';
Query OK, 0 rows affected (0.007 sec)

MariaDB [(none)]> CHANGE MASTER TO
    ->   MASTER_HOST='192.168.33.129',
    ->   MASTER_USER='zpp',
    ->   MASTER_PASSWORD='lucklyzpp',
    ->   MASTER_PORT=3306,
    ->   MASTER_LOG_FILE='zpp-master2-bin.000001',
    ->   MASTER_LOG_POS=472;
Query OK, 0 rows affected (0.032 sec
MariaDB [one]> create database two;
Query OK, 1 row affected (0.000 sec)

MariaDB [one]> show slave status\G
*************************** 1. row ***************************
                Slave_IO_State: Waiting for master to send event
                   Master_Host: 192.168.33.129
                   Master_User: zpp
                   Master_Port: 3306
                 Connect_Retry: 60
               Master_Log_File: zpp-master2-bin.000001
           Read_Master_Log_Pos: 604
                Relay_Log_File: zpp-master1-relay-bin.000002
                 Relay_Log_Pos: 693
         Relay_Master_Log_File: zpp-master2-bin.000001
              Slave_IO_Running: Yes
             Slave_SQL_Running: Yes
              
       Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| hellodb            |
| information_schema |
| mysql              |
| performance_schema |
| school             |
+--------------------+
5 rows in set (0.000 sec)

第二个主节点配置

#在第二个master节点上实现
[00:25:31 root@zpp-master2 ~]#cat /etc/my.cnf
[mysqld]
server-id=129
log-bin
auto_increment_offset=2
auto_increment_increment=2

[23:38:51 root@zpp-master2 ~]#systemctl restart mariadb
[23:38:58 root@zpp-master2 ~]#mysql


MariaDB [(none)]> show master logs;
+------------------------+-----------+
| Log_name               | File_size |
+------------------------+-----------+
| zpp-master2-bin.000001 |       604 |
+------------------------+-----------+


MariaDB [(none)]> reset slave;
Query OK, 0 rows affected (0.005 sec)

MariaDB [(none)]> CHANGE MASTER TO
    ->   MASTER_HOST='192.168.33.128',
    ->   MASTER_USER='zpp',
    ->   MASTER_PASSWORD='lucklyzpp',
    ->   MASTER_PORT=3306,
    ->   MASTER_LOG_FILE='zpp-master1-bin.000001',
    ->   MASTER_LOG_POS=1154;
Query OK, 0 rows affected (0.030 sec)

MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.002 sec)

MariaDB [(none)]> show savle status\G
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'savle status' at line 1
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
                Slave_IO_State: Waiting for master to send event
                   Master_Host: 192.168.33.128
                   Master_User: zpp
                   Master_Port: 3306
                 Connect_Retry: 60
               Master_Log_File: zpp-master1-bin.000001
           Read_Master_Log_Pos: 1154
                Relay_Log_File: zpp-master2-relay-bin.000002
                 Relay_Log_Pos: 561
         Relay_Master_Log_File: zpp-master1-bin.000001
              Slave_IO_Running: Yes
             Slave_SQL_Running: Yes
           
                    Last_Errno: 0
                    Last_Error: 
                  Skip_Counter: 0
           Exec_Master_Log_Pos: 1154
               Relay_Log_Space: 876
       Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| dbzpp1             |
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.000 sec)

MariaDB [(none)]> create database school;
Query OK, 1 row affected (0.000 sec)

xtrabackup实现全量+增量+binlog恢复库

数据库全量备份,

wget https://downloads.percona.com/downloads/Percona-XtraBackup-2.4/Percona-XtraBackup-2.4.23/binary/redhat/7/x86_64/percona-xtrabackup-24-2.4.23-1.el7.x86_64.rpm
[10:11:11 root@lucklyzpp7 ~]yum -y install  percona-xtrabackup-24-2.4.23-1.el7.x86_64.rpm
[10:12:11 root@lucklyzpp7 ~]xtrabackup -uroot -pZhengpeng@1 --backup --target-dir=/backup/base
[10:12:33 root@lucklyzpp7 backup]#ls
base
[10:59:11 root@lucklyzpp7 ~]#scp -r /backup/base root@192.168.33.130:/backup
[11:10:43 root@lucklyzpp7 ~]#mysql

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hellodb            |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.01 sec)

全量恢复

[root@Lucklyzpp ~]#systemctl stop mysqld
[root@Lucklyzpp ~]#xtrabackup --copy-back --target-dir=/backup/base
[root@Lucklyzpp ~]#rm -rf /data/mysql/
[root@Lucklyzpp ~]# xtrabackup --copy-back --target-dir=/backup/base
[root@Lucklyzpp ~]# service mysqld start
[root@Lucklyzpp ~]# systemctl start mysqld

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hellodb            |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql>

增量备份数据库

[root@Lucklyzpp ~]# xtrabackup -uroot -pZhengpeng@1 --backup --target-dir=/backup/base

mysql> create table student(
    -> id int auto_increment primary key,
    -> name varchar(10));
Query OK, 0 rows affected (0.03 sec)

mysql> insert into student (id , name ) values ('1','xiaopeng');
Query OK, 1 row affected (0.02 sec)

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

[root@Lucklyzpp ~]# xtrabackup -uroot -pZhengpeng@1 --backup --target-dir=/backup/inc1 --incremental-basedir=/backup/base
mysql> insert into student (id , name ) values ('2','pengpeng');
Query OK, 1 row affected (0.00 sec)

mysql> select * from student;
+----+----------+
| id | name     |
+----+----------+
|  1 | xiaopeng |
|  2 | pengpeng |
+----+----------+
2 rows in set (0.00 sec)

[root@Lucklyzpp ~]# xtrabackup -uroot -pZhengpeng@1 --backup --target-dir=/backup/inc2 --incremental-basedir=/backup/inc1
[root@Lucklyzpp ~]# ls /backup/
base/ inc1/ inc2/ 
[root@Lucklyzpp ~]# scp -r /backup/* root@'192.168.33.126':/backup/

增量恢复数据库

[11:11:38 root@lucklyzpp7 ~]#yum -y install percona-xtrabackup-24-2.4.23-1.el7.x86_64.rpm
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)
[17:05:45 root@lucklyzpp7 ~]#ls /backup/
base/ inc1/ inc2/ 
[17:21:06 root@lucklyzpp7 ~]##xtrabackup --prepare --apply-log-only --target-dir=/backup/base
[17:21:06 root@lucklyzpp7 ~]##xtrabackup --prepare --apply-log-only --target-dir=/backup/base --incremental-dir=/backup/inc1
[17:21:06 root@lucklyzpp7 ~]##xtrabackup --prepare --apply-log-only --target-dir=/backup/base --incremental-dir=/backup/inc2
[17:21:06 root@lucklyzpp7 ~]#systemctl stop mysqld
[17:21:06 root@lucklyzpp7 ~]#rm -rf /data/mysql/*
[17:21:06 root@lucklyzpp7 ~]#xtrabackup --copy-back --target-dir=/backup/base
[17:16:28 root@lucklyzpp7 ~]#chown -R mysql:mysql /data/mysql
[17:17:02 root@lucklyzpp7 ~]#service mysqld start
Starting MySQL.Logging to '/data/mysql/mysql.log'.

ysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hellodb            |
| luckly             |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
7 rows in set (0.01 sec

 

推荐阅读