基于docker的mysql8的主从复制

创建mysql的docker镜像

  • 构建docker镜像,其中数据卷配置内容在下面,结构目录如下

    version: '3.7'
    services:
    db:
    # images 8.x
    image: mysql
    restart: always
    environment:
    MYSQL_ROOT_PASSWORD: 456123
    command:
    --default-authentication-plugin=mysql_native_password
    --character-set-server=utf8mb4
    --collation-server=utf8mb4_general_ci
    --explicit_defaults_for_timestamp=true
    --lower_case_table_names=1
    ports:
    - 3309:3306
    volumes:
    - ./data:/var/lib/mysql
    - ./my.cnf:/etc/my.cnf

配置mysql的主库

  • 更新配置文件

    # Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
    #
    # This program is free software; you can redistribute it and/or modify
    # it under the terms of the GNU General Public License as published by
    # the Free Software Foundation; version 2 of the License.
    #
    # This program is distributed in the hope that it will be useful,
    # but WITHOUT ANY WARRANTY; without even the implied warranty of
    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    # GNU General Public License for more details.
    #
    # You should have received a copy of the GNU General Public License
    # along with this program; if not, write to the Free Software
    # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #
    # The MySQL Server configuration file.
    #
    # For explanations see
    # http://dev.mysql.com/doc/mysql/en/server-system-variables.html [mysqld] pid-file = /var/run/mysqld/mysqld.pid
    socket = /var/run/mysqld/mysqld.sock
    datadir = /var/lib/mysql
    secure-file-priv= NULL
    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0 # Custom config should go here
    # [必须]启用二进制日志
    log-bin=mysql-bin
    # [必须]服务器唯一ID,默认是1 1~255
    server-id=1
    sql_mode=STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION
  • 配置操作

    1. 启动mysql

      docker-compose up -d
    2. 更新配置

      • 查看容器

        docker ps
        
        

        • 进入mysql交互

           docker exec -it 518a92715f6f /bin/bash
          
          
        • 登录mysql

           mysql -u root -p
          
          

        • 配置

          #在主库上创建同步用户并授权
          CREATE USER 'replicate'@'112.74.41.236' IDENTIFIED BY '123456';
          GRANT REPLICATION SLAVE ON *.* TO 'replicate'@'112.74.41.236';
          FLUSH PRIVILEGES;
          #最后增加远程访问用户 并赋予所有权限,远程访问测试用
          CREATE USER antsdouble IDENTIFIED BY '123456';
          GRANT ALL ON *.* TO 'antsdouble'@'%';
          #用navicate12及以上可以不用修复修复远程登录报报 caching_sha2_password异常
          # mysql8 用新的驱动 driver-class-name: com.mysql.cj.jdbc.Driver
          ALTER USER 'antsdouble'@'%' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER;
          ALTER USER 'antsdouble'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
          FLUSH PRIVILEGES;
          #查询master的状态,此处File,Position数据在配置从库时用到
          show master status;

  • 相关说明

    1. server-id是唯一的,主从不能相同,server-id为1表示mysq数据库为主数据库,server-id为2表示mysql的数据库为从数据库

    2. 为何必须挂载到/etc/my.cnf 文件上,而不是/etc/mysql/my.cnf 文件上?因为mysql默认配置文件位置在/etc/mysql/my.cnf,挂在方式无法改变容器中文件内容,my.conf内容不会改变,my.cnf中没有我们自定义的配置内容,启动mysql容器会报错

      lower_case_table_names:忽略表名、列名等数据结构的大小写(注意:不是每行记录内容的大小写!)。
      
      log-bin:开启二进制记录。这是为了主从复制而做的设置。本文使用RBR(Row-Based Replication)模式。
      
      slow_query_log=1:开启慢查询日志。如果某一条SQL执行的时间超过long_query_time设置的秒数,那么就记录下来。记录文件路径可以使用show variables;命令,在变量名是slow_query_log_file下查找到具体的日志文件路径。
      
      long_query_time=1:单位是秒。指如果某一条SQL语句执行时间超过1秒,就记录下来。必须开启慢查询日志了以后,此变量才能使用。
      
      log_error:开启错误日志。show variables like 'log_error'; 就可以查询到日志文件的路径。mysql的docker官方镜像如果设置别的取值会导致容器无法正常启动。

配置mysql的从库

  • 更新配置文件

    # Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
    #
    # This program is free software; you can redistribute it and/or modify
    # it under the terms of the GNU General Public License as published by
    # the Free Software Foundation; version 2 of the License.
    #
    # This program is distributed in the hope that it will be useful,
    # but WITHOUT ANY WARRANTY; without even the implied warranty of
    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    # GNU General Public License for more details.
    #
    # You should have received a copy of the GNU General Public License
    # along with this program; if not, write to the Free Software
    # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #
    # The MySQL Server configuration file.
    #
    # For explanations see
    # http://dev.mysql.com/doc/mysql/en/server-system-variables.html [mysqld] pid-file = /var/run/mysqld/mysqld.pid
    socket = /var/run/mysqld/mysqld.sock
    datadir = /var/lib/mysql
    secure-file-priv= NULL
    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0 # Custom config should go here
    # [必须]启用二进制日志
    log-bin=mysql-bin
    # [必须]服务器唯一ID,默认是1
    server-id=1
    sql_mode=STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION
  • 配置操作

    1. 进入交互模式,方法同主库

    2. 配置从库

      change master to master_host='112.74.41.236',master_port=3309,master_user='replicate',master_password='123456',master_log_file='binlog.000006',master_log_pos=3862;
      • 说明

        master_port:Master的端口号,指的是容器的端口号
        
        master_user:用于数据同步的用户
        
        master_password:用于同步的用户的密码
        
        master_log_file:指定 Slave 从哪个日志文件开始复制数据,即上文中提到的 File 字段的值
        
        master_log_pos:从哪个 Position 开始读,即上文中提到的 Position 字段的值
        
        master_connect_retry:如果连接失败,重试的时间间隔,单位是秒,默认是60秒
    3. 查看

      show slave status\G
      # 查询slave的状态,Slave_IO_Running及Slave_SQL_Running进程必须正常运行,即YES状态,否则都是错误的状态 错误可以在2标记处查看到原因,也可以通过
      show slave status;

测试功能

  • 主库添加一条数
  • 在从库查看

常见问题

  • 可能产生的原因

    网络不通
    
    检查ip,端口
    
    密码不对
    
    检查是否创建用于同步的用户和用户密码是否正确
    
    pos不对
    
    检查Master的 Position
  • Could not execute Update_rows event on table oa.bui_bill_sum; Can't find record in 'bui_bill_sum', Error_code: 1032; handler error HA_ERR_KEY_NOT_FOUND; the event's master log mysql-bin.000138, end_log_pos 160051747

    1. 解决,临时

      进入sql 执行
      STOP SLAVE;
      SET GLOBAL sql_slave_skip_counter =1; #表示跳过一步错误,后面的数字可变
      START SLAVE;
    2. 永久

      在my.cnf中配置
      slave-skip-errors = 1032 就会跳过所有的1032的错误,多个用逗号分隔
  • mysql并没有从my.cnf文件中更新server_id,既然这样就只能手动修改了

set global server_id=2; #此处的数值和my.cnf里设置的一样就行
slave start;

基于docker的mysql8的主从复制的更多相关文章

  1. 基于docker/dockerfile实现redis主从复制

    今天我们来搭建基于docker实现redis主从复制集群 为什么要使用redis集群模式? Redis可以说是内存数据库,mysql的数据库是真实存储在硬盘里的,因此,redis的读取速度要比mysq ...

  2. 基于Docker Compose搭建mysql主从复制(1主2从)

    系统环境 * 3 Ubuntu 16.04 mysql 8.0.12 docker 18.06.1-ce docker-compose 1.23.0-rc3 *3 ==> PS  ###我用的是 ...

  3. 基于Docker的Mysql主从复制搭建

    来源:https://www.cnblogs.com/songwenjie/p/9371422.html?tdsourcetag=s_pctim_aiomsg   为什么基于Docker搭建? 资源有 ...

  4. 基于Docker的Mysql主从复制

    基于Docker的Mysql主从复制搭建 为什么基于Docker搭建? 资源有限 虚拟机搭建对机器配置有要求,并且安装mysql步骤繁琐 一台机器上可以运行多个Docker容器 Docker容器之间相 ...

  5. Docker部署Mysql8.0.20并配置主从复制

    1. Linux安装Mysql8.0.20并配置主从复制(一主一从,双主双从)   Linux安装Mysql8.0.20并配置主从复制(一主一从,双主双从) 2. 前提准备 # 创建主从数据库文件夹 ...

  6. Linux基于Docker的Redis主从复制、哨兵模式搭建

    本教程基于CentOS7,开始本教程前,请确保您的Linux系统已安装Docker. 1.使用docker下载redis镜像 docker pull redis 安装完成后,使用docker imag ...

  7. 基于Docker搭建MySQL主从复制

    摘要: 本篇博文相对简单,因为是初次使用Docker,MySQL的主从复制之前也在Centos环境下搭建过,但是也忘的也差不多了,因此本次尝试在Docker中搭建. 本篇博文相对简单,因为是初次使用D ...

  8. 基于 Docker 搭建 MySQL 主从复制

    本篇博文相对简单,因为是初次使用Docker,MySQL的主从复制之前也在Centos环境下搭建过,但是也忘的也差不多了,因此本次尝试在Docker中搭建. 根据网上教程走还是踩了一些坑,不过所幸最终 ...

  9. 如何利用docker快速构建MySQL主从复制环境

    在学习MySQL的过程中,常常会测试各种参数的作用.这时候,就需要快速构建出MySQL实例,甚至主从. 考虑如下场景: 譬如我想测试mysqldump在指定--single-transaction参数 ...

随机推荐

  1. Java 从入门到进阶之路(六)

    之前的文章我们介绍了 Java 的数组,本章我们来看一下 Java 的对象和类. Java 是一种面向对象语言,那什么是对象呢,对象在编程语言中属于一个很宽泛的概念,我们可以认为万事万物都是对象,每个 ...

  2. Java源码解析|String源码与常用方法

    String源码与常用方法 1.栗子 代码: public class JavaStringClass { public static void main(String[] args) { Strin ...

  3. mysql高级扩展

    有关网址: MySQL 性能优化神器 Explain 使用分析 MySQL - EXPLAIN详解 1.时间比较 mysql> SELECT something FROM table WHERE ...

  4. HTML5 原生拖放

    前言: HTML5提供专门的拖拽与拖放的API,可以方便的指定某个元素可拖动,可以创建自定义的可拖动元素和放置目标 相关知识点: 1.拖放事件 拖放元素时,将依次触发下列事件 dragstart  按 ...

  5. 针对于ECMA5Script 、ECMAScript6、TypeScript的认识

    什么是ECMAScript.什么又是ECMA? Ecma国际(Ecma International)是一家国际性会员制度的信息和电信标准组织.1994年之前,名为欧洲计算机制造商协会(European ...

  6. SpringBoot-ElasticJob封装快速上手使用(分布式定时器)

    elastic-job-spring-boot qq交流群:812321371 1 简介 Elastic-Job是一个分布式调度解决方案,由两个相互独立的子项目Elastic-Job-Lite和Ela ...

  7. (八十五)c#Winform自定义控件-引用区块

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  8. Redis系列总结--这几点你会了吗?

    文章原创于公众号:程序猿周先森.本平台不定时更新,喜欢我的文章,欢迎关注我的微信公众号. 前面几篇已经对Redis中几个关键知识点做了介绍,本篇主要对Redis系列做一下总结以及对Redis中常见面试 ...

  9. canvas实现平面迁徙图

    前言 最近在做自己维护的一个可视化工具的时候,在添加基于echart的雷达图的时候,按照echart官网案例写完发现在自己项目中无法正常运行,排查了一番发现是我项目中echart的版本太低.找到问题原 ...

  10. CSS样式手册

    字体属性:(font) 大小 {font-size: x-large;}(特大) xx-small;(极小) 一般中文用不到,只要用数值就可以,单位:PX.PD 样式 {font-style: obl ...