gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 17:27:44 26 4
gpt4 key购买 nike

从我的本地计算机,我可以使用 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容器停止mysql才能退出mysql容器。

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

最佳答案

尝试公开 MySQL 的默认端口

docker run -p 3306:3306 ...

关于mysql - 从容器内连接到远程 mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50864802/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com