2

I am coming from here:

How to run docker-compose with host network?

the problem I realized is that mysql_connect uses sockets when localhost is entered as host. As a result, php on my host system, cannot find the socket because that socket is in the container.

when I up with compose-docker I can read this

Version: '5.6.44' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server (GPL)

so what I thought I logically do, is to enter on my host, the mysql_connect socket configuration in php, to point to /var/run/mysqld/mysqld.sock

but that is in my container, so it cannot find it.

as a result, I tried mapping the container dir /var/run/mysqld to some host dir, so I can then use that host socket directory in my php.ini on my host.

But:

when I start up I get:

Can't start server : Bind on unix socket: Permission denied

Do you already have another mysqld server running on socket

so mapping the socket folder somehow gives read write problems to the docker container. Is there any solution to this?

my yml 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
      - /home/myuser/nmyapp_socket:/var/run/mysqld
11
  • Have you tried using "127.0.0.1" instead of "localhost"? That should make mysql connect on tcpip. Commented May 20, 2019 at 4:54
  • yes that works, that means as well that I now have to go and change 1000 files of the legacy app I am working on. So that's what I want to avoid Commented May 20, 2019 at 19:08
  • 1
    I understand your problem, but in my opinion is worth the effort. If you decide to move to containers and distributed applications then "localhost" has to disappear... It only causes problems. I hope for you that you will find a solution but I think it will be a temporary one and "localhost" will come to haunt you later on. Commented May 20, 2019 at 19:14
  • Setting "mysql.default_host" only in 1 place is also not an option? Commented May 20, 2019 at 19:21
  • @Mihai I can set mysql.default_host yes, but to what? if I add the socket from the container to the volumes, then it doesnt come up any longer saying the socket directory permission denied. Containers -> I use this for my personal setup. Nobody else in that company is using containers. Nobody else in that company has something else installed than the desired mysql version (which I dont have) Commented May 20, 2019 at 19:24

1 Answer 1

2

I just solved the same issue on CentOS 8. The MySQL process inside of the container does not have the permissions to write the sock-file.

  1. Do NOT map the socket file itself (e.g. mysqld.sock) - instead of this map the corresponding directory (e.g. /var/run/mysqld/) to the host system.

  2. Set the permissions for this directory (e.g. /var/run/mysqld/) correctly. Check the ownership of the databasedirectory on the host (in your case /home/myuser/nmyapp_db) and use the same user.

You will see - MySQL will start.

BUT:

/var/run/ points to /run and is a tmpfs - after a reboot the permissions are gone, MySQL will not start anymore.

I used am simple Trick:

systemctl edit docker

ExecStartPre=-mkdir /var/run/mariadb101/
ExecStartPre=chown systemd-coredump.input /var/run/mariadb101/

Change the path and username to match to your system.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.