一,查看mysql镜像源
root@xushi:~# docker search mysqlNAME DESCRIPTION STARS OFFICIAL AUTOMATEDmysql MySQL is a widely used, open-source relati... 5757 [OK] mariadb MariaDB is a community-developed fork of M... 1846 [OK] mysql/mysql-server Optimized MySQL Server Docker images. Crea... 397 [OK]zabbix/zabbix-server-mysql Zabbix Server with MySQL database support 92 [OK]hypriot/rpi-mysql RPi-compatible Docker Image with Mysql 79 centurylink/mysql Image containing mysql. Optimized to be li... 59 [OK]zabbix/zabbix-web-nginx-mysql Zabbix frontend based on Nginx web-server ... 48 [OK]tutum/mysql Base docker image to run a MySQL database ... 31 1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 29 [OK]centos/mysql-57-centos7 MySQL 5.7 SQL database server 23 mysql/mysql-cluster Experimental MySQL Cluster Docker images. ... 19 schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic back... 16 [OK]linuxserver/mysql A Mysql container, brought to you by Linux... 14 bitnami/mysql Bitnami MySQL Docker Image 13 [OK]zabbix/zabbix-proxy-mysql Zabbix proxy with MySQL database support 12 [OK]centos/mysql-56-centos7 MySQL 5.6 SQL database server 8 openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 ima... 6 circleci/mysql MySQL is a widely used, open-source relati... 4 dsteinkopf/backup-all-mysql backup all DBs in a mysql server 3 [OK]frodenas/mysql A Docker Image for MySQL 2 [OK]openzipkin/zipkin-mysql Mirror of https://quay.io/repository/openz... 0 ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 0 [OK]cloudfoundry/cf-mysql-ci Image used in CI of cf-mysql-release 0 cloudposse/mysql Improved `mysql` service with support for ... 0 [OK]astronomerio/mysql-sink MySQL sink 0 [OK]
二,下载mysql镜像
root@xushi:~# docker pull mysqlUsing default tag: latestlatest: Pulling from library/mysql2a72cbf407d6: Pull complete 38680a9b47a8: Pull complete 4c732aa0eb1b: Pull complete c5317a34eddd: Pull complete f92be680366c: Pull complete e8ecd8bec5ab: Pull complete 2a650284a6a8: Pull complete 5b5108d08c6d: Pull complete beaff1261757: Pull complete c1a55c6375b5: Pull complete 8181cde51c65: Pull complete Digest: sha256:691c55aabb3c4e3b89b953dd2f022f7ea845e5443954767d321d5f5fa394e28cStatus: Downloaded newer image for mysql:latest
查看下载的image
root@xushi:~# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEmysql latest 5195076672a7 11 days ago 371 MB
三,docker启动mysql
root@xushi:~# docker run -p 3307:3306 -v $PWD/docker/mysql:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql3307 -d 5195076672a7435efc72d00f39421dca6011dc96aa6e780c0dbdcb4c9e3f2aac985eaadeb35b
说明
编号 | 参数 | 说明 |
1 | -p 3307:3306 | 将容器的3306端口映射到主机的3307端口 |
2 | -v$PWD/root/docker/mysql:/var/lib/mysql | 将主机当前目录下的/root/docker/mysql挂载到容器的/var/lib/mysql; $PWD表示当前目录 |
3 | -e MYSQL_ROOT_PASSWORD=123456 | 初始化root用户的密码 |
4 | --name | 给容器命名,mysql3307 |
5 | -d 5195076672a7 | 刚才下载的mysql的imageID,也就是我们所说的镜像ID,通过docker images命令查看 |
通过docker ps查看
root@xushi:~# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES435efc72d00f 5195076672a7 "docker-entrypoint..." 8 minutes ago Up 8 minutes 0.0.0.0:3307->3306/tcp mysql3307
四,进入docker的mysql容器
root@xushi:~# docker exec -it mysql3307 bashroot@435efc72d00f:/# mysql -u root -p123456mysql: [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 5Server version: 5.7.21 MySQL Community Server (GPL)Copyright (c) 2000, 2018, 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> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || netsharp || performance_schema || sys |+--------------------+5 rows in set (0.00 sec)mysql>
编号 | 命令及参数 | 说明 |
1 | docker exec | 进入容器 |
2 | -t | 让docker分配一个伪终端,并绑定到容器的标准输入上 |
3 | -i | 让容器的标准输入保持打开; |
4 | mysql3307 | 是容器的名字 |
exit退出docker的伪终端
五,mysqlworkbench访问mysql数据库
说明:
1.docker默认使用官方源,特别慢,这篇文章介绍如何使用阿里的镜像,特别快,https://www.cnblogs.com/hutuchong/p/7748429.html
2.mysql的这个镜像,客户端工具链接会报一个奇怪的错误,执行如下命令:ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
3.没有vim而且下载又很慢,如下方式解决
mv /etc/apt/sources.list /etc/apt/sources.list.bak echo "deb http://mirrors.163.com/debian/ jessie main non-free contrib" >/etc/apt/sources.list echo "deb http://mirrors.163.com/debian/ jessie-proposed-updates main non-free contrib" >>/etc/apt/sources.list echo "deb-src http://mirrors.163.com/debian/ jessie main non-free contrib" >>/etc/apt/sources.list echo "deb-src http://mirrors.163.com/debian/ jessie-proposed-updates main non-free contrib" >>/etc/apt/sources.list #安装更新源 apt-get update
4.还可以参考下面这篇