Codeigniter4: Bug: php spark migrate throws wrongful CodeIgniter\Database\Exceptions\DatabaseException Unable to connect to the database

Created on 19 Jul 2020  路  31Comments  路  Source: codeigniter4/CodeIgniter4

Bug Description
Impossible to perform migrations through spark (experienced in versions 4.0.2 to 4.0.4)

Although DB credentials are set in .env and/orin app/Config/Database.php and successful query results obtained through Controller classes,

php spark migrate causes CodeIgniter\Database\Exceptions\DatabaseException

Exact command line output:


CodeIgniter CLI Tool - Version 4.0.4 - Server-Time: 2020-07-19 05:22:57am

Running all new migrations...
An uncaught Exception was encountered

Type:        CodeIgniter\Database\Exceptions\DatabaseException
Message:     Unable to connect to the database.
Filename:    /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/BaseConnection.php
Line Number: 425

    Backtrace:
                        -646 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/BaseConnection.php::initialize
                                -1548 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/BaseConnection.php::query
                                -1591 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/BaseConnection.php::listTables
                                -945 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/MigrationRunner.php::tableExists
                                -193 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/MigrationRunner.php::ensureTable
                                -128 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Commands/Database/Migrate.php::latest
                                -100 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CLI/Commands.php::run
                                -107 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CLI/CommandRunner.php::run
                                -85 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CLI/CommandRunner.php::index
                                -914 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CodeIgniter.php::_remap
                                -404 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CodeIgniter.php::runController
                                -312 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CodeIgniter.php::handleRequest
                                -86 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CLI/Console.php::run
                                -57 - /Applications/MAMP/ci4projs/deneme/spark::run

CodeIgniter 4 version
Last gotten in v4.0.4 release

Affected module(s)
spark

Expected behavior
All migrations performed successfully (without exceptions).

Steps to reproduce

  1. Open up a Terminal / command line window
  2. Change to the directory to the grand-parent level of your webserver's htdocs where htdocs will be substituted with the public directory of CI4
  3. composer create-project codeigniter4/appstarter deneme
  4. Open the project in your code editor and edit the .env file to set CI_ENVIRONMENT = development (duplicate it first from the file named 'env' if it doesn't exist - i.e. cp env .env)
  5. Perform one or both of steps 6 and 7:
  6. Enter DB credentials for MySQL in .env file (with database.default.DBDriver = MySQLi)
  7. Enter DB credentials in app/Config/Database.php
  8. Go back to the command line and run the following commands (in my example proj name is 'deneme':
    9: cd deneme
  9. php spark migrate:create AddBlog
  10. (Optional) Go back to your code editor and open the file whose name is resembles "2020_mm_dd_hhmmss_AddBlog.php" from app/Database/Migrations
  11. (Optional) Replace the contents of up() and down() methods with example from https://codeigniter.com/user_guide/dbmgmt/migration.html
  12. Go back to the command line and while still in the root directory of the CI4 project run
  13. php spark migrate
  14. Compare your Terminal output to the following output:
CodeIgniter CLI Tool - Version 4.0.4 - Server-Time: 2020-07-19 05:42:10am

Running all new migrations...
An uncaught Exception was encountered

Type:        CodeIgniter\Database\Exceptions\DatabaseException
Message:     Unable to connect to the database.
Filename:    /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/BaseConnection.php
Line Number: 425

    Backtrace:
                        -646 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/BaseConnection.php::initialize
                                -1548 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/BaseConnection.php::query
                                -1591 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/BaseConnection.php::listTables
                                -945 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/MigrationRunner.php::tableExists
                                -193 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/MigrationRunner.php::ensureTable
                                -128 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Commands/Database/Migrate.php::latest
                                -100 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CLI/Commands.php::run
                                -107 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CLI/CommandRunner.php::run
                                -85 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CLI/CommandRunner.php::index
                                -914 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CodeIgniter.php::_remap
                                -404 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CodeIgniter.php::runController
                                -312 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CodeIgniter.php::handleRequest
                                -86 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CLI/Console.php::run
                                -57 - /Applications/MAMP/ci4projs/deneme/spark::run

Context

  • OS: Experienced in macOS 10.3
  • Web server: Apache 2.x (in MAMP 5.4)
  • PHP version 7.3.7
  • MySQL 5.7
bug

Most helpful comment

Have you tried changing localhost to 127.0.0.1?

All 31 comments

May I know the database driver you used? I tried this using SQLite3 with :memory: as DB for the default connection and encountered no problems.

CodeIgniter CLI Tool - Version 4.0.4 - Server-Time: 2020-07-19 06:35:49am

Running all new migrations...
        Running: (App) 2020-07-19-113439_App\Database\Migrations\AddBlog
Done

I have exactly the same issue, tried firstly with custom migration, then switch back to tutorial migrations. When you try to use MySQLi as driver, this happens. I have also checked in the debugger for the database credentials, and everything looks fine

CodeIgniter CLI Tool - Version 4.0.4 - Server-Time: 2020-07-19 09:57:05am

An uncaught Exception was encountered

Type: CodeIgniter\Database\Exceptions\DatabaseException
Message: Unable to connect to the database.
Filename: /var/www/html/system/Database/BaseConnection.php
Line Number: 425

Backtrace:
                    -109 - /var/www/html/system/Database/Database.php::initialize
                            -148 - /var/www/html/system/Database/Config.php::loadForge
                            -15 - /var/www/html/app/Commands/SetupApp.php::forge
                            -100 - /var/www/html/system/CLI/Commands.php::run
                            -107 - /var/www/html/system/CLI/CommandRunner.php::run
                            -85 - /var/www/html/system/CLI/CommandRunner.php::index
                            -914 - /var/www/html/system/CodeIgniter.php::_remap
                            -404 - /var/www/html/system/CodeIgniter.php::runController
                            -312 - /var/www/html/system/CodeIgniter.php::handleRequest
                            -86 - /var/www/html/system/CLI/Console.php::run
                            -57 - /var/www/html/spark::run

`

Can you tell me the details of your DB config? Just exclude sensitive information like the password.


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

CI_ENVIRONMENT = development

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

database.default.hostname = localhost
database.default.database = deneme
database.default.username = arclab
database.default.password = Passw0rd
database.default.DBDriver = MySQLi

Everything else is as same as what shipped with the default env file.

May I know the database driver you used? I tried this using SQLite3 with :memory: as DB for the default connection and encountered no problems.

CodeIgniter CLI Tool - Version 4.0.4 - Server-Time: 2020-07-19 06:35:49am

Running all new migrations...
        Running: (App) 2020-07-19-113439_App\Database\Migrations\AddBlog
Done

Using database.default.DBDriver = MySQLi

@gosocial2 have you tried checking if the database deneme exists in the MySQL server? And/or the username/password is registered?

I was able to replicate this problem but only when there was no database. You have to create a database manually before running migrations.

CodeIgniter will create a database only for the SQLite3 driver.

Yes, this is reproducible if either the database does not exist or the username/password is not registered.

I want to add something to this. I've just retested my case.

If I try to connect to the database using a standard model, the connection works. The database exists, and everything works.
When I try to run the migration itself he doesn't work, I have this error again.

CodeIgniter CLI Tool - Version 4.0.4 - Server-Time: 2020-07-19 09:57:05am

An uncaught Exception was encountered

Type: CodeIgniter\Database\Exceptions\DatabaseException
Message: Unable to connect to the database.
Filename: /var/www/html/system/Database/BaseConnection.php
Line Number: 425

Via controller/request is working but is not the same when I try to use commands via spark, no.

DB connection works through code in Controllers running in the web application so database is accessible alright. It just doesn't work with spark. Spark is not seeing the DB config in .env or in app\Config\Database.php .

I also tested with ci-news app - same result. Did you try to reproduce through the steps in the issue description? (e.g. using composer to create a project from scratch)

@gosocial2 Yes, I followed steps from your description.

Okay... it seems like a bug but I can't reproduce it for now. And I have no idea what can cause it. I will try to deal with this tomorrow maybe.

If you think that there is something "specific" in your working environment, please share.

@paulbalandan @michalsn The problem is not the inexistence of the DB or models or controllers not connecting to the DB, guys. The issue, as already stated in the title, is php spark migrate is not working for ignoring the DB credentials set for MYSQLi driver.

@michalsn it happens when you create a new project with composer and add any migration. so no specific env settings.

Also no pressure intended. No need to hurry - just something to be aware of for the near future.

Have you tried changing localhost to 127.0.0.1?

Seems like the problem is MySQLi connection can't obtain a connection ID even after substituting the failover sets. So we need to know the error logged by MySQLi. @gosocial2 , please edit public $threshold in your Config\Logger to a value of 4 so that we can record also the runtime errors. Then run again the migrations. After that go to your writable/logs and get the log file for this day. It will show you the MySQL error.

Have you tried changing localhost to 127.0.0.1?

@paulbalandan I followed your instruction:
log with $threshold = 4 below (actually no different than level 3:

ERROR - 2020-07-20 01:57:08 --> Error connecting to the database: No such file or directory
CRITICAL - 2020-07-20 01:57:08 --> Unable to connect to the database.
#0 /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/BaseConnection.php(646): CodeIgniter\Database\BaseConnection->initialize()
#1 /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/BaseConnection.php(1548): CodeIgniter\Database\BaseConnection->query('SHOW TABLES FRO...')
#2 /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/BaseConnection.php(1591): CodeIgniter\Database\BaseConnection->listTables()
#3 /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/MigrationRunner.php(945): CodeIgniter\Database\BaseConnection->tableExists('migrations')
#4 /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/MigrationRunner.php(193): CodeIgniter\Database\MigrationRunner->ensureTable()
#5 /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Commands/Database/Migrate.php(128): CodeIgniter\Database\MigrationRunner->latest(NULL)
#6 /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CLI/Commands.php(100): CodeIgniter\Commands\Database\Migrate->run(Array)
#7 /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CLI/CommandRunner.php(107): CodeIgniter\CLI\Commands->run('migrate', Array)
#8 /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CLI/CommandRunner.php(85): CodeIgniter\CLI\CommandRunner->index(Array)
#9 /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CodeIgniter.php(914): CodeIgniter\CLI\CommandRunner->_remap('index', 'migrate')
#10 /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CodeIgniter.php(404): CodeIgniter\CodeIgniter->runController(Object(CodeIgniter\CLI\CommandRunner))
#11 /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CodeIgniter.php(312): CodeIgniter\CodeIgniter->handleRequest(NULL, Object(Config\Cache), false)
#12 /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CLI/Console.php(86): CodeIgniter\CodeIgniter->run()
#13 /Applications/MAMP/ci4projs/deneme/spark(57): CodeIgniter\CLI\Console->run()
#14 {main}

@lonnieezell

Have you tried changing localhost to 127.0.0.1?

Following your suggestion, issue is resolved. Changing localhost to 127.0.0.1 in .env (and not app/Config/Database.php) works for spark.

Thanks to all who spared time and effort in looking into this.

In my case, it creates the tables in the database. So, it connects and executes the table create functions. Yet, it gives the same error

ACTION:
php spark migrate -all

REACTION:
CodeIgniter CLI Tool - Version 4.0.4 - Server-Time: 2020-08-03 16:05:28pm

Running all new migrations...
An uncaught Exception was encountered

Type: CodeIgniter\Database\Exceptions\DatabaseException
Message: Unable to connect to the database.
Filename: /var/www/html/MyAPP/vendor/codeigniter4/framework/system/Database/BaseConnection.php
Line Number: 425

    Backtrace:
                                            -109 - /var/www/html/MyAPP/vendor/codeigniter4/framework/system/Database/Database.php::initialize
                                                            -148 - /var/www/html/MyAPP/vendor/codeigniter4/framework/system/Database/Config.php::loadForge
                                                            -78 - /var/www/html/MyAPP/vendor/codeigniter4/framework/system/Database/Migration.php::forge
                                                            -1026 - /var/www/html/MyAPP/vendor/codeigniter4/framework/system/Database/MigrationRunner.php::__construct
                                                            -223 - /var/www/html/MyAPP/vendor/codeigniter4/framework/system/Database/MigrationRunner.php::migrate
                                                            -128 - /var/www/html/MyAPP/vendor/codeigniter4/framework/system/Commands/Database/Migrate.php::latest
                                                            -100 - /var/www/html/MyAPP/vendor/codeigniter4/framework/system/CLI/Commands.php::run
                                                            -107 - /var/www/html/MyAPP/vendor/codeigniter4/framework/system/CLI/CommandRunner.php::run
                                                            -85 - /var/www/html/MyAPP/vendor/codeigniter4/framework/system/CLI/CommandRunner.php::index
                                                            -914 - /var/www/html/MyAPP/vendor/codeigniter4/framework/system/CodeIgniter.php::_remap
                                                            -404 - /var/www/html/MyAPP/vendor/codeigniter4/framework/system/CodeIgniter.php::runController
                                                            -312 - /var/www/html/MyAPP/vendor/codeigniter4/framework/system/CodeIgniter.php::handleRequest
                                                            -86 - /var/www/html/MyAPP/vendor/codeigniter4/framework/system/CLI/Console.php::run
                                                            -57 - /var/www/html/MyAPP/spark::run

Not only creating the tables; it also alters migration table
"1" "2017-11-20-223112" "Myth\Auth\Database\Migrations\CreateAuthTables" "default" "Myth\Auth" "1596488531" "1"

In my case, it creates the tables in the database. So, it connects and executes the table create functions. Yet, it gives the same error

I found the same! I got the same "Unable to connect to the database" message every time. I didn't think to check the database for tables as I assumed nothing would have changed if unable to connect.

Then I read the post by Ferhat Bingol and found the tables had been created! Thank you for spotting this and posting.

@lonnieezell

Have you tried changing localhost to 127.0.0.1?

Following your suggestion, issue is resolved. Changing localhost to 127.0.0.1 in .env (and not app/Config/Database.php) works for spark.

Thanks to all who spared time and effort in looking into this.

I couldn't solve with this way, is there anybody still having this issue?

I just found a solution to migrate with command() function. But still, from cli it does not work.
echo command("migrate");
in controller.

The issue resurrected with PHP 7.4 with no matter whether you set database.default.hostname = '127.0.0.1' or = 127.0.0.1 or =localhost

Type: CodeIgniter\Database\Exceptions\DatabaseException
Message: Unable to connect to the database.
Filename: /Applications/MAMP/ci4projs/cmseoign/vendor/codeigniter4/framework/system/Database/BaseConnection.php
Line Number: 425

Backtrace:
                    -646 - /Applications/MAMP/ci4projs/cmseoign/vendor/codeigniter4/framework/system/Database/BaseConnection.php::initialize
                            -1548 - /Applications/MAMP/ci4projs/cmseoign/vendor/codeigniter4/framework/system/Database/BaseConnection.php::query
                            -1591 - /Applications/MAMP/ci4projs/cmseoign/vendor/codeigniter4/framework/system/Database/BaseConnection.php::listTables
                            -945 - /Applications/MAMP/ci4projs/cmseoign/vendor/codeigniter4/framework/system/Database/MigrationRunner.php::tableExists
                            -193 - /Applications/MAMP/ci4projs/cmseoign/vendor/codeigniter4/framework/system/Database/MigrationRunner.php::ensureTable
                            -128 - /Applications/MAMP/ci4projs/cmseoign/vendor/codeigniter4/framework/system/Commands/Database/Migrate.php::latest
                            -100 - /Applications/MAMP/ci4projs/cmseoign/vendor/codeigniter4/framework/system/CLI/Commands.php::run
                            -155 - /Applications/MAMP/ci4projs/cmseoign/vendor/codeigniter4/framework/system/CLI/BaseCommand.php::run
                            -68 - /Applications/MAMP/ci4projs/cmseoign/vendor/agungsugiarto/boilerplate/src/Commands/InstallCommand.php::call
                            -100 - /Applications/MAMP/ci4projs/cmseoign/vendor/codeigniter4/framework/system/CLI/Commands.php::run
                            -107 - /Applications/MAMP/ci4projs/cmseoign/vendor/codeigniter4/framework/system/CLI/CommandRunner.php::run
                            -85 - /Applications/MAMP/ci4projs/cmseoign/vendor/codeigniter4/framework/system/CLI/CommandRunner.php::index
                            -914 - /Applications/MAMP/ci4projs/cmseoign/vendor/codeigniter4/framework/system/CodeIgniter.php::_remap
                            -404 - /Applications/MAMP/ci4projs/cmseoign/vendor/codeigniter4/framework/system/CodeIgniter.php::runController
                            -312 - /Applications/MAMP/ci4projs/cmseoign/vendor/codeigniter4/framework/system/CodeIgniter.php::handleRequest
                            -86 - /Applications/MAMP/ci4projs/cmseoign/vendor/codeigniter4/framework/system/CLI/Console.php::run
                            -57 - /Applications/MAMP/ci4projs/cmseoign/spark::run

(using MySQL 5.7)

We have a test coverage in place for this, so I'm pretty confident that the code is working fine.

It's probably something with your local configuration. Please take a look at your log file located in writable/logs - you will find there a detailed database error message. I would guess that it's something related to the wrong socket path configuration in php.ini.

I've got the same issue trying to run the spark migrate commands, and have tried using hostname and ip address for mysql server in the db configurations in .env
I am using a basic xampp install with PHP 7.4.8 on Windows

An extract of my log is attached:
ci4-migrate-errors.txt

Please check if you're using the correct ini file in CLI php --ini. You may try checking the mysqli.default_socket value in php.ini.

Also, please check writable/logs/ for detailed info about the error.

Okay, first things first. To properly diagnose this recurring issue, we need to check the following:

  1. Please try using the latest develop branch whether the errors will still persist.
  2. If yes, please set logs threshold to 4 to see the actual error message thrown by the db connection.
  3. If using older versions of MySQL, can you check if the password hashing system is using the latest (caching_sha2_password) or using the older (mysql_native_password). Differences in the hashing for the MySQL server against the MySQL client can still lead to invalid connections.
  4. Without using CI4, can you confirm that you can manually connect to mysql, such as using MySQL Shell or MySQL Workbench.

I change the php version and I forgot to add the php7.*-mysql extension. You can check it.

You may run your app in your server, but write commands from local terminal. Solution for that you can try to connect with ssh to your server. In this situation you may create a new migration file from terminal, but can't run migrations. Because you try to run it from your localhost and your application is connected to server's mysql, so it can't access there.

As this is most likely to be a user configuration error more than a framework error, I'm closing this.

For php 7.4
image
image
Works for MySqli

image
SQLite3 - in web wroks
image
in CLI not
image

last log
CRITICAL - 2020-10-28 13:03:55 --> Unable to connect to the database.

0 C:\pwc\www\makisystem\Database\BaseConnection.php(646): CodeIgniter\Database\BaseConnection->initialize()

1 C:\pwc\www\makisystem\Database\BaseConnection.php(1548): CodeIgniter\Database\BaseConnection->query('SELECT "NAME" F...')

2 C:\pwc\www\makisystem\Database\BaseConnection.php(1591): CodeIgniter\Database\BaseConnection->listTables()

3 C:\pwc\www\makisystem\Database\MigrationRunner.php(945): CodeIgniter\Database\BaseConnection->tableExists('migrations')

4 C:\pwc\www\makisystem\Database\MigrationRunner.php(193): CodeIgniter\Database\MigrationRunner->ensureTable()

5 C:\pwc\www\makisystem\Commands\Database\Migrate.php(128): CodeIgniter\Database\MigrationRunner->latest(NULL)

6 C:\pwc\www\makisystem\CLI\Commands.php(100): CodeIgniter\Commands\Database\Migrate->run(Array)

7 C:\pwc\www\makisystem\CLI\CommandRunner.php(107): CodeIgniter\CLI\Commands->run('migrate', Array)

8 C:\pwc\www\makisystem\CLI\CommandRunner.php(85): CodeIgniter\CLI\CommandRunner->index(Array)

9 C:\pwc\www\makisystem\CodeIgniter.php(914): CodeIgniter\CLI\CommandRunner->_remap('index', 'migrate')

10 C:\pwc\www\makisystem\CodeIgniter.php(404): CodeIgniter\CodeIgniter->runController(Object(CodeIgniter\CLI\CommandRunner))

11 C:\pwc\www\makisystem\CodeIgniter.php(312): CodeIgniter\CodeIgniter->handleRequest(NULL, Object(Config\Cache), false)

12 C:\pwc\www\makisystem\CLI\Console.php(86): CodeIgniter\CodeIgniter->run()

13 C:\pwc\www\makispark(57): CodeIgniter\CLI\Console->run()

14 {main}

For php 7.3
[28-Oct-2020 18:20:20 UTC] PHP Fatal error: Uncaught Error: Call to undefined function CodeIgniter\locale_set_default() in C:\pwc\www\makisystem\CodeIgniter.php:184
Stack trace:

0 C:\pwc\www\makisystem\bootstrap.php(181): CodeIgniter\CodeIgniter->initialize()

1 C:\pwc\www\maki\public\index.php(36): require('C:\pwc\www\maki...')

2 {main}

thrown in C:\pwc\www\makisystem\CodeIgniter.php on line 184

I change php cli for php7.4 and works
image

Bug Description
Impossible to perform migrations through spark (experienced in versions 4.0.2 to 4.0.4)

Although DB credentials are set in .env and/orin app/Config/Database.php and successful query results obtained through Controller classes,

php spark migrate causes CodeIgniter\Database\Exceptions\DatabaseException

Exact command line output:


CodeIgniter CLI Tool - Version 4.0.4 - Server-Time: 2020-07-19 05:22:57am

Running all new migrations...
An uncaught Exception was encountered

Type:        CodeIgniter\Database\Exceptions\DatabaseException
Message:     Unable to connect to the database.
Filename:    /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/BaseConnection.php
Line Number: 425

  Backtrace:
                      -646 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/BaseConnection.php::initialize
                              -1548 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/BaseConnection.php::query
                              -1591 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/BaseConnection.php::listTables
                              -945 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/MigrationRunner.php::tableExists
                              -193 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/MigrationRunner.php::ensureTable
                              -128 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Commands/Database/Migrate.php::latest
                              -100 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CLI/Commands.php::run
                              -107 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CLI/CommandRunner.php::run
                              -85 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CLI/CommandRunner.php::index
                              -914 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CodeIgniter.php::_remap
                              -404 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CodeIgniter.php::runController
                              -312 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CodeIgniter.php::handleRequest
                              -86 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CLI/Console.php::run
                              -57 - /Applications/MAMP/ci4projs/deneme/spark::run

CodeIgniter 4 version
Last gotten in v4.0.4 release

Affected module(s)
spark

Expected behavior
All migrations performed successfully (without exceptions).

Steps to reproduce

  1. Open up a Terminal / command line window
  2. Change to the directory to the grand-parent level of your webserver's htdocs where htdocs will be substituted with the public directory of CI4
  3. composer create-project codeigniter4/appstarter deneme
  4. Open the project in your code editor and edit the .env file to set CI_ENVIRONMENT = development (duplicate it first from the file named 'env' if it doesn't exist - i.e. cp env .env)
  5. Perform one or both of steps 6 and 7:
  6. Enter DB credentials for MySQL in .env file (with database.default.DBDriver = MySQLi)
  7. Enter DB credentials in app/Config/Database.php
  8. Go back to the command line and run the following commands (in my example proj name is 'deneme':
    9: cd deneme
  9. php spark migrate:create AddBlog
  10. (Optional) Go back to your code editor and open the file whose name is resembles "2020_mm_dd_hhmmss_AddBlog.php" from app/Database/Migrations
  11. (Optional) Replace the contents of up() and down() methods with example from https://codeigniter.com/user_guide/dbmgmt/migration.html
  12. Go back to the command line and while still in the root directory of the CI4 project run
  13. php spark migrate
  14. Compare your Terminal output to the following output:
CodeIgniter CLI Tool - Version 4.0.4 - Server-Time: 2020-07-19 05:42:10am

Running all new migrations...
An uncaught Exception was encountered

Type:        CodeIgniter\Database\Exceptions\DatabaseException
Message:     Unable to connect to the database.
Filename:    /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/BaseConnection.php
Line Number: 425

  Backtrace:
                      -646 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/BaseConnection.php::initialize
                              -1548 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/BaseConnection.php::query
                              -1591 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/BaseConnection.php::listTables
                              -945 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/MigrationRunner.php::tableExists
                              -193 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Database/MigrationRunner.php::ensureTable
                              -128 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/Commands/Database/Migrate.php::latest
                              -100 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CLI/Commands.php::run
                              -107 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CLI/CommandRunner.php::run
                              -85 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CLI/CommandRunner.php::index
                              -914 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CodeIgniter.php::_remap
                              -404 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CodeIgniter.php::runController
                              -312 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CodeIgniter.php::handleRequest
                              -86 - /Applications/MAMP/ci4projs/deneme/vendor/codeigniter4/framework/system/CLI/Console.php::run
                              -57 - /Applications/MAMP/ci4projs/deneme/spark::run

Context

  • OS: Experienced in macOS 10.3
  • Web server: Apache 2.x (in MAMP 5.4)
  • PHP version 7.3.7
  • MySQL 5.7

Hello Guys,

Had similar issue and found that php' mysqli extension was disabled from ini config files.

Enabled it > Restarted Apache > executed spark commands > Works Fine!馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zenabus picture zenabus  路  4Comments

ChibuezeAgwuDennis picture ChibuezeAgwuDennis  路  5Comments

Iamscalla picture Iamscalla  路  4Comments

jim-parry picture jim-parry  路  4Comments

catwithredhat picture catwithredhat  路  4Comments