CodeIgniter Forums
Problem with Forge createDatabase - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Problem with Forge createDatabase (/showthread.php?tid=76792)



Problem with Forge createDatabase - Ken McClain - 06-20-2020

I'm using CI 4.0.3
I get an exception 'unknown database 'ikl70fxb_TestDB' ' when I try to create a database defined in my default configuration.

I can make it work by creating another configuration called 'info' and setting 
'database' => 'information_schema' and then passing it to [font=Monaco, Consolas, Courier, monospace]$forge = \Config\Database::forge('info'); [/font]

This is my database config:
PHP Code:
    public $default = [
        
'DSN'      => '',
        
'hostname' => 'localhost',
        
'username' => 'ikl70fxb_root',
        
'password' => '123abc',
        
'database' => 'ikl70fxb_TestDB',
        
'DBDriver' => 'MySQLi',
        
'DBPrefix' => '',
        
'pConnect' => false,
        
'DBDebug'  => (ENVIRONMENT !== 'production'),
        
'cacheOn'  => false,
        
'cacheDir' => '',
        
'charset'  => 'utf8',
        
'DBCollat' => 'utf8_general_ci',
        
'swapPre'  => '',
        
'encrypt'  => false,
        
'compress' => false,
        
'strictOn' => false,
        
'failover' => [],
        
'port'     => 3306,
    ]; 


This is how how I call it:
PHP Code:
        $forge = \Config\Database::forge();
        $forge->createDatabase('ikl70fxb_TestDB',TRUE); 

Any ideas?


RE: Problem with Forge createDatabase - InsiteFX - 06-21-2020

In order to initialize the Forge class, your database driver must already be running, since the forge class relies on it.
But your database listed in database.php has not been created yet so it can not run.

In other words you are trying to use the database but it has not been created yet.


RE: Problem with Forge createDatabase - Ken McClain - 06-21-2020

So in order to create a database that doesn’t exist, you first need to connect to a database that does exist. That explains why connecting to ‘information_schema’ allowed me to create my database.


RE: Problem with Forge createDatabase - Chroma - 07-30-2021

Is there a way of doing this cleanly, so that we can connect to the database server and then create a new database on that database server.

This used to work before. I think, as I just deleted a database from the MySQL server that was only created recently.

This is what I did as a work around, it seems to work, but my be a bit skippy.

My init forge looks like this...

PHP Code:
$forge Database::forge('db_init'); 
Then in the Config/Database.php file I added a new connection...

PHP Code:
/**
 * The db_init dummy database connection.
 * 
 * Needed to enable the system to connect to the server,
 * but not to any database, this enable the creation of a
 * new database.
 *
 * @var array
 */
 
public $db_init = [
 
'DSN' => '',
 
'hostname' => 'localhost',
 
'username' => 'user',
 
'password' => 'pass',
 
'database' => '',
 
'DBDriver' => 'MySQLi',
 
'DBPrefix' => '',
 
'pConnect' => false,
 
'DBDebug' => (ENVIRONMENT !== 'production'),
 
'charset' => 'utf8',
 
'DBCollat' => 'utf8_general_ci',
 
'swapPre' => '',
 
'encrypt' => false,
 
'compress' => false,
 
'strictOn' => false,
 
'failover' => [],
 
'port' => 3306,
 ]; 

You will of course need to set the username and password fields correctly.


RE: Problem with Forge createDatabase - paulbalandan - 07-30-2021

You can use the spark command
PHP Code:
php spark db:create <your-database-name

I have fixed this command to allow creating databases even if there is no existing databases yet. Try it out.


RE: Problem with Forge createDatabase - InsiteFX - 07-31-2021

You can also do it using db query like so.
PHP Code:
$dbName 'test';
$sql "CREATE DATABASE {$dbName}";

$query $db->query($sql); 
Try that.