Docker 安装 MySQL

Posted miracle-luna

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker 安装 MySQL相关的知识,希望对你有一定的参考价值。

Docker 安装 mysql

方法一、docker pull mysql

查找 Docker Hub 上的 mysql 镜像:

docker search mysql

技术图片

 

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

docker pull mysql:5.6

技术图片

 

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

docker images | grep mysql

技术图片

 

方法二、通过 Dockerfile构建

创建 Dockerfile

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

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 <[email protected]>" 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创建一个镜像,替换成你自己的名字

docker build -t mysql .

 

使用mysql镜像

运行镜像,生成容器

使用 docker run 命令运行镜像,并设置容器中 mysql 数据库的 root 账号密码为 password

docker run --name mymysql -p 3306:3306 -v $PWD/conf:/etc/mysql/conf.d -v $PWD/logs:/logs -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=password -d mysql:5.6

技术图片

命令说明:

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

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

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

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

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

 

访问容器

使用 docker exec 命令访问容器 5c576d19d6b1,并使用 root/password 账号密码访问 MySQL 数据库:

docker exec -it 5c576d19d6b1 /bin/bash
service mysql status
mysql -uroot -p
show databases;

技术图片

 

创建 miracle 数据库,luna 数据表,测试 MySQL 的可用性:

create database miracle;
show databases;
use miracle;
create table luna ( name varchar(20), age int(3), sex varchar(5));
select * from luna;

技术图片

 

 PS:

参考:https://www.runoob.com/docker/docker-install-mysql.html

 

以上是关于Docker 安装 MySQL的主要内容,如果未能解决你的问题,请参考以下文章

docker安装mysql

docker 下安装 mysql 及登录

docker:安装mysql多个

记录下安装docker和docker下安装mysql的过程

docker安装mysql

使用docker安装mysql5.7