Unable to connect database No such file or directory

Posted on
Unable to connect  database No such file or directory

MySQLi No such file or directory

Unable to connect to the database. Main connection [MySQLi]: No such file or directory – If you get an error like:

[CodeIgniter\Database\Exceptions\DatabaseException]

Unable to connect to the database.
Main connection [MySQLi]: No such file or directory

That’s a sign there is a problem with the database connection. Now in your project folder find the “env” file. Then copy and paste. rename the file “env (paste)” to “.env”.

How to fix Unable to connect to the database?

Open the .env file, then adjust the database settings. For example I use the following rules:

#--------------------------------------------------------------------
# ENVIRONMENT
#--------------------------------------------------------------------

CI_ENVIRONMENT = development

#--------------------------------------------------------------------
# DATABASE
#--------------------------------------------------------------------

database.default.hostname = 127.0.0.1
database.default.database = crud_ci4
database.default.username = root
database.default.password = 
database.default.DBDriver = MySQLi
database.default.DBPrefix =

After that repeat the following command in terminal:

php spark migrate

If successful, it will appear as follows:

neon@neon:~/Documents/Project-CI4/crud_basic$ php spark migrate

CodeIgniter v4.1.5 Command Line Tool - Server Time: 2021-12-07 19:49:05 UTC-06:00

Running all new migrations...
        Running: (App) 2021-12-07-004422_App\Database\Migrations\Users
Done migrations.

Now, we will see if the table was created successfully in the database. Open phpmyadmin, navigate to the crud_ci4 database, then you will see a table named “users”.

I hope this article is useful.

Leave a Reply

Your email address will not be published.