下载 mysql 镜像
docker pull docker.io/mysql
启动 mysql 容器
docker run -d -p 3306:3306 --name mysql-test\
-e MYSQL_ROOT_PASSWORD=123 mysql
注意: -e MYSQL_ROOT_PASSWORD=123 是设置 mysql 的 root 密码为 123 的意思,这里 root 密码一定要设置。
客户端连接 mysql
docker run -it --link mysql-test:mysql --rm mysql sh\
-c 'exec mysql -h"$MYSQL_PORT_3306_TCP_ADDR"\
-P"$MYSQL_PORT_3306_TCP_PORT"\
-uroot -p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD"'
结果为:
mysql: [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 3
Server version: 5.7.19 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql>
可以在 mysql> 提示符下执行 mysql 的命令,输入 quit 退出 mysql 客户端后,所在的容器会一起删除。