当前位置: 移动技术网 > 网络运营>服务器>虚拟主机 > docker上安装使用mysql镜像

docker上安装使用mysql镜像

2019年04月19日  | 移动技术网网络运营  | 我要评论

背景:

现如今不管什么服务和应用基本都可以在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从入门到实践 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网