![](/img/trans.png)
[英]Can't connect to a remote mysql database from php within a docker container
[英]Connect to remote mysql database from within a container
从我的本地计算机,我可以使用 mysql cli 命令轻松连接到 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.
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 它只是挂起:
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 实例,还尝试直接在命令行中输入变量,结果都相同。 为什么它挂了,我怎样才能让它工作? 谢谢!
尝试公开 MySQL 的默认端口
docker run -p 3306:3306 ...
我使用的是 Google Cloud SQL,需要将 IP 地址列入白名单以允许访问。 所以添加解决了连接问题。
我尝试了所有方法,但下面的步骤解决了我的问题。 在容器内运行命令
dpkg-重新配置 tzdata
它会要求您选择地理区域和时区,然后您就完成了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.