用 Docker 部署 MySQL

下载 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 客户端后,所在的容器会一起删除。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容