skip to Main Content

I got Laravel SQLSTATE[HY000] [1049] Unknown database Error, when I try to migrate using php artisan migrate:install/ migrate. I have properly named database on my localhost SQL server. I’m using XAMPP to run it.

here’s my .env:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3307
DB_DATABASE=todo
DB_USERNAME=root
DB_PASSWORD=root

here’s database.php:

'default' => env('DB_CONNECTION', 'mysql'),
'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_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'),
            ]) : [],
        ],

also here’s create_todo_table.php file:

<?php

use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;

class CreateTodoTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('todo', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->string('yettodo');
            $table->string('done');
            $table->string('user');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('todo');
    }
}

but constantly I keep getting this:

C:UsersgracjtodotodovendorlaravelframeworksrcIlluminateDatabaseConnectorsConnector.php:70
      PDOException::("SQLSTATE[HY000] [1049] Unknown database 'todo'")

I am fully aware that there are plenty of threads like this on the Internet. The case is I feel like I’ve tried everything I could find (including installing different MySQL version – now I’m on 5.7, earlier I was on 8.0). I feel hopeless.

I had to change DB_PORT to 3307 since I had "3306 is occupied" error. I’m on Laravel 8 and PHP 8.0.2.

Please help!

3

Answers


  1. Chosen as BEST ANSWER

    Ok, fixed - very counterintuitive. Here's what I did.

    I deleted and created todo database on my server couple of times, every time "php artisan config:clear" afterwards. I did try it earlier, BUT, I changed the DB_PORT in my .env file back to 3306 EVEN TOUGH XAMPP says it's busy, so in XAMPP I had to change it to 3307 (image below). As you can see, these aren't the same! Could you explain that? I'm worried since it might work just once and after few lines of code or migrations, it is possible for it to come back...

    Different PORTS


  2. First set your Laravel .env file

    DB_CONNECTION=mysql
    DB_HOST=127.0.0.1
    DB_PORT=3306
    DB_DATABASE=todo
    DB_USERNAME=forge
    DB_PASSWORD=forge
    

    if so then run:

    php artisan config:clear
    
    Login or Signup to reply.
  3. Faced a similar issue. However, mine was solved after creating a table on myphpAdmin.
    Try:
    open XAMP->click on admin in MySQL row and create database as stated in your DB_DATABASE=

    try running php artisan migrate again.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search