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
Toskan
  • 13,911
  • 14
  • 95
  • 185
  • Have you tried using "127.0.0.1" instead of "localhost"? That should make mysql connect on tcpip. – Mihai May 20 '19 at 04: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 – Toskan May 20 '19 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. – Mihai May 20 '19 at 19:14
  • Setting "mysql.default_host" only in 1 place is also not an option? – Mihai May 20 '19 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) – Toskan May 20 '19 at 19:24
  • I'm no php developer so I rely on the documentation here: https://www.php.net/manual/en/function.mysql-connect.php. According to this if you set mysql.default_host to 127.0.0.1 then it will not take the default which is localhost. – Mihai May 20 '19 at 19:27
  • Also if changing lots of files in an application is a problem, maybe you can fix that with a simple "sed" command? The thing is the hybrid way will not work here. The socket isn't really meant to be accessed from outside the container. It does work the other way round. Another crazy idea... why don't you add your application in the same container with the database? very ugly... but it would work. – Mihai May 20 '19 at 19:32
  • > set mysql.default_host to 127.0.0.1 I wonder why I didn't have that idea, I was trying to set it to some file only... I will try that sed> yeah right, it's usually not a good idea to change 1000 files with a sed command thinking it will work. Optimally, if I change it, I would set it to a configuration file. Changing everything is one thing, but you have to retest it again. If I change it, then properly changing it is a good idea. – Toskan May 20 '19 at 19:35
  • @Mihai so doesnt seem to work with default host either. I will actually give up now and try to refactor all pages. Thanks a lot for your efforts, I appreciate it a lot – Toskan May 20 '19 at 22:49
  • Sorry couldn't help more. Putting everything in 1 container will definitely solve your issues so give it a thought. – Mihai May 21 '19 at 04:14
  • If you have to change 1000 files, the app is written wrong. There should be only 1 file that contains stuff like global configuration information. Also, and more importantly, when you have an app running in Docker containers, you no longer need to think about networking, Docker handles all of that for you since it uses its own internal network and handles its own DNS resolution. An app running in container A can connect to a server in container B by simply using the container name as the hostname. – Aquarelle Mar 18 '20 at 01:28

1 Answers1

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
slemke
  • 41
  • 3