基于docker mysql5.7主从
上篇文章写了docker安装mysql5.7
1.启动容器
由于上篇文章已经启动了一个(就让那个当主吧),所以这里我只起了一个就slave够了(记得提前准备/usr/local/mysql5.7a/conf/mysqld.cnf文件哦)
#主库mysqld.cnf文件配置
#opyright (c) 2014, 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, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation. The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# 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, version 2.0, 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]
## 同一局域网内注意要唯一
server-id=101
## 开启二进制日志功能,可以随便取(关键)
log-bin=mysql-bin
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
#log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
从库mysqld.cnf
#从库mysqld.cnf
#pyright (c) 2014, 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, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation. The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# 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, version 2.0, 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]
## 同一局域网内注意要唯一
server-id=100
## 开启二进制日志功能,可以随便取(关键)
log-bin=mysql-slave-bin
## relay_log配置中继日志
relay_log=edu-mysql-relay-bin
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
#log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
sudo docker run --name mysql_slave --restart always --privileged=true -p 3306:3306 -v /usr/local/mysql5.7a/conf/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf -v /usr/local/mysql5.7a/data:/var/lib/mysql -e MYSQL_USER="root" -e MYSQL_PASSWORD="root" -e MYSQL_ROOT_PASSWORD="root" -d mysql:5.7
前面Master对外映射的端口是3306,这里我们Slave对外映射的端口是3307。
2.进入主库容器,主库执行授权 :
grant replication slave on *.* to slave@'%' identified by "root";
show master status \G;
若反斜杠显示为W则调整xshell字体为courier new
顺便查下主库容器ip
docker inspect --format='{{.NetworkSettings.IPAddress}}' mysql_master;
记住主库的 file与position 从库配置使用
3.docker进入从库容器
修改主库指向
CHANGE MASTER TO master_host = '172.17.0.2',
master_user = 'slave',
master_password = 'root',
master_port = 3306,
master_log_file = 'mysql-bin.000003',
master_log_pos = 318,
master_connect_retry = 30;
查看从库状态
show slave status \G;
看到数据一致,在观察发现Slave_IO_Running与Slave_SQL_Running为no 是因为我们没有开启主从复制
start slave;
然后再查状态会发现已经为yes了 ,如果Slave_IO_Running 或Slave_SQL_Running不为yes则根据错误日志解决下,相关教程也能搜到
下面开始测试,在主库新建一个库
CREATE DATABASE test1;
然后刷新从库会发现表已经存在了
至此大功告成。
ps:目前这种主从复制原理的文章非常多,建议学习下。