CodeIgniter4: Bug: php spark migrate throws wrongful CodeIgniter\Database\Exceptions\DatabaseException Unable to connect to the database
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
- Open up a Terminal / command line window
- 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
- composer create-project codeigniter4/appstarter deneme
- 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
) - Perform one or both of steps 6 and 7:
- Enter DB credentials for MySQL in .env file (with
database.default.DBDriver = MySQLi
) - Enter DB credentials in app/Config/Database.php
- Go back to the command line and run the following commands (in my example proj name is ‘deneme’:
9:
cd deneme
php spark migrate:create AddBlog
- (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
- (Optional) Replace the contents of up() and down() methods with example from https://codeigniter.com/user_guide/dbmgmt/migration.html
- Go back to the command line and while still in the root directory of the CI4 project run
php spark migrate
- 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
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 36 (12 by maintainers)
Have you tried changing
localhost
to127.0.0.1
?@lonnieezell
Following your suggestion, issue is resolved. Changing
localhost
to127.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 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.
Via controller/request is working but is not the same when I try to use commands via spark, no.
If you are using MAMP like I am you will have to provide the path to the php you want to use.
Example: /Applications/MAMP/bin/php/php7.4.21/bin/php spark <command>
I had the same issue, after checking the writable/logs file, the only error message was that class SQLite3 was not found… after installing php-sqlite3, the migration worked.
This is weird given that I’m not using sqlite…
at .env files are set the same:
example:
`
I just found a solution to migrate with
command()
function. But still, from cli it does not work.echo command("migrate");
in controller.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)