How to Connect Remote Database in Laravel 5 using ssh

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

How to Connect Remote Databases in Laravel 5 using SSH: A Developer's Guide

Connecting a Laravel application to a remote database is a fundamental step in deployment, but when dealing with secured environments, the process often involves stepping outside the standard application layer and utilizing secure protocols like SSH. As a senior developer, understanding how these layers interact is crucial for robust and secure deployments.

This guide will walk you through the practical steps of accessing a remote MySQL server from your local machine via SSH and correctly configuring it within your Laravel 5 project's database.php file.

The Role of SSH in Remote Database Access

SSH (Secure Shell) is not the mechanism Laravel uses to talk directly to the database; rather, SSH serves as the secure gateway that allows you, the developer, to log into the remote server hosting the database and execute necessary commands, including setting up access permissions or verifying connection details.

When you are connecting a remote database, two primary concerns must be solved: network accessibility (firewalls) and authentication (credentials). SSH handles the authentication for accessing the operating system on the server, which is the prerequisite step before database access can occur.

Step 1: Establishing Secure SSH Connectivity

Before attempting any database connection, you must ensure your local machine can securely communicate with the remote server where the database resides.

  1. SSH Client: Use an SSH client (like PuTTY on Windows or the built-in terminal on Linux/macOS) to connect to the remote server using your provided credentials.
    ssh username@remote_server_ip
    
  2. Permissions Check: Once logged in, verify that the database user you intend to use (e.g., xxxxx_xx from your configuration) has the necessary permissions to connect from your specific IP address or network segment.
  3. Firewall Configuration: Crucially, ensure that the server's firewall (e.g., ufw or security groups in cloud environments) allows inbound traffic on the MySQL port (default 3306) from your connecting IP address. If this step is missed, even perfect credentials will fail due to network restrictions.

Step 2: Configuring the Laravel Database Connection

Once you have confirmed that the network path is open and the database user is valid, you can proceed with configuring Laravel. The configuration in database.php defines where Laravel should look for its data.

Based on your provided snippet, here is how that configuration translates into a real-world setup:

// config/database.php

'mysql' => [
    'driver'    => 'mysql',
    'host'      => 'www.xxxxx.in', // This IP must be reachable via SSH/network
    'port'      => '2222',        // Note: MySQL typically uses 3306, port 2222 is custom setup
    'database'  => 'xxxx_xxx',
    'username'  => 'xxxxx_xx',
    'password'  => 'xxxx0xx',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'strict'    => false,
    'engine'    => null,
],

Developer Insight: Notice that the host (www.xxxxx.in) and port (2222) must be correctly configured for network communication between your application server (where Laravel runs) and the database server. The SSH step ensures you have the necessary access to validate these parameters on the remote host before deploying this file.

Security Best Practices for Remote Connections

Connecting remotely introduces significant security risks. As a senior developer, you must implement robust security measures:

  1. Use SSH Keys: Avoid using passwords directly in scripts where possible. Configure SSH key-based authentication instead of relying solely on password entry via SSH.
  2. Principle of Least Privilege: Ensure the database user (xxxxx_xx) only has the minimum permissions required for the application to function (e.g., SELECT, INSERT, UPDATE on necessary tables, but not administrative rights).
  3. Environment Variables: Never hardcode sensitive credentials directly into your configuration files if you are deploying across multiple environments. Use Laravel's environment file (.env) and leverage environment variables for database connection details, which aligns with modern practices promoted by resources like those found on laravelcompany.com.

Conclusion

Connecting a remote database in Laravel 5 using SSH is less about the command itself and more about managing secure network access and credential management. The SSH session provides the necessary control to verify that the connection points defined in your database.php file are open, accessible, and properly authenticated. By treating the SSH connection as the foundation for network security and adhering to strong access controls, you ensure that your Laravel application remains both functional and secure in a remote environment.