美文网首页
docker中mysql主从配置实践

docker中mysql主从配置实践

作者: 有时右逝 | 来源:发表于2019-04-01 17:18 被阅读0次

    前言

    常言道:有备无患。没有备份早晚会出事得。

    直接在主库进行备份,会影响性能,同时也存在不安全。例如高峰时读写,此时备份会造成很大风险。

    因此通常需要开启主从功能,在从库进行备份操作。

    问题

    mysql主从配置的实践。

    本地环境是mac ,
    访问mysql工具使用了sequel pro
    docker版本version 17.12.0-ce-mac49 (21995)

    解决过程

    • 创建目录
      依次如下图目录
      这里我的目录是 mysql-backup
    tree
    .
    ├── mysql-master
    │   └── mysql.conf.d
    │       └── mysqld.cnf
    └── mysql-slave
        └── mysql.conf.d
            └── mysqld.cnf
    
    • 创建主库
    docker run --name mysql-master  -p 3309:3306  -v $(pwd)/mysql.conf.d:/etc/mysql/mysql.conf.d  -e MYSQL_ROOT_PASSWORD=123456 -d mysql:latest
    

    其中主库的配置文件

    # Copyright (c) 2014, 2016, 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
    #log-error  = /var/log/mysql/error.log
    # By default we only accept connections from localhost
    #bind-address   = 127.0.0.1
    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0
    
    log-bin=mysql-bin
    server-id=1
    
    • 创建从库
    # Copyright (c) 2014, 2016, 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
    #log-error  = /var/log/mysql/error.log
    # By default we only accept connections from localhost
    #bind-address   = 127.0.0.1
    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0
    
    log-bin=mysql-bin
    server-id=2
    
    • 配置账号-在主库执行。
      创建账号
    CREATE USER 'repl'@'%' IDENTIFIED BY 'doudouchidou123456'
    

    分配权限

    GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%'
    

    查看是否开启Bin日志

    show VARIABLES  LIKE '%log_%';
    
    image.png
    SHOW BINARY LOGS;
    
    image.png

    显示日志位置

    show master status
    
    image.png

    锁定读写。(非常重要。该操作确保主库暂时不再接受读写)

    FLUSH TABLES WITH READ LOCK;
    
    • 从库配置

    查看id

    show variables like 'server_id'
    
    image.png

    如果id相同则需要修改从库

    set global server_id=2
    

    配置访问账号

    CHANGE MASTER TO
    MASTER_HOST='172.17.0.6',
       MASTER_USER='repl',
       MASTER_PASSWORD='doudouchidou123456',
        MASTER_LOG_FILE='mysql-bin.000003',
        MASTER_LOG_POS=2678
    

    启动

    start slave
    

    查看运行状态

    show slave status
    
    image.png
    • 其他更多命令
    停止
    stop slave
    重置
    reset slave
    查看容器ip
    docker inspect mysql-master
    查看运行日志
    docker logs mysql-master
    

    相关文章

      网友评论

          本文标题:docker中mysql主从配置实践

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