mysql connect to remote database To connect remote database using ssh tunnel in laravel Example with demo step by step bellow.
mysql connect to remote database
We are using Laravel latest version. We can only connect to the remote server via a ssh tunnel. Quick way of connecting remote database with SSH key.
Step 1: Open SSH Tunnel
Syntax:
ssh -N -L 13306:127.0.0.1:3306 [USER]@[SERVER_IP]
Example:
ssh -N -L 13306:127.0.0.1:3306 [email protected]
Example with SSH Key:
ssh -i ./path/to/id_rsa -N -L 13306:127.0.0.1:3306 [email protected]
Step 2: Add MySQL Configuration
.env
... SERVER_DB_CONNECTION=mysql SERVER_DB_HOST=127.0.0.1 SERVER_DB_PORT=13306 SERVER_DB_DATABASE=tamilrokers_v1 SERVER_DB_USERNAME=manDsu_54 SERVER_DB_PASSWORD=example455455@555 ...
Don’t miss : PHP MySQL Connect Database Script
config/database.php
... 'server_mysql' => [ 'driver' => 'mysql', 'host' => env('SERVER_DB_HOST', '127.0.0.1'), 'port' => env('SERVER_DB_PORT', '3306'), 'database' => env('SERVER_DB_DATABASE', 'forge'), 'username' => env('SERVER_DB_USERNAME', 'forge'), 'password' => env('SERVER_DB_PASSWORD', ''), ], ...
Step 3: Connect to Remote Server Database
routes/web.php
Route::get('/server-db', function () { $details = \DB::connection('server_mysql') ->table('tamilrokers') ->get() ->toArray(); dd($details); });
Result
Array ( [0] => stdClass Object ( [id] => 1 [name] => pakainfo [detail] => pakainfo.com demo [created_at] => 2021-01-05 03:30:51 [updated_at] => 2021-01-05 03:31:07 ) )
I hope you get an idea about mysql connect to remote database.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.