文章目录
安装Docker
由于我用的是Windows10,所以我打开了Hyper-V,然后在Dcoker官网下载并安装了Docker。
CentOS 之间使用 yum install docker 安装docker然后使用systemctl start docker启动服务,systemctl enable docker开机启动docker服务。
使Docker容器和Windows10建立局域网连接
Docker创建一个局域网
docker network create --subnet=[IP]/[掩码位数] 子网名称
指令示例:
docker network create --subnet=192.168.100.0/24 homenet
在Windows10中将这个子网添加到路由表中 ,Linux重启即可
route add -p [局域网ip] mask [子网掩码] [经过网关(使用Docker默认网关)]
指令示例(管理员模式下运行指令):
route add -p 192.168.100.0 mask 255.255.255.0 10.0.75.2
简单Mysql持久化
预先准备
先启动一个mysql,查看需要持久化的文件有哪些。
docker run -itd --net homenet --ip 192.168.100.10 --name mysql000 -e MYSQL_ROOT_PASSWORD=00000000 mysql
docker exec -it mysql000 /bin/bash
# find / -name "mysql"
然后发现了以下需要映射的文件
/etc/mysql/my.cnf
/var/lib/mysql
保存一份my.cnf文件到宿主机,挂载文件时需要用到
然后删除刚才创建的Mysql,删除前顺便测试下局域网有没有用
mysql -h 192.168.100.10 -u root -p
[mysql]exit
docker stop mysql000
docker rm mysql000
启动mysql
然后创建mysql容器,命令示例(在挂载过程中需要输入Windows10密码授权本地文件操作):
docker run -itd --privileged=true --net homenet --ip 192.168.100.10 --name mysql000 -e MYSQL_ROOT_PASSWORD=00000000 -e MYSQL_USER="ovea" -e MYSQL_PASSWORD="00300100" -v /c/Users/OVEA/Documents/Docker/MySQL/MySQL000/etc/my.cnf:/etc/mysql/my.cnf -v /c/Users/OVEA/Documents/Docker/MySQL/MySQL000/varlib:/var/lib/mysql mysql
搭建MySQL集群
预习准备
docker pull mysql/mysql-cluster
docker run -itd --net homenet --ip 192.168.100.10 --name mysql000 -e MYSQL_ROOT_PASSWORD=00000000 mysql/mysql-cluster ndbd
然后查找需要映射的文件
/var/lib/mysql
/var/lib/mysql-files/
/var/lib/mysql-keyring/
/etc/my.cnf
/etc/mysql-cluster.cnf
最后两个文件保存一份到宿主机
docker stop mysql000
docker rm mysql000
修改配置文件
修改my.cnf [Linux下该文件权限应为644或744]
主要是manager以外的节点使用的,ip指向manager节点的IP
[mysqld]
ndbcluster
ndb-connectstring=192.168.100.10
user=mysql
[mysql_cluster]
ndb-connectstring=192.168.100.10
修改mysql-cluster.cnf [Linux下该文件权限应为644或744]
主要配置节点之间的关系和职责
# 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
[ndbd default]
NoOfReplicas=4
DataMemory=128M
IndexMemory=16M
[ndb_mgmd]
NodeId=1
hostname=192.168.100.10
datadir=/var/lib/mysql
[ndbd]
NodeId=2
hostname=192.168.100.11
datadir=/var/lib/mysql
[ndbd]
NodeId=3
hostname=192.168.100.12
datadir=/var/lib/mysql
[ndbd]
NodeId=4
hostname=192.168.100.13
datadir=/var/lib/mysql
[mysqld]
NodeId=5
hostname=192.168.100.14
启动5个节点
Windows 指令示例
docker run -itd --privileged=true --net homenet --ip 192.168.100.10 --name mysql-mgmd-000 -v /c/Users/OVEA/Documents/Docker/MySQL/MySQL000/etc/mysql-cluster.cnf:/etc/mysql-cluster.cnf -v /c/Users/OVEA/Documents/Docker/MySQL/MySQL000/varlib:/var/lib/mysql mysql/mysql-cluster ndb_mgmd
docker run -itd --privileged=true --net homenet --ip 192.168.100.11 --name mysql-node-000 -v /c/Users/OVEA/Documents/Docker/MySQL/MySQL001/etc/my.cnf:/etc/my.cnf -v /c/Users/OVEA/Documents/Docker/MySQL/MySQL001/etc/mysql-cluster.cnf:/etc/mysql-cluster.cnf -v /c/Users/OVEA/Documents/Docker/MySQL/MySQL001/varlib:/var/lib/mysql mysql/mysql-cluster ndbd
docker run -itd --privileged=true --net homenet --ip 192.168.100.12 --name mysql-node-001 -v /c/Users/OVEA/Documents/Docker/MySQL/MySQL002/etc/my.cnf:/etc/my.cnf -v /c/Users/OVEA/Documents/Docker/MySQL/MySQL002/etc/mysql-cluster.cnf:/etc/mysql-cluster.cnf -v /c/Users/OVEA/Documents/Docker/MySQL/MySQL002/varlib:/var/lib/mysql mysql/mysql-cluster ndbd
docker run -itd --privileged=true --net homenet --ip 192.168.100.13 --name mysql-node-002 -v /c/Users/OVEA/Documents/Docker/MySQL/MySQL003/etc/my.cnf:/etc/my.cnf -v /c/Users/OVEA/Documents/Docker/MySQL/MySQL003/etc/mysql-cluster.cnf:/etc/mysql-cluster.cnf -v /c/Users/OVEA/Documents/Docker/MySQL/MySQL003/varlib:/var/lib/mysql mysql/mysql-cluster ndbd
docker run -itd --privileged=true --net homenet --ip 192.168.100.14 --name mysql000 -v /c/Users/OVEA/Documents/Docker/MySQL/MySQL004/etc/my.cnf:/etc/my.cnf -v /c/Users/OVEA/Documents/Docker/MySQL/MySQL004/varlib:/var/lib/mysql -e MYSQL_RANDOM_ROOT_PASSWORD=true mysql/mysql-cluster mysqld
Linux命令示例:
docker run -itd --privileged=true --net homenet --ip 192.168.100.10 --name mysql-mgmd-000 -v /home/ovea/Documents/Docker/MySQL/MySQL000/etc/mysql-cluster.cnf:/etc/mysql-cluster.cnf -v /home/ovea/Documents/Docker/MySQL/MySQL000/varlib:/var/lib/mysql mysql/mysql-cluster ndb_mgmd
docker run -itd --privileged=true --net homenet --ip 192.168.100.11 --name mysql-node-000 -v /home/ovea/Documents/Docker/MySQL/MySQL001/etc/my.cnf:/etc/my.cnf -v /home/ovea/Documents/Docker/MySQL/MySQL001/etc/mysql-cluster.cnf:/etc/mysql-cluster.cnf -v /home/ovea/Documents/Docker/MySQL/MySQL001/varlib:/var/lib/mysql mysql/mysql-cluster ndbd
docker run -itd --privileged=true --net homenet --ip 192.168.100.12 --name mysql-node-001 -v /home/ovea/Documents/Docker/MySQL/MySQL002/etc/my.cnf:/etc/my.cnf -v /home/ovea/Documents/Docker/MySQL/MySQL002/etc/mysql-cluster.cnf:/etc/mysql-cluster.cnf -v /home/ovea/Documents/Docker/MySQL/MySQL002/varlib:/var/lib/mysql mysql/mysql-cluster ndbd
docker run -itd --privileged=true --net homenet --ip 192.168.100.13 --name mysql-node-002 -v /home/ovea/Documents/Docker/MySQL/MySQL003/etc/my.cnf:/etc/my.cnf -v /home/ovea/Documents/Docker/MySQL/MySQL003/etc/mysql-cluster.cnf:/etc/mysql-cluster.cnf -v /home/ovea/Documents/Docker/MySQL/MySQL003/varlib:/var/lib/mysql mysql/mysql-cluster ndbd
docker run -itd --privileged=true --net homenet --ip 192.168.100.14 --name mysql000 -v /home/ovea/Documents/Docker/MySQL/MySQL004/etc/my.cnf:/etc/my.cnf -v /home/ovea/Documents/Docker/MySQL/MySQL004/varlib:/var/lib/mysql -e MYSQL_RANDOM_ROOT_PASSWORD=true mysql/mysql-cluster mysqld
必须一次启动成功,否则将会导致无法正确运行!启动完前一个节点再启动下一个节点。
结果:
在日志中找到密码然后登录
docker logs mysql000
docker exec mysql000 /bin/bash
# mysql -u root -p
需要把-v挂载除配置文件外的其他选项(持久化集群)都取消掉才能正常打开mysqld服务,原因不明……而且错误也没有写入日志中!
解决方案,先停止所有容器,然后全部启动。(虽然这样是启动了,但是密码就不知道了)
Windows10
docker stop $(docker ps -q)
docker start $(docker ps -a -q)
Linux
sudo docker stop $(sudo docker ps -q)
sudo docker start $(sudo docker ps -a -q)
参考资料
https://www.jianshu.com/p/cc837360c9e7