準備
- docker下載鏡像使用mysql8(都2021了還有那么多mysql5教程我也是醉了)
docker pull mysql:8
獲得my.cnf文件
docker run --privileged \
--restart=unless-stopped \
--name mysql8-master \
-p 13306:3306 \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql:8
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/
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
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
讀寫分離
- 修改兩個節(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;
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;
show slave status\G
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
參考文檔
http://www.lxweimin.com/p/5fc9145393eb