Welcome Guest, Not a member yet? Register   Sign In
Problem with Forge createDatabase
#1

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 $forge = \Config\Database::forge('info'); 

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?
Reply
#2

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

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.
Reply
#4

(This post was last modified: 07-30-2021, 04:56 AM by Chroma. Edit Reason: Added possible solution. )

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.
Reply
#5

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.
Reply
#6

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

$query $db->query($sql); 
Try that.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB