1. 程式人生 > >Docker安裝Mysql叢集

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              328
edcd84f1b 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等,綜合起來還是有點收穫的。

轉載請註明作者和原連結