I have a task which is described like "Creating a new database with Yii2 migration, and use it by default". But I have some collusion: I have to set up at lest one default database in my `main-local.php` file to start migration. But also I have to create this database with initial migration.

```
return [
    'db' => [
        'class' => 'yii\db\Connection',
        'dsn' => 'mysql:host=localhost;dbname=default_base',
        'username' => 'root',
        'password' => '',
        'charset' => 'utf8',
    ],
];

```

How can I deal with it? To create a database with migration and set up as default one.

When I execute my migration (there is code from my migration file):

```
public function safeUp()
{
    $this->execute("CREATE DATABASE default_base");
}

```

I have an exception:

```
General error: 1007 Can't create database 'default_base'; database exists

```

I realize that is because I have already created default\_base in phpmyadmin (click button "created database"), and connected it with app in my `main-local.php` file. 
But I need this base to be created only when I execute `yii migrate`.

**UPD**

I have tried the way rob006 recommended but I get connection error

```
Exception 'yii\db\Exception' with message 'SQLSTATE[HY000] [1049] Unknown database 'default_base''

```

My main-local.php looks like the way exactly rob006 recommends, but I still can`t migrate. It works only if I set:

```
'preinstallDb' => [
                  'class'    => 'yii\db\Connection',
                  'dsn'      => 'mysql:host=localhost',
                  'username' => 'root',
                  'password' => '',
                  'charset'  => 'utf8',
            ],
            'db'           => [
                  'class'    => 'yii\db\Connection',
                  'dsn'      => 'mysql:host=localhost;dbname=already_exists_db',
                  'username' => 'root',
                  'password' => '',
                  'charset'  => 'utf8',
            ],

```

With the already exists database. What do I do wrong again?