1、在这里我们不再进行mysql镜像的构建了,直接从如下地址把镜像拉下来:

docker pull registry.cn-hangzhou.aliyuncs.com/huanzhidadi/mysql:5.7.24

2、在服务器上创建三个文件夹

/etc/mysql/master

/etc/mysql/s1

/etc/mysql/s2

3、这里我提供一个基础的my.cnf文件内容,大家把它放在上面三个文件夹中

# Copyright (c) 2015, 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 Community Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[client]
port		= 3306
socket		= /var/run/mysqld/mysqld.sock

[mysqld_safe]
pid-file	= /var/run/mysqld/mysqld.pid
socket		= /var/run/mysqld/mysqld.sock
nice		= 0

[mysqld]
user		= mysql
pid-file	= /var/run/mysqld/mysqld.pid
socket		= /var/run/mysqld/mysqld.sock
port		= 3306
basedir		= /usr
datadir		= /var/lib/mysql
tmpdir		= /tmp
lc-messages-dir	= /usr/share/mysql
explicit_defaults_for_timestamp

table_open_cache=256
innodb_buffer_pool_size = 64M
join_buffer_size = 8M
read_buffer_size = 2M
sort_buffer_size = 2M
read_rnd_buffer_size = 2M
query_cache_size = 8M
query_cache_limit = 2M
key_buffer_size = 4M
default-time_zone = '+8:00'
lower_case_table_names=1
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address	= 127.0.0.1

#log-error	= /var/log/mysql/error.log

# Recommended in standard MySQL setup
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#
!includedir /etc/mysql/conf.d/

4、修改三个my.cnf文件内容。

vi master/my.cnf      //编辑master数据库配置文件
在 [mysqld]模块下面添加以下几行
server_id = 11      #主服务器编号
log_bin=master_bin  #指定二进制日志
log_slave_updates=true      #允许从服务器更新
vi s1/my.cnf      //编辑从1数据库配置文件
在 [mysqld]模块下面添加以下几行
server_id = 22      #从服务器编号
relay_log=relay-log-bin #指定从服务器的中继日志
relay_log_index=slave-relay-bin.index   #定义中级日志的位置和名称
vi s2/my.cnf      //编辑从2数据库配置文件
在 [mysqld]模块下面添加以下几行
server_id = 22      #从服务器编号
relay_log=relay-log-bin #指定从服务器的中继日志
relay_log_index=slave-relay-bin.index   #定义中级日志的位置和名称

编辑完各个文件后要使用:wq命令保存关闭。

5、分别启动一主两从,共3个mysql容器:

docker run --name mysql57-M -v /usr/local/mysqldata/master:/var/lib/mysql -v /etc/mysql/master/my.cnf:/etc/mysql/my.cnf -e MYSQL_ROOT_PASSWORD=root -d -p 4001:3306 registry.cn-hangzhou.aliyuncs.com/huanzhidadi/mysql:5.7.24  --lower_case_table_names=1

docker run --name mysql57-S1 -v /usr/local/mysqldata/s1:/var/lib/mysql -v /etc/mysql/s1/my.cnf:/etc/mysql/my.cnf -e MYSQL_ROOT_PASSWORD=root -d -p 4002:3306 registry.cn-hangzhou.aliyuncs.com/huanzhidadi/mysql:5.7.24  --lower_case_table_names=1

docker run --name mysql57-S2 -v /usr/local/mysqldata/s2:/var/lib/mysql -v /etc/mysql/s2/my.cnf:/etc/mysql/my.cnf -e MYSQL_ROOT_PASSWORD=root -d -p 4003:3306 registry.cn-hangzhou.aliyuncs.com/huanzhidadi/mysql:5.7.24  --lower_case_table_names=1

6、输入docker ps查看是否启动成功

6、进入master数据库授权

使用docker exec -it mysql57-M bash进入master数据库所在容器。
mysql -u root -proot      //进入数据库
GRANT REPLICATION SLAVE ON *.* TO 'myslave'@'%' IDENTIFIED BY 'abc123'; //为所有从服务器授权所有数据库
FLUSH PRIVILEGES;       //刷新权限
show master status; //要记下 Position 列的值 (我的是592)

7、查看master服务器所在容器的IP地址

使用宿主机ip和对应的端口号

docker inspect --format='{{.NetworkSettings.IPAddress}}' mysql57-M

8、分别进入两个从数据库授权

使用docker exec -it mysql57-S1 bash进入master数据库所在容器。
mysql -uroot -proot   //进入数据库
change master to master_host='192.168.1.108', master_port=4001, master_user='myslave',master_password='abc123',master_log_file='master_bin.000001',master_log_pos=592;  //指定主服务器

start slave;    //开启从服务器

9、在从数据库所在容器的mysql中查看从数据库状态, 这两个状态都显示为Yes,说明从数据库配置成功

show slave status \G

10、在主数据库中新建数据库,查看从数据库中是否同步新建成功。

 

 

最后修改于 2019-11-14 21:29:27
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付
上一篇