搭建mysql集群目的是为springboot提供数据服务
这一篇文章主要是记录了在docker环境下搭建MySQL主从集群的过程,以及在准备搭建的过程中遇到的一些问题,问题主要包括docker版本的升级以及centos内核的升级。在执行docker restart ID命令重启容器经常会出现崩溃并且network等命令在1.7版本中无法使用(network命令1.9之后提供),所以把docker升级到更高的版本,这必须要升级centos的内核版本,由于阿里云的服务器的内核版本是2.6不能满足大部分docker版本的要求,固升级内核版本为3.1,升级的过程具体可以参考
版本升级的参考,执行过程如下:
1
2
3
4
5
6
7
8
9 1[root@iZwz9cps5bpzjurg8m4ax0Z bin]# curl -sSL -O https://get.docker.com/builds/Linux/x86_64/docker-1.9.1 && chmod +x docker-1.9.1 && mv docker-1.9.1 /usr/local/bin/docker
2mv: overwrite `/usr/local/bin/docker'? y
3[root@iZwz9cps5bpzjurg8m4ax0Z bin]# cp /usr/local/bin/docker /usr/bin/docker
4cp: overwrite `/usr/bin/docker'? y
5[root@iZwz9cps5bpzjurg8m4ax0Z bin]# service docker start
6Starting docker: [ OK ]
7[root@iZwz9cps5bpzjurg8m4ax0Z bin]# docker -v
8Docker version 1.9.1, build a34a1d5
9
在升级的过程中,如果发现docker的旧版本已经安装就会报冲突的错误,因此在安装新的版本之前应该使用yum remove docker命令将旧的移除。
主从MySQL集群总体的思路:
1、下载MySQL镜像
2、在虚拟机(或者windows等)中配置MySQL的配置文件
3、根据配置文件运行MySQL的docker镜像
4、修改master和slave主从数据库的关系
下载MySQL镜像
第一次尝试通过docker search mysql直接搜的,下载特别慢,后来经过查找发现下边这两种是比较快速的:
1
2
3 1docker pull daocloud.io/mysql
2docker pull hub.c.163.com/library/mysql:latest
3
1
2 1这两个都是国内的镜像,下载速度非常快
2
在虚拟机(或者windows等)中配置MySQL的配置文件
配置文件和非docker方式的mysql集群的配置一致,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 1# Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
2#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; version 2 of the License.
6#
7# This program is distributed in the hope that it will be useful,
8# but WITHOUT ANY WARRANTY; without even the implied warranty of
9# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10# GNU General Public License for more details.
11#
12# You should have received a copy of the GNU General Public License
13# along with this program; if not, write to the Free Software
14# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15
16
17#
18# The MySQL Community Server configuration file.
19#
20# For explanations see
21# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
22
23
24[client]
25port = 3306
26socket = /var/run/mysqld/mysqld.sock
27
28
29[mysqld_safe]
30pid-file = /var/run/mysqld/mysqld.pid
31socket = /var/run/mysqld/mysqld.sock
32nice = 0
33
34
35[mysqld]
36user = mysql
37pid-file = /var/run/mysqld/mysqld.pid
38socket = /var/run/mysqld/mysqld.sock
39port = 3306
40basedir = /usr
41datadir = /var/lib/mysql
42tmpdir = /tmp
43lc-messages-dir = /usr/share/mysql
44explicit_defaults_for_timestamp
45
46
47log-bin = mysql-bin
48server-id = 1
49
50
51# Instead of skip-networking the default is now to listen only on
52# localhost which is more compatible and is not less secure.
53#bind-address = 127.0.0.1
54
55
56#log-error = /var/log/mysql/error.log
57
58
59# Recommended in standard MySQL setup
60sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
61
62
63# Disabling symbolic-links is recommended to prevent assorted security risks
64symbolic-links=0
65
66
67# * IMPORTANT: Additional settings that can override those from this file!
68# The files must end with '.cnf', otherwise they'll be ignored.
69#
70!includedir /etc/mysql/conf.d/
71
1
2 1 在centos中的以下两个配置目录中新建以下两个文件,并将上述配置填入
2
/docker/mysql-cluster/master/my-m.cnf以及/docker/mysql-cluster/slave/my-s.cnf,my-m.cnf和my-s.cnf唯一的不同是server-id=1和server-id=2,server-id用来标识主从数据库
根据配置文件运行MySQL的docker镜像
1
2
3
4
5
6 1#master库
2docker run -d -e MYSQL_ROOT_PASSWORD=testcode --name mysql-master -v /docker/mysql-cluster/master/my-m.cnf:/etc/mysql/my.cnf -p 3307:3306 daocloud.io/mysql
3
4#slave库
5docker run -d -e MYSQL_ROOT_PASSWORD=testcode --name mysql-slave -v /docker/mysql-cluster/slave/my-s.cnf:/etc/mysql/my.cnf -p 3308:3306 daocloud.io/mysql
6
1
2 1 一些参数解释:
2
-d,表示Detached mode,后台运行
-t,让Docker分配一个伪终端(pseudo-tty)并绑定到容器的标准输入上
-i,让容器的标准输入保持打开,可以-it配合用,与-d相对应
-e,表示传入的环境变量
-p,表示暴露的端口,即本机端口与docker端口映射
-v,表示挂载文件,这里挂载了本地的文件夹到docker的文件夹
–name,表示给容器起的名字,方便查看,若要配置主从,
执行命令结束后可以看到
1
2
3
4
5 1[root@iZwz9cps5bpzjurg8m4ax0Z master]# docker ps
2CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3812b841b5422 daocloud.io/mysql "docker-entrypoint.sh" 4 seconds ago Up 3 seconds 0.0.0.0:3308->3306/tcp mysql-slave
433225198ecc3 daocloud.io/mysql "docker-entrypoint.sh" 2 minutes ago Up 2 minutes 0.0.0.0:3307->3306/tcp mysql-master
5
1
2 1mysql-master和mysql-slave两个基于daocloud.io/mysql镜像的容器被先后启动。由于daocloud.io/mysql镜像中包含了vi,不需要再单独进行安装,docker logs 容器的ID 可以查看容器运行的日志。
2
修改master和slave主从数据库的关系
执行docker exec -it mysql-master bash进去master库的vim环境中,执行mysql -uroot -p,进入docker中的mysql,执行
1
2
3
4
5
6
7
8
9 1mysql> GRANT REPLICATION SLAVE ON *.* to 'backup'@'%' identified by 'master_123';
2
3mysql> show master status;
4+------------------+----------+--------------+------------------+-------------------+
5| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
6+------------------+----------+--------------+------------------+-------------------+
7| mysql-bin.000003 | 439 | | | |
8+------------------+----------+--------------+------------------+-------------------+
9
同样方式进入slave库,执行:
1
2 1change master to master_host='对外主机IP',master_user='backup',master_password='master_123',master_log_file='mysql-bin.000003',master_log_pos=439,master_port=3307;
2
1
2 1这里边有一些对应关系要注意。
2
可以看到,在master上建立sdata库后,slave中也存了相同的库,主从关系搭建完毕
参考文章: http://blog.csdn.net/qq362228416/article/details/48569293 http://www.jianshu.com/p/12c34395e76e http://www.cnblogs.com/felixzh/p/5194527.html