CodeIgniter Forums
An uncaught Exception was encountered - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: An uncaught Exception was encountered (/showthread.php?tid=81578)

Pages: 1 2


An uncaught Exception was encountered - ChicagoPhil - 03-20-2022

I've searched this topic and yes, I'm using MAMP on mac. Migrations run from URl but when using spark via CLI I get this error for any database command. Seeding or Migrations.
That's all I got for tonight. I need sleep. I've seen a handful of threads that have addressed this problem but I haven't found the answer.

Code:
An uncaught Exception was encountered

Type:        CodeIgniter\Database\Exceptions\DatabaseException
Message:    Unable to connect to the database.
Main connection [MySQLi]: No such file or directory
Filename:    /tutorial/vendor/codeigniter4/framework/system/Database/BaseConnection.php
Line Number: 407



CRITICAL - 2022-03-20 07:03:22 --> Unable to connect to the database.
Main connection [MySQLi]: No such file or directory
#0 /tutorial/vendor/codeigniter4/framework/system/Database/Database.php(67): CodeIgniter\Database\BaseConnection->initialize()
#1 /tutorial/vendor/codeigniter4/framework/system/Database/Config.php(105): CodeIgniter\Database\Database->loadForge(Object(CodeIgniter\Database\MySQLi\Connection))
#2 /tutorial/vendor/codeigniter4/framework/system/Database/Seeder.php(98): CodeIgniter\Database\Config::forge(NULL)
#3 /tutorial/vendor/codeigniter4/framework/system/Commands/Database/Seed.php(69): CodeIgniter\Database\Seeder->__construct(Object(Config\Database))
#4 /tutorial/vendor/codeigniter4/framework/system/CLI/Commands.php(63): CodeIgniter\Commands\Database\Seed->run(Array)
#5 /tutorial/vendor/codeigniter4/framework/system/CLI/CommandRunner.php(70): CodeIgniter\CLI\Commands->run('db:seed', Array)
#6 /tutorial/vendor/codeigniter4/framework/system/CLI/CommandRunner.php(56): CodeIgniter\CLI\CommandRunner->index(Array)
#7 /tutorial/vendor/codeigniter4/framework/system/CodeIgniter.php(830): CodeIgniter\CLI\CommandRunner->_remap('index', 'db:seed', 'UserSeeder')
#8 /tutorial/vendor/codeigniter4/framework/system/CodeIgniter.php(419): CodeIgniter\CodeIgniter->runController(Object(CodeIgniter\CLI\CommandRunner))
#9 /tutorial/vendor/codeigniter4/framework/system/CodeIgniter.php(326): CodeIgniter\CodeIgniter->handleRequest(NULL, Object(Config\Cache), false)
#10 /tutorial/vendor/codeigniter4/framework/system/CLI/Console.php(48): CodeIgniter\CodeIgniter->run()
#11 /tutorial/spark(57): CodeIgniter\CLI\Console->run()
#12 {main}



RE: An uncaught Exception was encountered - kenjis - 03-20-2022

Try

PHP Code:
'hostname' => '127.0.0.1'



RE: An uncaught Exception was encountered - ChicagoPhil - 03-20-2022

(03-20-2022, 04:31 AM)kenjis Wrote: Try

PHP Code:
'hostname' => '127.0.0.1'

Same result. Same error. EDIT: not exactly the same error

Code:
An uncaught Exception was encountered

Type:        CodeIgniter\Database\Exceptions\DatabaseException
Message:    Unable to connect to the database.
Main connection [MySQLi]: Connection refused
Filename:    /tutorial/vendor/codeigniter4/framework/system/Database/BaseConnection.php
Line Number: 407



RE: An uncaught Exception was encountered - ChicagoPhil - 03-20-2022

So I solved the problem but its a terrible solution. I made a change in the frameworks system code and it works like a charm.

File: /vendor/codeigniter4/framework/system/Database/MySQLi/Connection.php

I changed this code:
Code:
public function connect(bool $persistent = false)
    {
        // Do we have a socket path?
        if ($this->hostname[0] === '/') {
            $hostname = null;
            $port    = null;
            $socket  = $this->hostname;
        } else {
            $hostname = ($persistent === true) ? 'p:' . $this->hostname : $this->hostname;
            $port    = empty($this->port) ? null : $this->port;
            $socket  = '';
        }

To this :
Code:
public function connect(bool $persistent = FALSE)
    {
        // Do we have a socket path?
        if($this->hostname[0] === '/')
        {
            $hostname = NULL;
            $port = NULL;
            $socket = $this->hostname;
        }
        else
        {
            $hostname = ($persistent === TRUE) ? 'p:' . $this->hostname : $this->hostname;
            $port = empty($this->port) ? NULL : $this->port;
            $socket = '/Applications/MAMP/tmp/mysql/mysql.sock';
        }

Essentially it was the socket having an empty string that was the problem. I hope there is a better way to set this setting that I just don't know about.


RE: An uncaught Exception was encountered - ChicagoPhil - 03-20-2022

I don't want to mark this thread as solved just yet. I'm looking for feedback on why what I did worked and if there is a way to do it without going into system files.


RE: An uncaught Exception was encountered - InsiteFX - 03-21-2022

Your solution there is telling me that it is not finding the MySQL files. Which maybe a configuration setup problem.
But wait and see if one of the development teams comments back to you.


RE: An uncaught Exception was encountered - ChicagoPhil - 03-21-2022

(03-21-2022, 12:47 AM)InsiteFX Wrote: Your solution there is telling me that it is not finding the MySQL files. Which maybe a configuration setup problem.
But wait and see if one of the development teams comments back to you.

I agree. If I understood the logic being used in the PHP code, I could suggest a solution to the problem. All I did was track back the error to a point where there had to be an issue. I seen the socket setting was blank for the mysqli connect function and dropped in the socket info. The thing I don't get is why isn't the setting in the database configuration? I see socket logic in the code but where is it being set at?


RE: An uncaught Exception was encountered - ChicagoPhil - 03-21-2022

I have confirmed that the missing socket setting is the problem. I think the Codeigniter team needs to consider adding one more setting to the database configuration file. It would be nice to have a socket setting for people that connect to their localhost MYSQL via unix socket. It would probably take 10 minutes to add it to the code and most of that time would be finding the files you need to open to edit.

it would be nice to see something like this:
Code:
public $default = [
        'DSN'      => '',
        'hostname' => 'localhost',
        'username' => 'root',
        'password' => 'root',
        'database' => 'codeigniter',
        'DBDriver' => 'MySQLi',
        'DBPrefix' => '',
        'pConnect' => false,
        'DBDebug'  => (ENVIRONMENT !== 'production'),
        'charset'  => 'utf8',
        'DBCollat' => 'utf8_general_ci',
        'swapPre'  => '',
        'encrypt'  => false,
        'compress' => false,
        'strictOn' => false,
        'failover' => [],
        'port'    => 3306,
        'socket' => '',
    ];



RE: An uncaught Exception was encountered - kenjis - 03-21-2022

try
PHP Code:
'hostname' => '/Applications/MAMP/tmp/mysql/mysql.sock'



RE: An uncaught Exception was encountered - ChicagoPhil - 03-22-2022

(03-21-2022, 07:03 PM)kenjis Wrote: try
PHP Code:
'hostname' => '/Applications/MAMP/tmp/mysql/mysql.sock'

That did the trick! I tried something very close to that and it didn't work. I put localhost:/Applications/MAMP/tmp/mysql/mysql.sock in there and no dice. I guess, I can mark this solved now.
I knew it had to be something small like that. That's why I got stuck on it.

Thanks again for the help.