How to connect to mysql with laravel?

How to connect to mysql with laravel?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Configuring MySQL Connections in Laravel Without Falling Into Common Pitfalls

Connecting a Laravel application to a local MySQL instance via WampServer or XAMPP is often where beginners encounter their first configuration headache. The frustration usually stems from a fundamental misunderstanding of how the framework handles database interactions versus how the underlying server manages them. When you run php artisan migrate and receive a PDOException stating that the database 'database' is unknown, it is not a bug in Laravel; it is a mismatch between your environment variables and your filesystem state.

The core issue lies in the config/database.php file located at the root of your project. While the source material suggests checking this file for correct settings, simply ensuring the user is set to root with an empty password is only half the battle. The critical step often overlooked is verifying that the database name defined in this configuration actually exists within your MySQL server.

When you execute commands like php artisan migrate:make create_tasks_table, Laravel attempts to connect immediately using the credentials and database name provided in the config array. If your .env file (which overrides defaults) or the direct config file points to a database named database, but that specific database has not been created in phpMyAdmin, the migration command will fail with an error code [HY000] [1049]. This is a standard SQL state indicating "Unknown database".

To resolve this, you must explicitly create the target database before running migrations. Using phpMyAdmin, navigate to the Databases tab and run CREATE DATABASE database; (or whatever name you chose). Alternatively, you can create it directly via the command line:

mysql -u root -e "CREATE DATABASE database CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"

It is important to note that Laravel does not automatically create the database for you upon installation. Unlike some SaaS platforms where the database is provisioned instantly, a local development environment requires manual setup. The framework assumes the infrastructure is already in place and trusts your configuration to point to it correctly.

Another common pitfall involves the DB_DATABASE key within your .env file. Even if you edit app/config/database.php, Laravel prioritizes the values defined in the .env file for security reasons. Ensure that DB_DATABASE, DB_USERNAME, and DB_PASSWORD in your .env file match exactly what is intended for your local setup. If you are using WampServer, ensure the MySQL service is running; otherwise, the connection attempt will time out or fail entirely before reaching the database name check.

Once the database exists, you can proceed with migrations. The command php artisan migrate will now successfully create the necessary tables based on your migrations directory. If you are using Backbone.js alongside Laravel, remember that this setup only handles the backend data layer. Your frontend assets and API endpoints must be correctly referenced to ensure the JavaScript framework can communicate with the newly created database structure.

While the source material suggests creating a database named database, developers often prefer names like laravel_local or project_name for clarity. The specific name does not matter as long as the configuration file matches the actual database in MySQL. However, consistency is key; changing the database name after the initial setup without updating the config will break your application immediately.

For production deployments, remember that the credentials used here are strictly for local development. Never commit your .env file to version control repositories containing sensitive information like passwords or hostnames. This practice ensures that while you share your codebase, you do not expose database access details to other developers or automated systems.

Our network