MySQL Database Configuration Connection in Laravel

Posted on

This is practically an article shown how to connect to MySQL Database Server configuration connection in Laravel framework. It is quite simple, just try to edit the following file named ‘.env’ which is located in the root folder of web-based application powered by Laravel framework.

Apparently, there is another file which is responsible for defining database configuration connection. It is named database.php and it is located in config folder. It is a default configuration file in the Laravel framework provided. Below is the content which is shown only the database configuration part which is mainly using MySQL Database for an example :

/*   |-----------------------------------------------------------------------
| Database Connections   |-----------------------------------------------------------------------
|
| Here are each of the database connections setup for your application.
| Of course, examples of configuring each database platform that is
| supported by Laravel is shown below to make development simple.
|
|
| All database work in Laravel is done through the PHP PDO facilities
| so make sure you have the driver for your particular database of
| choice installed on your machine before you begin development.
|
*/
    'connections' => [

        'mysql' => [
            'driver' => 'mysql',
            'host' => '127.0.0.1',
            'port' => '3306',
            'database' => 'test',
            'username' => 'root',
            'password' => 'myown-password',
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

    ],

As shown in the above snippet code of configuration consists of several lines taken from the file named database.php located in the ‘config’ folder stored in the root folder of Laravel-based web application, it is clearly defined all the parameters needed in other for the web-based application connected to the associated MySQL Database Server.

But Laravel framework is giving a practical implementation of connecting the application to the certain database. It is by the usage of the .env file which is placed in the root folder of web-based application installed with a Laravel framework.

As we can see, in the development process of web-based application, there might be a possibilities of several people involved. Furthermore, those people indeed have their own specific MySQL Database configuration themselves or their own specific database connection parameter. To make it easy without having to edit the core configuration file of Laravel itself, every person can actually edit the file specifically intended for this purpose. Below is the content of the file by default :

/*    |-----------------------------------------------------------------------
| Database Connections   |-----------------------------------------------------------------------
|
| Here are each of the database connections setup for your application.
| Of course, examples of configuring each database platform that is
| supported by Laravel is shown below to make development simple.
|
|
| All database work in Laravel is done through the PHP PDO facilities
| so make sure you have the driver for your particular database of
| choice installed on your machine before you begin development.
|
*/

  'connections' => [

      'sqlite' => [
          'driver' => 'sqlite',
          'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
        ],

      
        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

        'pgsql' => [
            'driver' => 'pgsql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '5432'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'schema' => 'public',
            'sslmode' => 'prefer',
        ],

The database configuration connection defined above exists in database.php configuration file by default which is located in the config folder.

But it will be overridden by the configuration which is defined in the file named .env located in the root folder of web-based application powered by a Laravel framework as shown below :

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

The database configuration defined in .env file will be considered as the valid database connection before Laravel decided to look into the database configuration defined in config/database.php. But in this context, although Laravel look in the  database configuration connection defined in the file named .env, the database configuration connection defined in  the config/database.php file cannot be erased. As long as there is the proper definition of mysql connection configuration needed which is shown in the following  config/database.php file :

        'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => true,
            'engine' => null,
        ],

If the above entry is erased from config/database.php, although the database configuration connected is defined properly in .env file, the following error will be presented upon the execution of web-based application powered by Laravel accessing database or performing any database operation :

One thought on “MySQL Database Configuration Connection in Laravel

Leave a Reply