详解Docker创建Mysql容器并通过命令行连接到容器
拉取网易蜂巢的mysql-server:5.6
dockerpullhub.c.163.com/nce2/mysql:5.6
创建mysql5.6容器1master+3个slave
dockerrun--namemysql-master-d-Phub.c.163.com/nce2/mysql:5.6 dockerrun--namemysql-slave1-d-Phub.c.163.com/nce2/mysql:5.6 dockerrun--namemysql-slave2-d-Phub.c.163.com/nce2/mysql:5.6 dockerrun--namemysql-slave3-d-Phub.c.163.com/nce2/mysql:5.6
验证容器状态
[root@bogon~]#dockerps CONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES 907bbbf25d25hub.c.163.com/nce2/mysql:5.6"/run.sh"5minutesagoUp5minutes3306/tcpmysql-slave3 a81df6c86808hub.c.163.com/nce2/mysql:5.6"/run.sh"5minutesagoUp5minutes3306/tcpmysql-slave2 375eabd4c598hub.c.163.com/nce2/mysql:5.6"/run.sh"5minutesagoUp5minutes3306/tcpmysql-slave1 1651d1cab219hub.c.163.com/nce2/mysql:5.6"/run.sh"14minutesagoUp14minutes3306/tcpmysql-master
通过主机命令行进入master容器
dockerexec-itmysql-masterbash [root@bogon~]#dockerexec-itmysql-masterbash root@1651d1cab219:/#
在master中创建一个数据库test_docker
root@1651d1cab219:/#mysql-uroot-p Enterpassword: WelcometotheMySQLmonitor.Commandsendwith;or\g. YourMySQLconnectionidis4 Serverversion:5.6.19-v1-logMySQLCommunityServer(GPL) Copyright(c)2000,2014,Oracleand/oritsaffiliates.Allrightsreserved. OracleisaregisteredtrademarkofOracleCorporationand/orits affiliates.Othernamesmaybetrademarksoftheirrespective owners. Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement. mysql>showdatabases; +--------------------+ |Database| +--------------------+ |information_schema| |#bak_database| |mysql| |performance_schema| |test| +--------------------+ 5rowsinset(0.02sec) mysql>createdatabasetest_docker; QueryOK,1rowaffected(0.06sec) mysql>showdatabases; +--------------------+ |Database| +--------------------+ |information_schema| |#bak_database| |mysql| |performance_schema| |test| |test_docker| +--------------------+ 6rowsinset(0.00sec)
在slave1中创建一个数据库test_docker
[root@bogon~]#dockerexec-itmysql-slavebash Errorresponsefromdaemon:Nosuchcontainer:mysql-slave [root@bogon~]#dockerexec-itmysql-slave1bash root@375eabd4c598:/#mysql-uroot-p Enterpassword: WelcometotheMySQLmonitor.Commandsendwith;or\g. YourMySQLconnectionidis4 Serverversion:5.6.19-v1-logMySQLCommunityServer(GPL) Copyright(c)2000,2014,Oracleand/oritsaffiliates.Allrightsreserved. OracleisaregisteredtrademarkofOracleCorporationand/orits affiliates.Othernamesmaybetrademarksoftheirrespective owners. Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement. mysql>showdatabases; +--------------------+ |Database| +--------------------+ |information_schema| |#bak_database| |mysql| |performance_schema| |test| +--------------------+ 5rowsinset(0.00sec)
通过以上的信息说明master与slave是数据隔离的,所以我们可以通过docker创建N个mysql容器, 然后就能以很小的代价就能学习《高可用MySQL》中的数据模型再也不用发愁机器不够用。
后续操作
登录到master容器
[root@bogon~]#dockerexec-itmysql-masterbash 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="Ubuntu14.04.3LTS"
但是执行apt-getinstall时候却什么也装不了
需要更改
cd/etc/apt/
没有vivimee编辑器只好追加内容到sources.list
echodebhttp://mirrors.163.com/ubuntu/trustymainrestricteduniversemultiverse>>sources.list echodebhttp://mirrors.163.com/ubuntu/trusty-securitymainrestricteduniversemultiverse>>sources.list echodebhttp://mirrors.163.com/ubuntu/trusty-updatesmainrestricteduniversemultiverse>>sources.list echodebhttp://mirrors.163.com/ubuntu/trusty-proposedmainrestricteduniversemultiverse>>sources.list echodebhttp://mirrors.163.com/ubuntu/trusty-backportsmainrestricteduniversemultiverse>>sources.list echodeb-srchttp://mirrors.163.com/ubuntu/trustymainrestricteduniversemultiverse>>sources.list echodeb-srchttp://mirrors.163.com/ubuntu/trusty-securitymainrestricteduniversemultiverse>>sources.list echodeb-srchttp://mirrors.163.com/ubuntu/trusty-updatesmainrestricteduniversemultiverse>>sources.list echodeb-srchttp://mirrors.163.com/ubuntu/trusty-proposedmainrestricteduniversemultiverse>>sources.list echodeb-srchttp://mirrors.163.com/ubuntu/trusty-backportsmainrestricteduniversemultiverse>>sources.list
然后更新源
apt-getupdate apt-getinstallvim
然后通过vim把sources.list文件的前两行删除掉再重新update一下。
apt-getupdate
安装一个网络工具获取ip
apt-getinstallnet-tools
获取到master的ip地址
root@1651d1cab219:/#ifconfig eth0Linkencap:EthernetHWaddr02:42:ac:11:00:02 inetaddr:172.17.0.2Bcast:0.0.0.0Mask:255.255.0.0 inet6addr:fe80::42:acff:fe11:2/64Scope:Link UPBROADCASTRUNNINGMULTICASTMTU:1500Metric:1 RXpackets:15119errors:0dropped:0overruns:0frame:0 TXpackets:12633errors:0dropped:0overruns:0carrier:0 collisions:0txqueuelen:0 RXbytes:34197557(34.1MB)TXbytes:897732(897.7KB) loLinkencap:LocalLoopback inetaddr:127.0.0.1Mask:255.0.0.0 inet6addr:::1/128Scope:Host UPLOOPBACKRUNNINGMTU:65536Metric:1 RXpackets:22errors:0dropped:0overruns:0frame:0 TXpackets:22errors:0dropped:0overruns:0carrier:0 collisions:0txqueuelen:0 RXbytes:2212(2.2KB)TXbytes:2212(2.2KB)
slave也需要这么做
还有一种方法
可以创建Dockerfile依赖mysql镜像创建一个新的镜像。
上诉命令通过RUN来执行创建的新容器会有安装的软件。
最后通过slave的docker连接到master的容器mysql服务器上
master的服务器mysql账号root赋值权限
mysql>grantallprivilegeson*.*toroot@'%'identifiedby''; QueryOK,0rowsaffected(0.02sec) mysql>flushprivileges; QueryOK,0rowsaffected(0.02sec)
slave服务器执行如下命令
[root@bogon~]#mysql-uroot-p-h172.17.0.2 Enterpassword: WelcometotheMariaDBmonitor.Commandsendwith;or\g. YourMySQLconnectionidis6 Serverversion:5.6.19-v1-logMySQLCommunityServer(GPL) Copyright(c)2000,2016,Oracle,MariaDBCorporationAbandothers. Type'help;'or'\h'forhelp.Type'\c'toclearthecurrentinputstatement. MySQL[(none)]>
在master上将test_docker数据库删除,看slave的终端是否也不显示已删除的库
master操作
mysql>showdatabases; +--------------------+ |Database| +--------------------+ |information_schema| |#bak_database| |mysql| |performance_schema| |test| |test_docker| +--------------------+ 6rowsinset(0.00sec) mysql>dropdatabasetest_docker; QueryOK,0rowsaffected(0.06sec) mysql>showdatabases; +--------------------+ |Database| +--------------------+ |information_schema| |#bak_database| |mysql| |performance_schema| |test| +--------------------+ 5rowsinset(0.00sec)
slave操作
MySQL[(none)]>showdatabases; +--------------------+ |Database| +--------------------+ |information_schema| |#bak_database| |mysql| |performance_schema| |test| +--------------------+ 5rowsinset(0.00sec) MySQL[(none)]>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。