• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SSH tunnel for MySQL on remote server

OK, so I got it working, but found a better way, so using shell_exec might be for convenience at some point. Here is what needed to be done:


1) Use sudo and an editor like nano to edit the file /etc/passwd. Duplicate the entry for www-data and make the two entries look like this:


2) Now become the root user momentarily then switch to www-data:

$ sudo su
# become the www-data user
# su www-data
# generate an SSH key for www-data (don't add a password!)
$ ssh-keygen -t rsa ...
# add the key to other server
$ cat /var/www/.ssh/id_rsa.pub | ssh [email protected] 'cat >> .ssh/authorized_keys'
# check the connection
$ ssh [email protected]
# ... exit
$ exit
# exit

3) Re-edit /etc/passwd so that www-data has /usr/sbin/nologin as its default shell once more.


The better way to handle all this, if you have the appropriate user account or know somebody who does, is to use autossh. Autossh will keep the ssh connection up permanently, and if goes down it will just put it right back up. You can even have autossh start at system boot.

Starting autossh once will look like this in the terminal:

autossh -M 0 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -o ExitOnForwardFailure=yes -f -N -p 2233 -L 3307: [email protected] &

After starting autossh, in this example I would just set up a CI DB connection that points to port 3307. It totally works, and I can switch between a local DB and one on the remote server anytime I want.

Messages In This Thread
RE: SSH tunnel for MySQL on remote server - by skunkbad - 02-19-2017, 11:04 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.