docker下配置mysql 主从

本机docker下配置mysql主从

首先安装docker mysql容器

$ docker pull mysql:5.6

拉取两个相同版本mysql

分别启动mysql 并设置root用户密码为admin:

//主mysql
docker run -d -e MYSQL_ROOT_PASSWORD=admin --name mysql-master -v /Volumes/docker/mysql/my-m.cnf:/etc/mysql/my.cnf -p 3307:3306 mysql:5.6

从mysql
docker run -d -e MYSQL_ROOT_PASSWORD=admin --name mysql-slave -v  /Volumes/docker/mysql/my-s.cnf:/etc/mysql/my.cnf -p 3308:3306 mysql:5.6

mysql master配置文件my-m.cnf:

 # Copyright (c) 2014, 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 Community Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[client]
port        = 3306
socket      = /var/run/mysqld/mysqld.sock

[mysqld_safe]
pid-file    = /var/run/mysqld/mysqld.pid
socket      = /var/run/mysqld/mysqld.sock
nice        = 0

[mysqld]
user        = mysql
pid-file    = /var/run/mysqld/mysqld.pid
socket      = /var/run/mysqld/mysqld.sock
port        = 3306
basedir     = /usr
datadir     = /var/lib/mysql
tmpdir      = /tmp
secure-file-priv = NULL
lc-messages-dir = /usr/share/mysql
explicit_defaults_for_timestamp

log-bin = mysql-bin
server-id = 100

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address   = 127.0.0.1

#log-error  = /var/log/mysql/error.log

# Recommended in standard MySQL setup
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

# Disabling symbolic-links is recommended to prevent assorted security risks
#symbolic-links=0

# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with ‘.cnf‘, otherwise they‘ll be ignored.
#
!includedir /etc/mysql/conf.d/

mysql master配置文件my-s.cnf:

 # Copyright (c) 2014, 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 Community Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[client]
port        = 3306
socket      = /var/run/mysqld/mysqld.sock

[mysqld_safe]
pid-file    = /var/run/mysqld/mysqld.pid
socket      = /var/run/mysqld/mysqld.sock
nice        = 0

[mysqld]
user        = mysql
pid-file    = /var/run/mysqld/mysqld.pid
socket      = /var/run/mysqld/mysqld.sock
port        = 3306
basedir     = /usr
datadir     = /var/lib/mysql
tmpdir      = /tmp
secure-file-priv = NULL
lc-messages-dir = /usr/share/mysql
explicit_defaults_for_timestamp

log-bin = mysql-bin
server-id = 101

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address   = 127.0.0.1

#log-error  = /var/log/mysql/error.log

# Recommended in standard MySQL setup
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with ‘.cnf‘, otherwise they‘ll be ignored.
#
!includedir /etc/mysql/conf.d/

注意:主配置文件和从配置文件的server-id不同。

//进入主mysql:bash -c "clear && docker exec -it mysql-master sh"

//链接mysqlmysql -uroot -padmin

//创建从用户
CREATE USER ‘slave‘@‘%‘ IDENTIFIED BY ‘12345678‘;
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO ‘slave‘@‘%‘;
show master status;

mysql>
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 | 2386 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

记录好file 和position

//链接从数据库bash -c "clear && docker exec -it mysql-slave sh"

//进入从数据库mysql -uroot -p

//创建change master to master_host=‘172.17.0.5‘, master_user=‘slave‘, master_password=‘12345678‘, master_port=3306, master_log_file=‘mysql-bin.000003‘, master_log_pos=2386, master_connect_retry=30;
ps:docker中查看主从mysql的ip

? lib docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e5b0b0e6e33c mysql "docker-entrypoint..." 47 hours ago Up 2 minutes 33060/tcp, 0.0.0.0:3308->3306/tcp mysql-slave
402ea137d6da mysql "docker-entrypoint..." 47 hours ago Up 3 minutes 33060/tcp, 0.0.0.0:3307->3306/tcp mysql-master
d905b168284e php:7.1.0-fpm "docker-php-entryp..." 2 days ago Up 4 hours 0.0.0.0:9000->9000/tcp myphp
ae37295e5360 nginx "nginx -g ‘daemon ..." 2 days ago Up 4 hours 0.0.0.0:8080->80/tcp mynginx
4725b5cd7515 redis:latest "docker-entrypoint..." 3 months ago Up 5 hours 0.0.0.0:6380->6379/tcp myredis
? lib
? lib docker inspect --format ‘{{ .NetworkSettings.IPAddress }}‘ 402
172.17.0.5

//启动同步start slave;

//查看状态

mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.17.0.5
Master_User: slave
Master_Port: 3306
Connect_Retry: 30
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 2386
Relay_Log_File: e5b0b0e6e33c-relay-bin.000002
Relay_Log_Pos: 2014
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 2386
Relay_Log_Space: 2229
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 100
Master_UUID: 7ce8f23c-a79f-11e8-a391-0242ac110005
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 0
1 row in set (0.00 sec)


ERROR:
No query specified


//都为yes表示成功Slave_IO_Running: YesSlave_SQL_Running: Yes

原文地址:https://www.cnblogs.com/oceanL/p/9538933.html

时间: 2025-01-12 05:34:54

docker下配置mysql 主从的相关文章

linux下配置mysql主从

为了做实验方便,我们在同一台机器上配置两个MySQL服务(开两个端口)  1.安装.配置MySQL 事先已经安装好mysql: [[email protected] ~]# cd /usr/local/ [[email protected] local]# cp -r mysql/ mysql_2 [[email protected] local]# cd mysql_2/ 初始化mysql2,如果出现两个 "OK" 并且生成/data/mysql2目录说明正确: [[email pr

Ubuntu配置Mysql主从数据库

MySQL数据库支持数据库的主从复制功能,因此在集群方面具有其独特的优势.众多国内外大型网站架构体系中,均采用了MySQL的主从数据库配置来实现查询负载.数据库热备等功能.本人在实际的Web项目中也涉及到这一需求,在此将如何配置实现做个简单小结. 本次环境:虚拟机下 服务器:Ubuntu 14.04 LTS数据库: 5.5.37端口:3306主IP:192.168.63.133从IP:192.168.63.134授权账号:user:suxhpassword:111111好了交代完环境:我们直接配

linux下搭建mysql主从

在master上创建repl账户,用于复制. grant replication slave on *.* to 'repl'@'%' identified by '[email protected]$$W0rd'; flush privileges; 与windows下搭建mysql主从的区别: 二进制日志的路径格式不一样 master: my.cnf部分配置(master): thread_handling = pool-of-threads thread_pool_oversubscrib

docker 下修改 mysql sql_mode和配置文件

原文:docker 下修改 mysql sql_mode和配置文件 打开PowerShell 首先创建mysql容器,这里我们指定使用mysql5.7的版本 docker run -d -p 3306:3306 --name mysql-docker -e MYSQL_ROOT_PASSWORD=root mysql:5.7 创建成功,查看一下运行状态 docker ps ? 可以看到我们的容器正在运行中,现在进入容器,查看一下配置文件 ? docker exec -it mysql-docke

Docker下配置nacos

前言 近段时间在学dubbo,dubbo-admin死活装不上,无论是本地还是docker,所以把目光投向了其他配置中心,我选定的是阿里新开源的nacos. 正文 拉取镜像到本地docker docker pull nacos/nacos-server 新建nacos容器 docker run --env MODE=standalone --name nacos -d -p 8848:8848 nacos/nacos-server 其中env参数是指定容器所处环境,这里是指建立单机版的nacos

Windows 2003 IIS下配置MySQL+PHP+ISAPI_Rewrite+Zend+Xcache

Windows 2003 IIS下配置MySQL+PHP+ISAPI_Rewrite+Zend+Xcache zend  mysql  php  iis  windows  extension 一.准备工作 windows 2003,自己买吧... 安装IIS 6.0:安装系统后在"控制面板"->"添加或删除程序"->"添加/删除Windows组件"->双击"应用程序服务器"->然后选中"In

ubuntu 下配置MySQL服务

第一步 安装MySQL sudo apt-get install mysql-server 第二步 配置MySQL 2.1 vim /etc/mysql/my.cnf 找到bind-address = 127.0.0.1. 2.2 注释掉: #bind-address = 127.0.0.1 或者把IP改为 0.0.0.0:bind-address = 0.0.0.0 允许任意IP访问 2.3 重启 mysql服务: sudo /etc/inid.d/mysql restart 第三步 授权用户

hibernate 在tomcat7.X 下配置mysql数据源

先说一点题外话,LZ最近学习java web.今天刚看到hibernate,发现在hibernate配置数据源时网上的资料都太久远了,一般以tomcat 5 版本下的配置居多.而tomcat 7下的配置略有变化,新手找资料困难,可能会略受打击,故整理资料与大家共享,也可作备忘之用!若有不当之处,还请不吝赐教! 1.添加数据库驱动包mysql-connector-java-5.1.13-bin.jar加入到tomcat目录下的lib包中. 注意:网上几乎所有版本都说需要添加commons-dbcp

CentOS 7下的 Mysql 主从配置

最近在玩mysql主从配置,在此记录一下 一.前言 1.安装两个虚拟机(CentOS 7).iP分别是192.168.47.131 和192.168.47.133.其中192.168.47.133作为主数据库,192.168.47.131作为从数据库 2.在线安装Mysql数据库.具体安装方法请参考: 3.在主从数据库中创建 testdb 数据库.(作为同步的数据库) 二.具体步骤 1.主数据库(master)配置 (1)在Master MySQL上创建一个用户‘test’,并允许其他Slave