Docker使用MySQL

j@j-PC:~$ docker search mysql
NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   8725                [OK]                
mariadb                           MariaDB is a community-developed fork of MyS…   3054                [OK]                
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   645                                     [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   63                                      
centurylink/mysql                 Image containing mysql. Optimized to be link…   61                                      [OK]
mysql/mysql-cluster               Experimental MySQL Cluster Docker images. Cr…   54                                      
deitch/mysql-backup               REPLACED! Please use http://hub.docker.com/r…   41                                      [OK]
bitnami/mysql                     Bitnami MySQL Docker Image                      36                                      [OK]
tutum/mysql                       Base docker image to run a MySQL database se…   34                                      
schickling/mysql-backup-s3        Backup MySQL to S3 (supports periodic backup…   28                                      [OK]
prom/mysqld-exporter                                                              23                                      [OK]
linuxserver/mysql                 A Mysql container, brought to you by LinuxSe…   22                                      
centos/mysql-56-centos7           MySQL 5.6 SQL database server                   16                                      
circleci/mysql                    MySQL is a widely used, open-source relation…   15                                      
mysql/mysql-router                MySQL Router provides transparent routing be…   13                                      
arey/mysql-client                 Run a MySQL client from a docker container      11                                      [OK]
imega/mysql-client                Size: 36 MB, alpine:3.5, Mysql client: 10.1.…   8                                       [OK]
yloeffler/mysql-backup            This image runs mysqldump to backup data usi…   6                                       [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…   4                                       [OK]
genschsa/mysql-employees          MySQL Employee Sample Database                  2                                       [OK]
ansibleplaybookbundle/mysql-apb   An APB which deploys RHSCL MySQL                2                                       [OK]
jelastic/mysql                    An image of the MySQL database server mainta…   1                                       
monasca/mysql-init                A minimal decoupled init container for mysql    0                                       
widdpim/mysql-client              Dockerized MySQL Client (5.7) including Curl…   0                                       [OK]
  • 选择镜像下载到本地
    详细参考:docker pull
j@j-PC:~$ docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
Digest: sha256:7345ce4ce6f0c1771d01fa333b8edb2c606ca59d385f69575f8e3e2ec6695eee
Status: Image is up to date for mysql:latest
docker.io/library/mysql:latest
j@j-PC:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              540a289bab6c        4 days ago          126MB
ubuntu              latest              cf0f3ca922e0        8 days ago          64.2MB
mysql               latest              c8ee894bd2bd        9 days ago          456MB
x11docker/deepin    latest              fa4322f67c96        2 months ago        2.86GB
docker/whalesay     latest              6b362a9f73eb        4 years ago         247MB
  • 运行一个容器
    详细参考:docker run
    主要用到四个参数:
    1.–name:分配容器运行的名字
    2.-p:设置端口
    3.-e:设置环境变量,这里修改的是root的密码,变量名为:MYSQL_ROOT_PASSWORD
    4.d:后台运行容器并返回容器ID
j@j-PC:~$ docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=admin -d mysql
a02f92b6bde32ed87d13f49e5a2c82b76ba78807f4067b6ede6aa07853e92861
j@j-PC:~$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
a02f92b6bde3        mysql               "docker-entrypoint.s…"   3 minutes ago       Up 3 minutes        0.0.0.0:3306->3306/tcp, 33060/tcp   mysql
  • 运行容器的命令行
    详细参考:docker exec
    这里用到两个参数:
    1.-t:分配伪TTY
    2.-i:即使未连接STDIN也保持打开状态
j@j-PC:~$ docker exec -it mysql bash
root@a02f92b6bde3:/# 
  • 运行MySQL
root@a02f92b6bde3:/# 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.18 MySQL Community Server - GPL

Copyright (c) 2000, 2019, 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>