详解Docker创建Mysql容器并通过命令行连接到容器
拉取网易蜂巢的mysql-server:5.6
docker pull hub.c.163.com/nce2/mysql:5.6
创建mysql5.6容器 1master+3个slave
docker run --name mysql-master -d -p hub.c.163.com/nce2/mysql:5.6 docker run --name mysql-slave1 -d -p hub.c.163.com/nce2/mysql:5.6 docker run --name mysql-slave2 -d -p hub.c.163.com/nce2/mysql:5.6 docker run --name mysql-slave3 -d -p hub.c.163.com/nce2/mysql:5.6
验证容器状态
[root@bogon ~]# docker ps container id image command created status ports names 907bbbf25d25 hub.c.163.com/nce2/mysql:5.6 "/run.sh" 5 minutes ago up 5 minutes 3306/tcp mysql-slave3 a81df6c86808 hub.c.163.com/nce2/mysql:5.6 "/run.sh" 5 minutes ago up 5 minutes 3306/tcp mysql-slave2 375eabd4c598 hub.c.163.com/nce2/mysql:5.6 "/run.sh" 5 minutes ago up 5 minutes 3306/tcp mysql-slave1 1651d1cab219 hub.c.163.com/nce2/mysql:5.6 "/run.sh" 14 minutes ago up 14 minutes 3306/tcp mysql-master
通过主机命令行进入master容器
docker exec -it mysql-master bash [root@bogon ~]# docker exec -it mysql-master bash root@1651d1cab219:/#
在master中创建一个数据库test_docker
root@1651d1cab219:/# mysql -uroot -p enter password: welcome to the mysql monitor. commands end with ; or \g. your mysql connection id is 4 server version: 5.6.19-v1-log mysql community server (gpl) copyright (c) 2000, 2014, 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> show databases; +--------------------+ | database | +--------------------+ | information_schema | | #bak_database | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.02 sec) mysql> create database test_docker; query ok, 1 row affected (0.06 sec) mysql> show databases; +--------------------+ | database | +--------------------+ | information_schema | | #bak_database | | mysql | | performance_schema | | test | | test_docker | +--------------------+ 6 rows in set (0.00 sec)
在slave1中创建一个数据库test_docker
[root@bogon ~]# docker exec -it mysql-slave bash error response from daemon: no such container: mysql-slave [root@bogon ~]# docker exec -it mysql-slave1 bash root@375eabd4c598:/# mysql -uroot -p enter password: welcome to the mysql monitor. commands end with ; or \g. your mysql connection id is 4 server version: 5.6.19-v1-log mysql community server (gpl) copyright (c) 2000, 2014, 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> show databases; +--------------------+ | database | +--------------------+ | information_schema | | #bak_database | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec)
通过以上的信息说明master与slave是数据隔离的,所以我们可以通过docker创建n个mysql容器, 然后就能以很小的代价就能学习《高可用mysql》中的数据模型 再也不用发愁机器不够用。
后续操作
登录到master容器
[root@bogon ~]# docker exec -it mysql-master bash root@1651d1cab219:/#
怎么查看容器的操作系统环境
一般就是
uname -a cat /etc/pro cat /etc/lsb-release
很幸运我们的容器是ubuntu14.04
root@1651d1cab219:/# cat /etc/lsb-release distrib_id=ubuntu distrib_release=14.04 distrib_codename=trusty distrib_description="ubuntu 14.04.3 lts"
但是执行 apt-get install时候却什么也装不了
需要更改
cd /etc/apt/
没有 vi vim ee 编辑器 只好追加内容到 sources.list
echo deb http://mirrors.163.com/ubuntu/ trusty main restricted universe multiverse >> sources.list echo deb http://mirrors.163.com/ubuntu/ trusty-security main restricted universe multiverse >> sources.list echo deb http://mirrors.163.com/ubuntu/ trusty-updates main restricted universe multiverse >> sources.list echo deb http://mirrors.163.com/ubuntu/ trusty-proposed main restricted universe multiverse >> sources.list echo deb http://mirrors.163.com/ubuntu/ trusty-backports main restricted universe multiverse >> sources.list echo deb-src http://mirrors.163.com/ubuntu/ trusty main restricted universe multiverse >> sources.list echo deb-src http://mirrors.163.com/ubuntu/ trusty-security main restricted universe multiverse >> sources.list echo deb-src http://mirrors.163.com/ubuntu/ trusty-updates main restricted universe multiverse >> sources.list echo deb-src http://mirrors.163.com/ubuntu/ trusty-proposed main restricted universe multiverse >> sources.list echo deb-src http://mirrors.163.com/ubuntu/ trusty-backports main restricted universe multiverse >> sources.list
然后 更新源
apt-get update apt-get install vim
然后通过vim把sources.list文件的前两行删除掉 再重新update一下。
apt-get update
安装一个网络工具获取ip
apt-get install net-tools
获取到master的ip地址
root@1651d1cab219:/# ifconfig eth0 link encap:ethernet hwaddr 02:42:ac:11:00:02 inet addr:172.17.0.2 bcast:0.0.0.0 mask:255.255.0.0 inet6 addr: fe80::42:acff:fe11:2/64 scope:link up broadcast running multicast mtu:1500 metric:1 rx packets:15119 errors:0 dropped:0 overruns:0 frame:0 tx packets:12633 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 rx bytes:34197557 (34.1 mb) tx bytes:897732 (897.7 kb) lo link encap:local loopback inet addr:127.0.0.1 mask:255.0.0.0 inet6 addr: ::1/128 scope:host up loopback running mtu:65536 metric:1 rx packets:22 errors:0 dropped:0 overruns:0 frame:0 tx packets:22 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 rx bytes:2212 (2.2 kb) tx bytes:2212 (2.2 kb)
slave也需要这么做
还有一种方法
可以创建dockerfile依赖mysql镜像创建一个新的镜像。
上诉命令通过run来执行创建的新容器会有安装的软件。
最后通过slave的docker连接到master的容器mysql服务器上
master的服务器mysql账号root赋值权限
mysql> grant all privileges on *.* to root@'%' identified by ''; query ok, 0 rows affected (0.02 sec) mysql> flush privileges; query ok, 0 rows affected (0.02 sec)
slave服务器执行如下命令
[root@bogon ~]# mysql -uroot -p -h 172.17.0.2 enter password: welcome to the mariadb monitor. commands end with ; or \g. your mysql connection id is 6 server version: 5.6.19-v1-log mysql community server (gpl) copyright (c) 2000, 2016, oracle, mariadb corporation ab and others. type 'help;' or '\h' for help. type '\c' to clear the current input statement. mysql [(none)]>
在master上将test_docker数据库删除,看slave的终端是否也不显示已删除的库
master操作
mysql> show databases; +--------------------+ | database | +--------------------+ | information_schema | | #bak_database | | mysql | | performance_schema | | test | | test_docker | +--------------------+ 6 rows in set (0.00 sec) mysql> drop database test_docker; query ok, 0 rows affected (0.06 sec) mysql> show databases; +--------------------+ | database | +--------------------+ | information_schema | | #bak_database | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec)
slave操作
mysql [(none)]> show databases; +--------------------+ | database | +--------------------+ | information_schema | | #bak_database | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec) mysql [(none)]>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 请问个php正则表达式的用法,求高手帮忙