Skip to content
Advertisement

Connecting Database Codeigniter 4 With Custom Ports

So, I’m trying to connect to my database container from my webserver container. I currently use Codeignter 4 for my PHP framework. Everything goes well in terms of communicating between different containers because that container is inside the same networks. Inside webserver container, I’ve tried to ping from and to database container with no problem, All the port is accessible because I can connect PHPMyAdmin which in their own snuggle little container and connect to my DB container with no problem.

This is the backtrace

SYSTEMPATH/Database/BaseConnection.php : 618   —  CodeIgniterDatabaseBaseConnection->initialize ()

I tried to DD from my controller hoping to override any return view

$db = ConfigDatabase::connect();
$apakek = $db->query("SELECT * FROM student_details_dummy");
dd($apakek);

This is my DB Array inside AppDatabase.php

public $default = [
    'DSN'      => '',
    'hostname' => '172.21.0.4',
    'username' => 'kr_rw',
    'password' => 'MrSLwwZvwC1KCRm6',
    'database' => 'kr_main',
    'DBDriver' => 'MySQLi',
    'DBPrefix' => '',
    'pConnect' => false,
    'DBDebug'  => (ENVIRONMENT !== 'development'),
    'cacheOn'  => false,
    'cacheDir' => '',
    'charset'  => 'utf8',
    'DBCollat' => 'utf8_general_ci',
    'swapPre'  => '',
    'encrypt'  => false,
    'compress' => false,
    'strictOn' => false,
    'failover' => [],
    'port'     => 21236,
];

I’ve tried using hostname db_mysql Which is pingable with the corresponding port to even check open port on canyouseeme.org returning fine.

How should I resolve this?

PS: all necessary extension is installed and enabled including php_mysqli php_mbstring php_pdo should any miss please do point out.

Advertisement

Answer

So, I’m myself not sure why this is the case. The network I created for those 3 containers acted like local network for a bunch of computer. So its basically boil downs to this:

  1. Outside the Docker Network: Use the Port Forwarding.
  2. Inside the Docker Network : Use the default port of the container.

So in the end I just use 3306 or the default port for accessing mysql.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement