美文网首页数据库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读写分离

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