HASSANAL AGUAM
HASSANAL AGUAM

Reputation: 55

Connect my Laravel to a external Database

How can i connect my laravel to an extneral Database?

Example: I have a laravel on my local machine which is running on xammp. And i want it to connect to a cloud Server database.

Upvotes: 4

Views: 14880

Answers (5)

Suresh Ramani
Suresh Ramani

Reputation: 121

.env

DB_CONNECTION_SECOND=mysql
DB_HOST_SECOND=127.0.0.1
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=laravel
DB_USERNAME_SECOND=root
DB_PASSWORD_SECOND=

config/database.php

'mysql_second' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST_SECOND', '127.0.0.1'),
            'port' => env('DB_PORT_SECOND', '3306'),
            'database' => env('DB_DATABASE_SECOND', 'forge'),
            'username' => env('DB_USERNAME_SECOND', 'forge'),
            'password' => env('DB_PASSWORD_SECOND', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
        ],

If You are use Eloquent Model

add this line

protected $connection = "mysql_second";

If You are using Database Facades

DB::connection('mysql_second')->table('table_name')->get();

Upvotes: 2

Ger
Ger

Reputation: 258

1. Enable MYSQL Remote

First, you need to read your hosting docs to get your server data and instructions to enable mysql remote for your local IP by whitelisting your IP or if you want to allow access to anyone you can use wildcard %

in my case HostGator: https://www.hostgator.com/help/article/how-to-connect-to-the-mysql-database-remotely,

2. Change .env configuration

then fill .env file with the provided data by your hosting provider.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1 //replace with your external server IP
DB_PORT=3306 // check open port for mysql usually is 3306
DB_DATABASE=laravel // replace with the name of your external database
DB_USERNAME=root //replace with the username associated with database
DB_PASSWORD= //put your username password

Upvotes: 1

Jainam Shah
Jainam Shah

Reputation: 304

In .env file you can set DB_CONNECTION with your database name and applicable databases are given in /config/database.php which are (SQLite, MySQL, pgSQL, SQLSRV) after that just type your username, password, and database name and you can use that database with port number.

Upvotes: 2

Udhav Sarvaiya
Udhav Sarvaiya

Reputation: 10071

Open the .env file and edit it. Just set up correct external DB credentials:

DB_CONNECTION=mysql 
DB_HOST=127.0.0.1         // set external DB_Host 
DB_PORT=3306             // Your Database Port
DB_DATABASE=            // Your Database Name
DB_USERNAME=           // Your Database Username
DB_PASSWORD=          // Your Database Password

DB_USERNAME should be set to root if you do not have a default username in the installation time

If no password is set on the database, just clear it DB_PASSWORD

After .env edit, must be clear cache:

php artisan config:cache

Upvotes: 2

SayAz
SayAz

Reputation: 760

In .env(This is in root folder) file change below credentials accordingly with external DB connection:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

and in config/database.php change the below credentials if trying to connect with external MySQL otherwise there are more options for others or please mention from which DB you want to connect with

'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

Upvotes: 1

Related Questions