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

JavaScript

.env file

JavaScript

php.ini extensions

JavaScript

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