数据库MySQL

docker部署mysql读写分离

2021-03-08  本文已影响0人  楚长铭

准备

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 
# 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容器

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
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

读写分离

#my.cnf文件修改
[mysqld]
server-id       = 1

配置主从

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='主节点ip',
master_port=主节点端口,
master_user='主节点用户名',
master_password='主节点用户名密码',
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

参考文档

https://www.jianshu.com/p/5fc9145393eb

上一篇下一篇

猜你喜欢

热点阅读