Mariadb主从复制

一、什么是主从复制

Mariadb主从复制是Mariadb集群的基础,在Mariadb集群中为了保证数据的一致型, 需要在Mariadb集群前端添加读写分离器,只将写入操作交给指定节点写入数据,这个 节点就是主节点(又叫Master);将读操作交给集群中其他不执行写入操作的节点,这 个节点就是从节点(又叫Slaves),那么从节点的数据从那里来?就是复制主节点的上 面的数据,

二、主从复制的作用

数据分布:能够将数据分散到多个位置,可以实现异地灾备 负载均衡:读操作,适用于读密集型的应用 备份:备份的时候可以停止从节点进行备份,备份更安全 高可用和故障切换:主节点故障,可以将从节点提升为主节点(需要手动实现,或者脚 本监控实现,不如corosync或者heartbeat之类高可用方案) Mariadb升级测试:高版本数据库通过复制获得数据,然后进行测试

三、复制的工作流程

mariadb 主从数据库重启 mariadb主从模式_二进制日志

1、用户向主节点数据库写入数据
2、主节点将所有引起数据库更改的语句记录到bin-log日志文件中
3、从库的IO进程,不停的发送信息询问主库是否有数据更新,发送请求之前会读取 master.info文件,获取上次记录的pos位置和连接主库的用户名和密码
4、主库IO线程收到从库请求,拿从库上次记录主库的pos点和主库bin-log日志当前 记录的pos点进行对比,如果主库pos点的数值大于从库上次记录的pos点,就说明数 据发生改变,发送bin-log的更新内容给从库;如果主库pos点的数值等于从库上次记 录的pos点,说明数据没有更改,则继续接收从库请求,进行判断
5、从库IO线程收到主库的更新内容,将pos的更新信息记录到master.info文件中, 下次从库在请求主库更新则是从当前记录的pos位置开始;并且将主库更新的SQL语 句记录到relay-log日志文件中。
6、SQL线程从relay-log中读取日志信息,在本地完成重放;(此时从库更新完成)
7、将所有引起从库更改的语句记录到从库的bin-log日志文件中;由于bin-log日志 主要是用于数据恢复,并且在主库已经存在所以在从库开启会占用不必要的性能开销 ,推荐关闭

四、复制时应该注意的问题

1:复制如何开始
主节点运行很长时间,且已经有一定规模的数据,如何启动复制? 解决方法:在主节点做一个完全备份,并记录二进制日志文件及位置;在从节点恢 复此完全备份,并在启动复制时从记录的二进制日志文件和位置开始;

2:如何限制从服务器只读
在从服务器启动read_only;但仅对非具有SUPER权限的用户有效; 阻止所有用户 :MariaDB> FLUSH TABLES WITH READ LOCK;

3:如何保证主从复制时的事务安全
二进制日志默认在内存中会有缓冲,当一个事务提交的时候,这个数据就会被持久 的存储到innodb文件中去了,但是和这个事务相关的二进制日志有可能还在内存 中,所以从服务器是无法找到这种二进制日志的,所以需要尽可能做到一旦事务提 交了,就立即把相关的二进制日志保存到硬盘中,以保证事务安全。

五、主从复制配置

master节点配置
############################################################
#修改主机名
[root@localhost ~]# vim /etc/hostname
m.gzt.org
#配置ip地址和其对应主机名的文件
[root@localhost ~]# vim /etc/hosts
192.168.22.160  m       m.gzt.org
192.168.22.161  s       s.gzt.org
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
~ iu
#SELinux模式修改
[root@localhost ~]# vim /etc/selinux/config 
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=permissive//此处有修改
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted
[root@localhost ~]# reboot
#修改防火墙
[root@m ~]# firewall-cmd --add-port=3306/tcp
success
[root@m ~]# firewall-cmd --add-port=3306/tcp --permanent 
success
#yum安装mariadb-server
[root@m ~]# yum -y install mariadb-server
Loaded plugins: fastestmirror, langpacks
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
base                                                     | 3.6 kB     00:00     
extras                                                   | 2.9 kB     00:00     
updates                                                  | 2.9 kB     00:00     
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
。。。
。。。
。。。
Dependency Updated:
  mariadb-libs.x86_64 1:5.5.68-1.el7                                            

Complete!

[root@m ~]#  vim /etc/my.cnf
[mysqld]
log-bin=/var/log/mysql/master-bin  // 启用二进制日志;
server-id= 1  //设置一个在当前集群中惟一的server-id(主/从服务器的ID不能重复);
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
...
...
[root@m ~]# cd /var/log
[root@m log]# mkdir mysql //创建目录用于存放日志文件
[root@m log]# chown -R mysql:mysql mysql/
[root@m log]# ll
drwxr-xr-x. 2 mysql        mysql       6 Jan 14 20:48 mysql
##设置mariadb开机启动,并启动mariadb
[root@m ~]# systemctl enable mariadb.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@m ~]# systemctl start  mariadb 
#初始化root
[root@m ~]# mysqladmin -u root password gzt041057
[root@m ~]# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
#创建一个有复制权限(REPLICATION SLAVE, REPLICATION CLIENT)账号;
MariaDB [(none)]> grant replication client,replication slave on *.* to 'repluser'@'192.168.22.16%' identified by'gzt041057';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]>  flush privileges;  #设置完成一定要刷新,不然不生效
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> exit
Bye

[root@m ~]#  vim /etc/my.cnf
[mysqld]
log-bin=/var/log/mysql/master-bin
server-id= 1
sync_binlog=1 //#同步二进制日志,事务提交则马上将内存中的二进制日志同步到磁盘
。。。。
。。。。
#####如果用到的为InnoDB存储引擎:
#####innodb_flush_log_at_trx_commit=1  #innodb在事务提交时立即将日志文件写入到磁盘
#####innodb_support_xa=on  #支持分布式事务
[root@m ~]# systemctl restart mariadb.service 


slave节点配置 /安装mariadb-server之前的步骤操作说明见master节点配置
############################################################
[root@localhost ~]# vim /etc/hostname
s.gzt.org
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33

[root@localhost ~]# vim /etc/hosts
192.168.22.160  m       m.gzt.org
192.168.22.161  s       s.gzt.org
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
~                                                           [root@localhost ~]# vim /etc/selinux/config 
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=permissive
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted
[root@localhost ~]# reboot
[root@s ~]# firewall-cmd --add-port=3306/tcp
success
[root@s ~]# firewall-cmd --add-port=3306/tcp --permanent 
success

[root@s ~]# yum -y install mariadb-server
Loaded plugins: fastestmirror, langpacks
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
base                                                     | 3.6 kB     00:00     
extras                                                   | 2.9 kB     00:00     
updates                                                  | 2.9 kB     00:00     
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
。。。
。。。
。。。
Dependency Updated:
  mariadb-libs.x86_64 1:5.5.68-1.el7                                            

Complete!

[root@s ~]#  vim /etc/my.cnf
[mysqld]
relay-log = relay-bin  # 启用中继日志;
server-id = 10
read-only = on    #关闭从服务器写入功能
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

[root@s ~]# systemctl enable mariadb.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[root@s ~]# systemctl start mariadb.service 
[root@s ~]# mysqladmin -u root password gzt041057
[root@s ~]# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.22.160',MASTER_USER='repluser',MASTER_PASSWORD='gzt041057';
Query OK, 0 rows affected (0.02 sec)
#启动Mariadb slave的 IO和SQL线程
MariaDB [(none)]>  start slave;
Query OK, 0 rows affected (0.03 sec)
#查看从服务器复制状态,IO和SQL线程还是处于关闭状态,需要手动启动才会开始复制
MariaDB [(none)]> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Connecting to master
                  Master_Host: 192.168.22.160
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: 
          Read_Master_Log_Pos: 4
               Relay_Log_File: relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: 
             Slave_IO_Running: Yes#值为no表示没有开启IO线程
            Slave_SQL_Running: Yes#值为no表示没有开启SQL线程
               。。。
               。。。
               
#"Slave_SQL_Running: No" 解决方案  
#首先在Slave主机上停掉Slave服务:
MariaDB [(none)]> stop slave;
Query OK, 0 rows affected (0.00 sec)


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

MariaDB [(none)]>change master to
master_host='192.168.22.160',master_user='repluser',master_password='gzt041057',master_port=3306,master_log_file='master-bin.000005',master_log_pos=245;

MariaDB [(none)]>  start slave;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.22.160
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000005
          Read_Master_Log_Pos: 245
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 530
        Relay_Master_Log_File: master-bin.000005
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
            
#############################################################
Master服务创建数据库并查看数据库  
MariaDB [(none)]> create database abc;
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| abc                |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)
#slave服务器查看数据库
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| abc                |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.10 sec)


DB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| abc                |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.10 sec)