docker中mysql主从配置文件_docker中mysql主从配置实践

前言

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

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

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

问题

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_%';

15b7cd3fff04?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image.png

SHOW BINARY LOGS;

15b7cd3fff04?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image.png

显示日志位置

show master status

15b7cd3fff04?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image.png

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

FLUSH TABLES WITH READ LOCK;

从库配置

查看id

show variables like 'server_id'

15b7cd3fff04?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

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

15b7cd3fff04?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image.png

其他更多命令

停止

stop slave

重置

reset slave

查看容器ip

docker inspect mysql-master

查看运行日志

docker logs mysql-master

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值