Docker安装Mysql集群

一.首先下载Docker
由于国内下载Github的很慢,推荐大家去
http://get.daocloud.io/ 去下载。根据自己的系统安装,注意的是win10的最好安装Docker for window ,win10以下的安装 DockerToolBox。
二.配置Docker加速
推荐使用 https://www.daocloud.io/mirror#accelerator-doc 配置Docker加速,不加速的话下载速度可能只有几十k。
三.下载mysql 镜像
都进入命令行 一条Docker命令

docker pull mysql 

用最新mysql镜像的就行了

等待安装完成。

docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
learn/ping          latest              d320964ebaf2        8 days ago          139.5 MB
php                 latest              84c6a05223c5        2 weeks ago         369.7 MB
centos              latest              328edcd84f1b        2 weeks ago         192.5 MB
mysql               latest              c73c7527c03a        4 weeks ago         412.4 MB
learn/tutorial      latest              a7876479f1aa        4 years ago         128 MB

看到安装的mysql镜像即可
四.启动mysql容器
1.先启动一个主数据库 mysqls1

docker run -p 6555:3306 --name mysqls1 -e MYSQL_ROOT_PASSWORD=123456 -v c:/hosts/mysql.conf/mysql.conf.d:/hosts/mysql.conf/mysql.conf.d -d mysql

挂载一个宿机的文件夹上去方便修改mysql的配置,这里大家可以去官网详细查询挂载的操作-v如果是windows需要配置shared dirves 这里我共享了c盘,大家根据自己需要配置。
2.同理同上 挂载一个从数据库 mysqls2

docker run -p 6556:3306 --name mysqls2 -e MYSQL_ROOT_PASSWORD=123456 -v c:/hosts/mysql.conf/mysql.conf.d:/hosts/mysql.conf/mysql.conf.d -d mysql

3.查看容器状态是否开启成功

docker ps

正常的状态是
这里写图片描述
大家可以查看下先启动容器再说
4.配置mysql配置
因为在上面我们挂载了宿机的一个文件夹到容器中了,在C:\hosts\mysql.conf\mysql.conf.d中新建一个文件mysqld.cnf文件对应mysql的mysqld配置,不同的mysql版本配置可能不同,我这选用了最新的mysql镜像。
配置主数据库

# Copyright (c) 2014, 2016, 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
#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

log_bin           = mysql-bin
server_id         = 1

然后进入主数据库容器mysqls1

docker exec -i -t mysqls1 /bin/bash

把我们的配置覆盖主数据库的配置即可,几个命令搞定

cd /hosts/mysql.conf/mysql.conf.d
cp -r mysqld.cnf /etc/mysql/mysql.conf.d

配置好主数据的配置了,从数据库配置同上,主要就区分server_id就可以了。

# Copyright (c) 2014, 2016, 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
#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

log_bin           = mysql-bin
server_id         = 2


同上修改了从数据库配置

五.主从配置
1.先配置主数据配置吧。
进入主数据库容器mysqls1

docker exec -i -t mysqls1 /bin/bash

进入mysql

mysql -u root -p

输入密码123456即可

然后创建用户 mysync 密码q123456 授权slave

GRANT REPLICATION SLAVE ON *.* to 'mysync'@'%' identified by 'q123456';

最后

show master status;

这里写图片描述

记录下File和Position,主数据库配置完成
2.从数据配置
同上 进入从数据库容器,进入mysql

change master to master_host='192.168.1.139',master_user='mysync',master_password='q123456',master_log_file='mysql-bin.000002',master_log_pos=2769,master_port=6555;

这里根据自己配置修改 ,Ip等
配置好主数据库信息后开启slave

start slave;

正常应该是这样
这里写图片描述

 Slave_IO_Running: Yes
 Slave_SQL_Running: Yes

Error信息都没有,如果有error查看一下错误信息,解决一下。
好了这里基本完成了 主从配置了,我们测试一下吧,为了方便不在黑窗口慢慢show databases create database了。直接上工具吧。

主数据库的数据
这里写图片描述

从数据库的数据
这里写图片描述

现在都是一样的,我们在主数据加点数据
这里写图片描述
然后检查从数据库的配置
这里写图片描述
同步完成,检查下进程正常
这里写图片描述
这个ip是docker分配本地局域网ip。检查一切正常。至此docker安装mysql集群已经实现,docker对我们搭建集群很方便了,以前要开虚拟机来配置。
六.总结
1.涉及到docker的一些命令 pull,ps,exec,stop,start等,2.涉及到文件挂载
3.涉及到mysql数据库主从的一些配置。涉及不多 4.linux的一些常用命令cp cd ls等,综合起来还是有点收获的。

转载请注明作者和原链接

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值