Kousha
Kousha

Reputation: 36219

Laravel Migration - Says unknown database, but it is created

when I use php artisan migrate, i get the error SQLSTATE[42000] [1049] Unknown database 'databaseName'.

But the database DOES exists! I even tried going back into terminal, logged into mysql and created the database again, and it said that database already exists!

Why is this giving me this error?

Upvotes: 4

Views: 12857

Answers (5)

Olie Cape
Olie Cape

Reputation: 1066

In my case, I had to create the database that matches the charset and collation.

database.php

'mysql' => [
  'driver' => 'mysql',
  'url' => env('DATABASE_URL'),
  'host' => env('DB_HOST', '127.0.0.1'),
  'port' => env('DB_PORT', '3306'),
  'database' => env('DB_DATABASE', 'test'),
  'username' => env('DB_USERNAME', 'test'),
  'password' => env('DB_PASSWORD', ''),
  'unix_socket' => env('DB_SOCKET', ''),
  'charset' => 'utf8mb4',
  'collation' => 'utf8mb4_unicode_ci',
  ...
],

If you are using TablePlus, you can simply do that when you create a new DB

enter image description here

Upvotes: 0

pera
pera

Reputation: 982

In my case I had MySQL installed on this port: 3308, and in Laravel env file there was 3306.

Upvotes: 0

user6008095
user6008095

Reputation: 31

I have the same problem. When I run: php artisan migrate. In my case I use Vagrant with scotch box.

To solve this, enter:

  1. vagrant ssh
  2. cd /var/www/yourproject
  3. php artisan migrate

And all work well.

Upvotes: 3

The Alpha
The Alpha

Reputation: 146191

In your app/config/database.php file change the default value from databaseName to a real database name that you are trying to use in your application, something like this (for mysql driver):

'mysql' => array(
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'your database name', //<-- put the database name
    'username'  => 'your user name',
    'password'  => 'your password',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
),

Upvotes: 2

majidarif
majidarif

Reputation: 19995

One thing could be your casing. If you read the docs on case sensitivity it says:

the case sensitivity of the underlying operating system plays a part in the case sensitivity of database and table names. This means database and table names are not case sensitive in Windows, and case sensitive in most varieties of Unix. One notable exception is Mac OS X, which is Unix-based but uses a default file system type (HFS+) that is not case sensitive.

Then go and check your application database setting found @ app/config/database.php.

Something that looks like this:

'mysql' => array(
    'read' => array(
        'host' => '192.168.1.1',
    ),
    'write' => array(
        'host' => '196.168.1.2'
    ),
    'driver'    => 'mysql',
    'database'  => 'databaseName',
    'username'  => 'root',
    'password'  => '',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
),

And double check everything.

Also try using snake_case rather than camelCase for you database name.

Upvotes: 0

Related Questions