一、使用官方镜像
可以使用官方镜像快速启动一个MySQL Server实例:
root@ubuntu:~# docker run --name hi-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -d mysql:latest
Unable to find image 'mysql:latest' locally
latest: Pulling from library/mysql
f7e2b70d04ae: Pull complete
df7f6307ff0a: Pull complete
e29ed02b1013: Pull complete
9cb929db392c: Pull complete
42cc77b24286: Pull complete
a6d57750cc73: Pull complete
79510826e343: Pull complete
0bff56f688ad: Pull complete
b7900ad0eff9: Pull complete
9ffa71cbd4ba: Pull complete
3d744b02cc8e: Pull complete
75b0db54e16c: Pull complete
Digest: sha256:4589ba2850b93d103e60011fe528fc56230516c1efb4d3494c33ff499505356f
Status: Downloaded newer image for mysql:latest
bcbbb3308927d9be924a91b1dd4c758e24cd5c9658ac84174a27a4c5cbe9d743
以上命令中,--name指定容器的名称,容器名称是hi-mysql;-e指定配置信息,此处配置mysql
的root用户
的登陆密码;-p是端口映射;-d指定成功启动容器后输出容器的完整ID。使用docker ps查看运行中的容器:
root@ubuntu:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bcbbb3308927 mysql:latest "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp hi-mysql
最后,使用navicat即可连接mysql: