美文网首页数据库MySQL
docker部署mysql读写分离

docker部署mysql读写分离

作者: 楚长铭 | 来源:发表于2021-03-08 10:13 被阅读0次

准备

  • docker下载镜像使用mysql8(都2021了还有那么多mysql5教程我也是醉了)
docker pull mysql:8

获得my.cnf文件

  • 先随便跑个mysql容器
docker run --privileged  \
--restart=unless-stopped \
--name mysql8-master     \
-p 13306:3306            \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql:8
  • 进入docker容器
docker exec -it mysql8-master /bin/bash 
  • 在/etc/mysql/my.cnf获得my.cnf文件
# 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

# Custom config should go here
!includedir /etc/mysql/conf.d/

  • 删除mysql容器
docker stop mysql8-master
docker rm mysql8-master

运行mysql容器

  • 运行master节点容器,启动应该会失败;但是没有关系!在/data/dockerMount/mysql/master/conf/my.cnf的路径下my.cnf是个文件夹,将其删除之后将上面得到的my.cnf文件放入即可
docker run --privileged  \
--restart=unless-stopped \
--name mysql8-master     \
-p 13306:3306            \
-v /data/dockerMount/mysql/master/conf/my.cnf:/etc/mysql/my.cnf \
-v /data/dockerMount/mysql/master/logs:/var/log/mysql \
-v /data/dockerMount/mysql/master/data:/var/lib/mysql  \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql:8
  • 重启master节点
docker restart mysql8-master
  • 运行slave节点,和master节点一样的方法运行
docker run --privileged  \
--restart=unless-stopped \
--name mysql8-slave1     \
-p 13307:3306            \
-v /data/dockerMount/mysql/slave1/conf/my.cnf:/etc/mysql/my.cnf \
-v /data/dockerMount/mysql/slave1/logs:/var/log/mysql \
-v /data/dockerMount/mysql/slave1/data:/var/lib/mysql  \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql:8
  • 到此两个mysql容器运行起来了

读写分离

  • 修改两个节点的my.cnf文件,server-id不同即可
#my.cnf文件修改
[mysqld]
server-id       = 1

配置主从

  • 进入master节点查看file和position的值然后记下来
docker exec -it mysql8-master /bin/bash 
mysql -u root -p123456
show master status;
 File          | Position | 
+---------------+----------+
| binlog.000001 |      911 
  • 配置用与同步的用户
alter user '用户名'@'%' identified by '密码' password expire never;

 alter user '用户名'@'%' identified with mysql_native_password by '密码';

flush privileges;
  • 进入slave节点
docker exec -it mysql8-slave1 /bin/bash 
mysql -u root -p123456


change master to
master_host='主节点ip',
master_port=主节点端口,
master_user='主节点用户名',
master_password='主节点用户名密码',
master_log_file='binlog.000001',
master_log_pos=911;

 start slave;
  • 如果配置错误,重新配置即可
stop slave;
  • 查看状态
show slave status\G
  • 看到以下两个yes即可
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

参考文档

https://www.jianshu.com/p/5fc9145393eb

相关文章

  • docker部署mysql读写分离

    准备 docker下载镜像使用mysql8(都2021了还有那么多mysql5教程我也是醉了) 获得my.cnf文...

  • Docker部署Amoeba+MySQL读写分离

    环境: Centos:7.5 Docker版本:18.09.0 服务器Master-IP: 192.168.189...

  • docker mycat mysql读写分离

    拉取mycat镜像 参考 基于docker的mysql主从,及其Mycat实现读写分离笔记 Docker安装Myc...

  • docker mysql 读写分离

    参考文档[https://www.cnblogs.com/jinjiangongzuoshi/p/9299567....

  • 什么是Docker?(部署篇)

    Docker部署 MySQL部署 在Docker容器中部署MySQL,并通过外部MySQL客户端去操作MySQL。...

  • Mysql读写分离

    常见读写分离方式 MySQL-Proxy读写分离 Amoeba读写分离 Mycat读写分离 基于程序读写分离 My...

  • mysqlrouter的使用

    环境 在vm的docker中使用mysql router实现group replication 组复制的读写分离和...

  • MySQL + Atlas --- 部署读写分离

    序章 Atlas是360团队弄出来的一套基于MySQL-Proxy基础之上的代理,修改了MySQL-Proxy的一...

  • mysql读写分离

    mysql读写分离 1、读写分离的介绍 MySQL读写分离基本原理是让master数据库处理写操作,slave数据...

  • docker搭建mysql读写分离

    写在前面 当mysql数据量不大且达到读写瓶颈的时候,读的性能消耗远大于写,我们可以将读和写分离开。本实例采用do...

网友评论

    本文标题:docker部署mysql读写分离

    本文链接:https://www.haomeiwen.com/subject/msqpqltx.html