Newbster
Newbster

Reputation: 1

How connect local java program with remote server java for mysql request?

My idea is like this: The provider does not allow external mysql requests. I have been searching for hours for a solution to connect local java with remote server java. The remote java server would then make the mysql request and send it back. Any ideas/Links for me?

Upvotes: 0

Views: 1382

Answers (1)

gustf
gustf

Reputation: 2017

You could do this with port forwarding, you can use ssh to setup a tunnel. So there is no need to write a java server program.

The last link have an example of your exact problem, though it is connecting to a PostgreSQL database, I translated that info to MySQL below.


An example here is when you need to connect to a database console, which only allows local connection for security reasons. Let’s say you’re running MySQL on your server, which by default listens on the port 3306.

$ ssh -L 9000:localhost:3306 [email protected]

The part that changed here is the localhost:3306, which says to forward connections from your local port 9000 to localhost:3306 on your server. Now we can simply connect to our database.

$ mysql -h localhost -p 9000

You should now be able connect your local java jdbc program with the url jdbc:mysql://localhost:9000/dbname and it will be tunneled to the remote database which would see your tunneled connection as a local connection.

Connection c = DriverManager.getConnection("jdbc:mysql://localhost:9000/dbname","username", "password");

Upvotes: 3

Related Questions