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:


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:


CI_ENVIRONMENT = development


database.default.hostname =
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.

1 comment

  1. Hi,

    Our AI writer is an intelligent and intuitive writing assistant that uses machine learning to generate content about ANY subject and ensure that it’s compelling, well-written, and authentic. Artificial intelligence makes it fast & easy to create content for your blog, social media, website, and more! That’s right, you can spend your time focusing on other parts of your business.

    If you would like to know more about how this tool can help you, please visit our site here for a free trial: https://jasper.ai?fpr=jasper-artificial-intelligence

    We are excited to make your life easier!

    Best regards,
    James Klein
    Jasper Artificial Intelligence Expert

Leave a Reply