简体   繁体   中英

MySQL-workbench and Docker Can't connect to MySQL server on '127.0.0.1' (61)

Om Mac I'm using docker for my local web and I want to access mysql with MySQL-workbench but after I did install MySQL-workbench and try to connect to database I get this error message

Can't connect to MySQL server on '127.0.0.1' (61)

图像

I tried to find solution for docker but I didn't find any.

docker inspect pkbook_mysql_1

"NetworkSettings": {
            "Bridge": "",
            "SandboxID": "cb984bedd303958298621c1571482c7c06a2a326df021902c15a7955b8c26b2c",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "3306/tcp": null
            },
            "SandboxKey": "/var/run/docker/netns/cb984bedd303",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "publiquip_default": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "mysql",
                        "ee6b2dd46fb8"
                    ],
                    "NetworkID": "8809726a588595aea20989df3608ae6646f2e64aa928974469df34b52febe7a4",
                    "EndpointID": "f41cf7f7e42f4921393e13c7db9ce046c784c159ad0ff3c74d45ed788323d4b1",
                    "Gateway": "172.20.0.1",
                    "IPAddress": "172.20.0.4",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:14:00:04",
                    "DriverOpts": null
                }

docker ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
e34b4dbad1e3        pkbook_joomla    "/entrypoint.sh apac…"   4 days ago          Up 5 hours          0.0.0.0:80->80/tcp   pkbook_joomla_1
ee6b2dd46fb8        mysql:5.6           "docker-entrypoint.s…"   6 days ago          Up 5 hours          3306/tcp             pkbook_mysql_1
0ccd811b7955        pkbook_gulp      "docker-entrypoint.s…"   6 days ago          Up 5 hours                               pkbook_gulp_1

您需要使用 -p opt 选项发布 3306 端口。

docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -p 3306:3306 mysql 

Instead of host 127.0.0.1 try to type container name and port. If it doesn't work.

In terminal type docker ps . (If You didn't gave the name during container creation, docker will name it by random name). If container is running You should see it on the list.

Then check IP and PORT:

docker inspect [CONTAINER-NAME]

and find under:

"NetworkSettings":

                "3306/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "33082"
                    }
                ],
                "33060/tcp": null
            },

So, in this situation it's 0.0.0.0:33082

You need to set the allow login from any ip with MYSQL_ROOT_HOST=%

Full docker command line as follows:

>docker run --name mysql-server -e MYSQL_ROOT_HOST=% -e MYSQL_ROOT_PASSWORD=12345 -p 3306:3306 -d mysql/mysql-server

--name means that it's container name

-d means that it's mysql docker image

When docker status is Up 3 minutes (healthy) you can easy connect to DB via mysql Workbench

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM