docker搭建主从复制mysql

步骤:

---启动两个master和slave容器

---修改配置master和slave的my.cnf

---master添加从服务器要要复制的用户并授权

---slave添加主服务器相关信息,并开启slave

1.启动两个容器

docker run -d --name mysql_master -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root mysql

docker run -d --name mysql_slave -p 3307:3306 -e MYSQL_ROOT_PASSWORD=root mysql

 2.修改两个配置文件

master

# 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
log-bin=mysql-bin
server_id=1
# Custom config should go here
!includedir /etc/mysql/conf.d/

 slave

# 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
server_id=233
relay_log=mysql-relay-bin 
# Custom config should go here
!includedir /etc/mysql/conf.d/

 3.添加授权用户,记录master状态

create user slave'@'%' identified by '123456';

grant replication slave on *.* to 'slave'@'%';

mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000002 |     1249 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

4.添加主服务器信息,开启slave模式

change master to master_host='172.17.0.2', master_user='slave', master_password='123456', master_port=3306, master_log_file='binlog.000002', master_log_pos=1249, master_connect_retry=30;  

start slave;

主服务器创建数据库验证

报错

1.可能会有下面的报错,解决方法:https://blog.csdn.net/sunbocong/article/details/81634296

The slave I/O thread stops because master and slave have equal MySQL server UUIDs

2.以前有过slave复制会开启失败,解决方法如下:

 
stop slave;
 
reset slave;
 
然后再重复上面的操作就可以了
 
参考文章:

https://juejin.im/post/5b0f91ab518825153749b1db

https://www.jianshu.com/p/ab20e835a73f

原文地址:https://www.cnblogs.com/soymilk2019/p/13409170.html