mysql的主从配置(docker)

本文详细介绍了如何在Docker环境下安装MySQL,包括创建配置文件,设置服务器ID,日志文件和日志格式。接着,展示了如何启动MySQL容器,以及如何通过Docker命令检查运行的容器。文章还涵盖了MySQL主从复制的配置,包括创建复制用户,获取主服务器IP,配置从服务器并开启复制,以及验证主从同步状态。
摘要由CSDN通过智能技术生成

docker 安装mysql

创建mysql配置文件

创建mysql配置目录
mkdir -p /usr/local/software/mysql

在这里插入图片描述

创建master配置

在这里插入图片描述

这里master配置文件所在的文件夹3306/conf已经提前创建好了,然后是创建mysql的配置文件my.cnf

# 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=200   #服务器id
log_bin=mysql-bin  #日志文件
binlog_format=row	#日志格式
-- 插入 --   

创建运行mysql容器(master)

创建容器并运行

docker run

  • -i:以交互模式运行容器
  • -t:为容器重新分配一个伪输入终端
  • —name :容器名称
  • —privileged: 设置容器公开权限(默认为true)
  • -p :映射端口 linux端口: 容器内置端口(mysql默认端口为3306)
  • -v : linux挂载文件夹/文件和容器内路径的映射
  • -e: 容器的环境变量(设置mysql默认用户名&密码)
  • -d: 后台运行容器,并返回容器ID
docker run -it \
--name mysql_3306 \
--privileged \
-p 3306:3306 \
-v /usr/local/software/mysql/3306/conf/my.cnf:/etc/mysql/my.cnf \
-v /usr/local/software/mysql/3306/data:/var/lib/mysql \
-v /usr/local/software/mysql/3306/mysql-files:/var/lib/mysql-files \
-e MYSQL_ROOT_PASSWORD=123 \
-d mysql

在这里插入图片描述

查看docker运行的容器

docker ps

在这里插入图片描述

进入容器

进入容器实质上也是进入了一个linux环境。

docker exec

  • -it:以交互模式运行容器 ,为容器重新分配一个伪输入终端。
  • bash: bash(GNU Bourne-Again Shell)是最常用的一种shell(运行在终端的互动程序)。
 docker exec -it mysql_3306 bash

在这里插入图片描述

容器中运行mysql客户端

登录mysql客户端: mysql -u用户名 -p密码

如果登录成功,则docker安装mysql成功。

在这里插入图片描述

退出容器,防火墙开放端口

在这里插入图片描述

开放指定端口

firewall-cmd —zone=public —add-port=端口号/tcp —permanent

—zone: public 公开端口

—add-port: 端口号/协议名称

—permanent: 永久开放

firewall-cmd --zone=public --add-port=3306/tcp --permanent
重新加载防火墙

firewall-cmd —reload

 firewall-cmd --reload
查看开放的端口号

firewall-cmd —zone=public —list-ports

firewall-cmd --zone=public --list-ports

navicat测试连接

在这里插入图片描述
在这里插入图片描述

mysql主从配置

准备工作

检查binlog是否开启

进入mysql容器,输入命令:

 show variables LIKE ‘log_%’;

ON: 表示已开启

在这里插入图片描述

当前mysql数据库binlog情况

登录mysql客户端: mysql -uroot -p123

使用命令: show master status;

在这里插入图片描述

创建与从(slave)服务通信的用户
create user 'slave'@'%' identified with mysql_native_password by '123';
GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO ‘slave’@’%’;
flush privileges;
获取master容器ip地址
 docker inspect mysql_3306 |grep IPA

在这里插入图片描述

创建slave从服务器3310配置

创建slave配置文件my.cnf

vim /usr/local/software/mysql/3310/conf/my.cnf

# 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=210
log_bin=mysql-slave01-bin
relay_log=wnhz-relay-bin
read_only=1 #设置只读
创建运行slave01容器
docker run -it \
--name mysql_3310 \
--privileged \
-p 3310:3306 \
-v /usr/local/software/mysql/3310/conf/my.cnf:/etc/mysql/my.cnf \
-v /usr/local/software/mysql/3310/data:/var/lib/mysql \
-v /usr/local/software/mysql/3310/mysql-files:/var/lib/mysql-files \
-e MYSQL_ROOT_PASSWORD=123 \
-d mysql

在这里插入图片描述

开放3310端口
firewall-cmd --zone=public --add-port=3310/tcp --permanent
firewall-cmd --reload 
配置主从

进入slave容器,打开mysql客户端

在这里插入图片描述

修改从(slave)服务器与master关联
change master to
master_host='172.17.0.2',
master_user='slave',
master_password='123',
MASTER_LOG_FILE='mysql-bin.000003',
MASTER_LOG_POS=848;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qGXgw6G3-1688472792455)(C:\Users\hjx\Desktop\images\image-20230704195921836.png)]

开启slave

start slave

在这里插入图片描述

查询slave状态

show slave status \G;

在这里插入图片描述

两个yes表示配置完成

创建从服务器登录用户
create user rdb identified with mysql_native_password by '123';
grant select on *.* to rdb;
flush privileges;

测试主从

连接slave

在这里插入图片描述

在master创建库book_db,查看从(slave)是否也创建成功。
创建从服务器登录用户
create user rdb identified with mysql_native_password by '123';
grant select on *.* to rdb;
flush privileges;

测试主从

连接slave
在master创建库book_db,查看从(slave)是否也创建成功。

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值