设为首页 收藏本站
查看: 1147|回复: 0

[经验分享] 使用Docker安装Mysql

[复制链接]

尚未签到

发表于 2017-11-16 18:08:20 | 显示全部楼层 |阅读模式
  最近使用阿里云服务器,学习一下Docker,今天学着使用Docker安装MySQL。
  首先,从阿里云的Docker Hub 上pull一个MySQL的image.



[centos@loovelj~]$ docker pull registry.cn-hangzhou.aliyuncs.com/acs-sample/mysql:5.7
5.7: Pulling from acs-sample/mysql
d4bce7fd68df: Pull complete
a3ed95caeb02: Pull complete
01588229585e: Pull complete
ada32b818a1a: Pull complete
ac7528e308ac: Pull complete
44e3fb8779c7: Pull complete
bfcca86efc6a: Pull complete
32da415dff2e: Pull complete
aae6d9712a36: Pull complete
3148136ce9cc: Pull complete
Digest: sha256:32ff2f404c3bd199aaec2e6d19d91d59673e40d7394732124f91dd72a2e1ed97
Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/acs-sample/mysql:5.7
  查看下载镜像,就会看到已经有了



[centos@loovelj~]$ docker images
REPOSITORY                                           TAG                 IMAGE ID            CREATED             SIZE
test/ubuntu                                          v1.0                523e7db0e264        11 minutes ago      98.3MB
ubuntu                                               latest              dd6f76d9cc90        7 days ago          122MB
hello-world                                          latest              725dcfab7d63        8 days ago          1.84kB
registry.cn-hangzhou.aliyuncs.com/acs-sample/mysql   5.7                 ec7e75e5260c        23 months ago       360MB
  名字太长,修改为短的tag



[centos@loovelj~]$ docker tag registry.cn-hangzhou.aliyuncs.com/acs-sample/mysql:5.7 mysql:5.7
[centos@loovelj~]$ docker images
REPOSITORY                                           TAG                 IMAGE ID            CREATED             SIZE
test/ubuntu                                          v1.0                523e7db0e264        12 minutes ago      98.3MB
ubuntu                                               latest              dd6f76d9cc90        7 days ago          122MB
hello-world                                          latest              725dcfab7d63        8 days ago          1.84kB
mysql                                                5.7                 ec7e75e5260c        23 months ago       360MB
registry.cn-hangzhou.aliyuncs.com/acs-sample/mysql   5.7                 ec7e75e5260c        23 months ago       360MB
  根据镜像创建容器



[centos@loovelj~]$ docker create -it mysql:5.7
62c975b37ad25b03914eb61e05088019f37ff9cb049a682ac02f20fac1761a4d
  启动MySQL容器



[centos@loovelj~]$ docker run --name mysqlserver -e MYSQL_ROOT_PASSWORD=sgcc -d -i -p 3306:3306  mysql:5.7
2a7a85124400be6fd47e0d97cf5d602456b1db1a11c6331747fe662481eea537
[centos@loovelj~]$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
2a7a85124400        mysql:5.7           "/entrypoint.sh my..."   9 seconds ago       Up 8 seconds        0.0.0.0:3306->3306/tcp   mysqlserver
188099665d1e        ubuntu:latest       "/bin/bash"              23 hours ago        Up 23 hours                                  angry_spence
  进入MySQL终端



[centos@liujun ~]$ docker exec -it  2a7a85124400  /bin/bash
root@2a7a85124400:/# mysql -h 127.0.0.1 -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.9 MySQL Community Server (GPL)
Copyright (c) 2000, 2015, 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>
  3.访问Mysql数据库
由于我们在上面使用了-p参数映射了容器的3306端口到宿主机的3306端口,此时我们可以直接访问宿主机的3306端口来访问Docker中的mysql服务
mysql -h 127.0.0.1 -u root -p
  其中,mysql 报错,我就在本机重新安装mysql,参照阿里云教程。
  但是启动mysql时报错



[iyunv@loovelj support-files]# /etc/init.d/mysqld start
Starting MySQL...The server quit without updating PID file [FAILED]cal/mysql/data/liujun.pid).
  经过查询,发现已经有一个运行的mysql。关闭后再重启



[iyunv@liujun support-files]# ps -ef|grep mysqld
root     10803  9758  0 13:26 pts/0    00:00:00 grep --color=auto mysqld
[iyunv@liujun support-files]# kill -9 10803
bash: kill: (10803) - No such process
[iyunv@liujun support-files]# kill -9 9758
Killed
[iyunv@liujun mysql]#  /etc/init.d/mysqld start
Starting MySQL.
  再执行mysql时,发现还是报错,查询原因是启动项不在/usr/bin下面。



ln -s /usr/local/mysql/bin/mysql /usr/bin 做个链接即可
  后来发现环境变量修改了,但是好像没有保存好,mysqladmin 命令还是不能用,所以又重新保存环境变量



[centos@liujun ~]$ vim ~/.bash_profile
#PATH=$PATH:$HOME/bin:/usr/local/apache/bin
#添加以下列
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin:/usr/local/mysql/lib
#:wq 保存退出
[centos@liujun ~]$ source ~/.bash_profile
  再次运行(第二天再次尝试的,上次的容器已经退出了)docker run



[centos@liujun ~]$ docker run --name mysqlserver -e MYSQL_ROOT_PASSWORD=sgcc -d -i -p 3306:3306  mysql:5.7
0cc2009d1d367903a0d4e47a6e69af1bc41e409e194a231b6dd1193cc27bf716
docker: Error response from daemon: driver failed programming external connectivity on endpoint mysqlserver (4e7ecdbc87918be626ac8920214ed76386784c83b572f5cd5d3ff0d46d453bb6): Error starting userland proxy: listen tcp 0.0.0.0:3306: bind: address already in use.
  发现已经存在这个容器,只好删除了重新建立容器




[centos@liujun ~]$ docker rm mysqlserver
mysqlserver
[centos@liujun ~]$ docker run --name mysqlserver -e MYSQL_ROOT_PASSWORD=sgcc -d -i -p 3306:3306  mysql:5.7
9455dbd9c9128e51eee84c29d356fb5dff8d31762179ec2585c563ead08ad413
  按照教程,通过mysql 远程连接就好了,但是说端口已经占有,估计是自己的mysql打开了,关闭本机的mysql



[centos@liujun ~]$ docker run --name mysqlserver -e MYSQL_ROOT_PASSWORD=sgcc -d -i -p 3306:3306  mysql:5.7
0cc2009d1d367903a0d4e47a6e69af1bc41e409e194a231b6dd1193cc27bf716
docker: Error response from daemon: driver failed programming external connectivity on endpoint mysqlserver
(4e7ecdbc87918be626ac8920214ed76386784c83b572f5cd5d3ff0d46d453bb6): Error starting userland proxy: listen tcp 0.0.0.0:3306: bind: address already in use.



[centos@liujun ~]$ sudo /etc/init.d/mysql stop
[sudo] password for centos:
sudo: /etc/init.d/mysql: command not found
[centos@liujun ~]$ sudo /etc/init.d/mysqld stop
Shutting down MySQL..                                      [  OK  ]
  最后再执行连接



[centos@liujun ~]$ mysql -h 127.0.0.1 -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.9 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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>
  成功!
  参考:

  http://blog.sina.com.cn/s/blog_7f2ac7b70102vpyl.html
  http://www.cnblogs.com/xiohao/p/5377609.html
  https://help.aliyun.com/document_detail/50774.html
  关于IP的设置
  https://severalnines.com/blog/mysql-docker-containers-understanding-basics

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-407650-1-1.html 上篇帖子: Centos7 安装Cobbler 下篇帖子: 7.1 安装软件包的三种方法 7.2 rpm包介绍 7.3 rpm工具用法 7.4 yum工具用法 7.5 yum搭建本地仓库(视频中ppt小错误: gpche
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表