dotkernel / frontend

DotKernel Frontend Application. Based on Mezzio microframework using Laminas components.
https://v5.dotkernel.net
MIT License
22 stars 5 forks source link

Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2002] Connection refused in /var/www/vendor/laminas/laminas-servicemanager/src/ServiceManager.php on line 672 #220

Closed goharsahi closed 2 years ago

goharsahi commented 3 years ago

I'm using Docker with PHP version 7.4 on Windows 10 (using Docker Desktop). I'm unable to open the application in my browser even after various attempts albeit with various configuration changes.

Following code just works perfectly.

<?php
$link = mysqli_connect("db", "root", "654321");

if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

echo "Success: A proper connection to MySQL was made!" . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;

Below is the configuration in local.php

$databases = [
    'default' => [
        'host' => 'db', // Also tried the IP Address of docker container - 172.18.0.2
        'dbname' => 'dbname',
        'user' => 'dbuser',
        'password' => '654321',
        'port' => 6603,
        'driver' => 'pdo_mysql',
        'charset' => 'utf8mb4',
        'collate' => 'utf8mb4_general_ci'
    ]
    // you can add more database connections into this array
];

Also another thing, The Phinx migrations ran perfectly, however, Phinx seeds are not running. Keeps giving the error 'Phinx.yml' not found.

Calin23 commented 3 years ago

Hi @goharsahi .

Do you have a docker-compose.yaml file in your root directory?

Example docker-compose.yaml configuration:

version: '3.3'
services:
  db:
    image: mysql/mysql-server:latest
    restart: always
    environment:
      MYSQL_DATABASE: 'dk3'
      MYSQL_USER: 'root'
      MYSQL_PASSWORD: 'password'
    ports:
      - '3306:3306'
    expose:
      - '3306'
      ...

And try to use 127.0.0.1 in your host configuration.

    'default' => [
        'host' => '127.0.0.1',
       ...

For Phinx seeds please specify which command you runed.

goharsahi commented 3 years ago

docker-compose.yml

version: "2"
services:

  web:
    build:
      context: .
      dockerfile: container-build/web/Dockerfile
    environment:
      - MYSQL_DATABASE=dbname
      - MYSQL_USER=dbuser
      - MYSQL_PASSWORD=654321
      - MYSQL_HOST=db
    ports:
      - "8080:80"
    volumes:
      - ./:/var/www
    depends_on:
      - db

  db:
    image: mysql:5.7
    ports:
      - "6603:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=654321
      - MYSQL_USER=dbuser
      - MYSQL_PASSWORD=654321
      - MYSQL_DATABASE=dbname
    volumes:
      - "mysql_data:/var/lib/mysql"

volumes:
  mysql_data: { driver: local }

Seed Command

php vendor/bin/phinx seed:run -s seed=data/database/seeds/RoleSeeder

Output

Phinx by CakePHP - https://phinx.org.

In FileLocator.php line 71:

  The file "phinx.yml" does not exist (in: "D:\dataD\ziwajun-muneerah\ziwajun_muneerah", "D:\dataD\ziwajun-muneerah\ziwajun_muneerah  
  \").                                                                                                                                

seed:run [-c|--configuration CONFIGURATION] [-p|--parser PARSER] [-e|--environment ENVIRONMENT] [-s|--seed SEED]
Calin23 commented 3 years ago

@goharsahi try to use php vendor/bin/phinx seed:run --configuration="config/migrations.php" --seed Data\\Database\\Seeds\\RoleSeeder

Calin23 commented 3 years ago

@goharsahi i tested your docker-compose.yml and is working.

Just use:

  'default' => [
        'host' => '127.0.0.1',
        'dbname' => 'dbname',
        'user' => 'dbuser',
        'password' => '654321',
        'port' => 6603,
        'driver' => 'pdo_mysql',
        'charset' => 'utf8mb4',
        'collate' => 'utf8mb4_general_ci'
    ]
goharsahi commented 3 years ago
D:\dataD\ziwajun-muneerah\ziwajun_muneerah>php vendor/bin/phinx seed:run --configuration="config/migrations.php" --seed Data\\Database\\Seeds\\RoleSeeder
Phinx by CakePHP - https://phinx.org.

using config file config\migrations.php
using config parser php
using migration paths
 - D:\dataD\ziwajun-muneerah\ziwajun_muneerah\data\database\migrations
using seed paths
 - D:\dataD\ziwajun-muneerah\ziwajun_muneerah\data\database\seeds
warning no environment specified, defaulting to: development
using adapter mysql
using database dbname

In Manager.php line 563:

  The seed class "Data\\Database\\Seeds\\RoleSeeder" does not exist  

seed:run [-c|--configuration CONFIGURATION] [-p|--parser PARSER] [-e|--environment ENVIRONMENT] [-s|--seed SEED]

I've also tried it without "Data\Database\" i.e. php vendor/bin/phinx seed:run --configuration="config/migrations.php" --seed RoleSeeder but same result. The seed class "RoleSeeder" does not exist

Calin23 commented 3 years ago

@goharsahi i run this command on my MacBook and is runing fine mabe if you use a windows pc you must adapt your command something like this php vendor/bin/phinx seed:run --configuration="config/migrations.php" --seed Data/Database/Seeds/RoleSeeder

goharsahi commented 3 years ago

@Calin23 Here's a development. Looks like its the same issue of connectivity. When I run it with following configuration..

databases = [
    'default' => [
        'host' => 'db',
        'dbname' => 'dbname',
        'user' => 'dbuser',
        'password' => '654321',
       ......
D:\dataD\ziwajun-muneerah\ziwajun_muneerah>php vendor/bin/phinx seed:run --configuration="config/migrations.php"
Phinx by CakePHP - https://phinx.org.

using config file config\migrations.php
using config parser php
using migration paths
 - D:\dataD\ziwajun-muneerah\ziwajun_muneerah\data\database\migrations
using seed paths
 - D:\dataD\ziwajun-muneerah\ziwajun_muneerah\data\database\seeds
warning no environment specified, defaulting to: development
using adapter mysql
using database dbname

 == Data\Database\Seeds\RoleSeeder: seeding

In PdoAdapter.php line 96:

  There was a problem connecting to the database: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host   
  is known.                                                                                                                           

In PdoAdapter.php line 84:

  SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known.  

In PdoAdapter.php line 84:

  PDO::__construct(): php_network_getaddresses: getaddrinfo failed: No such host is known.  

seed:run [-c|--configuration CONFIGURATION] [-p|--parser PARSER] [-e|--environment ENVIRONMENT] [-s|--seed SEED]

However, when I change the host from db to localhost i.e.

databases = [
    'default' => [
        'host' => 'localhost',
        'dbname' => 'dbname',
        'user' => 'dbuser',
        'password' => '654321',
       ......

It worked fine.

D:\dataD\ziwajun-muneerah\ziwajun_muneerah>php vendor/bin/phinx seed:run --configuration="config/migrations.php"
Phinx by CakePHP - https://phinx.org.

using config file config\migrations.php
using config parser php
using migration paths
 - D:\dataD\ziwajun-muneerah\ziwajun_muneerah\data\database\migrations
using seed paths
 - D:\dataD\ziwajun-muneerah\ziwajun_muneerah\data\database\seeds
warning no environment specified, defaulting to: development
using adapter mysql
using database dbname

 == Data\Database\Seeds\RoleSeeder: seeding
 == Data\Database\Seeds\RoleSeeder: seeded 0.2227s

All Done. Took 0.2243s
goharsahi commented 3 years ago

@Calin23 The issue of seeding stands resolved, however, website is still not working. Let me know if you are able to make it work.

bidi47 commented 2 years ago

please let us know if this is still an issue if not, we can close the ticket

goharsahi commented 2 years ago

Issue is resolved. Thanks. It was related to hostname configuration.

Gohar Sahi

On Fri, 9 Sept 2022 at 14:43, bidi47 @.***> wrote:

please let us know if this is still an issue if not, we can close the ticket

— Reply to this email directly, view it on GitHub https://github.com/dotkernel/frontend/issues/220#issuecomment-1241749028, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABK4IDIQTJUUSFWZ3DU7VMDV5MBD5ANCNFSM4Z5XWUCA . You are receiving this because you were mentioned.Message ID: @.***>

bidi47 commented 2 years ago

that's good to hear thank you for letting us know