内容纲要

在宿主机的 /mnt/src/docker-containers/wyrover-mysql/mysql-data 创建目录

在 wyrover-mysql 目录下创建 start-server.sh

docker run --name wyrover-mysql -v /mnt/src/docker-containers/wyrover-mysql/mysql-data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=password -d mysql:latest

启动后可在 mysql-data 目录下看到相关的数据库文件

然后在启一个 docker 容器连一下, 在 wyrover-mysql 目录下创建 start-client.sh

docker run -it --link wyrover-mysql: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"'

在宿主 bash 下运行

./start-client.sh

就登录到了 mysql,然后查看数据库

show databases;

整个过程

docker@default:/mnt/src/docker-containers/wyrover-mysql$ ./start-client.sh
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.16 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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> exit;
Bye
docker@default:/mnt/src/docker-containers/wyrover-mysql$

links

发表评论

电子邮件地址不会被公开。 必填项已用*标注