MariaDB数据库主从复制实现步骤

时间:2021-11-08 11:02:40

一.MariaDB简介

MariaDB数据库的主从复制方案,是其自带的功能,并且主从复制并不是复制磁盘上的数据库文件,而是通过binlog日志复制到需要同步的从服务器上.

MariaDB数据库支持单向、双向、链式级联等不同业务场景的复制.在复制的过程中,一台服务器充当主服务器(Master),接收来自用户的内容更新,而一个或多个其他的服务器充当从服务器(slave),接收来自Master上binlog文件的日志内容,解析出SQL,重新更新到Slave,使得主从服务器数据达到一致.

主从复制的逻辑有以下几种:

1)一主一从,单向主从同步模式,只能在Master端写入数据;

MariaDB数据库主从复制实现步骤

2)一主多从,

MariaDB数据库主从复制实现步骤

3)双主复制逻辑架构,此架构可以在Master1或Master2进行数据写入,或者两端同时写入(特殊设置);

MariaDB数据库主从复制实现步骤

在生产环境中,MySQL主从复制都是异步的复制方式,即不是严格的实时复制,但是给用户的体验都是实时的.MySQL主从复制集群功能使得MySQL数据库支持大规模高并发读写成为可能,且有效的保护了服务器宕机的数据备份.

二.应用场景

利用复制功能当Master服务器出现问题时,我们可以人工的切换到从服务器继续提供服务,此时服务器的数据和宕机时的数据几乎完全一致.复制功能也可用作数据备份,但是如果人为的执行drop,delete等语句删除,那么从库的备份功能也就失效了.

三.主从机制实现原理

(1)master将改变记录到二进制日志(binary log)中(这些记录叫做二进制日志事件,binary log events);

(2)slave将master的binary log events拷贝到它的中继日志(relay log);

(3)slave重做中继日志中的事件,将改变反映它自己的数据;

MariaDB数据库主从复制实现步骤

四.主从实验步骤

主库配置

1.查看数据库状态

systemctl status mariadb

2.停止mariadb

systemctl stop mariadb

3.修改配置文件

vi /etc/my.cnf
# 修改内容如下:
# server-id服务的唯一标识(主从之间都必须不同);
# log-bin启动二进制日志名称为mysql-bin
[mysqld]
server-id=1
log-bin=apollo-mysql-bin

4.重启mariadb

systemctl start mariadb

5.新建用于主从同步的用户apollo

[root@localhost etc]# mysql -uroot -p

MariaDB [mysql]> create user 'apollo'@'%' identified by 'apollo123';
Query OK, 0 rows affected (0.00 sec)

6.如果提示密码太简单不符合策略加在前面加这句,否则可以忽略.

mysql> set global validate_password_policy=0;

7.给从库账号授权

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

8.检查主库创建的复制账号

MariaDB [(none)]> select user,host,password from mysql.user;
+--------+-----------------------+-------------------------------------------+
| user | host | password |
+--------+-----------------------+-------------------------------------------+
| root | localhost | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root | localhost.localdomain | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root | 127.0.0.1 | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root | ::1 | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| apollo | % | *F4C3606BB4D20E38BDAC60DD383666A1F3D72A67 |
+--------+-----------------------+-------------------------------------------+
6 rows in set (0.00 sec)

9.检查授权账号的权限

MariaDB [(none)]> show grants for apollo@'%';
+-----------------------------------------------------------------------------------------------------------------+
| Grants for apollo@% |
+-----------------------------------------------------------------------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO 'apollo'@'%' IDENTIFIED BY PASSWORD '*91310B9B3DD9D3B34E510A8C8BEE1096516C0C94' |
+-----------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

10.实现对主数据库锁表只读,防止数据写入,数据复制失败.

MariaDB [mysql]> flush table with read lock;
Query OK, 0 rows affected (0.02 sec)

11.检查主库的状态

MariaDB [(none)]> show master status;
+--------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| a-mysql-bin.000001 | 615 | | |
+--------------------+----------+--------------+------------------+
1 row in set (0.01 sec) # File是二进制日志文件名,
# Position是日志开始的位置,后面从库会用到.

12.锁表后,一定要单独再打开一个SSH窗口,导出数据库的所有数据.

[root@localhost ~]# mysqldump -uroot -p --all-databases > /opt/root.sql

13.确保数据导出后,没有数据插入,完毕再查看主库状态.

MariaDB [(none)]> show master status;
+--------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| a-mysql-bin.000001 | 615 | | |
+--------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

14.将备份导出的数据scp至Slave数据库

[root@localhost ~]# scp /opt/zdk.sql root@192.168.13.183:/opt/

The authenticity of host '192.168.13.183 (192.168.13.183)' can't be established.
ECDSA key fingerprint is SHA256:inppMYl6FXIzV/XOV6TTlSUmP4TY96mz6sujwtTY7nk.
ECDSA key fingerprint is MD5:69:b7:4c:cc:18:86:94:be:d9:63:4d:4c:cb:62:fa:67.
# 输入yes
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.13.183' (ECDSA) to the list of known hosts.
# 输入数据库密码
root@192.168.13.183's password:
zdk.sql 100% 467KB 5.0MB/s 00:00

从库设置(192.168.13.183)

1.此时去从库的mysql上,登录,导入主库的数据,保持数据一致性.

mysql -uroot -p
source /opt/root.sql # 这一步完成后,再登录数据库,如出现密码不正确,请输入主库数据库密码.

2.查看数据库状态

systemctl status mariadb

3.停止mariadb

systemctl stop mariadb

4.从库的配置,写入my.cnf,从库的身份信息

vi /etc/my.cnf
[mysqld]
server-id=10

5.检查一下主库和从库的参数信息

主库:
MariaDB [(none)]> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 1 |
+---------------+-------+
1 row in set (0.00 sec) MariaDB [(none)]> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin | ON |
+---------------+-------+
1 row in set (0.00 sec)
从库:
MariaDB [(none)]> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 10 |
+---------------+-------+
1 row in set (0.00 sec) MariaDB [(none)]> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin | OFF |
+---------------+-------+
1 row in set (0.01 sec)

6.通过一条命令,开启主从同步

# 主库IP地址:'192.168.13.189'
# 如果以下信息不知道,可以通过命令show master status;
change master to master_host='192.168.13.189',
master_user='apollo',
master_password='apollo123',
master_log_file='a-mysql-bin.000001',
master_log_pos=615;
MariaDB [(none)]> change master to master_host='192.168.13.189',
-> master_user='apollo',
-> master_password='apollo123',
-> master_log_file='a-mysql-bin.000001',
-> master_log_pos=615;
Query OK, 0 rows affected (0.05 sec)

7.查看主从同步的状态

show slave status\G;

8.开启从库的slave同步

start slave;

9.再次查看主从同步的状态

show slave status\G; 

# 查看两条参数,确保主从正常
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

10.解锁主库,恢复可写

MariaDB [(none)]> unlock tables;
Query OK, 0 rows affected (0.00 sec)

11.主库创建一个数据库db2

# 这个是在主库里面执行的哦
MariaDB [(none)]> create database db2;
Query OK, 1 row affected (0.00 sec)

12.去从库里面查看数据库中是否有db2

MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| db1 |
| db2 |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
5 rows in set (0.00 sec)

五.总结

暂时没啥好总结的,就先按照上面的步骤进行吧!如有问题,后续更正!