1. 配置Master
以
mysql_3306
为例
- 查看已有容器中是否含有mysql_3306
docker ps -a
如果有mysql_3306正在运行,则 docker stop mysql_3306
停止运行
然后 docker rm mysql_3306
将其删除。
- 创建mysql_3306
首先,需要准备一个my.cof
配置文件,该文件存于/usr/local/softwares/mysql/3306/conf/my.cnf
另外还要设置your_network
然后在Linux命令行中输入下列命令代码,其中
–name 后是容器名称
–privileged 给予权限
–network 后表示创建容器时指定要连接的网络
–ip后表示容器ip地址
–v 后是挂载操作,: 前的是宿主机本地文件位置, 之后是容器中文件位置
–e后是附加的环境,MYSQL_ROOT_PASSWORD
是root用户的密码,需要设置
-d 后是所用镜像
docker run -it \
--name mysql_3306 \
--privileged \
-p 3306:3306 \
--network your_network \
--ip 172.18.12.2 \
-v /usr/local/softwares/mysql/3306/conf/my.cnf:/etc/mysql/my.cnf \
-v /usr/local/softwares/mysql/3306/data:/var/lib/mysql \
-v /usr/local/softwares/mysql/3306/mysql-files:/var/lib/mysql-files \
-e MYSQL_ROOT_PASSWORD=123 \
-d mysql
运行命令后,可以docker exec -it mysql_3306 bash
进入容器内,再mysql -u用户名 -p密码
就可以进入到mysql命令行,如果要退出,需要exit
退出到容器命令行,再exit
退出到Linux命令行。
并且,可以使用navicat远程连接一下,测试一下是否可以连接成功。
2 配置slave
以
mysql_3310
为例
- 查看已有容器中是否含有mysql_3310
- 创建mysql_3306
首先,需要准备一个my.cof
配置文件,该文件存于/usr/local/softwares/mysql/3310/conf/my.cnf
然后在Linux命令行中输入下列命令代码
docker run -it \
--name mysql_3310 \
--privileged \
-p 3310:3306 \
--network your_network \
--ip 172.18.12.3 \
-v /etc/localtime:/etc/localtime \
-v /usr/local/softwares/mysql/3310/conf/my.cnf:/etc/mysql/my.cnf \
-v /usr/local/softwares/mysql/3310/data:/var/lib/mysql \
-v /usr/local/softwares/mysql/3310/mysql-files:/var/lib/mysql-files \
-e MYSQL_ROOT_PASSWORD=123 \
-d mysql
- 测试是否创建成功
首先,创建连接mysql_3306、mysql_3310分别连接创建好的两个容器
- 在
Master
mysql_3306中新建查询,创建与从(slave)服务通信的用户
create user 'slave'@'%' IDENTIFIED WITH mysql_native_password BY '123';
GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'slave'@'%';
flush privileges;
- 创建slave完成后,
show master status
查看bin log信息
其 File, position需要用到,并且可能会发生改变,在需要用到时及时查看
- 切换到
Slave
mysql_3310中新建查询,配置主从关系
change master to
master_host='172.18.12.2',
master_user='slave',
master_password='123',
MASTER_LOG_FILE='master-log-bin.000002',
MASTER_LOG_POS=1462;
- 成功后,继续执行命令。开启slave
start slave;
- 最后执行命令查看是否配置是否成功
show slave status;
- 如果想设置Slave只读,则需要在mysql_3310连接中创建一个新的用户,并且仅赋
Select
的权限,然后使用改账号重新连接。
create user 'user_3310'@'%' IDENTIFIED WITH mysql_native_password BY '123';
GRANT SELECT ON *.* TO 'user_3310'@'%';
flush privileges
Master 的 my.cnf
# 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
default_authentication_plugin=mysql_native_password
# Custom config should go here
!includedir /etc/mysql/conf.d/
server-id=1 #随意设置的,不能重复
log_bin=master-logbin #设置log_bin文件名称
binlog_format=row #设置读取方式
Slave 的 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
default_authentication_plugin=mysql_native_password
# Custom config should go here
!includedir /etc/mysql/conf.d/
server-id=2
log_bin=slave-01-bin
relay_log=mysql-relay-bin01
read-only=1