广告
返回顶部
首页 > 资讯 > 数据库 >Mysql/Mariadb主从复制
  • 488
分享到

Mysql/Mariadb主从复制

Mysql/Mariadb主从复制 2021-03-22 03:03:47 488人浏览 绘本
摘要

概念 什么是·Mysql/Mariadb主从复制?     mysql/Mariadb主从复制:当Master(主)数据库发生变化的时候,变化实时会同步到slave(从)数据库中; 类似于:Samba共享文件(C/S)、NFS网络文件

概念

什么是·Mysql/Mariadb主从复制?    

mysql/Mariadb主从复制:当Master(主)数据库发生变化的时候,变化实时会同步到slave(从)数据库中;
类似于:Samba共享文件(C/S)、NFS网络文件共享(C/S),当服务端(Server)发生变化时,客户端(client)数据内容会根据服务端进行改变;

好处

  • 水平扩展数据库的负载能力,后备数据库,主数据库服务器故障后,可切换到从数据库继续工作;
  • 容错、高可用,从数据库可用来做备份、数据统计等工作,这样不影响主数据库的性能;
  • 数据分布;
  • 数据备份;

实现原理

在master机器上,主从同步事件会被写到特殊的log文件中(binary-log);

主从同步事件有3种形式:statement、row、mixed。

 statement:会将对数据库操作的sql语句写入到binlog中。
 row:会将每一条数据的变化写入到binlog中。
 mixed:statement与row的混合。Mysql决定什么时候写statement格式的,什么时候写row格式的binlog。

整体上来说,复制有3个步骤:

  • master将改变记录到二进制日志(binary log)中(这些记录叫做二进制日志事件,binary log events);
  • slave将master的binary log events拷贝到它的中继日志(relay log);
  • slave重做中继日志中的事件,将改变反映它自己的数据。

下面这章图可以详细解释其原理:

主从复制过程

说的简单一些就是:

当对Master数据库不管做了增、删、改还是创建了数据库、表等操作时,Slave就会快速的接受这些数据以及对象的操作,从而实现主从数据复制,保证数据的一致性。  

实战

我记得我学PHP开发的时候,教员经常说的一句话就是:学习半小时,实战一分钟;
好了,接下来到我们实战的时刻了,认真听讲哟!!! 

环境介绍

系统环境:系统基本上都差不多,一般多数都是linux平台和windows平台比较多,不管什么样的系统环境对这次实战的操作都影响不大,我在这里使用的是Docker虚拟出来的Centos操作系统,当然您可以选用ubuntu、RedHat以及Windows系统,这些都不会影响到大的操作;

我这里使用的系统版本:

[root@master /]# cat /etc/redhat-release
CentOS Linux release 8.0.1905 (Core) 

这里会用到两台服务器:其中一台MasterIP172.18.0.2,另外一个slaveIP172.18.0.3
数据库版本:(我这里使用的Mariadb,你可以使用mysql数据库)

[root@master /]# mysql --version
mysql  Ver 15.1 Distrib 10.3.11-MariaDB, for Linux (x86_64) using readline 5.1

配置Master数据库

  1.更改Master配置文件
    找到下面文件:

mysql数据库:/etc/mysql/mysql,conf.d/mysqld.cnf
mariadb数据库:/etc/my.cnf.d/mariadb.cnf

注意:我这里是使用yum进行安装的所以默认配置文件是在/etc下面,建议在修改上面两个文件时要先将配置文件进行备份 

修改以下配置:  

bind-address=172.18.0.2		\指定Master地址
server-id = 1					\指定唯一的serverid		部分版本没有需手动写入
log_bin = /var/log/mariadb/mariadb-bin.log				\开启binlog			部分版本没有需手动写入

注意:log_bin这个字段需根据实际情况来定,需找到数据库的日志文件,默认实在 /var/log

   2.重新启动数据库

[root@master my.cnf.d]# systemctl restart mariadb		\centos7、centos8、ubuntu重新启动方式
[root@master my.cnf.d]# server  mariadb  restart		\centos6及以下版本使用这个重新启动方式

  mysql重新启动: 

[root@master my.cnf.d]# systemctl restart mysqld		\centos7、centos8、ubuntu重新启动方式
[root@master my.cnf.d]# server  mysqld  restart		\centos6及以下版本使用这个重新启动方式

  3.初始化数据库

[root@master my.cnf.d]# mysql_secure_installation 

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we"ll need the current
passWord for the root user.  If you"ve just installed MariaDB, and
you haven"t set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 					//这里敲回车
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y						//这里是设置root密码,可不进行设置
New password: 					//新密码
Re-enter new password: 			//旧密码
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
Go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

NORMally, root should only be allowed to connect from "localhost".  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named "test" that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you"ve completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!
[root@master my.cnf.d]# 

  4.创建主从同步的用户 

 [root@master ~]# mysql -u root -p				\登陆数据库
Enter password: 			\输入root密码
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MariaDB connection id is 18
Server version: 10.3.11-MariaDB-log 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)]> GRANT REPLICATION SLAVE on *.* to "slave"@"%" IDENTIFIED BY "redhat";		
\创建用户,并设置相应的权限
	\此处%表示允许从任何地方(除本地外)使用此账号进行登陆使用,在正式环境建议具体到某台主机IP
Query OK, 0 rows affected (0.000 sec)			\表示sql语句执行成功

  5.更新Slave用户权限 

 MariaDB [(none)]> flush privileges;		\每次修改用户权限,都要使用这个sql语句进行更新
Query OK, 0 rows affected (0.000 sec)

  6.导出数据库中所有数据(此步骤取决于slave的权限)

[root@master ~]# mysqldump -u root -p --all-databases --master-data > mariadb.bat.sql
	--all-databases		\此参数表示备份所有数据库
	--master-data		\此参数表示将二进制的信息写入到输出文件中,在这里是写入到备份的sql文件中
Enter password:

  7.查看MASTERr REPLICATION LOG位置

 MariaDB [(none)]> show master status;
+--------------------+----------+--------------+------------------+
| File               | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| mariadb-bin.000002 |     1974 |              |                  |
+--------------------+----------+--------------+------------------+
1 row in set (0.000 sec)

配置Slave数据库

  1.更改Slave配置文件

   文件位置与Master位置一致

mysql数据库:/etc/mysql/mysql,conf.d/mysqld.cnf
mariadb数据库:/etc/my.cnf.d/mariadb.cnf

   注意:我这里是使用yum进行安装的所以默认配置文件是在/etc下面,建议在修改上面两个文件时要先将配置文件进行备份

  修改以下配置:

bind-address=172.18.0.3		\指定Master地址
server-id = 2					\指定唯一的serverid		部分版本没有需手动写入
log_bin = /var/log/mariadb/mariadb-bin.log				\开启binlog			部分版本没有需手动写入

  注意:log_bin这个字段需根据实际情况来定,需找到数据库的日志文件,默认实在 /var/log

  2.重新启动数据库

[root@master my.cnf.d]# systemctl restart mariadb		\centos7、centos8、ubuntu重新启动方式
[root@master my.cnf.d]# server  mariadb  restart		\centos6及以下版本使用这个重新启动方式

  mysql重新启动:

[root@master my.cnf.d]# systemctl restart mysqld		\centos7、centos8、ubuntu重新启动方式
[root@master my.cnf.d]# server  mysqld  restart		\centos6及以下版本使用这个重新启动方式

  3.初始化数据库

[root@master my.cnf.d]# mysql_secure_installation 

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we"ll need the current
password for the root user.  If you"ve just installed MariaDB, and
you haven"t set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 					//这里敲回车
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y						//这里是设置root密码,可不进行设置
New password: 					//新密码
Re-enter new password: 			//旧密码
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from "localhost".  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named "test" that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you"ve completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!
[root@master my.cnf.d]# 

  4.从Master将数据库备份复制到slave服务器

[root@slave my.cnf.d]# scp jia@172.18.0.2:/opt/mariadb.bat.sql /opt/
jia@172.18.0.2"s password: 
mariadb.bat.sql                   

  5.将备份数据恢复到slave数据库

[root@slave my.cnf.d]# mysql -u root -p < /opt/mariadb.bat.sql 
 Enter password: 

  6.使slave与master建立连接

[root@slave my.cnf.d]# mysql -u root -p
Enter password: 
ERROR 1045 (28000): Access denied for user "root"@"localhost" (using password: YES)
[root@slave my.cnf.d]# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MariaDB connection id is 22
Server version: 10.3.11-MariaDB-log 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)]> stop slave;
Query OK, 0 rows affected, 1 warning (0.000 sec)

MariaDB [(none)]> CHANGE MASTER TO
    -> MASTER_HOST = "172.18.0.2",		\指定Master数据库地址
    -> MASTER_USER = "slave",			\指定主从复制用户名
    -> MASTER_PASSWORD = "redhat",			\指定主从复制用户密码
    -> MASTER_LOG_FILE = "mariadb-bin.000002",		\指定Master数据库的binlog文件名
    -> MASTER_LOG_POS=1974;			
Query OK, 0 rows affected (0.290 sec)

MariaDB [(none)]> start slave;			\开启复制功能
Query OK, 0 rows affected (0.019 sec)

MariaDB [(none)]>

  注意:lMASTER_LOG_FILE="mariadb-bin.000002与MASTER_LOG_POS=1974的值,是从上面的 SHOW MASTER STATUS 得到的。

  到这里已经可以做到主从复制了下面让我们测试一下吧

验证数据库是否同步

测试方法很简单,只需要在主数据库上面创建数据库或者增加一条记录就可以测试是否主从复制配置成功

MariaDB [(none)]> show  databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.018 sec)

MariaDB [(none)]> create database a;			\在主数据库创建a数据库
Query OK, 1 row affected (0.063 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| a                  |
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.000 sec)

  下面我们来看看从数据库上面有没有a这个数据库吧

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| a                  |
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.075 sec)

  我们会发现已经有了a这个数据库

您可能感兴趣的文档:

--结束END--

本文标题: Mysql/Mariadb主从复制

本文链接: https://www.lsjlt.com/news/2098.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Mysql/Mariadb主从复制
    概念 什么是·Mysql/Mariadb主从复制?     Mysql/Mariadb主从复制:当Master(主)数据库发生变化的时候,变化实时会同步到slave(从)数据库中; 类似于:Samba共享文件(C/S)、NFS网络文件...
    99+
    2021-03-22
    Mysql/Mariadb主从复制
  • MariaDB 10.3 主从复制
    基本信息服务器1:     MariaDB01   192.168.10.178服务器2:     MariaDB02&nbs...
    99+
    2022-10-18
  • 初识MariaDB之8——GTID主从复制
    一、背景介绍在MySQL5.6之前,主从复制是通过binlog和position实现的,当A主机宕机后,B主机成为新的主节点,此时在C主机上需要使用sql语句:CHANGE MASTER TO MASTER...
    99+
    2022-10-18
  • mysql主从复制
    一.主从复制简介 2015年5月28日11时,12小时后恢复,损失:平均每小时106.48W$ 1)高可用 2)辅助备份 3)分担负载 复制是 MySQL 的一项功能,允许服务器将更改从一个实例复制到另一个实例。 1)主服务器将所有数据和...
    99+
    2019-04-07
    mysql主从复制
  • 【MySQL】主从复制
    纸上得来终觉浅,绝知此事要躬行。 概述 复制是指将主数据库的DDL 和 DML 操作通过二进制日志传到从库服务器中,然后在从库上对这些日志重新执行(也叫重做),从而使得从库和主库的数据保持同步。 MySQL支持一台主库同时向多...
    99+
    2015-07-01
    【MySQL】主从复制
  • mysql 主从复制
    1, 准备二台机器或者服务器 ,保持mysq 版本一样或者版本相差不大; 主机:114.215.198.39 从机:116.62.234.228 2    新建一个数据库 我的数据库是hlqzxm; 进入主机的mysql 配置文件中修改配置...
    99+
    2021-10-15
    mysql 主从复制 数据库入门 数据库基础教程 数据库 mysql
  • mysql主从复制
    两台mysql版本号为5.7以上   主库配置文件设置(注意设置在[mysqld]项中) 主库的ip地址为:192.168.1.1 server-id=1 #唯一id,主库设置1 log-bin=mysql-bin #日志记录...
    99+
    2018-10-04
    mysql主从复制 数据库入门 数据库基础教程 数据库 mysql
  • mysql主从复制--一主一从
    1、启动实例3306和33072、检查主库配置[root@client 3306]# egrep  "log-bin|server|sock"  my.cnfsocket &n...
    99+
    2022-10-18
  • mariadb多源主从复制错误跳过.md
    mysql 的主从错误跳过和mariadb的多源主从复制错误跳过操作不同,请注意: STOP SLAVE 'slave_account'; SET @@default_master_connection =...
    99+
    2022-10-18
  • centos8 mysql 主从复制
    ♥️作者:小刘在C站 ♥️个人主页:小刘主页 ♥️每天分享云计算网络运维课堂笔记,努力不一定有收获,但一定会有收获加油!一起努力,共赴美好人生! ♥️夕阳下,是最美的绽放,树高千尺,落叶归根人生不易,人间真情 目录 Linu...
    99+
    2023-08-31
    数据库 mysql 服务器
  • Mysql之主从复制
    文章目录 一.Mysql主从复制介绍 1.Mysql主从复制原理2.Mysql的复制类型3.Mysql主从复制的工作过程 二.搭建 Mysql主从复制1.首先关闭防火墙2.Mysql主从服...
    99+
    2023-09-04
    mysql java 数据库
  • mysql+ssl主从复制
    主从复制原理 作为主服务器Master, 会把自己的每一次改动都记录到 二进制日志 Binarylog 中。 (从服务器I/O thread会负责来读取master binary log, 然后写入自身r...
    99+
    2022-10-18
  • MySQL-5.5.33主从复制
    搭建主从同步需要在两个电脑上分别安装 MySQL ,我这里安装的是 CentOS6.7 64位,MySQL-5.5.33。MySQL 是二进制包安装的:http://aby028.blog.51cto.co...
    99+
    2022-10-18
  • Mysql 主从复制GTID
    --------------------------------------------安装准备配置/etc/my.cnf主master grant 分配复制帐号从slave ...
    99+
    2022-10-18
  • MySQL主从复制(二)
    主从架构中:从node是不接受w操作的,否则可能会导致数据不一致。一、复制架构中应该注意的问题: 1.限制slave为只读模式 可以设置在启动参数中。 > show global variable...
    99+
    2022-10-18
  • Mysql的主从复制
    Mysql的主从复制   Mysql的主从复制,我们一般用来保证数据间的同步关系,比如有分公司,我们就需要把数据同步给千里之外的分公司,这样就很方便快捷。这个实验我们还实现了ssl安全连...
    99+
    2022-10-18
  • MySQL 主从复制问题
    导致SQL线程故障原因分析及解决方案 原因 1. 版本差异,参数设定不同,比如:数据类型的差异,SQL_MODE影响 2. 要创建的数据库对象已经存在 3. 要删除或修改的对象不存在 4. DML语句不符合表定义及约束时. 原因是由...
    99+
    2021-08-11
    MySQL 主从复制问题
  • MySQL进阶——主从复制
      1.主从复制的基本原理 slave会从master读取binlog来进行数据同步。主要有以下三个步骤: ①master将改变记录到二进制日志(binary log),这些记录过程叫做二进制日志事件(binary log e...
    99+
    2019-05-12
    MySQL进阶——主从复制
  • Mysql主从复制搭建
    1.mysql主库会把所有的写操作记录在binlog日志中,并且生成log dump线程,将binlog日志传给从库的I/O线程,从库生成两个线程,一个是I/O线程,另外一个是SQL线程。 主将更改操作记录到binlog里从将主的binlo...
    99+
    2020-09-28
    Mysql主从复制搭建
  • MySQL设置主从复制
    主从复制: 单向,双向,环形,级联,一主多从 双机复制的5种情形1、异步主从(默认常规)2、双写(前段程序对两个数据库同时写,必须两边都落实,程序才返回成功)3、利用外挂软件实现实时主库Binlog日志抓...
    99+
    2022-10-18
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作