1. 程式人生 > 實用技巧 >docker搭建主從複製mysql

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; 然後再重複上面的操作就可以了