栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

docker上搭建mysql主从复制

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

docker上搭建mysql主从复制

docker上搭建mysql主从复制
  • 1.安装docker
  • 2.安装mysql,33061的mysql作为主机,33062的mysql作为从机
    • 2.1 主机,如果没有pull过mysql镜像,执行如下命令也会去拉取mysql的镜像
    • 2.2 从机
    • 2.3 查看正在运行的容器
    • 2.4 用本地navicat连接dokcer上的mysql
    • 2.5 如果用navicat连不上mysql可以参考以下地址
  • 3. 在主机some-mysql1建一个用户
  • 4. 主机some-mysql1的配置
    • 4.1 进入mysql容器
    • 4.2 查看mysql配置文件
    • 4.3 把原来的mysql配置文件的内容拷贝出来
    • 4.3 退出,然后新建一个mysqld.cnf文件,把刚才的内容拷贝进来,开启二进制功能,Esc+:wq保存退出
    • 4.4 把新建的mysqld.cnf文件替换掉原来的mysql配置文件,并且重启mysql
    • 4.5 使用show master status命令查看主机是否开启了二进制功能,记住File和Position这两参数
  • 4. 从机some-mysql2的配置
    • 4.1 进入到从机
    • 4.2 查看从机的配置文件,拷贝内容出来
    • 4.3 删除掉刚才创建的mysqld.cnf文件
    • 4.4 新建mysqld.cnf文件,把刚才的内容拷贝进来,加上server-id,保存退出
    • 4.5 将新建的mysqld.cnf替换掉从机的配置文件,并且重启从机
    • 4.6 登录进入到从机
    • 4.7 指定主机,开启同步
    • 4.8 使用show slave statusG;命令查看同步配置是否成功
  • 5. 测试
    • 5.1 在主机some-mysql1创建一个数据库和一个user表
    • 5.2 可以看到从机some-mysql2也跟着创建了demo1数据库和user表

1.安装docker

详细步骤链接: https://www.cnblogs.com/yufeng218/p/8370670.html.

2.安装mysql,33061的mysql作为主机,33062的mysql作为从机 2.1 主机,如果没有pull过mysql镜像,执行如下命令也会去拉取mysql的镜像
docker run --name some-mysql1 -p 33061:3306  -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
2.2 从机
docker run --name some-mysql2 -p 33062:3306  -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
2.3 查看正在运行的容器

docker ps

2.4 用本地navicat连接dokcer上的mysql

2.5 如果用navicat连不上mysql可以参考以下地址

链接1: https://blog.csdn.net/qq_38684778/article/details/107230725?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-8.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-8.no_search_link.

3. 在主机some-mysql1建一个用户

grant replication slave on *.* to ‘usr1’@’%’ identified by ‘123’;

4. 主机some-mysql1的配置 4.1 进入mysql容器

docker exec -it some-mysql1 bash

4.2 查看mysql配置文件

cat /etc/mysql//mysql.conf.d/mysqld.cnf

4.3 把原来的mysql配置文件的内容拷贝出来
# Copyright (c) 2014, 2021, Oracle and/or its affiliates.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation.  The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# 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, version 2.0, 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

4.3 退出,然后新建一个mysqld.cnf文件,把刚才的内容拷贝进来,开启二进制功能,Esc+:wq保存退出


4.4 把新建的mysqld.cnf文件替换掉原来的mysql配置文件,并且重启mysql

docker cp ./mysqld.cnf some-mysql1:etc/mysql/mysql.conf.d/
docker restart some-mysql1

4.5 使用show master status命令查看主机是否开启了二进制功能,记住File和Position这两参数

4. 从机some-mysql2的配置 4.1 进入到从机

docker exec -it some-mysql2 bash

4.2 查看从机的配置文件,拷贝内容出来

cat /etc/mysql//mysql.conf.d/mysqld.cnf

# Copyright (c) 2014, 2021, Oracle and/or its affiliates.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation.  The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# 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, version 2.0, 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

4.3 删除掉刚才创建的mysqld.cnf文件

4.4 新建mysqld.cnf文件,把刚才的内容拷贝进来,加上server-id,保存退出


4.5 将新建的mysqld.cnf替换掉从机的配置文件,并且重启从机

docker cp ./mysqld.cnf some-mysql2:etc/mysql/mysql.conf.d/
docker restart some-mysql2

4.6 登录进入到从机

4.7 指定主机,开启同步
change master to master_host='192.168.244.129',master_port=33061,master_user='usr1',master_password='123',master_log_file='binlog.000001',master_log_pos=154;

4.8 使用show slave statusG;命令查看同步配置是否成功

5. 测试 5.1 在主机some-mysql1创建一个数据库和一个user表

5.2 可以看到从机some-mysql2也跟着创建了demo1数据库和user表

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/277056.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号