Can't connect to mysql in docker container from phpbb

1.1k Views Asked by At

My docker container is working nicely, and I can connect to my database using my graphical sql client.

However when I try to connect to my phpbb instance, I get this error:

General Error SQL ERROR [ mysqli ]

No such file or directory [2002]

An sql error occurred while fetching this page. Please contact an administrator if this problem persists.

The phpbb config looks like this:

$dbms = 'mysqli';
$dbhost = 'localhost';
$dbport = '';
$dbname = 'xxxxx_xxxx';
$dbuser = 'xxxxxxxxx';
$dbpasswd = 'xxxxxx';

Would the file or directory it can't find relate to the mysql.so? If so where would I set this in the docker configs?

It not then does anyone know what's going on?

Edit for clarity:

My docker-compose.yml looks like the below and I am running it with 'docker-compose up'

php: image: webdevops/php-nginx links: - db:database volumes: - "/home/xxx/code/mytest:/mytest-now" ports: - 80:80 environment: - WEB_DOCUMENT_ROOT=/mytest/public

db:
  image: mariadb:latest
  volumes:
    - "mytest-db:/var/lib/mysql"
  ports:
    - "3306:3306"
  environment:
    MYSQL_ROOT_PASSWORD: admin
    MYSQL_DATABASE: dbname
    MYSQL_USER: user
    MYSQL_PASSWORD: password
2

There are 2 best solutions below

1
On

The reason your connection is not working is the use of localhost. I assume you ran your mysql container either through docker-compose or docker and mapped port 3306 form host to 3306 on container.

Now using the graphical GUI you use localhost:3306 and it works, this is because your forwarded the docker port to the host port.

But your phpbb container the localhost refers to the container itself and hence the connection cannot be made. So there few ways to get it working

Host IP

Use the host IP which where you have mapped the port 3306

$dbhost = '192.168.0.102';

This is not a recommended approach as your host IP will change when you connect to different networks

Use mysql container name

Run your mysql container with a --name mysqldb and use the this name in the connection

$dbhost = 'mysqldb';

Use mysql service name

If you use docker-compose and added mysql as a service

version: '3'
services:
  mysqldb2:
    image: mysql
    environment:
      - MYSQL_ROOT_PASSWORD=root
  phpbb:
    .....

Then you can use the service name in your connection settings

$dbhost = 'mysqldb2';
0
On

After running 'docker inspect containername' I noticed an ip address under: NetworkSettings.Networks.bridge.gateway.

When I tried that, it worked!