首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从容器内连接到远程mysql数据库

从容器内连接到远程mysql数据库
EN

Stack Overflow用户
提问于 2018-06-15 03:43:11
回答 3查看 1.9K关注 0票数 0

在我的本地计算机上,我可以使用mysql cli命令轻松地连接到mysql数据库的远程实例(假设设置了环境变量):

代码语言:javascript
复制
# mysql -u root -p$DB_PASSWORD -h $DB_HOST --port=$DB_PORT

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 7365
Server version: 5.7.14-google (Google)

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 |
+--------------------+
3 rows in set (0.05 sec)

然而,使用docker它就会挂起:

代码语言:javascript
复制
docker run --rm -it --name mysql -e MYSQL_RANDOM_ROOT_PASSWORD=yes mysql \
mysql -u root -p$DB_PASSWORD -h $DB_HOST --port=$DB_PORT

mysql: [Warning] Using a password on the command line interface can be insecure.
show databases;
^C
^C
exit
exit
^C

并且必须从另一个终端窗口执行docker container stop mysql才能退出mysql容器。

我甚至尝试附加到容器并运行相同的命令,但都无济于事。尝试连接到AWS MySQL实例和Google Cloud SQL实例,还尝试直接在命令行中输入变量,结果都相同。为什么它会挂起来,我怎么才能让它工作呢?谢谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-06-19 06:59:18

我使用的是Google Cloud SQL,需要将IP地址列入白名单才能允许访问。因此,添加这些解决了连接问题。

票数 0
EN

Stack Overflow用户

发布于 2018-06-15 08:30:34

尝试公开MySQL的默认端口

代码语言:javascript
复制
docker run -p 3306:3306 ...
票数 0
EN

Stack Overflow用户

发布于 2019-12-26 18:49:34

我尝试了所有的方法,但下面的步骤已经解决了我的问题。在容器内运行命令

dpkg-重新配置tzdata

它将要求您选择地理区域和时区,然后您就完成了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50864802

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档