Welcome Guest, Not a member yet? Register   Sign In
CI4 Spark migrate fails to connect to db with devilbox
#1

Hello all - first time poster here...

I am using a corporate restricted laptop requiring me to develop in containers.  I am using Devilbox in conjunction with Docker desktop, which is a tool that nicely puts together various LAMP stack options in containers to provide for a development environment.  Each component (httpd, mysql, php, etc) is in a separate container and thus has a different IP address. Devilbox requires that the IP address be used for the db hostname within its own internal network - the ports are then mapped to the host computer allowing for the host computer to appear as though it were running the application locally/natively.

Devilbox is up and running as is CI4 within the devilbox environment.  I have connectivity to the mysql db via CI4 and have validated it via the following code in the home::index controller:

PHP Code:
$db db_connect();
$db->listTables(); 



The CI4 debugger is reporting a good connection to the db via the code above:

PHP Code:
Database (1 total Query1 unique across 1 Connection)
Time Query String
4.39 ms SHOW TABLES FROM 
`ci4dbAPPPATH/Controllers/Home.php:11 


The issue is that when I attempt to run spark migrate from the command line, it cannot connect to the db.  The command used which fails is:
PHP Code:
php spark migrate --all 



The error message returned is the following:

PHP Code:
CodeIgniter v4.4.3 Command Line Tool Server Time2023-12-06 20:53:23 UTC+00:00

Running all 
new migrations...

[
CodeIgniter\Database\Exceptions\DatabaseException]

Unable to connect to the database.
Main connection [MySQLi]: Operation timed out

at SYSTEMPATH
/Database/BaseConnection.php:428

Backtrace
:
  1    SYSTEMPATH/Database/BaseConnection.php:575
      CodeIgniter
\Database\BaseConnection()->initialize()

  2    SYSTEMPATH/Database/BaseConnection.php:1410
      CodeIgniter
\Database\BaseConnection()->query('SHOW TABLES FROM `ci4db`')

  3    SYSTEMPATH/Database/BaseConnection.php:1429
      CodeIgniter
\Database\BaseConnection()->listTables()

  4    SYSTEMPATH/Database/MigrationRunner.php:764
      CodeIgniter
\Database\BaseConnection()->tableExists('migrations')

  5    SYSTEMPATH/Database/MigrationRunner.php:168
      CodeIgniter
\Database\MigrationRunner()->ensureTable()

  6    SYSTEMPATH/Commands/Database/Migrate.php:84
      CodeIgniter
\Database\MigrationRunner()->latest(null)

  7    SYSTEMPATH/CLI/Commands.php:65
      CodeIgniter
\Commands\Database\Migrate()->run([...])

  8    SYSTEMPATH/CLI/Console.php:46
      CodeIgniter
\CLI\Commands()->run('migrate', [...])

  9    ROOTPATH/spark:102
      CodeIgniter
\CLI\Console()->run() 


The logs for the error message are as follows:

PHP Code:
ERROR 2023-12-06 21:02:43 --> Error connecting to the databasemysqli_sql_exceptionOperation timed out in /Users/p482017/devilbox-3.0.0-4/data/www/ci4.db/htdocs/vendor/codeigniter4/framework/system/Database/MySQLi/Connection.php:184
Stack trace
:
#0 /Users/p482017/devilbox-3.0.0-4/data/www/ci4.db/htdocs/vendor/codeigniter4/framework/system/Database/MySQLi/Connection.php(184): mysqli->real_connect('172.16.238.12', 'root', Object(SensitiveParameterValue), 'ci4db', 3306, '', 0)
#1 /Users/p482017/devilbox-3.0.0-4/data/www/ci4.db/htdocs/vendor/codeigniter4/framework/system/Database/BaseConnection.php(392): CodeIgniter\Database\MySQLi\Connection->connect(false)
#2 /Users/p482017/devilbox-3.0.0-4/data/www/ci4.db/htdocs/vendor/codeigniter4/framework/system/Database/BaseConnection.php(575): CodeIgniter\Database\BaseConnection->initialize()
#3 /Users/p482017/devilbox-3.0.0-4/data/www/ci4.db/htdocs/vendor/codeigniter4/framework/system/Database/BaseConnection.php(1410): CodeIgniter\Database\BaseConnection->query('SHOW TABLES FRO...')
#4 /Users/p482017/devilbox-3.0.0-4/data/www/ci4.db/htdocs/vendor/codeigniter4/framework/system/Database/BaseConnection.php(1429): CodeIgniter\Database\BaseConnection->listTables()
#5 /Users/p482017/devilbox-3.0.0-4/data/www/ci4.db/htdocs/vendor/codeigniter4/framework/system/Database/MigrationRunner.php(764): CodeIgniter\Database\BaseConnection->tableExists('migrations')
#6 /Users/p482017/devilbox-3.0.0-4/data/www/ci4.db/htdocs/vendor/codeigniter4/framework/system/Database/MigrationRunner.php(168): CodeIgniter\Database\MigrationRunner->ensureTable()
#7 /Users/p482017/devilbox-3.0.0-4/data/www/ci4.db/htdocs/vendor/codeigniter4/framework/system/Commands/Database/Migrate.php(84): CodeIgniter\Database\MigrationRunner->latest(NULL)
#8 /Users/p482017/devilbox-3.0.0-4/data/www/ci4.db/htdocs/vendor/codeigniter4/framework/system/CLI/Commands.php(65): CodeIgniter\Commands\Database\Migrate->run(Array)
#9 /Users/p482017/devilbox-3.0.0-4/data/www/ci4.db/htdocs/vendor/codeigniter4/framework/system/CLI/Console.php(46): CodeIgniter\CLI\Commands->run('migrate', Array)
#10 /Users/p482017/devilbox-3.0.0-4/data/www/ci4.db/htdocs/spark(102): CodeIgniter\CLI\Console->run()
#11 {main}

Next CodeIgniter\Database\Exceptions\DatabaseExceptionOperation timed out in /Users/p482017/devilbox-3.0.0-4/data/www/ci4.db/htdocs/vendor/codeigniter4/framework/system/Database/MySQLi/Connection.php:229
Stack trace
:
#0 /Users/p482017/devilbox-3.0.0-4/data/www/ci4.db/htdocs/vendor/codeigniter4/framework/system/Database/BaseConnection.php(392): CodeIgniter\Database\MySQLi\Connection->connect(false)
#1 /Users/p482017/devilbox-3.0.0-4/data/www/ci4.db/htdocs/vendor/codeigniter4/framework/system/Database/BaseConnection.php(575): CodeIgniter\Database\BaseConnection->initialize()
#2 /Users/p482017/devilbox-3.0.0-4/data/www/ci4.db/htdocs/vendor/codeigniter4/framework/system/Database/BaseConnection.php(1410): CodeIgniter\Database\BaseConnection->query('SHOW TABLES FRO...')
#3 /Users/p482017/devilbox-3.0.0-4/data/www/ci4.db/htdocs/vendor/codeigniter4/framework/system/Database/BaseConnection.php(1429): CodeIgniter\Database\BaseConnection->listTables()
#4 /Users/p482017/devilbox-3.0.0-4/data/www/ci4.db/htdocs/vendor/codeigniter4/framework/system/Database/MigrationRunner.php(764): CodeIgniter\Database\BaseConnection->tableExists('migrations')
#5 /Users/p482017/devilbox-3.0.0-4/data/www/ci4.db/htdocs/vendor/codeigniter4/framework/system/Database/MigrationRunner.php(168): CodeIgniter\Database\MigrationRunner->ensureTable()
#6 /Users/p482017/devilbox-3.0.0-4/data/www/ci4.db/htdocs/vendor/codeigniter4/framework/system/Commands/Database/Migrate.php(84): CodeIgniter\Database\MigrationRunner->latest(NULL)
#7 /Users/p482017/devilbox-3.0.0-4/data/www/ci4.db/htdocs/vendor/codeigniter4/framework/system/CLI/Commands.php(65): CodeIgniter\Commands\Database\Migrate->run(Array)
#8 /Users/p482017/devilbox-3.0.0-4/data/www/ci4.db/htdocs/vendor/codeigniter4/framework/system/CLI/Console.php(46): CodeIgniter\CLI\Commands->run('migrate', Array)
#9 /Users/p482017/devilbox-3.0.0-4/data/www/ci4.db/htdocs/spark(102): CodeIgniter\CLI\Console->run()
#10 {main} 

I am looking for information preferably how to resolve this db connection issue for spark, but at a minimum, understanding on how spark works for db connections and why it appears to be different than the CI4 methods.
TIA...
Reply
#2

Try home controller as CLI? Connecting ok?
https://codeigniter4.github.io/userguide...un-via-cli

$ cd /path/to/project/public
$ php index.php home index
Simple CI 4 project for beginners codeigniter-expenses
Reply
#3

You need to go into the docker container to use PHP in the container.

If you want to use PHP on your host OS, you need to configure for it.
Reply
#4

(This post was last modified: 12-07-2023, 04:36 PM by Pizote.)

(12-07-2023, 02:51 PM)kenjis Wrote: You need to go into the docker container to use PHP in the container.

If you want to use PHP on your host OS, you need to configure for it.

Host computer is configured for PHP.  Otherwise, it would return that php can't be found, and spark migrate would not run at all.  spark migrate is running and attempting to connect.

[EDIT]
You are right. This seems to work - running it from the container. My apologies for the quick and inaccurate response. An explanation of why this is the case would be helpful to me.
Reply
#5

A docker container is kind of another PC in your Host PC.
That is, you have two (or more) PCs.

You just configured the docker containers by devilbox.
So PHP in the container can access MySQL in the container.

But you did not configure PHP (I don't know you installed PHP) on your host PC.
If you want to access MySQL in the container from your host PC,
you need to configure the network, and PHP and its database configuration.
Reply
#6

(12-07-2023, 05:05 PM)kenjis Wrote: A docker container is kind of another PC in your Host PC.
That is, you have two (or more) PCs.

You just configured the docker containers by devilbox.
So PHP in the container can access MySQL in the container.

But you did not configure PHP (I don't know you installed PHP) on your host PC.
If you want to access MySQL in the container from your host PC,
you need to configure the network, and PHP and its database configuration.

Yup - I got that much thx.  I have PHP installed on the host computer - I don't know why spark migrate didn't connect to the db, but it did run on the host, it just failed to complete. The host computer has access to the db, that's how I manage the db.

At this point, it is working, but I still don't understand why it didn't work from the host.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB