MySQL在Docker容器中不接受凭据。

huangapple go评论47阅读模式
英文:

Mysql in docker container doesn't accept credentials

问题

我有一个Dockerfile,之后我想将一些初始的SQL转储文件导入其中:

FROM mysql:debian

EXPOSE 3306

ENTRYPOINT ["docker-entrypoint.sh"]

CMD ["mysqld"]

然后是我的docker-compose.yaml

version: '3'

services:
  mysql:
    container_name: ${CONTAINER_NAME}
    build:
      context: .
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=${ROOT_PASSWORD}
      - MYSQL_USER=${MYSQL_USER}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
      - MYSQL_DATABASE=${MYSQL_DATABASE}

我可以使用docker exec -it sqlcontainer mysql -uroot -p成功进入我的容器(也可以使用$MYSQL_USER),但当我尝试从外部执行相同的操作,例如 mysql -uroot -p,我总是得到ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

我想不出问题出在哪。容器内部与mysqld的通信显然正常,否则我将不会收到任何响应。我在Windows上,如果这对任何人有帮助的话。但上述情况对PowerShell、CMD和甚至Git Bash都适用。

英文:

I have a Dockerfile which I later want to feed some initial sql dump into:

FROM mysql:debian

EXPOSE 3306

ENTRYPOINT ["docker-entrypoint.sh"]

CMD ["mysqld"]

Then there is my docker-compose.yaml:

version: '3'

services:
  mysql:
    container_name: ${CONTAINER_NAME}
    build:
      context: .
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=${ROOT_PASSWORD}
      - MYSQL_USER=${MYSQL_USER}
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
      - MYSQL_DATABASE=${MYSQL_DATABASE}

I can docker exec -it sqlcontainer mysql -uroot -p into my container just fine (works with the $MYSQL_USER as well), but when I try to do the same from outside, e.g. with mysql -uroot -p, I always get ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES).

I'm out of my wits. What am I doing wrong? The communication to mysqld inside the container apparently works, otherwise I wouldn't get back any response.
I'm on Windows, if that's a hint for anyone. But the above is true for PowerShell, CMD and even Git Bash.

答案1

得分: 1

很尴尬地承认,但评论者 @RiggsFolly 和 @DavidMaze 都说得对:我已经有一段时间没有在我的机器上使用 MySQL 了。当我检查任务管理器时,我确实发现一个本地的 mysqld 进程已经在运行,当然使用了不同的凭据。在终止该进程(并在Compose文件中添加丢失的端口)后,我成功连接到了我的容器化实例。

希望这将来能帮助到其他人,他们在将来也会掉入同样的陷阱。

英文:

It is embarrassing to admit, but both commenters @RiggsFolly and @DavidMaze are right in what they said: I hadn't used MySQL for quite a while on my machine. When I checked the task manager, I indeed found a local mysqld running already, which of course was primed with different credentials. After killing the process (and adding the missing port in the compose file), I was able to connect to my containerized instance.

Hope this will help out somebody else in the future who falls into the same trap.

huangapple
  • 本文由 发表于 2023年6月2日 00:15:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76383862.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定