利用周末两天进一步了解了一下docker,发现docker确实是一个好东西,能够最大化的利用有限的计算机资源。以前如果需要弄mysql的主从复制,必须在两台机器上安装mysql,然后进行配置,非常繁琐,而docker恰好解决了这个问题,在同一台机器上也能够安装多个mysql,由于docker的沙箱机制,多个容器之间相互隔离,这保证了数据的安全性。好了,不多说了,上正题,介绍这篇文章的前提是你已经学会了基本的docker安装使用,如果没玩过docker,可以看一下我之前写的一篇基础文章(http://blog.csdn.net/llll13797483659/article/details/73554923)。
1.在系统上安装mysql-server和mysql-client(都有用)
我用的是ubantu,命令如下:
root@ubuntu:/home/kevin# apt-get install mysql-server
root@ubuntu:/home/kevin# apt-get isntall mysql-client
2.拉取mysql的镜像,本人是从网易蜂巢拉取的,淘宝也行,官网网速太慢,400M估计得几个小时
命令如下:
root@ubuntu:/home/kevin# docker pull hub.c.163.com/library/mysql:5.7
可以查看一下是否拉取成功,查看docker镜像的命令是docker iamges
root@ubuntu:/home/kevin# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hub.c.163.com/library/mysql 5.7 9e64176cd8a2 11 weeks ago 407.1 MB
hub.c.163.com/library/mysql latest 9e64176cd8a2 11 weeks ago 407.1 MB
root@ubuntu:/home/kevin#
如果列表中有tag为5.7的hub.c.163.com/library/mysql镜像则拉取成功。
3.建几个目录使数据保存在宿主机上
root@ubuntu:/home/kevin# mkdir -pv /home/docker/mysql/data
root@ubuntu:/home/kevin# mkdir -pv /home/docker/mysql/101
root@ubuntu:/home/kevin# mkdir -pv /home/docker/mysql/102
4.配置服务器配置
4.1主服务器
root@ubuntu:/home/kevin# vim /home/docker/mysql/101/101.cnf
进入后配置:如下
[mysqld]
log-bin=mysql-bin
server-id=101
4.2从服务器
root@ubuntu:/home/kevin# vim /home/docker/mysql/102/102.cnf
进入后配置:如下
[mysqld]
log-bin=mysql-bin
server-id=102
5.比较重要的一步,创建主从服务器容器
主:
root@ubuntu:/home/kevin# docker create --name mysqlsrv101 -v /home/docker/mysql/data/mysql101:/var/lib/mysql -v /home/docker/mysql/101:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 -p 3307:3306 hub.c.163.com/library/mysql
从:
root@ubuntu:/home/kevin# docker create --name mysqlsrv102 -v /home/docker/mysql/data/mysql102:/var/lib/mysql -v /home/docker/mysql/102:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 -p 3317:3306 hub.c.163.com/library/mysql
查看一下容器是否创建成功(docker ps -a),记住一定要加-a查询所有,应为连个容器还没启动
root@ubuntu:/home/kevin# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2d7d75332e17 hub.c.163.com/library/mysql "docker-entrypoint.sh" About a minute ago Created mysqlsrv102
76bae634fad3 hub.c.163.com/library/mysql "docker-entrypoint.sh" 2 minutes ago Created mysqlsrv101
root@ubuntu:/home/kevin#
容器创建成功,可以启动了
root@ubuntu:/home/kevin# docker start mysqlsrv101
mysqlsrv101
root@ubuntu:/home/kevin# docker start mysqlsrv102
mysqlsrv102
查看容器是否启动,此时可以用(docker ps查看已经启动的容器)
root@ubuntu:/home/kevin# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2d7d75332e17 hub.c.163.com/library/mysql "docker-entrypoint.sh" 4 minutes ago Up About a minute 0.0.0.0:3317->3306/tcp mysqlsrv102
76bae634fad3 hub.c.163.com/library/mysql "docker-entrypoint.sh" 6 minutes ago Up About a minute 0.0.0.0:3307->3306/tcp mysqlsrv101
端口映射成功,容器已经启动
6.登录到mysql的master容器
root@ubuntu:/home/kevin# docker exec -it mysqlsrv101 bash
root@76bae634fad3:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.18-log MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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>
登录成功!接下来主库创建备份用户
mysql> SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
Query OK, 0 rows affected (0.01 sec)
mysql> GRANT REPLICATION SLAVE ON *.* to 'backup'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>
登录从服务器的mysql,设置与主服务器相关的配置参数
root@ubuntu:/home/kevin# docker exec -it mysqlsrv102 bash
root@2d7d75332e17:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.18-log MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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.
先查看一下master的状态
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000005 | 439 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)
在从库上配置参数
mysql> SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
Query OK, 0 rows affected (0.00 sec)
mysql> change master to master_host='192.168.30.13',master_port=3307,master_user='backup',master_password='123456',master_log_file='mysql-bin.000003', master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
master_host是宿主机的IP(ifconfig查到的操作系统IP,不是容器的IP,一定不能搞错)
master_port是主服务器的映射到3306的端口(默认3306)
master_user为创建的备份用户
master_password备份用户的密码
master_log_file和master_log_pos则是show master status列表里面的两个值,分别是mysql日志名和起始备份位置
接下来查看效果
接下来start slave;
show slave status;
如果是Waiting for master to send event则说明主从复制成功了,若是Connecting to master肯定是配置错误,楼主就错了几次,重删除配置一下就行,这就是容器的好处,删除的代价很小,短时间就可以恢复。如果是Waiting for master to send event,对主库的增删改查从库都会同步修改。
同步之前的主库:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
从库:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
在主库添加一个kevin的数据库:
mysql> create database kevin;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| kevin |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
查看从库:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| kevin |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
一定要注意在从库配置完主库的信息之后一定要start slave,只有这样从库的修改才能复制到主库。今天的介绍就到这里,谢谢。