gpt4 book ai didi

mysql - docker + MySQL 8 : connection issues

转载 作者:行者123 更新时间:2023-12-02 19:49:58 26 4
gpt4 key购买 nike

Docker 中的 MySQL 无法连接。

我已经查看了其他帖子,但我找不到我做错了什么。使用 MySQL 5.6 它连接良好。但我想使用 MySQL 8。

这是我的 docker-compose.yml 的摘录文件:


version: '2'

# ...other services and...

mysql:
container_name: mysql
image: mysql:8.0
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: "the_database"
MYSQL_USER: "my_user"
MYSQL_PASSWORD: "some_password_123"
MYSQL_ROOT_PASSWORD: "some_root_password123"
volumes:
- ./data/mysql:/var/lib/mysql
- ./data/mysql-dumps:/var/mysql-dumps
# Tried this command to use mysql_native_password but still cant log in
command:
- "--default-authentication-plugin=mysql_native_password"

我可以进入容器并尝试登录:

$ docker container exec -it mysql /bin/bash

$ root@7b4d6ada50fb:/# mysql -h localhost -u my_user -p
Enter password:
ERROR 1045 (28000): Access denied for user 'my_user'@'localhost' (using password: YES)
root@7b4d6ada50fb:/#

我是 非常肯定密码被正确插入。但没有成功。看到我正在尝试连接甚至在容器内部。端口是“绑定(bind)”3306:3306。找不到其他东西来检查它。非常感谢任何帮助。

当运行我的 docker container inspect mysql这些是一些摘录:


"Env": [
"MYSQL_DATABASE=the_database",
"MYSQL_USER=my_user",
"MYSQL_PASSWORD=some_password_123",
"MYSQL_ROOT_PASSWORD=some_root_password123",
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"GOSU_VERSION=1.12",
"MYSQL_MAJOR=8.0",
"MYSQL_VERSION=8.0.19-1debian10"
],

# ... and network:
"NetworkSettings": {
"Bridge": "",
"SandboxID": "873ee1fe38431b8b2c2a55e3b7c1b1649e14c5c8946c00e65820be72a0ff5b73",
"HairpinMode": false,
"LinkLocalIPv6Address": "",
"LinkLocalIPv6PrefixLen": 0,
"Ports": {
"3306/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "3306"
}
],
"33060/tcp": null
},
"SandboxKey": "/var/run/docker/netns/873ee1fe3843",
"SecondaryIPAddresses": null,
"SecondaryIPv6Addresses": null,
"EndpointID": "",
"Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"IPAddress": "",
"IPPrefixLen": 0,
"IPv6Gateway": "",
"MacAddress": "",
"Networks": {
"project_default": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"mysql",
"7b4d6ada50fb"
],
"NetworkID": "2f6e5947d747fb8518628361660193bce331efe2d79ce86e3e7f5cacf81ce26a",
"EndpointID": "8bc41562ce39de01cd4162cc5c9b9cb42b1a4f8eed8af884a31766b64eda8199",
"Gateway": "172.18.0.1",
"IPAddress": "172.18.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:12:00:02",
"DriverOpts": null
}
}
}

最佳答案

我知道它在做什么。

docker-compose 创建自己的网络。因此,正如我在上面的问题中发布的那样,使用检查的网络结果:

"Networks": {
"project_default": {
"IPAMConfig": null,
"Links": null,
"Aliases": [
"mysql", <- HERE!!
"7b4d6ada50fb"
],

我决定在字符串连接中尝试该别名,如下所示:

mysql -h mysql -u my_user -p

太棒了!我可以连接到容器!这可能是因为 MySQL 所做的比较与用户和每个用户可以或不能访问的主机有关。但我对此并不完全确定。

我很感谢任何人都可以提供的任何其他信息。

而且,由于我使用的是 Laravel,我在我的 .env 中做了同样的事情。文件:

DB_CONNECTION=mysql
DB_HOST=mysql <- HERE!!
DB_PORT=3306
DB_DATABASE=your_db_name
DB_USERNAME=your_user
DB_PASSWORD=your_password

关于mysql - docker + MySQL 8 : connection issues,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61434512/

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