您的位置:首页 > 运维架构 > Docker

centos7下docker-mysql5.7实现主从复制(精简教程)

weixin_39592623 2019-06-12 07:31 453 查看 https://blog.csdn.net/weixin_3

原创文章:转载请注明文章出处

主机 名称
192.168.2.675 master
192.168.2.62 slave

 

准备两台主机:master和slave

1.master:拉去mysql:5.7镜像

[code]docker pull mysql:5.7

2.master:创建挂载文件和配置文件

[code]mkdir /home/mastermysql
mkdir -p /home/mastermysql/conf
vim my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip-name-resolve=1
slow_query_log =1
slow_query_log_file=/tmp/mysql_slow.log
symbolic-links=0
pid-file=/var/run/mysqld/mysqld.pid
lower_case_table_names=1
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
max_connections=100000
server_id=101
log-bin=mysql-bin
:wq

3.master:启动容器

[code]docker run -d -p 3399:3306 --privileged=true -v /home/mastermysql/conf/my.cnf:/etc/mysql/my.cnf -v /home/mastermysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mastermysql ba7a93aae2a8

3399映射到3306端口

--privileged=true 一定要加上 不然会报错提示没有权限

-v /home/mastermysql/conf/my.cnf:/etc/mysql/my.cnf 挂载配置文件目录

-v /home/mastermysql/data:/var/lib/mysql 挂载mysql数据目录

-e MYSQL_ROOT_PASSWORD=123456  初始化默认密码为123456

--name mastermysql  重命名为:mastermysql

[code][root@localhost ~]# docker ps | grep mastermysql
2b286ac61cd0        ba7a93aae2a8        "docker-entrypoint..."   2 hours ago         Up 2 hours          33060/tcp, 0.0.0.0:3399->3306/tcp   mastermysql
[root@localhost ~]#
[code][root@localhost conf]# docker run -d -p 3399:3306 --privileged=true -v /home/mastermysql/conf/my.cnf:/etc/mysql/my.cnf -v /home/mastermysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mastermysql ba7a93aae2a8
2b286ac61cd09a4eea8ade4ae976c36df8e1a6b66bdcd33373e6955e73980a28

 以上可以看到服务已经起来了

4.master:创建主从复制用户

[code][root@localhost ~]# mysql -uroot -p -h192.168.2.75 -P3399
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.24-log MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> CREATE USER 'slave'@'%' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.06 sec)

mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';
Query OK, 0 rows affected (0.02 sec)

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      609 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

mysql>

创建用户 

[code]CREATE USER 'slave'@'%' IDENTIFIED BY '123456';

 授权

[code]GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';

show master status; 

[code]mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      609 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

 

5.slave:拉去mysql:5.7镜像

[code]docker pull mysql:5.7

6.slave:创建挂载文件和配置文件

[code]mkdir /home/slavemysql
mkdir -p /home/slavemysql/conf
vim my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
skip-name-resolve=1
slow_query_log =1
slow_query_log_file=/tmp/mysql_slow.log
symbolic-links=0
pid-file=/var/run/mysqld/mysqld.pid
lower_case_table_names=1
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
max_connections=100000
server_id=101
log-bin=mysql-bin
:wq

7.slave:启动容器

[code]docker run -d -p 3399:3306 --privileged=true -v /home/slavemysql/conf/my.cnf:/etc/mysql/my.cnf -v /home/slavemysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name slavemysql ba7a93aae2a8

8.slave进入docker-mysql

[code]mysql> change master to master_host='192.168.2.75', master_user='slave', master_password='123456', master_port=3399, master_log_file='mysql-bin.000001', master_log_pos=609, master_connect_retry=30;
Query OK, 0 rows affected, 2 warnings (0.24 sec)
[code]mysql> start slave;
Query OK, 0 rows affected (0.02 sec)

 

[code]mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.2.75
Master_User: slave
Master_Port: 3399
Connect_Retry: 30
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 609
Relay_Log_File: 5e461c327f17-relay-bin.000002
Relay_Log_Pos: 320
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: 609
Relay_Log_Space: 534
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: 100
Master_UUID: 31f3bd0a-4541-11e9-90c8-0242ac110005
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_IO_Running: Yes 和 Slave_SQL_Running: Yes  表明配置成功。

在master创建数据库测试

[code]mysql> create database wori;
Query OK, 1 row affected (0.05 sec)

在slave查询数据库

[code]mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| wori               |
+--------------------+

至此docker-mysql主从复制已经部署成功了

扫码关注微信公众号:天天程序员,每天分享程序员故事。更多资源下载尽在天天程序员

 

标签: