最近使用阿里云服务器,学习一下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. 523e7db0e264 minutes ago .3MB
ubuntu latest dd6f76d9cc90 days ago 122MB
hello-world latest 725dcfab7d63 days ago .84kB
registry.cn-hangzhou.aliyuncs.com/acs-sample/mysql 5.7 ec7e75e5260c 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. 523e7db0e264 minutes ago .3MB
ubuntu latest dd6f76d9cc90 days ago 122MB
hello-world latest 725dcfab7d63 days ago .84kB
mysql 5.7 ec7e75e5260c months ago 360MB
registry.cn-hangzhou.aliyuncs.com/acs-sample/mysql 5.7 ec7e75e5260c 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 :  mysql:5.7
2a7a85124400be6fd47e0d97cf5d602456b1db1a11c6331747fe662481eea537
[centos@loovelj~]$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2a7a85124400 mysql:5.7 "/entrypoint.sh my..." seconds ago Up seconds 0.0.0.0:->/tcp mysqlserver
188099665d1e ubuntu:latest "/bin/bash" hours ago Up 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
Server version: 5.7. MySQL Community Server (GPL) Copyright (c) , , 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时报错

[root@loovelj support-files]# /etc/init.d/mysqld start
Starting MySQL...The server quit without updating PID file [FAILED]cal/mysql/data/liujun.pid).

经过查询,发现已经有一个运行的mysql。关闭后再重启

[root@liujun support-files]# ps -ef|grep mysqld
root : pts/ :: grep --color=auto mysqld
[root@liujun support-files]# kill -
bash: kill: () - No such process
[root@liujun support-files]# kill -
Killed
[root@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 :  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:: 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 : mysql:5.7
9455dbd9c9128e51eee84c29d356fb5dff8d31762179ec2585c563ead08ad413

按照教程,通过mysql 远程连接就好了,但是说端口已经占有,估计是自己的mysql打开了,关闭本机的mysql

[centos@liujun ~]$ docker run --name mysqlserver -e MYSQL_ROOT_PASSWORD=sgcc -d -i -p :  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:: 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
Server version: 5.7. MySQL Community Server (GPL) Copyright (c) , , 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

使用Docker安装Mysql的更多相关文章

  1. Docker学习实践 - Docker安装MySql数据库

    Docker安装MySQL数据库 1.Ubuntu安装MySQL安装 (1)安装编译源码需要的包 sudo apt-get install make cmake gcc g++ bison libnc ...

  2. 使用docker安装mysql和redis

    本文介绍在linux下使用docker安装mysql和redis. 原文地址:代码汇个人博客 http://www.codehui.net/info/59.html 测试环境:centos7.6,do ...

  3. 解决Docker安装MySQL不区分大小写问题

    Docker安装MySQL忽略大小写问题的问题连接MySQL: 查看当前mysql的大小写敏感配置 show global variables like '%lower_case%'; +------ ...

  4. [转]Docker学习之四:使用docker安装mysql

    本文转自:https://blog.csdn.net/qq_19348391/article/details/82998391 Docker学习之一:注册Docker Hub账号 Docker学习之二 ...

  5. SpringBoot Docker Mysql安装,Docker安装Mysql

    SpringBoot Docker Mysql安装,Docker安装Mysql ================================ ©Copyright 蕃薯耀 2018年4月8日 ht ...

  6. docker 安装mysql mongo

    Docker安装Mysql 1.拉取镜像 docker pull mysql/mysql-server 2.运行mysql docker run -d -p : --name [Name] [Imag ...

  7. Docker安装MySQL忽略大小写问题的问题

    原文:Docker安装MySQL忽略大小写问题的问题 连接MySQL: 查看当前mysql的大小写敏感配置show global variables like '%lower_case%';+---- ...

  8. 【Linux】【自学笔记】Linux下面docker安装mysql

    写在前面: 捣腾继续,之前把一个SpringBoot的程序安装在docker上面,参考链接:https://www.cnblogs.com/aki-stones/p/2019-11-01-note.h ...

  9. Docker 安装mysql、oracle

    来源:唐山网站优化 Docker 安装mysql.oracle 使用ssh工具登录docker docker 的ip一般默认为192.168.99.100可以通过安装docker-machine之后, ...

随机推荐

  1. handlebar JS模板使用笔记

    直接上代码: (定义模板) (编译注入) ***知识点*** //数据必须为Json数据(强调:jsonp数据不行,和json是两种数据,jsonp多了callback回调函数来包裹json数据) 遍 ...

  2. PHP抓取网页图片

    <?php set_time_limit(0);//抓取不受时间限制 if($_POST['Submit']=="开始抓取"){ $URL=$_POST['link']; g ...

  3. 关于Websockets问题:

     Websockets是一种与服务器进行全双工,双向通信的信道,它不使用http协议,他有自己的协议即自定义协议,ws协议:它的安全协议为wss协议.这种协议专门为快速传输小数据而设计的.对服务其有一 ...

  4. WPF Popup全屏 弹出方法。解决只显示75%的问题。

    WPF Popup全屏 弹出方法.解决只显示75%的问题.   WPF 中 Popup 有一个特点.当Popup的高度超过屏幕的75%的时候,只显示75%的高度. 如下代码: <Window x ...

  5. zookeeper curator使用caches实现各种监听

    1.篇首语 curator是zookeeper的一个高级api开发包.封装了zookeeper众多的recipes,并且实现了一些新的recipes原语,最重要的是基于zookeeper提供的各种机制 ...

  6. App 监控、推广

    一.监控 酷传  http://www.kuchuan.com/ 应用雷达  http://www.ann9.com/ AppAnnie https://www.appannie.com/cn/ ap ...

  7. python 目录文件

    每天写一点,总有一天我这条咸鱼能变得更咸 python 中对文件及目录的操作基本依赖与os,shutil模块,其中以os模块为主,最主要的几个方法实例如下: 1.判断文件/目录是否存在(os.path ...

  8. win10 UWP 圆形等待

    看到一个圆形好像微软ProgressRing 我们可以用自定义控件 按ctrl+shift+a 用户控件 我们可以用Rectangle做圆形边 只要Rectangle RadiusX>0圆角 因 ...

  9. win10 uwp 自定义控件 SplitViewItem

    本文主要是因为汉堡菜单里面列出的菜单很多重复的图标和文字,我把它作为控件,因为是随便写,可能存在错误,如果发现了,请和我说或关掉浏览器,请不要发不良言论. 我们使用汉堡菜单,经常需要一个 需要一个图标 ...

  10. 开发人员的福音:微软、谷歌、Mozilla将他们所有的web API文档放在同一个地方

    Tips 原文作者:Liam Tung  原文地址:Developers rejoice: Microsoft, Google, Mozilla are putting all their web A ...