0

After going through the following links from SOF- I still am facing the issue and there is no reasoning or solution available.

References:

Mysql 5.7 is running as container in my local machine. Once the server is up, connecting to the server using root or the new user created fails over localhost/0.0.0.0/127.0.0.1. Even after disabling the firewald, the connectivity failed with the same error. I might have done something terribly wrong about this connectivity. Any advice would be helpful.

version: '3.5'
services:
  service:
    image: mysql:5.7
    #build:
    #  context: ./
    #  dockerfile: Dockerfile
    environment:
      MYSQL_USER: user
      MYSQL_PASSWORD: manage
      MYSQL_ROOT_PASSWORD: manage
      MYSQL_DATABASE: testapp
    ports:
      - published: 3306
        target: 3306

Following is the output after docker-compose up -d

docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
45ba02e6e457        mysql:5.7           "docker-entrypoint.s…"   52 seconds ago      Up 50 seconds       0.0.0.0:3306->3306/tcp, 33060/tcp   

Below are the attempts to connect to the mysql container.

mysql -u root -h localhost -pmanage
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

mysql -u root -h 0.0.0.0 -pmanage
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

mysql -u root -h 127.0.0.1 -pmanage
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

IP of the running container works fine. But this is not a solution as the IP can change with time.

mysql -u root -h 172.31.0.2 -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.30 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

I am not looking for solutions with another docker command. How to connect to mysql container from docker host using mysql client.

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
nashter
  • 1,181
  • 1
  • 15
  • 33
  • Have you tried with the ip address of the running container? (`docker inspect `) – Christophe De Troyer Jun 01 '20 at 19:57
  • Yes. With IP address of the running the mysql container, connection works fine. – nashter Jun 01 '20 at 19:59
  • I'm not 100% sure, but the problem is probably related due to the local mysql trying to connect over the socket file, instead of using a network socket. Can you try binding the socket to the local machine? (add volume `- /var/run/mysqld/mysqld.sock:/var/run/mysqld/mysqld.sock`) – Christophe De Troyer Jun 01 '20 at 20:03
  • Ok. Tried and I got this error. "ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (13)" – nashter Jun 01 '20 at 20:07
  • Just found this duplicate: https://stackoverflow.com/questions/33001750/connect-to-mysql-in-a-docker-container-from-the-host – Christophe De Troyer Jun 01 '20 at 20:10
  • 2
    Does this answer your question? [Connect to mysql in a docker container from the host](https://stackoverflow.com/questions/33001750/connect-to-mysql-in-a-docker-container-from-the-host) – Christophe De Troyer Jun 01 '20 at 20:10
  • `0.0.0.0:3306->3306/tcp, 33060/tcp` the `33060` seems suspect to me here. Have you made a tweak in your MySQL to make it listen to `33060` rather than `3306` maybe? – β.εηοιτ.βε Jun 01 '20 at 20:11
  • Yes @ChristopheDeTroyer. Had set bind-address= 0.0.0.0 in /etc/my.cnf to make mysql listen over all interfaces . – nashter Jun 01 '20 at 20:15
  • No @β.εηοιτ.βε. Made changes to mysqld. The change is bind-address= 0.0.0.0. No other change related to 33060 – nashter Jun 01 '20 at 20:16
  • Mhm, ok, and so answering myself: MySQL image does expose `33060`, interesting. https://github.com/docker-library/mysql/blob/master/5.7/Dockerfile#L89 – β.εηοιτ.βε Jun 01 '20 at 20:19

1 Answers1

1

You need to use the --protocol=tcp in your connection url.

mysql -u root -h localhost --protocol=tcp

Your mysql is running inside the docker container. So, you have to connect through TCP and local socket is not available for that. This is basically duplicate of this question.

techtabu
  • 23,241
  • 3
  • 25
  • 34
  • With a standard mysql:5.7, this works for root with --protocol. But when a custom cnf file is added (to alter max_packet_size), this approach does not work. – nashter Jun 02 '20 at 13:59