0

I am using Laravel 5 and I am having problems with my database conneciton:

Here is my database.php file:

'mysql' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', 'localhost'),
        'database'  => env('DB_DATABASE', 'my_scene'),
        'username'  => env('DB_USERNAME', 'root'),
        'password'  => env('DB_PASSWORD', 'root'),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
        'engine'    => null,
    ],

and here is my .env file:

DB_HOST=localhost

DB_DATABASE=my_scene

DB_USERNAME=root

DB_PASSWORD=null

however when i then run "php artisan migrate" i get the error

[PDOException] SQLSTATE[HY000] [1049] Unknown database 'my_scene'

I have tried php artisan cache:clear and config:cache and also reloaded my xampp controller but it has not worked.

any ideas?

4
  • I think it's trying with null as a string. Try DB_PASSWORD= and env('DB_PASSWORD', ''), instead Commented Feb 24, 2016 at 14:14
  • still giving me an error 1049 unknown database Commented Feb 24, 2016 at 14:33
  • Try to unset DB_PASSWORD (remove) from .env file and set env('DB_PASSWORD', null). I've never try with a null password. I thought it was empty, nor null. Commented Feb 24, 2016 at 14:55
  • still saying unknown database Commented Feb 24, 2016 at 15:03

3 Answers 3

4

Its not a connection error.

[PDOException] SQLSTATE[HY000] [1049] Unknown database 'my_scene'

indicates you've successfully connected to MySQL server. but there is no database matching the name my_scene

You can create a database by using any of the following methods.

1. MySQL Command Line Utility

You just have to create a database by executing this MySQL statement in your terminal

CREATE DATABASE my_scene;

read Creating Database via terminal

2. PHPMyAdmin

If you've a MySQL GUI i.e PHPMyAdmin you can easily create a database.

Refer to this article to read more about creating databases via PHPMyAdmin

3. Laravel's Command Line Utility [Artisan]

Maybe the simplest option using Laravel's artisan utility

  1. Open Your terminal
  2. Run php artisan tinker
  3. Use the following snippet to create your database

    DB::connection()->statement('CREATE DATABASE my_scene');

Sign up to request clarification or add additional context in comments.

5 Comments

what is the command for creating a database in terminal? also i am simply trying to access the user db that is already created in laravel
You've to create a database before running you migrations. see my updated answer to read how you can create a database via Terminal by using MySQL command line utility
i mam simply trying to migrate the create users table and the create password table that are preinstalled with laravel
to create users table first you need a database and this is what you've to create.
checkout the 3rd option if you are not familiar with MySQL and PHPMyAdmin
0

In your config file, make DB_PASSWORD equal the empty string "" and leave the DB_PASSWORD out of the .env file.

Or, you can remove the null and leave the DB_PASSWORD as empty:

DB_PASSWORD=

This will signify an empty string for the password.

5 Comments

Does the database exist?
what do you mean does that database exist?
You need to create the database before you can successfully connect to it. Laravel doesn't create that for you.
but does the user table and password reset table not count as pre installed tables?
Tables are not databases. You must create a database first, then use Laravel to create the users and password resets table.
0

I was facing the same error and I was able to solve it.

I'm using Laravel 7 with WampServer 3.2 and I have installed two databases MySQL and Maria DB

So I created the database with MySQL using PhpMyAdmin and I was getting the 1049 error when using php artisan migrate

Then I created the database with MariaDB (using PhpMyAdmin) and the Migration was successful so Laravel was trying to connect to MariaDB database but there was no database (with the correct connection values)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.