简体   繁体   中英

Use SSH tunneling to connect to a remote mysql server using “localhost”

Hey I'm a bit new to this kind of thing, I hope the title makes sense.

Basically I have a mysql server running on a LAMP stack, with some mean legacy code, by which all connections to the database are using mysql_connect("localhost", "user", "pass") . (I know it's bad, I didn't write it!).

I would like to run my own local version of our website for debugging/dev purposes, but I can't figure out how to connect to our database (we have a test-database running remotely that is updated every day with the live data). I have tried remotely connecting but I can't figure it out.

I was wondering if I keep the code (with "localhost" connection written in), and use some kind of SSH tunneling to "trick" the code into connecting to the remote database? Or would it perhaps be easier to set up some kind of mysql 'server' on my local machine? Using phpstorm's in built "database" functionality, I managed to connect to our database using ssh tunneling, but from there I'm completely lost. Again I'm new to this, so I apologise if this sounds very naive.

Any kind of help for a solution would be great.

If you are on Linux you must install Stunnel on both Client and Server.

Take a look here: https://blog.thesysadmins.co.uk/using-stunnel-to-encrypt-unsecure-connections.html

By configuring properly Stunnel you will be able to use mysqli_connect with "locahost" while actually referring to a remote server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM