基于Centos7的Docker安装MySQL 8.0

4 篇文章 0 订阅
4 篇文章 0 订阅

1. 安装Docker

# 安装Dokcer
[root@test2 ~]# yum install -y docker
# 查看是否安装成功 通过yum安装完成列表并筛选docker安装信息如下所示
[root@test2 ~]# yum list installed | grep docker
docker.x86_64                        2:1.13.1-96.gitb2f74b2.el7.centos @extras  
docker-client.x86_64                 2:1.13.1-96.gitb2f74b2.el7.centos @extras  
docker-common.x86_64                 2:1.13.1-96.gitb2f74b2.el7.centos @extras

​ 注:-y表示不再询问,使用默认配置进行安装。

2. 启动Docker服务

# 启用Docker服务
[root@test2 ~]# systemctl start docker
# 查看Docker状态
[root@test2 ~]# systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
   Active: active (running) since 日 2019-06-30 13:28:20 CST; 9s ago
     Docs: http://docs.docker.com
 Main PID: 40684 (dockerd-current)
   CGroup: /system.slice/docker.service
           ├─40684 /usr/bin/dockerd-current --add-runtime docker-runc=/usr/libexec/docker/docker-runc-cur...
           └─40695 /usr/bin/docker-containerd-current -l unix:///var/run/docker/libcontainerd/docker-cont...

6月 30 13:28:18 test2 dockerd-current[40684]: time="2019-06-30T13:28:18.507160774+08:00" level=info ...695"
6月 30 13:28:19 test2 dockerd-current[40684]: time="2019-06-30T13:28:19.765323180+08:00" level=info ...nds"
6月 30 13:28:19 test2 dockerd-current[40684]: time="2019-06-30T13:28:19.767794756+08:00" level=info ...rt."
6月 30 13:28:19 test2 dockerd-current[40684]: time="2019-06-30T13:28:19.997012431+08:00" level=info ...lse"
6月 30 13:28:20 test2 dockerd-current[40684]: time="2019-06-30T13:28:20.245956439+08:00" level=info ...ess"
6月 30 13:28:20 test2 dockerd-current[40684]: time="2019-06-30T13:28:20.458345533+08:00" level=info ...ne."
6月 30 13:28:20 test2 dockerd-current[40684]: time="2019-06-30T13:28:20.490475169+08:00" level=info ...ion"
6月 30 13:28:20 test2 dockerd-current[40684]: time="2019-06-30T13:28:20.490503694+08:00" level=info ...13.1
6月 30 13:28:20 test2 systemd[1]: Started Docker Application Container Engine.
6月 30 13:28:20 test2 dockerd-current[40684]: time="2019-06-30T13:28:20.506191917+08:00" level=info ...ock"
Hint: Some lines were ellipsized, use -l to show in full.
# 使用Docker命令
[root@test2 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

​ 注:裸机状态下Docker镜像列表为空。

3. 使用Docker安装MySQL

# 使用Docker pull拉取Docker hub仓库中的MySQL镜像
[root@test2 ~]# docker pull mysql:8.0
Trying to pull repository docker.io/library/mysql ... 
8.0: Pulling from docker.io/library/mysql
fc7181108d40: Pull complete 
787a24c80112: Pull complete 
a08cb039d3cd: Pull complete 
4f7d35eb5394: Pull complete 
5aa21f895d95: Pull complete 
a742e211b7a2: Pull complete 
0163805ad937: Pull complete 
87f18876c3ff: Pull complete 
78082f25f167: Pull complete 
0a510f055c17: Pull complete 
312b0999e433: Pull complete 
f864cfdc0264: Pull complete 
Digest: sha256:415ac63da0ae6725d5aefc9669a1c02f39a00c574fdbc478dfd08db1e97c8f1b
Status: Downloaded newer image for docker.io/mysql:8.0

​ 注:docker pull 默认到官方参考拉取 mysql:8.0 镜像名:镜像tag,官方地址为国外地址,下载速度缓慢可进行配置。

# 修改镜像文件拉取地址为ustc
[root@test2 ~]# vi /etc/docker/daemon.json

​ 注:insert编辑内容如下所示,并以wq保存并退出。

{
"registry-mirrors":["https://docker.mirrors.ustc.edu.cn"]
}

​ 修改完毕后重启Docker服务。

# 重启Docker服务
[root@test2 ~]# systemctl restart docker
# 查看DOcker内的镜像文件确保MySQL 8.0已成功拉取
[root@test2 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/mysql     8.0                 c7109f74d339        2 weeks ago         443 MB

4. 启动MySQL镜像

# 启动Docker中的MySQL
[root@test2 ~]# docker run --name mysql8.0 -p 3307:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:8.0
1c52224315bfd65766724e3238d1c66a9ac93c6b04795776e432aed97b437c28

​ 注:

  • –name 为服务启动别名设置
  • -p 端口映射 宿主机端口:镜像运行端口
  • -d 镜像名:tag 使用守护进程模式启动
  • -e 设置root账号密码
# 查看运行的镜像
[root@test2 ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
1c52224315bf        mysql:8.0           "docker-entrypoint..."   56 seconds ago      Up 55 seconds       33060/tcp, 0.0.0.0:3307->3306/tcp   mysql8.0

5. 进入镜像运行MySQL

# 进入MySQL镜像
[root@test2 ~]# docker exec -it mysql8.0 /bin/bash

​ 注::exec docker进入容器命令 -it 容器中服务别名 /bin/bash 表示命令行模式 与 -d 后台守护进行模式启动 形成两种运行方式

# 进入MySQL
root@1c52224315bf:/# cd /usr/bin
root@1c52224315bf:/usr/bin# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.16 MySQL Community Server - GPL

​ 至此Doker中安装使用MySQL 8.0完成。

6. 其他命令

# 删除安装包
yum remove -y 安装包名
# 停止/开始服务
systemctl start docker
systemctl stop docker
# 移除已经安装的镜像
docker rmi 镜像名:tag or 镜像id
# 停止/启动/删除容器服务
docker stop 容器服务别名  or 容器id
docker start 容器服务别名  or 容器id
docker rm 容器服务别名  or 容器id

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值