Welcome Guest, Not a member yet? Register   Sign In
Unable to connect to the database
#1
Sad 

Hi everyone ! 
First time hitting this error and first time using Docker and Docker compose.
I have just started a brand new project and CI can't connect to the database. 
  • Tried replacing localhost with 127.0.0.1 in Database.php, 
  • Switched from mariaDB to Mysql, 
  • Changed credentials from some real credentials to 'root' just in case... 
  • Deleted my table to see if there was an other error message.

Can someone help me please ? I'm desperate Sad Sad Sad


See below all the files I thought were usefull.

Here is the DB name in PhpMyAdmin : ressourcesrelationnelles

Here is my docker-compose.yml : 
Code:
services:
  mysql:
    image: docker.io/bitnami/mysql:8.0.32
    environment:
      # ALLOW_EMPTY_PASSWORD is recommended only for development.
      - ALLOW_EMPTY_PASSWORD=yes
      - MYSQL_ROOT_PASSWORD=root
      - MSQL_USER=root
      - MYSQL_PASSWORD=root
      - MSQL_DATABASE=ressourcesRelationnelles
  ressources-relationnelles:
    image: docker.io/bitnami/codeigniter:4.1.8-debian-10-r32
    ports:
      - '80:8000'
    environment:
      # ALLOW_EMPTY_PASSWORD is recommended only for development.
      - ALLOW_EMPTY_PASSWORD=yes
      - CODEIGNITER_DATABASE_HOST=mysql
      - CODEIGNITER_DATABASE_PORT_NUMBER=3306
      - CODEIGNITER_DATABASE_USER=root
      - CODEIGNITER_DATABASE_NAME=ressourcesrelationnelles
    volumes:
      - './ressources-relationnelles:/app'
    depends_on:
      - mysql
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    restart: always
    links:
      - mysql:mysql
    ports:
      - 8081:80
    depends_on:
      - mysql
    environment:
      - PMA_HOST=mysql
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_USER=root
Here is my .env file
Code:
database.default.hostname = localhost
database.default.database = ressourcesrelationnelles
database.default.username = root
database.default.password = root
database.default.DBDriver = MySQLi
Here is my Database.php

Code:
    public $default = [
        'DSN'      => '',
        'hostname' => 'localhost',
        'username' => 'root',
        'password' => 'root',
        'database' => 'ressourcesrelationnelles',
        'DBDriver' => 'MySQLi',
        'DBPrefix' => '',
        'pConnect' => false,
        'DBDebug'  => (ENVIRONMENT !== 'production'),
        'charset'  => 'utf8',
        'DBCollat' => 'utf8_general_ci',
        'swapPre'  => '',
        'encrypt'  => false,
        'compress' => false,
        'strictOn' => false,
        'failover' => [],
        'port'    => 3306,
    ];
Here is my Controller 

Code:
namespace App\Controllers;
use App\Models\Fo_user_model;
use App\Entities\Fo_user_entity;


class Statics extends BaseController
{
    public function index()
    {
        echo('test');
        $objUser_model = new Fo_user_model;
        $objUser = new Fo_user_entity;
        $arrUser['fo_user_pseudo'] = 'test';
        $arrUser['fo_user_password'] = 'testpass';
        $arrUser['fo_user_mail'] = 'testmail';
        $objUser->fill($arrUser);
        var_dump($objUser);
        $objUser_model->save($objUser);
    }
}

Here is my error log

Code:
ERROR - 2023-02-05 08:38:49 --> Error connecting to the database: No such file or directory
CRITICAL - 2023-02-05 08:38:49 --> Unable to connect to the database.
Main connection [MySQLi]: No such file or directory
#0 /app/myapp/system/Database/BaseConnection.php(577): CodeIgniter\Database\BaseConnection->initialize()
#1 /app/myapp/system/Database/BaseBuilder.php(1771): CodeIgniter\Database\BaseConnection->query()
#2 /app/myapp/system/Model.php(239): CodeIgniter\Database\BaseBuilder->insert()
#3 /app/myapp/system/BaseModel.php(738): CodeIgniter\Model->doInsert()
#4 /app/myapp/system/Model.php(625): CodeIgniter\BaseModel->insert()
#5 /app/myapp/system/BaseModel.php(658): CodeIgniter\Model->insert()
#6 /app/myapp/app/Controllers/Statics.php(20): CodeIgniter\BaseModel->save()
#7 /app/myapp/system/CodeIgniter.php(825): App\Controllers\Statics->index()
#8 /app/myapp/system/CodeIgniter.php(412): CodeIgniter\CodeIgniter->runController()
#9 /app/myapp/system/CodeIgniter.php(320): CodeIgniter\CodeIgniter->handleRequest()
#10 /app/myapp/public/index.php(37): CodeIgniter\CodeIgniter->run()
#11 {main}
Reply
#2

Try:
Code:
database.default.hostname = mysql
Reply
#3

(02-05-2023, 04:02 PM)kenjis Wrote: Try:
Code:
database.default.hostname = mysql
Oh damn !!!! It worked ! Thank you SO MUCH !! Is it because in my docker compose, the service name is mysql ?
Reply
#4

(This post was last modified: 02-07-2023, 04:15 AM by kenjis.)

In the case, localhost means your computer host OS.
The docker mysql container is not localhost.
Reply
#5

Hi everyone,

what about this , i got first time hitting this error and newbie in CI4, just started a new project with php spark serve and can't connect to database
an error :
CodeIgniter v4.1.5 Command Line Tool - Server Time: 2023-02-08 10:53:47 UTC+07:00


[CodeIgniter\Database\Exceptions\DatabaseException]

Unable to connect to the database.
Main connection [MySQLi]: No connection could be made because the target machine actively refused it


does anybody help me?
thank you
Reply




Theme © iAndrew 2016 - Forum software by © MyBB