How to connect to mysql with laravel?

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Connecting Laravel to MySQL Through Localhost PhpMyAdmin: A Comprehensive Guide for Beginners

Connecting Laravel to your local MySQL database is relatively straightforward, but there are some important steps you need to follow to ensure a seamless integration. This blog post aims to help beginners understand how to establish the connection between Laravel and MySQL using Localhost PhpMyAdmin. Let's dive into the details.

Step 1: Install and Configure Laravel

To begin, you need a working Laravel installation. If you haven't set it up already, follow these instructions: 1. Download Laravel from Laravel's official website, and extract the files in your preferred directory. 2. Ensure you have a working PHP environment with the latest version of Composer installed. (Refer to Laravel's documentation for installing dependencies.) 3. Install Laravel using composer create-project laravel/laravel appname --prefer-dist. This will create a new Laravel application in your chosen directory. 4. Check that the default configuration is suitable: C:\wamp\www\laravel-project\app\config\database.php has 'root' as the username, and an empty password for now (we will change it later). MySQL is set to use root by default with no password under Localhost.

Step 2: Configure Laravel Database Connection

Open your chosen database.php file in the project directory. This file contains database connection details. In this case, we'll assume you are using a local MySQL server without any special configuration, so the default settings should suffice.
'mysql' => [
   'driver'    => 'mysql',
   'host'      => env('DB_HOST', 'localhost'),
   'port'      => env('DB_PORT', '3306'),
   'database'  => env('DB_DATABASE', 'laravel'),
   'username'  => env('DB_USERNAME', 'root'),
   'password'  => env('DB_PASSWORD', ''), // Change this to your MySQL root password if not empty
   'charset'   => 'utf8mb4',
   'collation' => 'utf8mb4_unicode_ci',
],
Adjust these settings according to your requirements: - Host: localhost or the IP address of your MySQL server (default is localhost) - Port: 3306, unless you've changed it in MySQL configuration - Database: The name assigned in phpMyAdmin (default is 'laravel') - Username: The username for accessing the database ("root" by default) - Password: Your MySQL root password if not empty

Step 3: Create and Migrate Tables

To create and manage your tables, we will use Laravel's built-in Artisan command line tool. Run the following commands in your project directory: 1. php artisan migrate:make create_tasks_table --table tasks --create creates a new migration file for creating the 'tasks' table. You can add columns, indexes, and other elements you need to this migration file. 2. php artisan migrate will execute the database migrations, including your newly created tasks table. This command may fail if the database does not exist or has incorrect credentials.

Step 4: Ensure Proper Database Configuration

The most common mistake beginners make is having a mismatch between their Laravel configuration and their actual MySQL server settings. Always double-check that your database name, username, password, and host details match in C:\wamp\www\laravel-project\app\config\database.php and your phpMyAdmin settings.

Conclusion

In this blog post, we learned how to connect Laravel with MySQL through Localhost PhpMyAdmin. We discussed the importance of configuring correct database connection details in Laravel's database.php file and creating a working environment for both Laravel and MySQL. By following these guidelines and paying attention to the matching configuration, you can establish a reliable connection between your Laravel application and the underlying database.