1

I was reading a, here, here, here, here, here

none of which answers my question.

What I want to do?

I have a mysql docker-compose image and want to connect to it from my ubuntu host using

localhost:3306

this does not work

it does work if I use

0.0.0.0:3306

which is not what I want to do. Why do I want to do all of that? because I have to start to work on an oooold legacy app, that has an old mysql version. Now I have mysql 8.0 on my computer and dont want to downgrade just for that one project. The legacy code has about 1000 references to localhost:3306 in it. Now I could refactor all that, create a config file etc... but better would be, if I could make it work so that localhost:3306 actually accesses my mysql docker-compose container. Is that possible? What do i have to add to my docker-compose yaml file?

my mysql docker-compose yaml file is this:

version: '3.3'
services:
  sciodb:
    container_name: sciodb
    image: mysql:5.6
    restart: always
    environment:
      MYSQL_DATABASE: 'db'
      # So you don't have to use root, but you can if you like
      MYSQL_USER: 'myuser'
      # You can use whatever password you like
      MYSQL_PASSWORD: 'test1234'
      # Password for root access
      MYSQL_ROOT_PASSWORD: 'test1234'
    ports:
      # <Port exposed> : < MySQL Port running inside container>
      - '3306:3306'
    expose:
      # Opens port 3306 on the container
      - '3306'
      # Where our data will be persisted
    volumes:
      - /home/myuser/nmyapp_db:/var/lib/mysql
      - /media/sf_vmwareshare:/var/vmwareshare
Toskan
  • 13,911
  • 14
  • 95
  • 185
  • Is this your whole yml file? Is there a network portion? By default the docker should run in bridge mode and since you've exposed the port to mysql (and assigned to a network?), you should be able to connect via localhost:3306. Is Everything else configured properly within the mysql config files to allow remote connections in /home/myuser/nmyapp_db? for reference, I have a webapp running in a container on a centOS server on port 7878 and curl localhost:7878 returns the webpage. Can you connect via telnet to localhost:3306? What does docker ps show for the container? – user1628449 May 19 '19 at 04:18
  • Did you try to connect your DB from your system or a other container? For the last case: Did you tried `network_mode: host` in your [docker-compose](https://docs.docker.com/compose/compose-file/#network_mode)? – Wie May 19 '19 at 07:18
  • @user1628449 this is my whole yml file, no network portion. If I run `telnet localhost 3306` I get asked "`mysql_native_password`" so it seems to work. It can be that the old php 5.6 `mysql_connect` somehow cannot do it for some weird reason. Curl works the same from command line.... @Wie I tried to connect from my own system to the docker container. `network_mode: host` would need to be on what level? I put it directly into the service e.g. sciodb. But still get `No such file or directory` when trying to connect via php to mysql – Toskan May 19 '19 at 19:08
  • @user1628449 how is that a remote connection? btw I noticed I can connect with phpmyadmin, just not `mysql_connect` – Toskan May 19 '19 at 19:14
  • where does the app that is running php live? In another container or on the host machine? localhost within the container is not loaclhost on the host machine as the docker network has it's own subnet. Also, what error is php throwing? – user1628449 May 20 '19 at 01:26
  • @user1628449 the app runs on the host. mysql_connect returns false, and then accessing `mysql_errno()` "No such file or directory" `mysql_error()` is `2002` – Toskan May 20 '19 at 01:34
  • I think this is more of a php/mysql error than docker at this point. See this post for reference: https://stackoverflow.com/questions/1676688/mysql-connection-not-working-2002-no-such-file-or-directory Something is misconfigured with mysql – user1628449 May 20 '19 at 01:48
  • you could also try ports: - 127.0.0.1:3306:3306 Not sure if it would help though as the port is open and mysql is responding – user1628449 May 20 '19 at 01:53
  • @user1628449 `127.0.0.1` does work if I use it in the php files but is not what I need... you mean in the yml config? – Toskan May 20 '19 at 01:56
  • yes, in the yml. It binds the port to only 127.0.0.1 (only the localhost can access). In the tread I linked someone advised it worked for them to change in the mysql config files. – user1628449 May 20 '19 at 01:59
  • @user1628449 doesnt seem to work. What I noticed, when I start up `docker-compose up` I can read "Version: '5.6.44' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server (GPL)" so `/var/run/mysqld/mysqld.sock` is the socket of the container. But that socket I cannot reach from outside of the container. If I try to map a volume, it complains about the socket being in use already – Toskan May 20 '19 at 03:27
  • looks like I dont understand the volume mapping. I am trying to map a folder of the container to the host, but doesn't seem to be so straight forward – Toskan May 20 '19 at 03:34
  • @user1628449 interestingly: "Note: Whenever you specify "localhost" or "localhost:port" as server, the MySQL client library will override this and try to connect to a local socket (named pipe on Windows). If you want to use TCP/IP, use "127.0.0.1" instead of "localhost". If the MySQL client library tries to connect to the wrong local socket, you should set the correct path as in your PHP configuration and leave the server field blank." https://stackoverflow.com/questions/22135897/access-docker-socket-within-container/33183227 – Toskan May 20 '19 at 03:37
  • - /home/myuser/nmyapp_db:/var/lib/mysql will map the /var/lib/mysql folder within the docker container to /home/myuser/nmyapp_db on the host. Does ls /home/myuser/nmyapp_db show you anything? Your configs should be housed here and any modifications to these files in the /home directory will impact the docker. Example, echo 'hi mom' >> /home/myuser/nmyapp_db/mom.txt, mom.txt will show in the /var/lib/mysql folder when the docker is launched. – user1628449 May 20 '19 at 10:52
  • @user1628449 yes I think so, but the socket is in a different directory, and when I add that directory to the yml, to docker-compose up fails to get up – Toskan May 20 '19 at 19:07

0 Answers0