docker 搭建 mysql 单机及主从

文章目录

  • 1 下载镜像
    • 1.1 快速搭建 mysql 单机
  • 2 文件目录准备
  • 2 启动主 mysql 和 从 mysql
  • 3 主从配置修改 (mysqld.cnf)
  • 4 开启主从
  • 参考

1 下载镜像

docker pull mysql:5.7

1.1 快速搭建 mysql 单机

docker run --name mysql57 -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -d mysql:5.7

2 文件目录准备

  • mysqld.cnf
# 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

2 启动主 mysql 和 从 mysql

  • 主 挂载外部目录

docker run -p 3339:3306 --name mysql-master --privileged=true -v /usr/local/mysql/config/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf -v /usr/local/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7

  • 从 (挂载外部目录)

docker run -p 3340:3306 --name mysql-slaver --privileged=true -v /usr/local/mysql-slaver/config/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf -v /usr/local/mysql-slaver/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7

3 主从配置修改 (mysqld.cnf)

## 同一局域网内注意要唯一
server-id=100  
## 开启二进制日志功能,可以随便取(关键)
log-bin=mysql-bin
## 设置server_id,注意要唯一
server-id=101  
## 开启二进制日志功能,以备Slave作为其它Slave的Master时使用
log-bin=mysql-slave-bin   
## relay_log配置中继日志
relay_log=edu-mysql-relay-bin  

4 开启主从

  • 在主库执行,记录 File, 和 Position, 后面有用
show master status;
  • 在从库执行
change master to master_host='172.17.0.2', master_user='slave', master_password='123456', master_port=3306, master_log_file='mysql-bin.000001', master_log_pos= 2830, master_connect_retry=30;

start slave

参考

  • https://www.cnblogs.com/songwenjie/p/9371422.html
  • https://www.cnblogs.com/0oliumino0/p/10538207.html

你可能感兴趣的:(docker)