Updated June 2026. Tested on Laravel 13 and PHP 8.4.
MySQL is the default database for most Laravel apps, and connecting to it is mostly a matter of filling in your .env. Here is the whole thing.
Set the .env
The values for your machine go in .env.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=techalyst
DB_USERNAME=techalyst
DB_PASSWORD=secret
DB_HOST is usually 127.0.0.1 for a database on the same machine, or the server's IP if it is remote. DB_PORT is 3306 unless yours runs on a different port.
The connection in config/database.php
You normally do not edit this, but it helps to see what those .env values feed into. Laravel ships with a mysql connection already defined.
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'laravel'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
],
A word on the charset. Use utf8mb4, not the older utf8. MySQL's utf8 is a three byte encoding that cannot store emojis or some characters, which leads to the classic "incorrect string value" error. utf8mb4 is full Unicode and is the right default.
Make sure the PDO extension is there
Laravel talks to MySQL through PHP's pdo_mysql extension. On most setups it is already enabled, but if you get a "could not find driver" error, that is the cause. Check with:
php -m | grep pdo_mysql
If it is missing, install it (sudo apt install php8.4-mysql on Ubuntu) and restart PHP.
Test the connection
The quickest check is to run a migration or ping the database from tinker.
php artisan migrate
If it runs without a connection error, you are connected. Questions about connecting Laravel to MySQL are welcome in the comments.
All comments ()
No comments yet
Be the first to leave a comment on this post.