Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tenzan's avatar

Easy way to check if laravel app has connection with existing MySQL DB

I'm going to use an existing MySQL database and have setup in config/database.php :

  'connections' => [

        'mysql' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'database'  => env('DB_DATABASE', 'my-existing-db'),
            'username'  => env('DB_USERNAME', 'root'),
            'password'  => env('DB_PASSWORD', 'root'),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],

    ],

Now I need to check if it's able to connect. What's the easiest way?

For example, in Rails it will throw error during server startup if you have any incorrect information on settings.

0 likes
12 replies
zachleigh's avatar

I would just try to hit the database with a route in the browser.

TerrePorter's avatar
Level 12

A couple suggestions, from http://stackoverflow.com/questions/33432752/laravel-5-1-checking-a-database-connection

if(DB::connection()->getDatabaseName())
   {
     echo "conncted sucessfully to database ".DB::connection()->getDatabaseName();
   }

or catch the error and redirect

App::error(function(PDOException $exception)
{
    Log::error("Error connecting to database: ".$exception->getMessage());

    return "Error connecting to database";
});

both sound like good options to me.

9 likes
tenzan's avatar

@TerrePorter Hi!

For the option #1, where should I paste this code?

As for the option #2, it has comment in SO, that that solution is for Laravel 4...

TerrePorter's avatar

@tenzan it would depend on where you want to test the db connection and how you want to handle if it is not setup. You could put it in a controllers construct function, or in one of the service providers, or make your own service provider, or ... the list goes on.

1 like
tenzan's avatar

@TerrePorter

Either way is fine. Just want to see info that shows if connections established or not...

kashifkhan's avatar

I tried this code but it always shows "connected successfully to database", to my remote server database even when I disconnect my internet.

patgilmour's avatar

Laravel 5.8.* — the following quick and dirty database connection test works. In your fresh Laravel install, temporarily replace the default route with the following then hit the homepage:

use Illuminate\Database\Connection;

Route::get('/', function () {

    // Test database connection
    try {
        DB::connection()->getPdo();
        echo "Connected successfully to: " . DB::connection()->getDatabaseName();
    } catch (\Exception $e) {
        die("Could not connect to the database. Please check your configuration. error:" . $e );
    }

    return view('welcome');
});
5 likes
giovannipds's avatar

The most appropriate way I've found is atapp\Providers\AppServiceProvider.php:

<?php

namespace App\Providers;

use DB;
use Exception;
use Illuminate\Support\ServiceProvider;
use PDOException;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        // Handle offline database
        try {
            DB::connection()
                ->getPdo();
        } catch (Exception $e) {
            abort($e instanceof PDOException ? 503 : 500);
        }

        // ...
    }

    // ...
}
1 like
hanisharun's avatar

Is there any idea to check whether the db allow or refused a connection? I got this error at certain time when connect to external mysql db.

SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused

Please or to participate in this conversation.