mysql数据库主从复制

文章目录

  • mysql数据库主从复制
    • 1:简介
    • 2:主从形式
    • 3:主从复制原理
      • 4.1 确保主从数据库里的数据都要一样
      • 4.2在主数据库里创建一个同步账号授权给从数据库使用
      • 4.3 配置主数据库配置文件
      • 4.4配置从数据库配置文件
      • 4.5 关闭主从服务器的防火墙和selinux
      • 4.6 测试
      • 5:mysql多实例做主从复制
      • 6:mysql配置systemctl管理

1:简介

数据库运行时,一些因素可能会导致服务运行不正常,用户访问数据受阻。对于互联网公司,尤其是购物网站而言,这种情况造成的损失是无法估量的。因此,对数据库进行“备份”也是必不可少的操作。当主要的数据库死机时,系统能够快速地切换到备用的数据库上。本章将详细介绍数据库集群中的主从复制原理和操作流程。

2:主从形式

一主一从
主主复制
一主多从—扩展系统读取的性能,因为读是在从库读取的
多主一从—5.7开始支持
联级复制

3:主从复制原理

主从复制又被称为AB复制,主要用于实现数据库集群中的数据同步。实现MySQL的AB复制时,数据库的版本应尽量保持一致。
在主从复制集群中,主数据库把数据更改的操作记录到二进制日志中,从数据库分别启动I/O线程和SQL线程,用于将主数据库中的数据复制到从数据库中。其中,I/O线程主要将主数据库上的日志复制到自己的中继日志中,SQL线程主要用于读取中继日志中的事件,并将其重放到从数据库之上。另外,系统会将I/O线程已经读取的二进制日志的位置信息存储在master.info文件中,将SQL线程已经读取的中继日志的位置信息存储在relay-log.info文件中。随着版本的更新,在MySQL 5.6.2之后,MySQL允许将这些状态信息保存在Table中,不过在更新之前需要用户在配置文件中进行声明,具体的参数如下。

4:主从复制配置
步骤
1·确保从数据库与主数据库里的数据一样
2·在主数据库里创建一个同步账号授权给从数据库使用
3·配置主数据库(修改配置文件)
4·配置从数据库(修改配置文件)

环境说明

数据库角色 ip 应用与系统版本 有无数据
主数据库 192.168.226.139 centos8
从数据库 192.168.226.158 redhat8

在实验开始之前要确保两台服务器上有mysql服务

4.1 确保主从数据库里的数据都要一样

查看有哪些库哪些表


mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| nian               |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.01 sec)mysql>
mysql> use nian;Database changed
mysql> show tables;
+----------------+
| Tables_in_nian |
+----------------+
| xuan           |
+----------------+
1 row in set (0.00 sec)mysql>

全备主数据库,并给数据库上锁

mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)[root@master ~]# mysqldump -uroot -p123456789 --all-databases > all-2022-07-29.sql                                                                          mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@master ~]# ls
公共  模板  视频  图片  文档  下载  音乐  桌面  all-2022-07-29.sql  anaconda-ks.cfg  initial-setup-ks.cfg
[root@master ~]# scp all-2022-07-29.sql  root@192.168.226.158:/
The authenticity of host '192.168.226.158 (192.168.226.158)' can't be established.
ECDSA key fingerprint is SHA256:ruZxylllox/yy17nVIQPDXNcQoydqXbjt6rjkhTQgyE.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.226.158' (ECDSA) to the list of known hosts.
root@192.168.226.158's password:
all-2022-07-29.sql                                                                                                          100%  856KB  65.8MB/s   00:00
[root@master ~]#
[root@clave /]# ls
all-2022-07-29.sql  bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

解除主库的锁表状态,直接退出即可


mysql> exit
Bye

在从库上恢复主库的备份并查看从库有哪些库,确保与主库一致

[root@slave /]# mysql -uroot -p123456789
mysql> source all-2022-07-29.sqlmysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| nian               |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)mysql> use nian;
Database changed
mysql> show tables;
+----------------+
| Tables_in_nian |
+----------------+
| xuan           |
+----------------+
1 row in set (0.00 sec)

4.2在主数据库里创建一个同步账号授权给从数据库使用

mysql> create user 'xuanning'@'192.168.226.158' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'xuanning'@'192.168.226.158';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

4.3 配置主数据库配置文件


[root@master local]# vim /etc/my.cnflog-bin=g
server-id=2000
[root@master data]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
mysql> show master status;
+----------+----------+--------------+------------------+-------------------+
| File     | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------+----------+--------------+------------------+-------------------+
| g.000002 |      154 |              |                  |                   |
+----------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

4.4配置从数据库配置文件

[root@slave /]# vim /etc/my.cnf
log-bin=j
server-id=2001[root@slave /]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL.Logging to '/opt/data/slave.err'.SUCCESS!mysql> change master to-> master_host='192.168.226.139' ,-> master_user = 'xuanning' ,-> master_password = '123456' ,-> master_log_file = 'g.000002' ,-> master_log_pos = 154;
Query OK, 0 rows affected, 2 warnings (0.02 sec)mysql> start slave;
Query OK, 0 rows affected (0.02 sec)mysql> show slave status \G
*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 192.168.226.139Master_User: xuanningMaster_Port: 3306Connect_Retry: 60Master_Log_File: g.000003Read_Master_Log_Pos: 154Relay_Log_File: slave-relay-bin.000007Relay_Log_Pos: 351Relay_Master_Log_File: g.000003Slave_IO_Running: Yes   <<这里一定要是yesSlave_SQL_Running: Yes

4.5 关闭主从服务器的防火墙和selinux

[root@slave /]# systemctl stop firewalld.service
[root@slave /]# vim /etc/selinux/config
SELINUX=disable
[root@slave /]# setenforce 0

4.6 测试

查看主从服务器表的内容

mysql> use nian;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
mysql> select * from xuan;
+----+----------+------+
| id | name     | age  |
+----+----------+------+
|  1 | xuanning |   21 |
|  2 | nianxia  |   22 |
|  3 | zhangsan |   23 |
|  4 | lisi     |   24 |
+----+----------+------+
4 rows in set (0.00 sec)mysql> use nian;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
mysql> select * from xuan;
+----+----------+------+
| id | name     | age  |
+----+----------+------+
|  1 | xuanning |   21 |
|  2 | nianxia  |   22 |
|  3 | zhangsan |   23 |
|  4 | lisi     |   24 |
+----+----------+------+
4 rows in set (0.00 sec)

在主服务器中插入数据


mysql> insert xuan(id,name,age) values (5,'zhaosi',30);
Query OK, 1 row affected (0.01 sec)mysql> select * from xuan;
+----+----------+------+
| id | name     | age  |
+----+----------+------+
|  1 | xuanning |   21 |
|  2 | nianxia  |   22 |
|  3 | zhangsan |   23 |
|  4 | lisi     |   24 |
|  5 | zhaosi   |   30 |
+----+----------+------+
5 rows in set (0.00 sec)

在从数据库中查看是否同步过来

mysql> select * from xuan;
+----+----------+------+
| id | name     | age  |
+----+----------+------+
|  1 | xuanning |   21 |
|  2 | nianxia  |   22 |
|  3 | zhangsan |   23 |
|  4 | lisi     |   24 |
|  5 | zhaosi   |   30 |
+----+----------+------+
5 rows in set (0.00 sec)

5:mysql多实例做主从复制

在主数据库中创建远程连接用户用作同步

mysql> create user 'xuanning'@'192.168.226.158' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'xuanning'@'192.168.226.158';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

关闭防火墙和selinux


[root@slave /]# systemctl stop firewalld.service
[root@slave /]# vim /etc/selinux/config
SELINUX=disable
[root@slave /]# setenforce 0

配置本地文件开启二进制日志并设置服务id主库id一定要比从库小


[mysqld3306]
datadir = /opt/data/3306
port = 3306
socket = /tmp/mysql3306.sock
pid-file = /opt/data/3306/mysql_3306.pid
log-error=/var/log/3306.log
log-bin=g
server-id=2000[mysqld3307]
datadir = /opt/data/3307
port = 3307
socket = /tmp/mysql3307.sock
pid-file = /opt/data/3307/mysql_3307.pid
log-error=/var/log/3307.log
log-bin=o
server-id=2001

进入数据库连接


mysql> change master to-> master_host='192.168.226.139' ,-> master_user='xuanning' ,-> master_password='123456' ,-> master_log_file='g.000001' ,-> master_log_pos=154 ,-> master_port=3306;  //端口
Query OK, 0 rows affected, 2 warnings (0.02 sec)mysql> start slave;
Query OK, 0 rows affected (0.00 sec)mysql> show slave status \G
*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 192.168.226.139Master_User: xuanningMaster_Port: 3306Connect_Retry: 60Master_Log_File: g.000001Read_Master_Log_Pos: 154Relay_Log_File: localhost-relay-bin.000002Relay_Log_Pos: 312Relay_Master_Log_File: g.000001Slave_IO_Running: Yes  //此处一定要是yesSlave_SQL_Running: YesReplicate_Do_DB:

查看主数据库库与从数据库里面的数据


[root@localhost ~]# mysql -uroot -p123456 -S /tmp/mysql3306.sock
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.37-log MySQL Community Server (GPL)Copyright (c) 2000, 2022, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)mysql> exit
Bye
[root@localhost ~]# mysql -uroot -p123456 -S /tmp/mysql3307.sock
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.37-log MySQL Community Server (GPL)Copyright (c) 2000, 2022, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)mysql>

测试

[root@localhost ~]# mysql -uroot -p123456 -S /tmp/mysql3306.sock
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.37-log MySQL Community Server (GPL)Copyright (c) 2000, 2022, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> create database nian;
Query OK, 1 row affected (0.01 sec)mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| nian               |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)mysql> exit
Bye
[root@localhost ~]# mysql -uroot -p123456 -S /tmp/mysql3307.sock
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.37-log MySQL Community Server (GPL)Copyright (c) 2000, 2022, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| nian               |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)mysql>

6:mysql配置systemctl管理

写入配置文件,关闭mysql服务


# vim /usr/lib/systemd/system/mysql.service[Unit]
Description= mysql server daemon
After=network.target[Service]
Type=forking
ExecStart=/usr/local/mysql/bin/mysqld_multi start mysql
ExecStop=ps -ef | grep mysql | grep -v grep | awk '{print$2}' | xargs kill -9
ExecReload=/bin/kill -HUP $MAINPID[Install]
WantedBy=muyi-user.target
[root@localhost local]# systemctl  stop mysqld.service
[root@localhost local]# ss -antl
State             Recv-Q            Send-Q                         Local Address:Port                         Peer Address:Port            Process
LISTEN            0                 128                                  0.0.0.0:111                               0.0.0.0:*
LISTEN            0                 32                             192.168.122.1:53                                0.0.0.0:*
LISTEN            0                 128                                  0.0.0.0:22                                0.0.0.0:*
LISTEN            0                 5                                  127.0.0.1:631                               0.0.0.0:*
LISTEN            0                 128                                127.0.0.1:6010                              0.0.0.0:*
LISTEN            0                 128                                     [::]:111                                  [::]:*
LISTEN            0                 128                                     [::]:22                                   [::]:*
LISTEN            0                 5                                      [::1]:631                                  [::]:*
LISTEN            0                 128                                    [::1]:6010                                 [::]:*

mysql配置主从复制和mysql多实例配置主从复制相关推荐

  1. mysql5.5多实例配置_mysql-5.5.32多实例配置

    一.安装依赖包 yum install ncurses-devel libaio-devel -y 二.下载cmake2.8.8和mysql.5.5.32 PS:我这里直接下载好了所以就用rz上传了 ...

  2. MySQL 5.5.35 单机多实例配置详解

    一.前言 二.概述 三.环境准备 四.安装MySQL 5.5.35 五.新建支持多实例的配置文件(我这里配置的是四个实例) 六.初始化多实例数据库 七.提供管理脚本 mysqld_multi.serv ...

  3. mysql 5.5.35 单机多实例配置详解_MySQL 5.5.35 单机多实例配置详解

    一.前言 二.概述 三.环境准备 四.安装MySQL 5.5.35 五.新建支持多实例的配置文件(我这里配置的是四个实例) 六.初始化多实例数据库 七.提供管理脚本 mysqld_multi.serv ...

  4. ​Mysql数据读写分离(多实例配置)

    目录 一.读写分离概述 二. 读写分离实施maxscale 1.环境准备 2.maxscale服务器配置 一.读写分离概述 使用读写分离的原因 数据库写入效率要低于读取效率 一般来说,数据读取频率高于 ...

  5. 生产环境MySQL 5.5.x单机多实例配置实践

    背景需求: 1)在一台新采购的服务器上通过源码编译安装一个版本为5.5.x以上的MySQL数据库,并将所有配置文件与数据等均存放在/opt/mysql,以便于今后实现快速迁移.复制和整体备份: 2)在 ...

  6. mysql 5.5.35 单机多实例配置详解_基于mysql-5.5.32的单机多实例多配置文件的

    1.安装环境: [root@localhost ~]# [root@localhost ~]# uname -a Linux localhost.localdomain 2.6.32-504.16.2 ...

  7. 华为交换机vlan配置举例_华为三层交换机实例配置方式心得

    我们前面发布了华为交换机的配置方式,那么今天就用一些小实例来增加对华为交换机配置的了解. 一.给交换机划分VLAN Vlan是虚拟局域网的意思,它相当于一个局域网工作组."vlan几&quo ...

  8. Vue复习知识点(理解MVVM模式、Vue实例配置的各选项、Vue的各种内置指令....)

    Vue复习知识点 一.理解MVVM模式 VVM是一种设计模式,它将应用程序分为三个部分:模型(Model).视图(View)和视图模型(ViewModel).它的主要目的是将应用程序的界面逻辑与业务逻 ...

  9. mysql数据丢失_图解MySQL | 「原理解析」 MySQL使用固定的server_id导致数据丢失

    原创作者:爱可生开源社区 本文我们来看一个场景,两台MySQL实例使用主从复制,当master故障,触发高可用切换,新master上线后,通过备份重建旧master并建立复制后,数据发生丢失. 以下我 ...

最新文章

  1. php mvc实例下载,php实现简单的MVC框架实例
  2. 用户和用户组的简单总结
  3. 对一致性Hash算法,Java代码实现的深入研究
  4. F5金飞:“双十一”安全事项三部曲
  5. [html] html哪个标签属性可以通过预解析DNS?
  6. 蚂蚁金服终端实验室演进之路
  7. 高性能Web动画和渲染原理系列(2)——渲染管线和CPU渲染
  8. php修改linux文件权限设置,linux怎么给文件设置权限
  9. Akka与设备组一起工作《twelve》译
  10. iOS 创建单例的方法
  11. 移动端图片上传老失败
  12. 大学生就业新神器 网络电话“通”职场
  13. Laravel源码解析【转】
  14. Holo 使用场景说明
  15. 2021鹏业安装算量软件常见问题整理(三)
  16. 多点移动电子地图定位
  17. use SWR为什么能成为我的最爱React库?
  18. python打开网页被禁止_Python请求无法刮取403禁止的网页
  19. 计算机高中竞赛自主招生,自主招生必备常识:高校认可的各类竞赛
  20. 20220221量化打板模型预测

热门文章

  1. 操作必须使用一个可更新的查询问题
  2. 软件单元测试操作步骤(java版)
  3. Simscape - 关节添加摩擦力
  4. 大数据算法_大数据算法解析,如何创建用户画像实现千人千面?
  5. 抑郁症自我测试皮肤软件,39健康自测_中国最大的在线健康自测平台
  6. 数据分析真题日刷 | 商汤科技2018校招C++/算法开发/大数据/后端/运维/测试/数据挖掘开发工程师笔试第二场
  7. linux tar 打包 解压包
  8. C++ Primer Plus习题及答案-第四章
  9. STM32掌机教程3,工程模板与带灯按键测试
  10. 教育技术学 (教育学二级学科)