MySQL主从配置

主从原理

1. 配置Master

mysql_3306为例

  1. 查看已有容器中是否含有mysql_3306
docker ps -a

如果有mysql_3306正在运行,则 docker stop mysql_3306 停止运行
然后 docker rm mysql_3306 将其删除。

  1. 创建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为例

  1. 查看已有容器中是否含有mysql_3310
  2. 创建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
  1. 测试是否创建成功
    首先,创建连接mysql_3306、mysql_3310分别连接创建好的两个容器

在这里插入图片描述

  1. Mastermysql_3306中新建查询,创建与从(slave)服务通信的用户
create user 'slave'@'%' IDENTIFIED WITH mysql_native_password BY '123';

GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'slave'@'%';

flush privileges;
  1. 创建slave完成后,show master status查看bin log信息

在这里插入图片描述
File, position需要用到,并且可能会发生改变,在需要用到时及时查看

  1. 切换到Slavemysql_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;
  1. 成功后,继续执行命令。开启slave
start slave;
  1. 最后执行命令查看是否配置是否成功
show slave status;

在这里插入图片描述

  1. 如果想设置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


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值