Skip to content
Advertisement

Laravel – SQLSTATE[HY000] [1049] Unknown database

Solved: Check your ports people!

.

Tools:

I am using Windows 10, Wamp 3.2, and Laravel. I have a Laravel 6.5.1 project which I backed up by zipping its www project file and I also downloaded the database in an “.sql” file using phpMyAdmin. I recently made a clean installation of Windows 10, and installed Wamp 3.2, composer and of course, Laravel. I have also installed php 7.4.1 separately, and I am currently using that in my PATH (instead of Wamp’s php 7.4.0).

Goal:

What I simply want to do now, is to restore my project so I can continue its development. I unzipped the project files into the www directory, and uploaded the database back into phpMyAdmin, with the exact name and collation as before. I also edited the PHP7 “php.ini” file, in order to allow the necessary extensions to run (for this step I copied the enabled extensions from wamp’s php 7.4.0 coherent file).

Problem:

At this point I would expect everything to run normally, but not exactly. The project does seem to run fine when the requested page does not involve the database (other projects that do not involve the database also work fine). When the database is involved, this error is thrown: “IlluminateDatabaseQueryException SQLSTATE[HY000] [1049] Unknown database ‘sportlog’ (relevant database query is placed right after)”.

Tried:

I tried some commands asked in this similar question, like php artisan cache:clear and php artisan migrate:install but none of these brought a different result. I changed the value of the DB_DATABASE variable of the .env file from “sportlog” to “space“, which is the name of another existing database, yet the same error occured, updated to “space” this time.

Can anyone help me solve this ?

database.php file

/*
    |--------------------------------------------------------------------------
    | Default Database Connection Name
    |--------------------------------------------------------------------------
    |
    | Here you may specify which of the database connections below you wish
    | to use as your default connection for all database work. Of course
    | you may use many connections at once using the Database library.
    |
    */

    'default' => env('DB_CONNECTION', 'mysql'),

    /*
    |--------------------------------------------------------------------------
    | Database Connections
    |--------------------------------------------------------------------------
    |
    | Here are each of the database connections setup for your application.
    | Of course, examples of configuring each database platform that is
    | supported by Laravel is shown below to make development simple.
    |
    |
    | All database work in Laravel is done through the PHP PDO facilities
    | so make sure you have the driver for your particular database of
    | choice installed on your machine before you begin development.
    |
    */
    'connections' => [
    
            'sqlite' => [
                'driver' => 'sqlite',
                'url' => env('DATABASE_URL'),
                'database' => env('DB_DATABASE', database_path('database.sqlite')),
                'prefix' => '',
                'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
            ],
    
            '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' => 'InnoDB', //default value was null, InnoDB is supposed to allow key constraints aka database foreign keys to work properly
                'options' => extension_loaded('pdo_mysql') ? array_filter([
                    PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
                ]) : [],
            ],
    
            'pgsql' => [
                'driver' => 'pgsql',
                'url' => env('DATABASE_URL'),
                'host' => env('DB_HOST', '127.0.0.1'),
                'port' => env('DB_PORT', '5432'),
                'database' => env('DB_DATABASE', 'forge'),
                'username' => env('DB_USERNAME', 'forge'),
                'password' => env('DB_PASSWORD', ''),
                'charset' => 'utf8',
                'prefix' => '',
                'prefix_indexes' => true,
                'schema' => 'public',
                'sslmode' => 'prefer',
            ],
    
            'sqlsrv' => [
                'driver' => 'sqlsrv',
                'url' => env('DATABASE_URL'),
                'host' => env('DB_HOST', 'localhost'),
                'port' => env('DB_PORT', '1433'),
                'database' => env('DB_DATABASE', 'forge'),
                'username' => env('DB_USERNAME', 'forge'),
                'password' => env('DB_PASSWORD', ''),
                'charset' => 'utf8',
                'prefix' => '',
                'prefix_indexes' => true,
            ],
    
        ],
    
        /*
        |--------------------------------------------------------------------------
        | Migration Repository Table
        |--------------------------------------------------------------------------
        |
        | This table keeps track of all the migrations that have already run for
        | your application. Using this information, we can determine which of
        | the migrations on disk haven't actually been run in the database.
        |
        */
    
        'migrations' => 'migrations',
    
        /*
        |--------------------------------------------------------------------------
        | Redis Databases
        |--------------------------------------------------------------------------
        |
        | Redis is an open source, fast, and advanced key-value store that also
        | provides a richer body of commands than a typical key-value system
        | such as APC or Memcached. Laravel makes it easy to dig right in.
        |
        */
    
        'redis' => [
    
            'client' => env('REDIS_CLIENT', 'phpredis'),
    
            'options' => [
                'cluster' => env('REDIS_CLUSTER', 'redis'),
                'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
            ],
    
            'default' => [
                'url' => env('REDIS_URL'),
                'host' => env('REDIS_HOST', '127.0.0.1'),
                'password' => env('REDIS_PASSWORD', null),
                'port' => env('REDIS_PORT', 6379),
                'database' => env('REDIS_DB', 0),
            ],
    
            'cache' => [
                'url' => env('REDIS_URL'),
                'host' => env('REDIS_HOST', '127.0.0.1'),
                'password' => env('REDIS_PASSWORD', null),
                'port' => env('REDIS_PORT', 6379),
                'database' => env('REDIS_CACHE_DB', 1),
            ],
    
        ],

.env file

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:somekey
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

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

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

php.ini extensions

extension=bz2
extension=curl
;extension=dba
;extension=com_dotnet
;extension=enchant
;extension=ffi
extension=fileinfo
;extension=ftp
extension=gd2
extension=gettext
extension=gmp
extension=intl
extension=imap
extension=ldap
extension=mbstring
extension=exif      ; Must be after mbstring as it depends on it
extension=mysqli
;extension=oci8_12c  ; Use with Oracle Database 12c Instant Client
;extension=odbc
extension=openssl
;extension=pdo_firebird
extension=pdo_mysql
;extension=pdo_oci
;extension=pdo_odbc
;extension=pdo_pgsql
extension=pdo_sqlite
;extension=pgsql
;extension=phpdbg_webhelper
;extension=shmop

; The MIBS data available in the PHP distribution must be installed.
; See http://www.php.net/manual/en/snmp.installation.php
;extension=snmp

extension=soap
extension=sockets
;extension=sodium
;extension=sqlite3
;extension=sysvshm
;extension=tidy
extension=xmlrpc
extension=xsl
;extension=zend_test

Advertisement

Answer

(TL;DR in bottom)

I finally found the problem, after various tries and after re-creating the whole project in Laravel 7, and it was not obvious at all. The idea for the solution came when I first tried to run the migrations for the new database. A couple of errors appeared, the last of which said: “PDO::__construct(“mysql:host=127.0.0.1;port=3306;dbname=sportlog”, “root”, “”, [])”.

After receiving that error, it hit me that something with the port (3306) must be wrong. I right clicked wamp’s icon and navigated to “Tools” and noticed that it said: “Port Used By MariaDB:3306“! And I also noticed right below that it said: “Port Used By MySQL:3308“! Then I edited the “.env” file and changed the “DB_PORT” value to 3308 (since I’m trying to use a MySQL database this whole time), checked back and everything worked fine (migrations run fine and the website itself also successfully connects to the database and fetches data).

In conclusion, it appears that the newer version(/s) of WAMP (I’m using version 3.2.0 right now) have changed the port for MySQL to 3308, while Laravel uses 3306 for MySQL by default (which is confirmed by the fact that both Laravel 6.5.* files (shown above) and the fresh Laravel 7.18.* installation use the port 3306 in both the “.env” and “database.php” files (meaning the default value of the latter: “‘port’ => env(‘DB_PORT’, ‘3306’),”)).

TL;DR

If you are getting the same error shown in the title of this post, and you use correct (/default) values of Laravel for the “.env” and “database.php” files, then check which port WAMP uses for the database which you are trying to connect to. Edit the files mentioned above by changing the port value to 3308 if you are trying to connect to a MySQL database and 3306 if you are trying to connect to a MariaDB database.

Of course, you can edit WAMP’s respective ports instead, so do as you wish.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement