Docker安裝MySQL多實(shí)例及主從復(fù)制

一、單實(shí)例安裝

1.1 下載鏡像

使用docker search mysql

INDEX       NAME                DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
docker.io   docker.io/mysql     MySQL is a widely used, open-source relati...   8178      [OK]
docker.io   docker.io/mariadb   MariaDB is a community-developed fork of M...   2785      [OK]
//...

選擇第一個(gè)鏡像docker.io/mysql,可以自己指定版本號(hào)掂僵,這里使用的是5.7航厚。使用如下命令下載mysql鏡像:

# docker pull docker.io/mysql:5.7
Trying to pull repository docker.io/library/mysql ...
5.7: Pulling from docker.io/library/mysql
743f2d6c1f65: Pull complete
3f0c413ee255: Pull complete
aef1ef8f1aac: Pull complete
f9ee573e34cb: Pull complete
3f237e01f153: Pull complete
f9da32e8682a: Pull complete
4b8da52fb357: Pull complete
6f38e9cfd49b: Pull complete
9f4834b3f44f: Pull complete
af631d92fdba: Pull complete
0e771ddab25c: Pull complete
Digest: sha256:196fe3e00d68b2417a8cf13482bdab1fcc2b32cf7c7575d0906c700688b352b4
Status: Downloaded newer image for docker.io/mysql:5.7

1.2 啟動(dòng)mysql

啟動(dòng)mysql除了端口映射之外,還需要使用-e MYSQL_ROOT_PASSWORD指定一下root密碼锰蓬,命令如下:

# docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
d16116181b61544db1681dde4f07fc79774f08144044617b20a19f5d46128071
# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
d16116181b61        mysql:5.7           "docker-entrypoint..."   3 seconds ago       Up 3 seconds        0.0.0.0:3306->3306/tcp, 33060/tcp   mysql

使用docker ps可以看到mysql已經(jīng)正常啟動(dòng)起來了幔睬。可以用命令行或者mysql客戶端連接上去看看芹扭。

如果mysql是安裝在遠(yuǎn)程服務(wù)器麻顶,使用本地的mysql客戶端連接不上的話,一般有兩個(gè)原因:

    1. 服務(wù)器端口號(hào)未開放
    1. mysql未開啟遠(yuǎn)程登錄

解決方法:

對(duì)于原因1舱卡,檢查下服務(wù)器的防火墻等相關(guān)配置辅肾。
對(duì)于原因2,需在服務(wù)器上登錄mysql并開啟遠(yuǎn)程登錄轮锥。

開啟遠(yuǎn)程登錄方法如下:

在服務(wù)器上通過docker命令進(jìn)入mysql實(shí)例內(nèi)部并登錄mysql
# docker exec -it d16116181b61 /bin/bash
root@d16116181b61:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26 MySQL Community Server (GPL)

// ....

mysql>
開啟mysql遠(yuǎn)程登錄

這一步只需要在mysql的user表中設(shè)置一條host='%'并且user='root'的記錄即可矫钓。

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select host,user from user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| %         | root          |
| localhost | mysql.session |
| localhost | mysql.sys     |
| localhost | root          |
+-----------+---------------+
4 rows in set (0.00 sec)

檢查下mysql數(shù)據(jù)庫(kù)的user表中是否有host='%'并且user='root'的記錄,用Docker安裝的mysql默認(rèn)是有的舍杜。
如果沒有新娜,可以執(zhí)行如下的更新語句:

update set host = '%' where host = 'localhost' and user = 'root';
flush privileges;

user表中默認(rèn)都會(huì)有一條user='root'并且host='localhost'的記錄,可以直接將其host改為%既绩。

二概龄、主從安裝

2.1 主服務(wù)器安裝

2.1.1 配置主服務(wù)器

首先為主服務(wù)器創(chuàng)建一個(gè)文件夾,例如/home/mysql1饲握,用來存放主服務(wù)器的配置文件和數(shù)據(jù)文件私杜。

在mysql1下創(chuàng)建conf/my.cnf,文件內(nèi)容如下互拾。

# 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

# 主數(shù)據(jù)庫(kù)端ID號(hào)
server_id = 1
# 開啟二進(jìn)制日志
log-bin = mysql-bin
# 需要復(fù)制的數(shù)據(jù)庫(kù)名歪今,如果復(fù)制多個(gè)數(shù)據(jù)庫(kù),重復(fù)設(shè)置這個(gè)選項(xiàng)即可
# binlog-do-db = db
# 復(fù)制時(shí)需要忽略的數(shù)據(jù)庫(kù)
binlog-ignore-db=mysql

# 為每個(gè)session分配的內(nèi)存颜矿,在事務(wù)過程中用來存儲(chǔ)二進(jìn)制日志的緩存
binlog_cache_size=1M
# 主從復(fù)制的格式(mixed, statement, row, 默認(rèn)格式是statement)
binlog_format=mixed
# 將從服務(wù)器從主服務(wù)器收到的更新記入到從服務(wù)器自己的二進(jìn)制日志文件中
log-slave-updates
# 控制binlog的寫入頻率。每執(zhí)行多少次事務(wù)寫入一次(這個(gè)參數(shù)性能消耗很大嫉晶,但可減小MySQL崩潰造成的損失)
sync_binlog = 1
# 這個(gè)參數(shù)一般用在主主同步中骑疆,用來錯(cuò)開自增值, 防止鍵值沖突
auto_increment_offset = 1
# 這個(gè)參數(shù)一般用在主主同步中,用來錯(cuò)開自增值, 防止鍵值沖突
auto_increment_increment = 1
# 二進(jìn)制日志自動(dòng)刪除的天數(shù)替废,默認(rèn)值為0,表示“沒有自動(dòng)刪除”箍铭,啟動(dòng)時(shí)和二進(jìn)制日志循環(huán)時(shí)可能刪除
expire_logs_days = 7
# 將函數(shù)復(fù)制到slave
log_bin_trust_function_creators = 1

前面都是基礎(chǔ)配置,從server-id開始是主服務(wù)器的配置椎镣。

2.1.2 啟動(dòng)主服務(wù)器

使用docker run來啟動(dòng)一個(gè)mysql實(shí)例作為主服務(wù)器

# docker run --name mysql1 -v /home/mysql1/conf/my.cnf:/etc/mysql/my.cnf -v /home/mysql1/data:/var/lib/mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci
8a54b5ee92d2137b96879eec77c79c20507346d3c57cc34fbddbe3f6c6bcc323
2.1.3 創(chuàng)建復(fù)制賬號(hào)

登錄mysql诈火,為從服務(wù)器創(chuàng)建一個(gè)賬號(hào),并賦予其相應(yīng)權(quán)限状答。

root@8a54b5ee92d2:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create user 'slave'@'%' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> grant replication slave, replication client on *.* to 'slave'@'%';
Query OK, 0 rows affected (0.00 sec)
2.1.4 查看master狀態(tài)

使用show master status查看當(dāng)前的狀態(tài)冷守,并記錄下File和Position刀崖,后面從服務(wù)器要用。

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |      617 |              | mysql            |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
2.1.5 獲取master IP

使用docker inspect查看主服務(wù)器IP拍摇,后面從服務(wù)器要用亮钦。

# docker inspect --format='{{.NetworkSettings.IPAddress}}' 4264f76d7003
172.17.0.2

2.2 從實(shí)例安裝

2.2.1 配置從服務(wù)器

同樣也為從服務(wù)器創(chuàng)建一個(gè)文件夾,例如/home/mysql2充活,用來存放從服務(wù)器的配置文件和數(shù)據(jù)文件蜂莉。

在mysql2下創(chuàng)建conf/my.cnf,文件內(nèi)容如下:

# 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

# 從數(shù)據(jù)庫(kù)端ID號(hào)
server_id = 2
#開啟二進(jìn)制日志
log-bin = mysql-bin
# 需要復(fù)制的數(shù)據(jù)庫(kù)名混卵,如果復(fù)制多個(gè)數(shù)據(jù)庫(kù)映穗,重復(fù)設(shè)置這個(gè)選項(xiàng)即可
# binlog-do-db = db
# 復(fù)制時(shí)需要忽略的數(shù)據(jù)庫(kù)
binlog-ignore-db = mysql
# 為每個(gè)session分配的內(nèi)存
binlog_cache_size = 1M
# 主從復(fù)制的格式(mixed、statement幕随、row男公,默認(rèn)是statement)
binlog_format = mixed
# 將從服務(wù)器從主服務(wù)器收到的更新記入到從服務(wù)器自己的二進(jìn)制日志文件中
log_slave_updates = 1
# 這個(gè)參數(shù)一般用在主主同步中,用來錯(cuò)開自增值, 防止鍵值沖突
# auto_increment_offset = 1
# 這個(gè)參數(shù)一般用在主主同步中合陵,用來錯(cuò)開自增值, 防止鍵值沖突
# auto_increment_increment = 1
# 二進(jìn)制日志自動(dòng)刪除的天數(shù)枢赔,默認(rèn)值為0,表示“沒有自動(dòng)刪除”,啟動(dòng)時(shí)和二進(jìn)制日志循環(huán)時(shí)可能刪除
expire_logs_days = 7
#將函數(shù)復(fù)制到slave
log_bin_trust_function_creators = 1
# relay_log 配置中繼日志
relay_log = mysql-relay-bin
# 防止改變數(shù)據(jù)
read_only = 1
2.2.2 啟動(dòng)從服務(wù)器

使用docker run來啟動(dòng)一個(gè)mysql實(shí)例作為從服務(wù)器拥知。

# docker run --name mysql2 -v /home/mysql2/conf/my.cnf:/etc/mysql/my.cnf -v /home/mysql2/data:/var/lib/mysql -p 3308:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci
ceb421c503a6533ead15708a92534c47f544410a32dd5d0f1d8bdf036c9e215d
2.2.3 為從服務(wù)器指定主服務(wù)器
mysql> change master to master_host='172.17.0.2', master_user='slave', master_password='123456', master_port=3306, master_log_file='mysql-bin.000002', master_log_pos=617, master_connect_retry=30;
Query OK, 0 rows affected, 2 warnings (0.03 sec)

說明:

  • master_host: 主服務(wù)器ip踏拜,對(duì)應(yīng)上面2.1.5中獲取到的ip
  • master_user: 復(fù)制用戶,對(duì)應(yīng)上面2.1.3中創(chuàng)建的用戶
  • master_password: 復(fù)制用戶密碼低剔,對(duì)應(yīng)上面2.1.3中的創(chuàng)建的用戶密碼
  • master_log_file: 日志文件名速梗,對(duì)應(yīng)上面2.1.4中的File
  • master_log_pos: 日志文件位置,對(duì)應(yīng)上面2.1.4中的Position
2.2.4 查看從服務(wù)器狀態(tài)
mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State:
                  Master_Host: 172.17.0.2
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 30
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 617
               Relay_Log_File: mysql-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: No
            Slave_SQL_Running: No

              // 省略

1 row in set (0.00 sec)

ERROR:
No query specified

Slave_IO_Running和Slave_SQL_Running都是NO襟齿,表明slave還沒開始復(fù)制過程

2.2.5 開啟主從同步
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.17.0.2
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 30
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 617
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              
              // 省略

1 row in set (0.00 sec)

到這里主從配置就已經(jīng)好了姻锁,接下來就可以進(jìn)行測(cè)試了。先在主服務(wù)器中進(jìn)行操作猜欺,然后看從服務(wù)器有沒有自動(dòng)同步主服務(wù)器的數(shù)據(jù)位隶。

注: 對(duì)于同一個(gè)mysql實(shí)例,先使用docker stop再使用docker start开皿,即時(shí)沒加-v參數(shù)指定數(shù)據(jù)文件涧黄,之前的數(shù)據(jù)也不會(huì)丟失。如果啟動(dòng)了一個(gè)新實(shí)例赋荆,同時(shí)還想用已有的實(shí)例的數(shù)據(jù)笋妥,則可以先使用docker cp mysql:/var/lib/mysql /home/tmp/mysql/data將已有實(shí)例的數(shù)據(jù)拷貝出來。然后在啟動(dòng)新實(shí)例時(shí)使用-v參數(shù)指定該數(shù)據(jù)文件窄潭。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末春宣,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌月帝,老刑警劉巖躏惋,帶你破解...
    沈念sama閱讀 218,284評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異嫁赏,居然都是意外死亡其掂,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門潦蝇,熙熙樓的掌柜王于貴愁眉苦臉地迎上來款熬,“玉大人,你說我怎么就攤上這事攘乒∠团#” “怎么了?”我有些...
    開封第一講書人閱讀 164,614評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵则酝,是天一觀的道長(zhǎng)殉簸。 經(jīng)常有香客問我,道長(zhǎng)沽讹,這世上最難降的妖魔是什么般卑? 我笑而不...
    開封第一講書人閱讀 58,671評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮爽雄,結(jié)果婚禮上蝠检,老公的妹妹穿的比我還像新娘。我一直安慰自己挚瘟,他們只是感情好叹谁,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,699評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著乘盖,像睡著了一般焰檩。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上订框,一...
    開封第一講書人閱讀 51,562評(píng)論 1 305
  • 那天析苫,我揣著相機(jī)與錄音,去河邊找鬼布蔗。 笑死藤违,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的纵揍。 我是一名探鬼主播,決...
    沈念sama閱讀 40,309評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼议街,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼泽谨!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,223評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤吧雹,失蹤者是張志新(化名)和其女友劉穎骨杂,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體雄卷,經(jīng)...
    沈念sama閱讀 45,668評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡搓蚪,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,859評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了丁鹉。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片妒潭。...
    茶點(diǎn)故事閱讀 39,981評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖揣钦,靈堂內(nèi)的尸體忽然破棺而出雳灾,到底是詐尸還是另有隱情,我是刑警寧澤冯凹,帶...
    沈念sama閱讀 35,705評(píng)論 5 347
  • 正文 年R本政府宣布谎亩,位于F島的核電站,受9級(jí)特大地震影響宇姚,放射性物質(zhì)發(fā)生泄漏匈庭。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,310評(píng)論 3 330
  • 文/蒙蒙 一浑劳、第九天 我趴在偏房一處隱蔽的房頂上張望阱持。 院中可真熱鬧,春花似錦呀洲、人聲如沸紊选。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,904評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽兵罢。三九已至,卻和暖如春滓窍,著一層夾襖步出監(jiān)牢的瞬間卖词,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,023評(píng)論 1 270
  • 我被黑心中介騙來泰國(guó)打工吏夯, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留此蜈,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,146評(píng)論 3 370
  • 正文 我出身青樓噪生,卻偏偏與公主長(zhǎng)得像裆赵,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子跺嗽,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,933評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容