Server to Server MySQL SSH Tunnel
Introduction
- Overview of Autossh and its benefits for SSH tunneling.
- Purpose: Guide on using Autossh for a stable MySQL tunnel between two servers.
Section 1: Installing Autossh
- Updating Package Lists:
- Command:
sudo apt-get update
. - Purpose: Ensures you have the latest package listings.
- Command:
- Installing Autossh:
- Command:
sudo apt-get install autossh
. - Note: Perform this on both servers.
- Command:
Section 2: Enabling SSH Tunneling on Remote Server
- Editing SSH Configuration:
- Command:
sudo nano /etc/ssh/sshd_config
. - Addition: Append
AllowTcpForwarding yes
to the file.
- Command:
- Restarting SSH Service:
- Command:
sudo service ssh restart
. - Purpose: Apply changes to SSH configuration.
- Command:
Section 3: Setting Up the SSH Tunnel with Autossh
- Establishing SSH Tunnel:
- Command:
autossh -M 0 -N -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -L 3306:localhost:3306 remote_user@remote_host
- Replace
remote_user
andremote_host
with appropriate credentials. - Explanation:
-M 0
: Disables monitoring mode.-N
: No command execution on remote.-o
options: Keepalive settings for stability.
- Command:
Section 4: Connecting to MySQL Through the Tunnel
- Using MySQL Client:
- Connect to
localhost:3306
. - Use remote MySQL credentials.
- Connect to
- Verification:
- Test the connection to ensure it’s operational.
Conclusion
- Recap of setting up and using Autossh for MySQL tunneling.
- Emphasize the reliability and automatic reconnection features.
Additional Resources
- Autossh documentation for advanced configurations.
- SSH and MySQL best practices.
Interactive Elements:
- Quiz: Test understanding of Autossh setup and SSH tunneling.
- Hands-on Lab: Practice setting up the SSH tunnel with Autossh.
- Discussion Board: Share experiences and troubleshoot common issues.
This lesson gives a comprehensive guide to using Autossh for a stable and reliable MySQL tunnel over SSH, ensuring a secure and consistent connection between servers.