docker.io mysql_Docker上运行MySQL服务

背景:

现如今不管什么服务和应用基本都可以在docker里跑一跑了,但是在我个人的印象中,像数据库这种比较重要大型且数据容易受伤的应用是不适合在docker里跑的。但是也有很多人尝试在docker中跑MySQL等数据库,所以也试着尝试一下。(好吧,重点是领导喜欢~~)

获取镜像:

mysql的镜像可以自己用dockerfile制作一个,或者直接到官方的docker镜像库中下载,本文用的是官方镜像。

# docker pull mysql

# docker images

REPOSITORY                                          TAG                IMAGE ID            CREATED            SIZE

docker.io/mysql                                    latest              d9124e6c552f        12 days ago        383.4 MB

运行容器:

1:正常运行。

启动容器:

# docker run --name cmh-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d docker.io/mysql

进入容器:

# docker-enter cmh-mysql

进入mysql:

root@3a2b8ab0d971:~# mysql -u root -pmy-secret-pw

mysql: [Warning] Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 2

Server version: 5.7.16 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>

以上就创建了一个mysql的docker容器,可以看到版本为5.7.16。但是这样创建的容器有两个问题,一是容器删除后,数据就丢失了,二是要访问数据库,必须进入到容器里面才可以。

2:持久化数据,映射开放mysql端口

创建宿主机数据存放目录:

# mkdir -p /opt/data/mysql

启动容器:

# docker run --name cmh-mysql -v /opt/data/mysql/:/var/lib/mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d docker.io/mysql

c38f50a540ff4d5ecf1a5ec49fb721335a8e1b79dec58229cf5e00553f988e44

查看容器:

# docker ps

CONTAINER ID        IMAGE                    COMMAND                  CREATED            STATUS              PORTS        NAMES

c38f50a540ff        docker.io/mysql          "docker-entrypoint.sh"  9 seconds ago      Up 8 seconds        0.0.0.0:3306->3306/tcp        cmh-mysql

查看端口:

# netstat -ntpl

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address          Foreign Address        State      PID/Program name

tcp6      0      0 :::3306                :::*                    LISTEN      28657/docker-proxy

查看宿主机上的mysql数据:

# cd /opt/data/mysql

# ll

total 188452

-rw-r-----. 1 systemd-bus-proxy ssh_keys      56 Dec  6 16:01 auto.cnf

-rw-r-----. 1 systemd-bus-proxy ssh_keys    1325 Dec  6 16:01 ib_buffer_pool

-rw-r-----. 1 systemd-bus-proxy ssh_keys 79691776 Dec  6 17:16 ibdata1

-rw-r-----. 1 systemd-bus-proxy ssh_keys 50331648 Dec  6 17:16 ib_logfile0

-rw-r-----. 1 systemd-bus-proxy ssh_keys 50331648 Dec  6 16:01 ib_logfile1

-rw-r-----. 1 systemd-bus-proxy ssh_keys 12582912 Dec  6 17:16 ibtmp1

drwxr-x---. 2 systemd-bus-proxy ssh_keys    4096 Dec  6 16:01 mysql

drwxr-x---. 2 systemd-bus-proxy ssh_keys    8192 Dec  6 16:01 performance_schema

drwxr-x---. 2 systemd-bus-proxy ssh_keys    8192 Dec  6 16:01 sys

-p 3306:3306把容器的mysql端口3306映射到宿主机的3306端口,这样想访问mysql就可以直接访问宿主机的3306端口。

-v /opt/data/mysql:/var/lib/mysql,即把宿主机/opt/data/mysql/目录映射到容器的/var/lib/mysql目录。

注意事项:

1:在使用-v选项映射目录时,宿主机需关闭SElinux:

# setenforce 0

或者给数据目录添加相关selinux权限:

# chcon -Rt svirt_sandbox_file_t /my/own/datadir

2:-v 选项原本是把宿主机的目录映射进容器,但是在本文中,是反过来的。即是把容器中的目录映射出宿主机,这是因为官方镜像在制作的时候使用了VOLUME /var/lib/mysql选项。这使得容器中/var/lib/mysql成为一个单独的卷组,在使用挂载选项-v时,就可以把该目录映射出宿主机。

可以参考官方mysql镜像的dockerfile:

https://github.com/docker-library/mysql/blob/4dd33136c4739667a223d39b6f829beb27b235cf/5.7/Dockerfile

更多Docker相关教程见以下内容:

Docker 的详细介绍:请点这里

Docker 的下载地址:请点这里

0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值