ThingEngineer / PHP-MySQLi-Database-Class

Wrapper for a PHP MySQL class, which utilizes MySQLi and prepared statements.
Other
3.3k stars 1.34k forks source link

Use several database connections in one docker container #1010

Closed blood73 closed 6 months ago

blood73 commented 1 year ago

I tried to use several databases with docker container. In the PHP I have:

$db = new MysqliDb(
    $config['db_host'],
    $config['db_user'],
    $config['db_password'],
    $config['db_name'],
    '4307'
);

$db->addConnection('ua', [
    'host' => $config['db_host'],
    'username' => $config['db_user'],
    'password' => $config['db_password'],
    'db'=> $config['db_name_ua'],
    '4307'
]);

In docker-compose.yml I have this:

mysql-5.7:
    platform: linux/amd64
    image: mysql:5.7
    container_name: mysql-5.7
    restart: unless-stopped
    volumes:
      - ./mysql-5.7/data:/var/lib/mysql
      - ./mysql-5.7/conf.d:/etc/mysql/conf.d
      - ./mysql-5.7/logs:/var/log/mysql/
      - ./mysql-5.7/dump:/dump
    ports:
      - "${MYSQL_5_7_PORT}:3306"
    security_opt:
      - seccomp:unconfined
    environment:
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      TZ: ${WORKSPACE_TIMEZONE}
    networks:
      - backend

So, it works with phpmyadmin or adminer. I can see 2 databases, I can work with them. But from PHP I receive this error mysqli::__construct(): (HY000/2002): Connection refused when I uncomment this part of code:

$db->addConnection('ua', [
    'host' => $config['db_host'],
    'username' => $config['db_user'],
    'password' => $config['db_password'],
    'db'=> $config['db_name_ua'],
    '4307'
]);

So, I don't undertand where is problem. Is this a library problem or problem in docker?

Full docker-compose.yml

`version: '3'

networks:
  backend:

services:

  nginx:
    platform: linux/arm64/v8
    image: nginx:stable-alpine
    container_name: nginx
    ports:
      - "${NGINX_PORT}:80"
    volumes:
      - ./projects:/var/www
      - ./nginx/conf.d:/etc/nginx/conf.d
      - ./nginx/logs:/var/log/nginx/
    depends_on:
      - php-7.3
      - mysql-5.7
      - redis
    environment:
      TZ: ${WORKSPACE_TIMEZONE}
    networks:
      - backend

  redis:
    platform: linux/arm64/v8
    image: redis:latest
    ports:
      - "${REDIS_PORT}:6379"
    container_name: redis
    volumes:
      - ./redis/data:/var/lib/redis
      - ./redis/conf:/usr/local/etc/redis/redis.conf
    environment:
      TZ: ${WORKSPACE_TIMEZONE}
    networks:
      - backend

  mysql-5.7:
    platform: linux/amd64
    image: mysql:5.7
    container_name: mysql-5.7
    restart: unless-stopped
    volumes:
      - ./mysql-5.7/data:/var/lib/mysql
      - ./mysql-5.7/conf.d:/etc/mysql/conf.d
      - ./mysql-5.7/logs:/var/log/mysql/
      - ./mysql-5.7/dump:/dump
    ports:
      - "${MYSQL_5_7_PORT}:3306"
    security_opt:
      - seccomp:unconfined
    environment:
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      TZ: ${WORKSPACE_TIMEZONE}
    networks:
      - backend

  phpmyadmin:
    platform: linux/amd64
    image: phpmyadmin/phpmyadmin
    links: 
      - mysql-5.7:db
    ports:
      - 8080:80
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      UPLOAD_LIMIT: 64M
    networks:
      - backend
    depends_on:
      - mysql-5.7

  php-7.3:
    build:
      context: ./php-workspace
      dockerfile: Dockerfile
      args:
        DOCKER_PHP_VERSION: '7.3'
        DOCKER_PHP_ENABLE_XDEBUG: ${DOCKER_PHP_ENABLE_XDEBUG}
        TZ: ${WORKSPACE_TIMEZONE}
    working_dir: /var/www
    container_name: php-7.3
    volumes:
      - ./.ssh:/home/www-data/.ssh
      - ./projects:/var/www
      - ./php-ini/7.3/php.ini:/usr/local/etc/php/php.ini
      - ./php-workers/7.3/supervisor.d:/etc/supervisor.d
    ports:
      - "${PHP_7_3_PORT}:9000"
    networks:
      - backend
    extra_hosts:
      - 'anti.localhost:192.168.65.2'
huseyinaslim commented 10 months ago

I think you should check your ports. You have configured your connections over port 4307. However, your yml files indicate that your MySQL server is running on 3306 (default port). If you don't have any other redirection from 4307 to 3306 at this point, your port information seems to be incorrect. Try connecting to 3306 instead of 4307.