4

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)

img

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
wbdevlop
  • 95
  • 1
  • 9

3 Answers3

7

You need to publish the 3306 port with the -p opt option.

docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -p 3306:3306 mysql 
Thomas Martin
  • 678
  • 8
  • 19
  • I typed : docker run -p 3306:3306 mysql on docker but i get Failed to Connect to MySQL at 127.0.0.1:3306 with user pkbook – wbdevlop Sep 03 '19 at 18:46
  • From where are you failing to connect ? Your host machine or another container ? – Thomas Martin Sep 04 '19 at 08:18
  • Try to connect with root. The password is set by an env variable when mysql image is run like this : docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -p 3306:3306 mysql – Thomas Martin Sep 04 '19 at 14:58
  • I did trying to connecting using root that too not working – wbdevlop Sep 04 '19 at 14:59
  • Can you run this command please ? sudo lsof -iTCP -sTCP:LISTEN -n -P – Thomas Martin Sep 04 '19 at 15:10
  • `COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME com.docke 446 Lotfi 7u IPv4 0x1816959b5bbf7377 0t0 TCP 127.0.0.1:49218 (LISTEN) com.docke 450 Lotfi 15u IPv6 0x1816959b5bc04437 0t0 TCP *:80 (LISTEN) Code\x20H 1232 Lotfi 41u IPv4 0x1816959b5b8bf377 0t0 TCP 127.0.0.1:10288 (LISTEN)` – wbdevlop Sep 04 '19 at 15:21
  • You don't seem to have a Mysql listening to 3306, please make sure you publish the 3306 port from docker as mentionned in my answer. – Thomas Martin Sep 04 '19 at 15:57
  • you command give this message: `error: database is uninitialized and password option is not specified You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD` – wbdevlop Sep 04 '19 at 16:44
  • Well yes you have the solution in one of the comments I gave. I'll edit the answer for more clarity. – Thomas Martin Sep 04 '19 at 18:40
  • I used your code but seems nothing work that what I did `docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=pkbook -p 3306:3306 mysql` – wbdevlop Sep 04 '19 at 19:11
  • Give us the lsof and the connection result now. – Thomas Martin Sep 04 '19 at 19:13
  • `Failed to Connect to MySQL at 127.0.0.1:3306 with user pkbook` I did on docker `I did docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=pkbook -p 3306:3306 mysql` – wbdevlop Sep 04 '19 at 19:17
  • Connect as root, not pkbook. Please give a bit more attention to answers you get, I'm kind of losing patience. Thanks. – Thomas Martin Sep 04 '19 at 19:19
  • `Failed to Connect to MySQL at 127.0.0.1:3306 with user root` – wbdevlop Sep 04 '19 at 19:24
  • I used your code exact except I changed the password `docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=pkbook -p 3306:3306 mysql` – wbdevlop Sep 04 '19 at 19:25
  • Is the error code still (61) ? Does the lsof command shows your mysql now ? – Thomas Martin Sep 04 '19 at 19:27
  • I get this error `Access denied for user 'pkbook'@'172.17.0.1' (using password: YES)` My database is pkbook the user is pkbook and password is pkbook why I use root – wbdevlop Sep 04 '19 at 19:33
  • You just run a mysql docker image, it does not have pkbook user. Connect as root and then create the user(s) you need. The error you quoted is still related to this non-existent pkbook user. Run my command, connect as root, if you have trouble post the entire error message and the lsof command result. – Thomas Martin Sep 04 '19 at 19:38
  • it access but my database pkbook isn't there for me I was trying to open my database pkbook – wbdevlop Sep 04 '19 at 20:17
  • Well of course there is not your database, nor your pkbook user, you just created a new container, hence a new mysql instance. This is now a different question, your connection problem is resolved, you can accept my answer. About persisting data with a mysql container, you'll need to mount a volume, see this [post] (https://stackoverflow.com/questions/39175194/docker-compose-persistent-data-mysql) for instance. – Thomas Martin Sep 04 '19 at 20:38
  • I did Accept your answer – wbdevlop Sep 04 '19 at 20:40
0

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

fr4gm3nt
  • 24
  • 5
  • I edit my question and posted result from docker inspect pkbook_mysql_1 – wbdevlop Sep 03 '19 at 20:51
  • Looks like You're missing publishing the ports. Pretty much what Thomas Martin said but he called it exposing the ports. Which is not the same. https://stackoverflow.com/questions/22111060/what-is-the-difference-between-expose-and-publish-in-docker If this won't help You please write how You start the container? By docker commands or docker-compose? – fr4gm3nt Sep 04 '19 at 07:52
-1

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

phancuongviet
  • 311
  • 2
  • 11