以下操作使用root用户操作,所以执行语句前没加sudo
1、搜索版本
docker search mysql
root@kobe:/opt/software/mysql# docker search mysqlNAME DESCRIPTION STARS OFFICIAL AUTOMATEDmysql MySQL is a widely used, open-source relation… 5507 [OK] mariadb MariaDB is a community-developed fork of MyS… 1703 [OK] mysql/mysql-server Optimized MySQL Server Docker images. Create… 378 [OK]percona Percona Server is a fork of the MySQL relati… 311 [OK] hypriot/rpi-mysql RPi-compatible Docker Image with Mysql 76 zabbix/zabbix-server-mysql Zabbix Server with MySQL database support 67 [OK]centurylink/mysql Image containing mysql. Optimized to be link… 57 [OK]zabbix/zabbix-web-nginx-mysql Zabbix frontend based on Nginx web-server wi… 40 [OK]tutum/mysql Base docker image to run a MySQL database se… 30 1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 22 [OK]schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 16 [OK]centos/mysql-57-centos7 MySQL 5.7 SQL database server 15 mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 15 linuxserver/mysql A Mysql container, brought to you by LinuxSe… 13 bitnami/mysql Bitnami MySQL Docker Image 11 [OK]openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 image… 6 centos/mysql-56-centos7 MySQL 5.6 SQL database server 6 dsteinkopf/backup-all-mysql backup all DBs in a mysql server 3 [OK]frodenas/mysql A Docker Image for MySQL 3 [OK]circleci/mysql MySQL is a widely used, open-source relation… 2 cloudfoundry/cf-mysql-ci Image used in CI of cf-mysql-release 0 openzipkin/zipkin-mysql Mirror of https://quay.io/repository/openzip… 0 cloudposse/mysql Improved `mysql` service with support for `m… 0 [OK]astronomerio/mysql-sink MySQL sink 0 [OK]inferlink/landmark-mysql landmark-mysql 0 [OK]
2、下载镜像
docker pull mysql
3、查看镜像
docker images
root@kobe:/opt/software/mysql# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEmysql 5.6 15a5ee56ec55 11 days ago 299MB
4、创建运行一个实例
docker run --name first-mysql -p 3308:3306 -e MYSQL\_ROOT\_PASSWORD=123456 -d mysql
run 运行一个容器
--name 给容器起的一个名字,用于区分 -p 3308:3306 表示在这个容器中使用3306端口映射到本机的端口号也为3308 -d 表示使用守护进程运行,即服务挂在后台5、查看运行状态
docker ps
root@kobe:/opt/software/mysql# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES688e83c55129 mysql "docker-entrypoint.s…" 25 hours ago Up 25 hours 0.0.0.0:3308->3306/tcp first-mysql
6、安装一个mysql-client
apt-get install mysql-client-core-5.6
7、连接数据库
mysql -h192.168.163.170 -P3308 -uroot -p123456
-h后加主机IP -P加端口(大写的P,本地端口) -u加username -p加password
**有人问到重启了虚拟机后怎么再次使用之前的容器,这里简单说一下操作
首先使用docker ps看下是否是运行状态,如果没显示则不是运行状态,接着就用docker ps -a,找到该容器的NAMES(这里示例容器的NAMES是first-mysql)。执行
docker start first-mysql
启动容器后,然后执行
docker exec -it first-mysql bash
就可以进入容器操作了(退出容器用exit)。
启动后也可以直接操作第七步连接数据库操作。