系统: Centos 7.4

数据库版本:8.0.20

两台机器做相同操作

安装Docker

  1. export VERSION=18.06 && curl -fsSL http://rainbond-pkg.oss-cn-shanghai.aliyuncs.com/releases/docker/install-docker.sh | bash -s docker

启动并开机自启

  1. systemctl start docker
  2. systemctl enable docker

拉取镜像

  1. docker pull mysql

附配置文件,在容器启动时分别挂载主从的配置文件

  1. # Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; version 2 of the License.
  6. #
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program; if not, write to the Free Software
  14. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. #
  16. # The MySQL Server configuration file.
  17. #
  18. # For explanations see
  19. # http://dev.mysql.com/doc/mysql/en/server-system-variables.html
  20. [mysqld]
  21. pid-file = /var/run/mysqld/mysqld.pid
  22. socket = /var/run/mysqld/mysqld.sock
  23. datadir = /var/lib/mysql
  24. secure-file-priv= NULL
  25. # Disabling symbolic-links is recommended to prevent assorted security risks
  26. symbolic-links=0
  27. # 服务端默认utf8编码
  28. character-set-server=utf8mb4
  29. # 默认存储引擎
  30. default-storage-engine=INNODB
  31. # 主从配置
  32. log-bin=binlog
  33. server-id=1
  34. gtid-mode=on
  35. enforce-gtid-consistency=on
  36. log-slave-updates=on
  37. expire_logs_days=14
  38. # Compatible with versions before 8.0
  39. default_authentication_plugin=mysql_native_password
  40. skip-host-cache
  41. skip-name-resolve
  42. [client]
  43. #设置客户端编码
  44. default-character-set=utf8mb4
  45. [mysql]
  46. # 设置mysql客户端默认编码
  47. default-character-set=utf8mb4
  48. # Custom config should go here
  49. !includedir /etc/mysql/conf.d/
  50. # Custom config should go here
  51. !includedir /etc/mysql/conf.d/

  1. # Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; version 2 of the License.
  6. #
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program; if not, write to the Free Software
  14. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. #
  16. # The MySQL Server configuration file.
  17. #
  18. # For explanations see
  19. # http://dev.mysql.com/doc/mysql/en/server-system-variables.html
  20. [mysqld]
  21. pid-file = /var/run/mysqld/mysqld.pid
  22. socket = /var/run/mysqld/mysqld.sock
  23. datadir = /var/lib/mysql
  24. secure-file-priv= NULL
  25. # Disabling symbolic-links is recommended to prevent assorted security risks
  26. symbolic-links=0
  27. # 服务端默认utf8编码
  28. character-set-server=utf8mb4
  29. # 默认存储引擎
  30. default-storage-engine=INNODB
  31. # 主从配置
  32. server-id=2
  33. gtid-mode=on
  34. enforce-gtid-consistency=on
  35. log-slave-updates=on
  36. expire_logs_days=14
  37. # Compatible with versions before 8.0
  38. default_authentication_plugin=mysql_native_password
  39. skip-host-cache
  40. skip-name-resolve
  41. [client]
  42. #设置客户端编码
  43. default-character-set=utf8mb4
  44. [mysql]
  45. # 设置mysql客户端默认编码
  46. default-character-set=utf8mb4
  47. # Custom config should go here
  48. !includedir /etc/mysql/conf.d/
  49. # Custom config should go here
  50. !includedir /etc/mysql/conf.d/

主数据库

启动数据库

  1. docker run --name mysql_master --restart=always -p 3306:3306 --privileged=true -v /root/mysql/my.cnf:/etc/mysql/my.cnf -v /root/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql

查看数据库字符编码(可选),并创建用户授权

  1. # 进入数据库
  2. docker exec -it mysql_master bash
  3. # 查看字符编码
  4. mysql> show global variables like'%character_set%';
  5. # 创建用户授权
  6. mysql> CREATE USER 'slave'@'%' IDENTIFIED WITH mysql_native_password BY 'slave';
  7. mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%';
  8. mysql> flush privileges;

获取主节点当前binary log文件名和位置(position)

  1. mysql> SHOW MASTER STATUS;
  2. +---------------+----------+--------------+------------------+------------------------------------------+
  3. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  4. +---------------+----------+--------------+------------------+------------------------------------------+
  5. | binlog.000003 | 868 | | | 1b009ef8-a67f-11ea-8c9a-0242ac110002:1-8 |
  6. +---------------+----------+--------------+------------------+------------------------------------------+
  7. 1 row in set (0.00 sec)

从数据库

启动数据库

  1. docker run --name mysql_slave --restart=always -p 3306:3306 --privileged=true -v /root/mysql/my.cnf:/etc/mysql/my.cnf -v /root/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql

配置主从复制

  1. # 进入数据库
  2. docker exec -it mysql_slave bash
  3. # 主从配置
  4. mysql> CHANGE MASTER TO
  5. mysql> MASTER_HOST='192.168.0.162',
  6. mysql> MASTER_USER='slave',
  7. mysql> MASTER_PASSWORD='slave',
  8. mysql> MASTER_PORT=3306,
  9. mysql> MASTER_LOG_FILE='binlog.000003',
  10. mysql> MASTER_LOG_POS=868;
  11. # 开启主从同步
  12. mysql> start slave;
  13. # 再查看主从同步状态
  14. mysql> show slave status;

这里只要看到两个参数Slave_IO_Running和Slave_SQL_Running都为true且Error字段都为空则代码主从正常复制

测试

通过在主服务器创建数据库建表插入数据的方式来进行测试,查看从服务器是否同步更新了数据

在主库创建库

  1. mysql> create database kong;

在从库查看

  1. mysql> show databases;
  2. +--------------------+
  3. | Database |
  4. +--------------------+
  5. | information_schema |
  6. | kong |
  7. | mysql |
  8. | performance_schema |
  9. | sys |
  10. +--------------------+
  11. 5 rows in set (0.00 sec)

数据同步成功,主从复制部署完成

创建用户与授权

  1. ALTER USER 'username'@'ip_address' IDENTIFIED WITH mysql_native_password BY 'password';

容器化安装Mysql 8.0 并部署主从复制的更多相关文章

  1. 在Ubuntu 18.04 安装 MySQL 8.0

    在Ubuntu 18.04 安装 MySQL 8.0 ① 登入 mysql 官网,在官网中下载 deb 包,点击该链接,即可下载. https://dev.mysql.com/downloads/re ...

  2. windows 系统如何安装 mysql 8.0.15 数据库?

    windows 系统如何安装 mysql 8.0.15 数据库? 1. 下载安装包 下载地址:https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0. ...

  3. win10 安装mysql 8.0.18 解决Navicat初次连接报错

    win10 安装mysql 8.0.18 解决Navicat初次连接报错 win10 安装mysql 8.0.18-winx64 一,先去官网下载mysql 安装包 https://dev.mysql ...

  4. 安装Mysql 8.0的艰难体验

    背景: Mysql 8.0 以后版本,在性能等方面有了很大提升,而且在自动编号.Timestamp等字段的设置上有了很方便的进步,因此在一年前即开始将原有的基于5.5版本的服务器逐渐向8.0转移.但转 ...

  5. CentOS 7 下安装 MySQL 8.0

    前言 本篇文章主要介绍在 CentOS 7 环境下安装 MySQL 8.0. 正文 1. 配置yum源 首先在 https://dev.mysql.com/downloads/repo/yum/ 找到 ...

  6. Ubuntu18 安装 MySQL 8.0.22

    Ubuntu18 安装 MySQL 8.0.22 网上教程都比旧,也不是第一次安装了,但依然还是花了比较多的时间,特此记录本次安装过程.因是安装完毕后回忆记录,或有错漏. 第一步: 下载 mysql ...

  7. 在 CentOS 7.5 64位上使用 yum 安装 MySQL 8.0

    前段时间在 CentOS 7.5 64位上安装 MySQL 8.0.查了些资料,在这里记录一下详细的安装和设置步骤. 一.安装 使用yum安装MySQL之前需要先下载对应的.rpm文件,下载方法: 去 ...

  8. 超级简单!CentOS-8 安装 MySQL 8.0,比喝水还简单

    中国人不骗中国人 果然是系统和MySQL的版本越高安装越便利了 在阿里云的 CentOS-8 比喝开水还简单的安装 MySQL 8.0,开始~ 1.以 root 用户通过 CentOS 软件包管理器来 ...

  9. centos 8及以上安装mysql 8.0

    本文适用于centos 8及以上安装mysql 8.0,整体耗时20分钟内,不需要FQ 1.环境先搞好 systemctl stop firewalld //关闭防火墙 systemctl disab ...

随机推荐

  1. Neighbour-Joining (NJ算法)

    clc;clear all;close all; Distance = [0,2,4,6,6,8; 2,0,4,6,6,8; 4,4,0,6,6,8; 6,6,6,0,4,8; 6,6,6,4,0,8 ...

  2. Blogs实现顶部的欢迎信息

    简单,就直接上代码: <div style="text-align: center; font-size:20px; margin-bottom:0px; margin-top:0px ...

  3. Excel 快速跳到表格最后一行/第一行

    快速跳到表格的最后一行 首先鼠标选中一个带有数据的单元格,点击shift键,把鼠标放到该单元格底部的边缘地带,出现带四个方向的箭头为止,再连续点击鼠标左键两次,直接跳到表格的最后一行 快速跳到表格的最 ...

  4. jfinal项目报java.lang.ClassNotFoundException: com.jfinal.core.JFinalFilter

    在eclipse中启动jfinal项目时,项目报错如下:首先:右击项目–>Build Path–>Source查看Default output folder如果是目录/WEB-INF/cl ...

  5. Spring源码深度解析之Spring MVC

    Spring源码深度解析之Spring MVC Spring框架提供了构建Web应用程序的全功能MVC模块.通过策略接口,Spring框架是高度可配置的,而且支持多种视图技术,例如JavaServer ...

  6. 【Java并发编程】阿里最喜欢问的几道线程池的面试题?

    引言 上一篇文章我们有介绍过线程池的一个基本执行流程<[Java并发编程]面试必备之线程池>以及它的7个核心参数,以及每个参数的作用.以及如何去使用线程池 还留了几个小问题..建议看这篇文 ...

  7. 万恶的NPE差点让我半个月工资没了

    引言 最近看到<阿里巴巴Java开发手册>(公众号回复[开发手册]免费获取)第11条规范写到: 防止 NPE ,是程序员的基本修养 NPE(Null Pointer Exception)一 ...

  8. 为什么spring 被@Repository注解标识注入后是代理类

    背景 今天发现一个奇怪的问题,有一个类是用的@Repository注解标识注入的,并且这个类并没有配置任何带代理和aop配置.但是得到的这个类不是一个原生类,而是一个代理类,如果换成了Componen ...

  9. 【代码周边】npm是What?

    社区 程序员自古以来就有社区文化: 社区的意思是:拥有共同职业或兴趣的人们,自发组织在一起,通过分享信息和资源进行合作.虚拟社区的参与者经常会在线讨论相关话题,或访问某些网站.前端程序员也有社区,世界 ...

  10. 手摸手带你用Hexo撸博客(一)

    原文地址 手摸手带你用Hexo撸博客(一) 环境搭建 安装 node 狂点下一步 命令行输入此条命令 如果能看到版本号则安装成功 node -v 安装Git (同上) 实在不会的小伙伴百度一下,教程很 ...