Laravel Connect Remote Database using SSH Tunnel Example
Hi,
This tutorial will give you example of laravel ssh tunnel mysql. you will learn laravel connect database using ssh tunnel. I’m going to show you about laravel php connect to database through ssh tunnel. This article goes in detailed on laravel database connection through ssh. Alright, let’s dive into the steps.
In this tutorial, i will show you how to connect server database using ssh tunnel in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version. you need to just follow bellow step and you can connect to remote database.
Sometime we need to connect server database in another server or in local environment at that time we can use ssh tunnel to connect remote server database. ssh tunnel will allows to connect database using port. so let's follow bellow steps:
Step 1: Open SSH Tunnel
You need to go on your server(root@111.111.111) and run following command:
sudo nano /etc/ssh/sshd_config
Now, let's find GatewayPorts and make it Yes as like the bellow command:
GatewayPorts yes
Then you need to restart your sshd server with following command:
sudo systemctl restart sshd
sudo service sshd restart
Step 2: Connect SSH Tunnel
you can open ssh tunnel using ssh command as bellow:
Syntax:
ssh -N -L 13306:127.0.0.1:3306 [USER]@[SERVER_IP]
Example:
ssh -N -L 13306:127.0.0.1:3306 root@111.111.111
Example with SSH Key:
ssh -i ./path/to/id_rsa -N -L 13306:127.0.0.1:3306 root@111.111.111
now, it will open ssh tunnel, but if you want to keep alive open that ssh tunnel then you can follow bellow tutorial:
How to Reliably Keep an SSH Tunnel Open in Server?
Step 3: Add MySQL Configuration
next, we need to add another mysql configure in database.php file. so let's add server mysql database username and password on .env file and use it in database.php file:
.env
...
SERVER_DB_CONNECTION=mysql
SERVER_DB_HOST=127.0.0.1
SERVER_DB_PORT=13306
SERVER_DB_DATABASE=laravel_demo
SERVER_DB_USERNAME=demo
SERVER_DB_PASSWORD=demo123456
...
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 4: Connect to Remote Server Database
Here, we will create simple route and get server database table records and print it. so let's add following route:
routes/web.php
Route::get('/server-db', function () {
$records = \DB::connection('server_mysql')
->table('products')
->get()
->toArray();
dd($records);
});
Output:
Array
(
[0] => stdClass Object
(
[id] => 1
[name] => ItSolutionStuff
[detail] => itsolutionstuff.com demo
[created_at] => 2022-01-05 03:30:51
[updated_at] => 2022-01-05 03:31:07
)
)
Now, you can check your own.
i hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Generate and Add SSH Key in Github?
- How to Install Laravel in Ubuntu Server?
- How to Reliably Keep an SSH Tunnel Open in Server?
- How to Connect SSH using ppk File Ubuntu?
- SSH Command to Connect to Server using Pem File Example
- How to Connect to a Remote Server using SSH on Ubuntu?
- Laravel Firebase Push Notification to Android and IOS App Example
- How to Override Auth Login Method in Laravel 8?
- Laravel Two Factor Authentication using Email Tutorial
- How to Get Last 12 Months Data in Laravel?
- Laravel 8 Multiple Database Connection Example