安装 MySQL
编辑教程Docker 安装 MySQL
方法一、通过 Dockerfile构建
创建Dockerfile
首先,创建目录mysql,用于存放后面的相关东西。
jsrun@jsrun:~$ mkdir -p ~/mysql/data ~/mysql/logs ~/mysql/conf
data目录将映射为mysql容器配置的数据文件存放路径
logs目录将映射为mysql容器的日志目录
conf目录里的配置文件将映射为mysql容器的配置文件
进入创建的mysql目录,创建Dockerfile
FROM debian:jessie
# add our user and group first to make sure their IDs get assigned consistently, regardless of whatever dependencies get added
RUN groupadd -r mysql && useradd -r -g mysql mysql
# add gosu for easy step-down from root
ENV GOSU\_VERSION 1.7
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
RUN mkdir /docker-entrypoint-initdb.d
# FATAL ERROR: please install the following Perl modules before executing /usr/local/mysql/scripts/mysql\_install\_db:
# File::Basename
# File::Copy
# Sys::Hostname
# Data::Dumper
RUN apt-get update && apt-get install -y perl pwgen --no-install-recommends && rm -rf /var/lib/apt/lists/\*
# gpg: key 5072E1F5: public key "MySQL Release Engineering <mysql-build@oss.oracle.com>" imported
RUN apt-key adv --keyserver ha.pool.sks-keyservers.net --recv-keys A4A9406876FCBD3C456770C88C718D3B5072E1F5
ENV MYSQL\_MAJOR 5.6
ENV MYSQL\_VERSION 5.6.31-1debian8
RUN echo "deb http://repo.mysql.com/apt/debian/ jessie mysql-${MYSQL\_MAJOR}" > /etc/apt/sources.list.d/mysql.list
# 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)
# also, we set debconf keys to make APT a little quieter
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
&& chmod 777 /var/run/mysqld
# comment out a few problematic configuration values
# don't reverse lookup hostnames, they are usually another container
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
VOLUME /var/lib/mysql
COPY docker-entrypoint.sh /usr/local/bin/
RUN ln -s usr/local/bin/docker-entrypoint.sh /entrypoint.sh # backwards compat
ENTRYPOINT \["docker-entrypoint.sh"\]
EXPOSE 3306
CMD \["mysqld"\]
通过Dockerfile创建一个镜像,替换成你自己的名字
jsrun@jsrun:~/mysql$ docker build -t mysql .
创建完成后,我们可以在本地的镜像列表里查找到刚刚创建的镜像
jsrun@jsrun:~/mysql$ docker images |grep mysql
mysql 5.6 2c0964ec182a 3 weeks ago 329 MB
方法二、docker pull mysql
查找Docker Hub上的mysql镜像
jsrun@jsrun:/mysql$ docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relati... 2529 \[OK\]
mysql/mysql-server Optimized MySQL Server Docker images. Crea... 161 \[OK\]
centurylink/mysql Image containing mysql. Optimized to be li... 45 \[OK\]
sameersbn/mysql 36 \[OK\]
google/mysql MySQL server for Google Compute Engine 16 \[OK\]
appcontainers/mysql Centos/Debian Based Customizable MySQL Con... 8 \[OK\]
marvambass/mysql MySQL Server based on Ubuntu 14.04 6 \[OK\]
drupaldocker/mysql MySQL for Drupal 2 \[OK\]
azukiapp/mysql Docker image to run MySQL by Azuki - http:... 2 \[OK\]
...
这里我们拉取官方的镜像,标签为5.6
jsrun@jsrun:~/mysql$ docker pull mysql:5.6
等待下载完成后,我们就可以在本地镜像列表里查到REPOSITORY为mysql,标签为5.6的镜像。
使用mysql镜像
运行容器
jsrun@jsrun:~/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
21cb89213c93d805c5bacf1028a0da7b5c5852761ba81327e6b99bb3ea89930e
jsrun@jsrun:~/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用户的密码
查看容器启动情况
jsrun@jsrun:~/mysql$ docker ps
CONTAINER ID IMAGE COMMAND ... PORTS NAMES
21cb89213c93 mysql:5.6 "docker-entrypoint.sh" ... 0.0.0.0:3306->3306/tcp mymysql
Mos固件,小电视必刷固件
ES6 教程
Vue.js 教程
JSON 教程
jQuery 教程
HTML 教程
HTML 5 教程
CSS 教程
CSS3 教程
JavaScript 教程
DHTML 教程
JSON在线格式化工具
JS在线运行
JSON解析格式化
jsfiddle中国国内版本
JS代码在线运行
PHP代码在线运行
Java代码在线运行
C语言代码在线运行
C++代码在线运行
Python代码在线运行
Go语言代码在线运行
C#代码在线运行
JSRUN闪电教程系统是国内最先开创的教程维护系统, 所有工程师都可以参与共同维护的闪电教程,让知识的积累变得统一完整、自成体系。
大家可以一起参与进共编,让零散的知识点帮助更多的人。
X
选择支付方式:
立即支付
¥
9.99
无法付款,请点击这里
金额: 0 元
备注:
转账时请填写正确的金额和备注信息,到账由人工处理,可能需要较长时间
备注:
转账时请填写正确的金额和备注信息,到账由人工处理,可能需要较长时间
如有疑问请联系QQ:565830900
正在生成二维码, 此过程可能需要15秒钟