CodeIgniter Forums
Main connection [MySQLi]: Connection refused - Printable Version

+- CodeIgniter Forums (
+-- Forum: CodeIgniter 4 (
+--- Forum: CodeIgniter 4 Support (
+--- Thread: Main connection [MySQLi]: Connection refused (/showthread.php?tid=80507)

Main connection [MySQLi]: Connection refused - mrbryo - 11-09-2021


I've setup a docker with: php:7.4.24-apache, mariadb:10.3.27. The routing, controller and views all are working because I can see the results. When I try to connect to the database I get "Main connection [MySQLi]: Connection refused". I am able to connect to the database from the MariaDB docker CLI and a desktop application called TablePlus. After all the googling I will attempt to answer all the same questions people seem to ask. Not saying I've caught them all but these are the most common I've seen.

  1. The mysqli driver is installed and should be working. I validated this by view the phpinfo() on a view I created.
  2. The credentials to logon to the DB I know must be correct since I can use the CLI and TablePlus to logon to the database. At first TablePlus wouldn't connect but then realized I didn't have my port specified in the docker. I've resolved the port config and now TablePlus connects. I retried my page to connect to the DB but same error.
  3. I've triple checked my DB settings in the .env file and all should be good based on what I've found.
  4. I originally had "localhost" set for hostname but that returned a different error (Main connection [MySQLi]: No such file or directory) but searching for answer said to switch to local IP address.

I'm a noob with docker so there is probably something I've missed...yet everything I've read indicates it is setup correctly. Think I might installed mysqladmin to see if it can connect to rule out connectivity with in the container.

Here is my .env file:
database.default.hostname = ''
database.default.database = 'friends'
database.default.username = 'root'
database.default.password = 'junk'
database.default.DBDriver = 'MySQLi'
database.default.DBPrefix = 'f_'
database.default.DBDebug = true
database.default.charset  = 'utf8mb4'
database.default.DBCollat = 'utf8mb4_unicode_ci'

charset and DBCollat are the same value I used in the docker config for MariaDB.

Here is the code for calling the DB:

PHP Code:
// initialize database class
$db = \Config\Database::connect();

// Check connection
if ($db->connect_error)
¬† exit("Connection failed: " $db->connect_error);

PHP Code:
echo "Connected successfully";

// fetch listing of database tables
$tables $db->listTables();

foreach (
$tables as $table) {
¬† echo $table;

// close db connection

Pretty sure it doesn't get past the first line. I have not modified my Database.php file in the Config folder. I'm at a loss on what to check next.

Thank you for your time and support.

RE: Main connection [MySQLi]: Connection refused - mrbryo - 11-09-2021

OK it was my hostname in the .env file. The way I have docker setup I gave MariaDB a "name" called "friends-db". Once I used that as the hostname it connected. Hopefully, always find the solution after you ask for help...anyone hopefully this helps someone out there! Also resolved issue with phpmyadmin connecting.