gpt4 book ai didi

mysql - 无法从本地主机连接到 mysql 容器

转载 作者:太空宇宙 更新时间:2023-11-03 10:38:36 30 4
gpt4 key购买 nike

我正在尝试设置一个用于开发的 MySQL 容器。

所以我使用了docker-compose来设置它。

容器和 mysql 看起来不错。问题是我想从 DBeaver 客户端连接到它,但我找不到如何连接它。

这是我的docker-compose.yml:

version: '2'
services:
db:
image: 'mysql:5.7'
volumes:
- '~/dev/dbs-data/mysql:/var/lib/mysql'
restart: 'always'
expose:
- '3306'
ports:
- '3306:3306'
environment:
MYSQL_ROOT_PASSWORD: 'pass'
MYSQL_DATABASE: 'db'
MYSQL_USER: 'user'
MYSQL_PASSWORD: 'pass'

当我尝试从 DBeaver 连接它时,我得到:

java.sql.SQLException: null,  message from server: 
"Host '172.18.0.1' is not allowed to connect to this MySQL server"

更新

我没有尝试使用 IP 172.18.0.1 进行连接。我尝试使用 localhost:3306127.0.0.1:3306 进行连接,docker 给了它子 IP 0.0.0.0:3306/p>

更新

在我的 Mac 上成功连接后,我再次尝试在我的 Linux 上使用 DBeaver 并再次尝试: enter image description here

尝试连接其他工具,mysql workbench: enter image description here

最佳答案

正如您在官方图像文档中看到的那样:

MYSQL_ROOT_HOST : By default, MySQL creates the 'root'@'localhost' account. This account can only be connected to from inside the container, requiring the use of the docker exec command as noted under Connect to MySQL from the MySQL Command Line Client. To allow connections from other hosts, set this environment variable. As an example, the value "172.17.0.1", which is the default Docker gateway IP, will allow connections from the Docker host machine.

因此您必须使用地址 172.18.0.1 设置 MYSQL_ROOT_HOST 变量,您可以在错误消息中看到该地址。

关于mysql - 无法从本地主机连接到 mysql 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42880479/

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