阅读 71

docker 快速搭建 mysql

快速搭建本地 mysql 环境

作为一名前端开发,逐渐接触后端(node)的开发,业务需求逐渐累加,node 层从数据聚合层也逐渐有使用 mysql 、redis 的需求, 今天分享一下使用 docker 来快速搭建 mysql ,也以便自我记录。

docker

首先简单介绍一下 docker 的几个基本概念: 简单来说 docker 也是一种跨平台的解决方案,保证在各平台上面所运行的环境都一致。每个 docker 容器都创建了一个独立的、隔离的环境。

docker image : 可以理解为镜像,根据这些基本的镜像可以创建默认的、初始的 docker 容器。

dockerfile : 可根据 dockerfile 生成容器,记录创建容器时或创建容器后的初始化命令,比如下载依赖、修改配置等。

docker hub : docker 官方提供一个存储 docker image 的远程仓库,提供管理 image 的能力(可以理解为 github)

正式开始

  • 使用 search 命令搜索 hub 上面所有可选的 mysql 版本
╭─~/ShopeeWorkspace/NodejsWorkspace/themis-be  ‹dev›
╰─➤  docker search mysql
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   9542                [OK]
mariadb                           MariaDB is a community-developed fork of MyS…   3461                [OK]
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   699                                     [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   75
mysql/mysql-cluster               Experimental MySQL Cluster Docker images. Cr…   69
centurylink/mysql                 Image containing mysql. Optimized to be link…   61                                      [OK]
deitch/mysql-backup               REPLACED! Please use http://hub.docker.com/r…   41                                      [OK]
bitnami/mysql                     Bitnami MySQL Docker Image                      39                                      [OK]
tutum/mysql                       Base docker image to run a MySQL database se…   35
schickling/mysql-backup-s3        Backup MySQL to S3 (supports periodic backup…   30                                      [OK]
prom/mysqld-exporter                                                              28                                      [OK]
linuxserver/mysql                 A Mysql container, brought to you by LinuxSe…   25
centos/mysql-56-centos7           MySQL 5.6 SQL database server                   19
circleci/mysql                    MySQL is a widely used, open-source relation…   19
databack/mysql-backup             Back up mysql databases to... anywhere!         18
mysql/mysql-router                MySQL Router provides transparent routing be…   15
arey/mysql-client                 Run a MySQL client from a docker container      14                                      [OK]
openshift/mysql-55-centos7        DEPRECATED: A Centos7 based MySQL v5.5 image…   6
fradelg/mysql-cron-backup         MySQL/MariaDB database backup using cron tas…   6                                       [OK]
genschsa/mysql-employees          MySQL Employee Sample Database                  5                                       [OK]
devilbox/mysql                    Retagged MySQL, MariaDB and PerconaDB offici…   3
ansibleplaybookbundle/mysql-apb   An APB which deploys RHSCL MySQL                2                                       [OK]
jelastic/mysql                    An image of the MySQL database server mainta…   1
widdpim/mysql-client              Dockerized MySQL Client (5.7) including Curl…   0                                       [OK]
monasca/mysql-init                A minimal decoupled init container for mysql    0

复制代码

查看搜索结果第一个就是 mysql ,且标识官方出品,可放心下载。

  • 一般这种官方提供的镜像会提供多个版本,具体版本可和官方同步,也可以在 docker hub 上查看

docker 官方为提供查看远程版本的命令

可使用此链接查看: hub.docker.com/_/mysql?tab…

  • 使用 pull 命令指定版本下载镜像至本地
╭─~/ShopeeWorkspace/NodejsWorkspace/ops-data-be  ‹lei.liao/SPPC-10092/listing-overview-1.1*›
╰─➤  docker pull mysql:8.0                                                                                                                                                                                                                1 ↵
8.0: Pulling from library/mysql
afb6ec6fdc1c: Downloading [============>                                      ]  7.027MB/27.1MB
0bdc5971ba40: Download complete
97ae94a2c729: Download complete
f777521d340e: Download complete
1393ff7fc871: Download complete
a499b89994d9: Downloading [==========>                                        ]  2.932MB/13.44MB
7ebe8eefbafe: Download complete
597069368ef1: Waiting
ce39a5501878: Waiting
7d545bca14bf: Waiting
211e5bb2ae7b: Waiting
5914e537c077: Waiting


+ 查看本地镜像
```bash
╭─~/ShopeeWorkspace/NodejsWorkspace/ops-data-be  ‹lei.liao/SPPC-10092/listing-overview-1.1*›
╰─➤  docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               8.0                 30f937e841c8        3 days ago          541MB
mysql               5.7                 e73346bdf465        11 days ago         448MB
mysql               latest              a7a67c95e831        3 weeks ago         541MB
nginx               latest              231d40e811cd        6 months ago        126MB
hello-world         latest              fce289e99eb9        17 months ago       1.84kB
复制代码

docker rmi 可删除本地进行,带上 IMAGE ID 即可

╭─~/ShopeeWorkspace/NodejsWorkspace/themis-be  ‹dev›
╰─➤  docker rmi a7a67c95e831
Untagged: mysql:latest
Untagged: mysql@sha256:61a2a33f4b8b4bc93b7b6b9e65e64044aaec594809f818aeffbff69a893d1944
Deleted: sha256:a7a67c95e83189d60dd24cfeb13d9f235a95a7afd7749a7d09845f303fab239c
Deleted: sha256:7972c7c2b8269f6d954cae13742dea63b6b8b960adacfd2d6c4b3c9dd6f9104b
Deleted: sha256:417f0df153747fa5ff5860d49b391b3b79b7520c603b3e2e7432ade1dce74666
Deleted: sha256:4f949440ec440d9d25a5e3a00b20f1455112d43960cd4568538f6375702308cb
Deleted: sha256:28ed79ccec804217192861862893dd50c3e0a49ccc6d5c57787d3a709a9a6219
Deleted: sha256:bf971a50a8d9589df4aa7422003999e9f944ff1b9c14f3bf48ed0fa3e21f2a48
Deleted: sha256:af3a3fda5da43f7f03f8b3ca0330d120f9a7e5c0d9c5fc7b738ac45468a7de38
Deleted: sha256:86dceae8843638ef500e2a796a326ecb2ba1fd31cff817c4537afb67f04d6ff2
Deleted: sha256:2021ba03e116f319f5e8a90e3498d338ed213f1e036f59ffacc98266e7d3da6b
Deleted: sha256:3a3e1773b14d8b704f018b086292f46309cc27535f7484e0dfbf5a4308c81202
Deleted: sha256:372a8a077b4e29fa382990aeee3e38181481043fe145cb979ccd52588b4f36be
Deleted: sha256:821bf1f5688724dd59945682c34c0d33694af9c554cc1c3c69ca3aa9ba5d79ea
Deleted: sha256:c2adabaecedbda0af72b153c6499a0555f3a769d52370469d8f6bd6328af9b13
复制代码

如果提示 image 已经被占用,可以先 stop and rm 该 container ,然后 rmi

ps -a 可以查看所有容器

╭─~/ShopeeWorkspace/NodejsWorkspace/themis-be  ‹dev›
╰─➤  docker ps -a                                                                                                                                   1 ↵
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                    PORTS                               NAMES
a23404b6a225        mysql:5.7           "docker-entrypoint.s…"   10 days ago         Up 10 days                0.0.0.0:3306->3306/tcp, 33060/tcp   mysql-lei-5.7
298bdf4cd729        mysql               "docker-entrypoint.s…"   11 days ago         Exited (0) 10 days ago                                        mysql-lei
8a2da3ec4ed4        hello-world         "/hello"                 5 months ago        Exited (0) 5 months ago                                       elastic_murdock

╭─~/ShopeeWorkspace/NodejsWorkspace/themis-be  ‹dev›
╰─➤  docker stop 298bdf4cd729
298bdf4cd729

╭─~/ShopeeWorkspace/NodejsWorkspace/themis-be  ‹dev›
╰─➤  docker rm mysql-lei                                                                                                                            
mysql-lei
复制代码
  • 使用 run 命令启动镜像
╭─~/ShopeeWorkspace/NodejsWorkspace/themis-be  ‹dev›
╰─➤  docker run --name=mysql-8.0 -e MYSQL_ROOT_HOST=% -e MYSQL_ROOT_PASSWORD=123456 -p 6606:6606 -d mysql:8.0
a9c2c5992a712dfe4cf615e00332c83f610624543c441b02fc34028a6d8a520b
复制代码

--name 可以指定该容器的名字,后续使用该名字进行启动 -p 映射本机和容器的端口,前者为本机,后者为容器

  • logs 命令可查看容器是否正常运行
─~/ShopeeWorkspace/NodejsWorkspace/themis-be  ‹dev›
╰─➤  docker logs mysql-8.0
2020-05-25 07:57:53+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.20-1debian10 started.
2020-05-25 07:57:53+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2020-05-25 07:57:53+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.20-1debian10 started.
2020-05-25 07:57:53+00:00 [Note] [Entrypoint]: Initializing database files
复制代码
  • 使用 exec 进入容器
╭─~/ShopeeWorkspace/NodejsWorkspace/themis-be  ‹dev›
╰─➤  docker exec -it mysql-8.0 mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.20 MySQL Community Server - GPL

Copyright (c) 2000, 2020, 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 环境
╭─~/ShopeeWorkspace/NodejsWorkspace/themis-be  ‹dev›
╰─➤   docker exec -it mysql-8.0 /bin/bash                                                                                                         130 ↵
root@a9c2c5992a71:/# mysql -u root -p
Enter password:
复制代码
  • 本机访问 localhost:6606 就可以访问 mysql ,需要注意的是,使用 docker 新建的 mysql 时区为 unix 0 时区。
关注下面的标签,发现更多相似文章
评论