我正在设置 CakePHP 3.7 应用程序并使用 docker compose。我也有一个 mysql 服务,我正在尝试连接,但我收到此错误:Access denied for user 'ws_user'@'172.20.0.3' (using password: YES)
我向用户授予权限,如下所示:GRANT ALL PRIVILEGES ON mydb.* TO 'ws_user'@'%' IDENTIFIED BY '<superSecretPasswordHere>'
.
如果我使用 root 凭据,cakephp 能够很好地建立连接。
我还将端口 3030 上的 mysql 服务公开到我的本地计算机,并且我能够很好地使用 ws_user 凭据进行连接。
我还使用相同的凭据设置了在本地计算机上运行的 mysql,并且 cake 能够连接到主机 172.17.0.1
也很好。
我很困惑可能是什么问题。看起来确实是一个权限问题(因为错误消息),但我可以通过命令行通过暴露的端口进行连接。我的下一个想法是,这可能是因为密码中存在特殊字符,但是,如果我连接到在我的主机上运行的 mysql,它可以使用相同的密码正常工作。
这是我的 docker-compose 文件:
version: '2'
# define all services
services:
# our service is called CakePHP ;-)
cakephp:
# we want to use the image which is build from our Dockerfile
build:
context: .
dockerfile: Dockerfile
# apache is running on port 80 but we want to expose this to port 4000 on our local machine
ports:
- "80:80"
# we are depending on the mysql backend
depends_on:
- mysql
# we mount the working dir into the container, handy for development
volumes:
- .:/var/www/html/
environment:
- SECURITY_SALT
- MYSQL_HOST
- MYSQL_USERNAME
- MYSQL_PASSWORD
mysql:
# we use the mysql base image, version 5.6.36
#image: mysql:5.6.39
build:
context: .
dockerfile: Dockerfile.mysql
ports:
- "3030:3006"
# we mount a datavolume to make sure we don't lose data
volumes:
- mysql_data:/var/lib/mysql
# setting some envvars to create the DB
environment:
- MYSQL_ROOT_PASSWORD
- MYSQL_DATABASE
volumes:
mysql_data:
最佳答案
从“cakephp”连接到“mysql:3306”。这应该在您的连接字符串中。
您可以从您的主机连接到“127.0.0.1:3030”以验证您的数据库是否接受远程登录。
然后您应该检查凭据是否相同。我建议您将它们放入 .env 文件中,然后通过“复制粘贴”值来测试连接。
您可以通过运行以下命令来检查实际传递给容器的值:
docker-compose config
这会向您显示将发送到 docker 引擎的 docker-compose 文件的确切版本。
希望这有效,否则请给我评论。
关于mysql - 无法连接到数据库: Access denied for user,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55485934/