1.硬件环境
ubuntu-16.04
docker-17.09
mysql-5.7.25
2.拉取MySQL的docker镜像
在每台服务器上执行以下命令,拉取MySQL镜像
root@server05:/home/topcom# docker pull mysql:5.7.25
5.7.25: Pulling from library/mysql
6ae821421a7d: Pull complete
a9e976e3aa6d: Pull complete
e3735e44a020: Pull complete
bfd564e9483f: Pull complete
df705f26e488: Pull complete
0c5547f73d62: Pull complete
f437382cf8a1: Pull complete
4f22805bb6d6: Pull complete
394f0f652697: Pull complete
fb068b9b9d1f: Pull complete
fdfc96ad0937: Pull complete
Digest: sha256:8c15b2612051244d0a2b6ceb6f9bf82ddc0e909555c1067c098e5f935e2751a7
Status: Downloaded newer image for mysql:5.7.25
3.启动MySQL容器
master:
root@server04:/home/topcom# docker run --name MysqlMaster \
--volume /etc/localtime:/etc/localtime \
--restart=always \
-p 20139:3306 \
-h MysqlMaster \
-v mysql_data_MysqlMaster:/var/lib/mysql \
-v mysql_conf_MysqlMaster:/etc/mysql/ \
-e MYSQL_ROOT_PASSWORD=topcom123 -d mysql:5.7.25
dd3241aaab6ddda29ceb455c4b49f2fff0e7c05a3739416935fd0f2b6ef4f7db
slave:
root@server05:/home/topcom# docker run --name MysqlSlave \
--volume /etc/localtime:/etc/localtime \
--restart=always \
-p 20139:3306 \
-h MysqlServer \
-v mysql_data_MysqlSlave:/var/lib/mysql \
-v mysql_conf_MysqlSlave:/etc/mysql/ \
-e MYSQL_ROOT_PASSWORD=topcom123 -d mysql:5.7.25
e231073ad24a7de4ff0925a1604f91923998827bf56e39e7be1a32c101184775
4.配置master节点
- 在master上的my.cnf配置:
root@server04:/home/topcom# vim /data/docker/volumes/mysql_conf_MysqlMaster/_data/mysql.conf.d/mysqld.cnf
# Copyright (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 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
#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
#开启MySQL主主复制
server-id = 1
log-bin = mysql-bin
binlog-ignore-db = mysql,information_schema
sync_binlog = 1
binlog_checksum = none
binlog_format = mixed
auto-increment-increment = 2
auto-increment-offset = 1
slave-skip-errors = all
- 退出保存,重启MySQL服务
root@server04:/home/topcom# docker restart MysqlMaster
MysqlMaster
root@server04:/home/topcom#
- 数据同步授权
防火墙开启20139端口,要确保对方机器能使用下面权限连接到本机mysql
root@server04:/home/topcom# docker exec -it MysqlMaster bash
root@MysqlMaster:/# mysql -uroot -ptopcom123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.25-log MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> GRANT REPLICATION SLAVE,reload,super ON *.* to 'topcom'@'%' identified by 'topcom123';
FLUSH PRIVILEGES;Query OK, 0 rows affected, 1 warning (0.08 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)
mysql>
最好将库锁住,仅仅允许读,以保证数据一致性;待主主同步环境部署后再解锁;锁住后,就不能往表里写数据,但是重启mysql服务后就会自动解锁!
mysql>FLUSH TABLES WITH READ LOCK; //注意该参数设置后,如果自己同步对方数据,同步前一定要记得先解锁!
Query OK, 0 rows affected (0.01 sec)
mysql>show master status;
+------------------+----------+--------------+--------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------+-------------------+
| mysql-bin.000001 | 596 | | mysql,information_schema | |
+------------------+----------+--------------+--------------------------+-------------------+
1 row in set (0.00 sec)
5.配置slave节点
- 在slave上的my.cnf配置:
root@server05:/home/topcom# vim /data/docker/volumes/mysql_conf_MysqlSlave/_data/mysql.conf.d/mysqld.cnf
# Copyright (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 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
#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
#开启slave主主复制
server-id = 2
log-bin = mysql-bin
binlog-ignore-db = mysql,information_schema
sync_binlog = 1
binlog_checksum = none
binlog_format = mixed
auto-increment-increment = 2
auto-increment-offset = 2
slave-skip-errors = all
- 退出保存,重启MySQL服务
root@server05:/home/topcom# docker restart MysqlSlave
MysqlSlave
root@server05:/home/topcom#
- 数据同步授权
防火墙开启20139端口,要确保对方机器能使用下面权限连接到本机mysql,
同理,slave也要授权给master机器远程同步数据的权限
root@server05:/home/topcom# docker exec -it MysqlSlave bash
root@MysqlServer:/# mysql -uroot -ptopcom123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.25-log MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> GRANT REPLICATION SLAVE,reload,super ON *.* to 'topcom'@'%' identified by 'topcom123';
Query OK, 0 rows affected, 1 warning (0.46 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.02 sec)
mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+--------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------+-------------------+
| mysql-bin.000001 | 596 | | mysql,information_schema | |
+------------------+----------+--------------+--------------------------+-------------------+
1 row in set (0.00 sec)
5.执行主从同步操作
先在slave数据库上做同步master的设置。(确保slave上要同步的数据,提前在master上存在。最好双方数据保持一致)
- 在slave服务器上执行以下操作
root@server05:/home/topcom# docker exec -it MysqlSlave bash
root@MysqlServer:/# mysql -uroot -ptopcom123
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> change master to master_host='192.168.1.14',master_user='topcom',master_password='topcom123',master_log_file='mysql-bin.000001',master_log_pos=0,master_port=20139;
Query OK, 0 rows affected, 2 warnings (0.31 sec)
mysql> start slave;
Query OK, 0 rows affected (0.09 sec)
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.14
Master_User: topcom
Master_Port: 20139
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 596
Relay_Log_File: MysqlServer-relay-bin.000002
Relay_Log_Pos: 801
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes #这个显示成这样是正确的
Slave_SQL_Running: Yes #这个显示成这样是正确的
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 596
Relay_Log_Space: 1006
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: d48b511b-340f-11e9-bf4b-0242ac110008
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
这样就实现了slave->master的同步环境。
- 在master服务器上执行以下操作
再在master数据库上做同步slave的设置。(确保slave上要同步的数据,提前在master上存在。最好双方数据保持一致)
mysql -uroot -ptopcom123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.25-log MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> change master to master_host='192.168.1.15',master_user='topcom',master_password='topcom123',master_log_file='mysql-bin.000001',master_log_pos=0,master_port=20139;
Query OK, 0 rows affected, 2 warnings (0.10 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.15
Master_User: topcom
Master_Port: 20139
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 596
Relay_Log_File: MysqlMaster-relay-bin.000002
Relay_Log_Pos: 801
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes #这个显示成这样是正确的
Slave_SQL_Running: Yes #这个显示成这样是正确的
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 596
Relay_Log_Space: 1006
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 2
Master_UUID: e2ae948b-3410-11e9-93fc-0242ac110007
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
这样就实现了master->slave的同步环境。至此,主主双向同步环境已经实现!