- 作者:missjin
- 交流WX:hcj6932600
預(yù)期目標(biāo)圖
實(shí)現(xiàn)后的效果圖
****【master-mysql 創(chuàng)建了一個數(shù)據(jù)庫】****
****【slaver-mysql 的數(shù)據(jù)庫】****
【1】docker服務(wù)啟動
systemctl restart docker
# 驗證啟動
docker version
# 拉取指定鏡像
docker pull mysql:5.7.24
【2】先建立好掛載目錄的配置文件
# 建主數(shù)據(jù)的配置文件和數(shù)據(jù)存放目錄
mkdir -p /data/mysql/master-mysql/data
touch /data/mysql/master-mysql/mysql.cnf
# 建從數(shù)據(jù)的配置文件和數(shù)據(jù)存放目錄
mkdir -p /data/mysql/slaver-mysql/data
touch /data/mysql/slaver-mysql/mysql.cnf
# ----拷貝原始的配置文件 my.cnf--------------------
# Copyright (c) 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
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
#vi /etc/mysql/my.cnf
[mysqld]
# [必須]啟用二進(jìn)制日志
log-bin=mysql-bin
# [必須]服務(wù)器唯一ID master=140;slaver=130
server-id=140
【3】創(chuàng)建容器
# 創(chuàng)建master-mysql
docker run --name master-mysql -v /data/mysql/master-mysql/mysql.cnf:/etc/mysql/mysql.cnf -v /data/mysql/master-mysql/data:/var/lib/mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=missjin -d mysql:5.7.24 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
# 創(chuàng)建slaver-mysql
docker run --name slaver-mysql -v /data/mysql/slaver-mysql/mysql.cnf:/etc/mysql/mysql.cnf -v /data/mysql/slaver-mysql/data:/var/lib/mysql -p 3307:3306 -e MYSQL_ROOT_PASSWORD=missjin -d mysql:5.7.24 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
# 查看啟動狀態(tài)
docker ps
# 查看容器內(nèi)網(wǎng)分配的IP
docker inspect master-mysql | grep IP #172.17.0.2
docker inspect slaver-mysql | grep IP #172.17.0.3
【4】進(jìn)入容器master-mysql
# 進(jìn)入master-mysql
docker exec -it master-mysql /bin/bash
【5】Master配置[docker里面掛載目錄雏婶,改配置]
#vi /etc/mysql/my.cnf
[mysqld]
# [必須]啟用二進(jìn)制日志
log-bin=mysql-bin
# [必須]服務(wù)器唯一ID绞旅,默認(rèn)是1谓谦,一般取IP最后一段
server-id=140
# 登入服務(wù)
mysql -uroot -pmissjin
# 授權(quán)給slaver-mysql訪問的權(quán)限
mysql>grant replication slave on *.* to 'reader'@'%' identified by '123456';
# 一般不用root帳號,“%”表示所有客戶端都可能連雹仿,只要帳號锭弊,密碼正確填硕,此處可用具體客戶端IP代替债沮,如172.17.0.2重虑,加強(qiáng)安全。
# 記錄master數(shù)據(jù)的狀態(tài)秦士,一配置從數(shù)據(jù)庫
show master status;
【6】進(jìn)入slaver-mysql,配置從數(shù)據(jù)庫
# 進(jìn)入slaver-mysql
docker exec -it slaver-mysql /bin/bash
# 進(jìn)入數(shù)據(jù)庫
mysql -uroot -p
# 改變同步
change master to master_host='172.17.0.2',master_user='reader',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=439;
# 啟用從數(shù)據(jù)庫
start slave;
【7】驗證效果
****【master-mysql 創(chuàng)建了一個數(shù)據(jù)庫】****
****【slaver-mysql 的數(shù)據(jù)庫】****