课程表

Docker 安装和使用

Docker 实例

Docker 参考手册

工具箱
速查手册

Docker 安装 MySQL

当前位置:免费教程 » 程序设计 » Docker

方法一、通过 Dockerfile构建

创建Dockerfile

首先,创建目录mysql,用于存放后面的相关东西。

  1. w3xue@w3xue:~$ mkdir -p ~/mysql/data ~/mysql/logs ~/mysql/conf

data目录将映射为mysql容器配置的数据文件存放路径

logs目录将映射为mysql容器的日志目录

conf目录里的配置文件将映射为mysql容器的配置文件

进入创建的mysql目录,创建Dockerfile

  1. FROM debian:jessie
  2.  
  3. # add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
  4. RUN groupadd -r mysql && useradd -r -g mysql mysql
  5.  
  6. # add gosu for easy step-down from root
  7. ENV GOSU_VERSION 1.7
  8. RUN set -x && apt-get update && apt-get install -y --no-install-recommends ca-certificates wget && rm -rf /var/lib/apt/lists/* && wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture)" && wget -O /usr/local/bin/gosu.asc "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$(dpkg --print-architecture).asc" && export GNUPGHOME="$(mktemp -d)" && gpg --keyserver ha.pool.sks-keyservers.net --recv-keys B42F6819007F00F88E364FD4036A9C25BF357DD4 && gpg --batch --verify /usr/local/bin/gosu.asc /usr/local/bin/gosu && rm -r "$GNUPGHOME" /usr/local/bin/gosu.asc && chmod +x /usr/local/bin/gosu && gosu nobody true && apt-get purge -y --auto-remove ca-certificates wget
  9.  
  10. RUN mkdir /docker-entrypoint-initdb.d
  11.  
  12. # FATAL ERROR: please install the following Perl modules before executing /usr/local/mysql/scripts/mysql_install_db:
  13. # File::Basename
  14. # File::Copy
  15. # Sys::Hostname
  16. # Data::Dumper
  17. RUN apt-get update && apt-get install -y perl pwgen --no-install-recommends && rm -rf /var/lib/apt/lists/*
  18.  
  19. # gpg: key 5072E1F5: public key "MySQL Release Engineering <mysql-build@oss.oracle.com>" imported
  20. RUN apt-key adv --keyserver ha.pool.sks-keyservers.net --recv-keys A4A9406876FCBD3C456770C88C718D3B5072E1F5
  21.  
  22. ENV MYSQL_MAJOR 5.6
  23. ENV MYSQL_VERSION 5.6.31-1debian8
  24.  
  25. RUN echo "deb http://repo.mysql.com/apt/debian/ jessie mysql-${MYSQL_MAJOR}" > /etc/apt/sources.list.d/mysql.list
  26.  
  27. # the "/var/lib/mysql" stuff here is because the mysql-server postinst doesn't have an explicit way to disable the mysql_install_db codepath besides having a database already "configured" (ie, stuff in /var/lib/mysql/mysql)
  28. # also, we set debconf keys to make APT a little quieter
  29. RUN { echo mysql-community-server mysql-community-server/data-dir select ''; echo mysql-community-server mysql-community-server/root-pass password ''; echo mysql-community-server mysql-community-server/re-root-pass password ''; echo mysql-community-server mysql-community-server/remove-test-db select false; } | debconf-set-selections && apt-get update && apt-get install -y mysql-server="${MYSQL_VERSION}" && rm -rf /var/lib/apt/lists/* && rm -rf /var/lib/mysql && mkdir -p /var/lib/mysql /var/run/mysqld && chown -R mysql:mysql /var/lib/mysql /var/run/mysqld # ensure that /var/run/mysqld (used for socket and lock files) is writable regardless of the UID our mysqld instance ends up having at runtime
  30. && chmod 777 /var/run/mysqld
  31.  
  32. # comment out a few problematic configuration values
  33. # don't reverse lookup hostnames, they are usually another container
  34. RUN sed -Ei 's/^(bind-address|log)/#&/' /etc/mysql/my.cnf && echo 'skip-host-cache\nskip-name-resolve' | awk '{ print } $1 == "[mysqld]" && c == 0 { c = 1; system("cat") }' /etc/mysql/my.cnf > /tmp/my.cnf && mv /tmp/my.cnf /etc/mysql/my.cnf
  35.  
  36. VOLUME /var/lib/mysql
  37.  
  38. COPY docker-entrypoint.sh /usr/local/bin/
  39. RUN ln -s usr/local/bin/docker-entrypoint.sh /entrypoint.sh # backwards compat
  40. ENTRYPOINT ["docker-entrypoint.sh"]
  41.  
  42. EXPOSE 3306
  43. CMD ["mysqld"]

通过Dockerfile创建一个镜像,替换成你自己的名字

  1. w3xue@w3xue:~/mysql$ docker build -t mysql .

创建完成后,我们可以在本地的镜像列表里查找到刚刚创建的镜像

  1. w3xue@w3xue:~/mysql$ docker images |grep mysql
  2. mysql 5.6 2c0964ec182a 3 weeks ago 329 MB

方法二、docker pull mysql

查找Docker Hub上的mysql镜像

  1. w3xue@w3xue:/mysql$ docker search mysql
  2. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  3. mysql MySQL is a widely used, open-source relati... 2529 [OK]
  4. mysql/mysql-server Optimized MySQL Server Docker images. Crea... 161 [OK]
  5. centurylink/mysql Image containing mysql. Optimized to be li... 45 [OK]
  6. sameersbn/mysql 36 [OK]
  7. google/mysql MySQL server for Google Compute Engine 16 [OK]
  8. appcontainers/mysql Centos/Debian Based Customizable MySQL Con... 8 [OK]
  9. marvambass/mysql MySQL Server based on Ubuntu 14.04 6 [OK]
  10. drupaldocker/mysql MySQL for Drupal 2 [OK]
  11. azukiapp/mysql Docker image to run MySQL by Azuki - http:... 2 [OK]
  12. ...

这里我们拉取官方的镜像,标签为5.6

  1. w3xue@w3xue:~/mysql$ docker pull mysql:5.6

等待下载完成后,我们就可以在本地镜像列表里查到REPOSITORY为mysql,标签为5.6的镜像。


使用mysql镜像

运行容器

  1. w3xue@w3xue:~/mysql$ docker run -p 3306:3306 --name mymysql -v $PWD/conf/my.cnf:/etc/mysql/my.cnf -v $PWD/logs:/logs -v $PWD/data:/mysql_data -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6
  2. 21cb89213c93d805c5bacf1028a0da7b5c5852761ba81327e6b99bb3ea89930e
  3. w3xue@w3xue:~/mysql$

命令说明:

  • -p 3306:3306:将容器的3306端口映射到主机的3306端口

  • -v $PWD/conf/my.cnf:/etc/mysql/my.cnf:将主机当前目录下的conf/my.cnf挂载到容器的/etc/mysql/my.cnf

  • -v $PWD/logs:/logs:将主机当前目录下的logs目录挂载到容器的/logs

  • -v $PWD/data:/mysql_data:将主机当前目录下的data目录挂载到容器的/mysql_data

  • -e MYSQL_ROOT_PASSWORD=123456:初始化root用户的密码

查看容器启动情况

  1. w3xue@w3xue:~/mysql$ docker ps
  2. CONTAINER ID IMAGE COMMAND ... PORTS NAMES
  3. 21cb89213c93 mysql:5.6 "docker-entrypoint.sh" ... 0.0.0.0:3306->3306/tcp mymysql
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号