简体   繁体   中英

how can I solve laravel php artisan migrate error problem?

I'm using laravel socialite to make facebook login, following this website: Social Logins with Socialite

as soon as I type a command in the terminal:

php artisan migrate

This shows the error:

Illuminate\Database\QueryException: SQLSTATE[HY000] [1049] Unknown database 'laravel' (SQL: select * from information_schema.tables where table_schema = laravel and table_name = migrations and table_type = 'BASE TABLE')

Open the .env file and edit it. Just set up correct DB credentials:

DB_CONNECTION=mysql 
DB_HOST=127.0.0.1 
DB_PORT=3306 
DB_DATABASE=            // Your Database Name
DB_USERNAME=           // Yout Database Username
DB_PASSWORD=          // Your Database Password 

The DB_USERNAME should be set to root if you do not have a default username

If no password is set on the database, clear it DB_PASSWORD

After completion of .env edit must be clear cache: php artisan config:cache

After Run the migrate Artisan command: php artisan migrate

The database called laravel cannot be found in PhpMyAdmin

Please follow these steps to solve this problem :

  1. Go to phpmyadmin in your localhost dashboard

  2. Create a database and name it laravel

  3. run the php artisan migrate CMD or Terminal of your editor

I had the same issue But changing localhost with 127.0.0.1 solved it for me

DB_CONNECTION=mysql
DB_PORT=3306
DB_HOST=127.0.0.1
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

Question is not about facebook login. You migration command could not establish connection with your database actually.

Check you .env file, you need to have something like this

DB_CONNECTION=mysql
DB_PORT=3306
DB_HOST=localhost
DB_DATABASE=laravel // This is line which you need to update, set here the real database name
DB_USERNAME=root
DB_PASSWORD=

If you have not file .env you can rename .env.example to .env , setup you db configs, and you will be able to migrate your tables with migrate command.

If you want to call you db as laravel, it means you forgot to create that database, first create database using command line with mysql or any application which can work with mysql (phpMyAdmin, DataGrip, phpStorm, jetbrain Db, etc)

Looks like you forgot to setup your Laravel application. You can specify database connections and other basic settings in your .env file in the root of your project.

At first stance this points to a problem in the .env and ./config/database.php files. In the .env file you should have something like this

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

and in ./config/database.php something like this

'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DATABASE_HOST','localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DATABASE_NAME','laravel'),
            'username' => env('DATABASE_USERNAME','root'),
            'password' => env('DATABASE_PASSWORD',''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

If your console were showing you something like this

Database hosts array is empty. (SQL: select * from information_schema.tables where table_schema = laravel and table_name = migrations and table_type = 'BASE TABLE')

you had to ensure to have specified the host. This would've happen if you had in ./config/database.php something like this (notice that it doesn't refer 'localhost' like before)

'host' => env('DATABASE_HOST'),

Assuming all is fine here and you have the database name you want to use, then it means that your database doesn't exist. Just go to phpmyadmin, or so, and create one.

The issue for me was the database.php config file did not have the PORT config, so it was default to 3306, but my database used a different port. I just added the line of config to the mysql config:

'mysql' => [
'port' => env('DB_PORT', '3306')
]

Solution founded. I had the same problem. the reason for the problem is there is no any database called "laravel" in phpmyadmin. It may be deleted or renamed. So check whether there is a database called 'laravel'. If it's not present create database in phpmyadmin using the name,

laravel

After that run the command,

php artisan migrate

This worked for me

从上面的答案中可以看出,您需要更改 .env 文件中的数据库名称,请确保您在 .env 而不是 .env.example 文件上进行更改

php artisan route:cache

apply this command and solved my error

i solved the problem by writing the database name in single quotes...

DB_HOST=localhost
DB_PORT=3306
DB_DATABASE='laravel'
DB_USERNAME=root
DB_PASSWORD=

I had the same issue But changing DB_HOST=127.0.0.1 to DB_HOST:localhost solved it for me

DB_HOST=localhost //DB_HOST=127.0.0.1 to DB_HOST:localhost solved
DB_PORT=3306
DB_DATABASE=laravel_multiauth
DB_USERNAME=root
DB_PASSWORD=

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM