title: How to create a new database by Yii2 migration, when I have to use it by default?
body: <p>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 <code>main-local.php</code> file to start migration. But also I have to create this database with initial migration.</p>
  
  <pre><code>return [
      'db' =&gt; [
          'class' =&gt; 'yii\db\Connection',
          'dsn' =&gt; 'mysql:host=localhost;dbname=default_base',
          'username' =&gt; 'root',
          'password' =&gt; '',
          'charset' =&gt; 'utf8',
      ],
  ];
  </code></pre>
  
  <p>How can I deal with it? To create a database with migration and set up as default one.</p>
  
  <p>When I execute my migration (there is code from my migration file):</p>
  
  <pre><code>public function safeUp()
  {
      $this-&gt;execute("CREATE DATABASE default_base");
  }
  </code></pre>
  
  <p>I have an exception:</p>
  
  <pre><code>General error: 1007 Can't create database 'default_base'; database exists
  </code></pre>
  
  <p>I realize that is because I have already created default_base in phpmyadmin (click button "created database"), and connected it with app in my <code>main-local.php</code> file. 
  But I need this base to be created only when I execute <code>yii migrate</code>.</p>
  
  <p><strong>UPD</strong></p>
  
  <p>I have tried the way rob006 recommended but I get connection error</p>
  
  <pre><code>Exception 'yii\db\Exception' with message 'SQLSTATE[HY000] [1049] Unknown database 'default_base''
  </code></pre>
  
  <p>My main-local.php looks like the way exactly rob006 recommends, but I still can`t migrate. It works only if I set:</p>
  
  <pre><code>'preinstallDb' =&gt; [
                    'class'    =&gt; 'yii\db\Connection',
                    'dsn'      =&gt; 'mysql:host=localhost',
                    'username' =&gt; 'root',
                    'password' =&gt; '',
                    'charset'  =&gt; 'utf8',
              ],
              'db'           =&gt; [
                    'class'    =&gt; 'yii\db\Connection',
                    'dsn'      =&gt; 'mysql:host=localhost;dbname=already_exists_db',
                    'username' =&gt; 'root',
                    'password' =&gt; '',
                    'charset'  =&gt; 'utf8',
              ],
  </code></pre>
  
  <p>With the already exists database. What do I do wrong again?</p>