docker部署mysql讀寫分離

準備

  • docker下載鏡像使用mysql8(都2021了還有那么多mysql5教程我也是醉了)
docker pull mysql:8

獲得my.cnf文件

  • 先隨便跑個mysql容器
docker run --privileged  \
--restart=unless-stopped \
--name mysql8-master     \
-p 13306:3306            \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql:8
  • 進入docker容器
docker exec -it mysql8-master /bin/bash 
  • 在/etc/mysql/my.cnf獲得my.cnf文件
# 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

#
# 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
secure-file-priv= NULL

# Custom config should go here
!includedir /etc/mysql/conf.d/

  • 刪除mysql容器
docker stop mysql8-master
docker rm mysql8-master

運行mysql容器

  • 運行master節(jié)點容器,啟動應該會失敗;但是沒有關系!在/data/dockerMount/mysql/master/conf/my.cnf的路徑下my.cnf是個文件夾,將其刪除之后將上面得到的my.cnf文件放入即可
docker run --privileged  \
--restart=unless-stopped \
--name mysql8-master     \
-p 13306:3306            \
-v /data/dockerMount/mysql/master/conf/my.cnf:/etc/mysql/my.cnf \
-v /data/dockerMount/mysql/master/logs:/var/log/mysql \
-v /data/dockerMount/mysql/master/data:/var/lib/mysql  \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql:8
  • 重啟master節(jié)點
docker restart mysql8-master
  • 運行slave節(jié)點,和master節(jié)點一樣的方法運行
docker run --privileged  \
--restart=unless-stopped \
--name mysql8-slave1     \
-p 13307:3306            \
-v /data/dockerMount/mysql/slave1/conf/my.cnf:/etc/mysql/my.cnf \
-v /data/dockerMount/mysql/slave1/logs:/var/log/mysql \
-v /data/dockerMount/mysql/slave1/data:/var/lib/mysql  \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql:8
  • 到此兩個mysql容器運行起來了

讀寫分離

  • 修改兩個節(jié)點的my.cnf文件,server-id不同即可
#my.cnf文件修改
[mysqld]
server-id       = 1

配置主從

  • 進入master節(jié)點查看file和position的值然后記下來
docker exec -it mysql8-master /bin/bash 
mysql -u root -p123456
show master status;
 File          | Position | 
+---------------+----------+
| binlog.000001 |      911 
  • 配置用與同步的用戶
alter user '用戶名'@'%' identified by '密碼' password expire never;

 alter user '用戶名'@'%' identified with mysql_native_password by '密碼';

flush privileges;
  • 進入slave節(jié)點
docker exec -it mysql8-slave1 /bin/bash 
mysql -u root -p123456


change master to
master_host='主節(jié)點ip',
master_port=主節(jié)點端口,
master_user='主節(jié)點用戶名',
master_password='主節(jié)點用戶名密碼',
master_log_file='binlog.000001',
master_log_pos=911;

 start slave;
  • 如果配置錯誤,重新配置即可
stop slave;
  • 查看狀態(tài)
show slave status\G
  • 看到以下兩個yes即可
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

參考文檔

http://www.lxweimin.com/p/5fc9145393eb

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容