docker mysql8.0 主从复制

docker mysql8.0 主从复制

基于mysql8.0 配置的主从复制,排除一些坑。

安装docker

ubuntu18 安装最新版docker
如果能访问此链接,就按教程走,否则自行百度:访问docker连接
成功后继续

  1. 更新索引包
    sudo apt update
  2. 通过HTTPS使用仓库(repository)安装:
    sudo apt install apt-transport-https ca-certificates curl software-properties-common
  3. 添加Docker官方的GPG密钥:
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  4. 添加源:
    sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
  5. 安装:
    sudo apt install docker-ce
  6. 查看是否启动:
    systemctl status docker
    启动成功
  7. 需要配置镜像加速,否则拉取镜像会很慢,本人用的阿里镜像加速,自己去阿里云申请配置即可,免费的
    sudo vim /etc/docker/daemon.json
    { "registry-mirrors": ["××××××××××.mirror.aliyuncs.com"] }

配置主从复制

  1. 拉取镜像(目前我拉取的最新版本是8.0.19):
    $ sudo docker pull mysql
  2. 查看进行:
    sudo docker images
    拉取成功
  3. 安装mysql-client,用于访问mysql容器数据库:
    $ sudo apt install mysql-client-core-5.7
  4. 创建工作目录,保存mysql数据到本地:
    cd ~/
    mkdir docker-mysql
    cd docker-mysql
    mkdir -p mysql-master/data mysql-master/cnf mysql-slave1/data mysql-slave1/cnf
  5. 创建主从mysql配置文件:
    vim mysql-master/cnf /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
# 主从配置服务期id
server_id       = 1
# 开启日志文件
log-bin         = mysql-bin
# 不参与主从的数据库名
binlog-ignore-db= mysql
# 需要进行 binlog 日志记录的数据库  `test`为你自己的数据库名称
binlog-do-db    = test
# Custom config should go here
!includedir /etc/mysql/conf.d/

~ 从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
# 主从配置服务期id
server_id       = 2
# 开启日志文件
log-bin         = mysql-bin
# 不参与主从的数据库名
binlog-ignore-db= mysql
# 需要进行 binlog 日志记录的数据库  `test`为你自己的数据库名称
binlog-do-db    = test
# Custom config should go here
!includedir /etc/mysql/conf.d/
  1. 运行主myqsl容器(目录记得改成自己的):
    run 运行一个容器
    –name 后面是这个镜像的名称
    -p 3307:3306 3307表示映射到本机的端口号,306表示在这个容器中使用的端口
    -d 表示使用守护进程运行,即服务挂在后台

    sudo docker run -di -e MYSQL_ROOT_PASSWORD=123456 --name mysql-master -v /home/xiyeming/docker-mysql-data/mysql-master/data:/var/lib/mysql -v /home/xiyeming/docker-mysql-data/mysql-master/cnf/my.cnf:/etc/mysql/my.cnf -p 3307:3306 -d mysql
  2. 运行从myqsl容器(目录记得改成自己的):
    sudo docker run -d -e MYSQL_ROOT_PASSWORD=123456 --name mysql-slave1 -v /home/xiyeming/docker-mysql-data/mysql-slave1/data:/var/lib/mysql -v /home/xiyeming/docker-mysql-data/mysql-slave1/cnf/my.cnf:/etc/mysql/my.cnf -p 3308:3306 mysql
  3. 连接容器数据库:
    -h 连接的ip
    –port 连接的端口

    ~ 主:
    mysql -uroot -p123456 -h127.0.0.1 --port 3307
    ~ 从:
    mysql -uroot -p123456 -h127.0.0.1 --port 3308
  • 数据库库配置:
    • 主库配置:
      • 创建账号:
      • create user 'slave1'@'%' identified by 'slave1';
      • 授权:
      • grant all privileges ON *.* TO 'slave1'@'%';
      • 验证方式修改为上一版的:
      • ALTER USER slave1@'%' IDENTIFIED WITH mysql_native_password BY 'slave1';
      • 刷新:
      • flush privileges;
      • 查看状态
      • mysql> show master status;
      • 成功后
    • 从库配置
    • master_log_file 对应File
      master_log_pos对应Position
    • change master to master_host='192.168.1.102',master_user='slave1',master_password='slave1',master_log_file='mysql-bin.000003',master_log_pos=1298,master_port=3307;
    • start slave;
    • show slave status \G
    • 成功后
    • 主从配置已完成,可以自己测试,在主库中写入数据,到从库中查看数据。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值