docker上安装使用mysql镜像
文章目录
背景:
现如今不管什么服务和应用基本都可以在docker里跑一跑了,但是在我个人的印象中,像数据库这种比较重要大型且数据容易受伤的应用是不适合在docker里跑的。但是也有很多人尝试在docker中跑mysql等数据库,所以也试着尝试一下。(好吧,重点是领导喜欢~~)
获取镜像:
mysql的镜像可以自己用dockerfile制作一个,或者直接到官方的docker镜像库中下载,本文用的是官方镜像。
docker pull mysql# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEdocker.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-pwmysql: [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 2Server 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 itsaffiliates. Other names may be trademarks of their respectiveowners.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

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论