How to connect remotely via tcp to the h2 database? - java

I am trying to start H2 in server mode to connect an application from another computer. But no matter how hard I have tried, I have not succeeded.
I have seen the documentation and to start the server from the command line is executed:
java -cp h2-2.1.214.jar org.h2.tools.Server -tcpAllowOthers
output:
TCP server running at tcp://127.0.1.1:9092 (others can connect)
PG server running at pg://127.0.1.1:5435 (only local connections)
Web Console server running at https://127.0.1.1:8082 (others can connect)
now from the other pc, as I understand I must execute the connection in the following way:
jdbc:h2:tcp://[server][:port]/[path]/[databaseName]
then it should be:
jdbc:h2:tcp://127.0.1.1:9092/home/mateo/database
But I have read that 127.0.1.1 only works locally. I have also noticed that when I open H2 Console in the examples I have seen, the machine's ip appears, that is to say: 192.168.X.
What am I doing wrong?
(Update)
I am currently using Linux.
I have launched the server from Windows and managed to connect it from Linux successfully following the above steps. But, I still don't understand why it doesn't work in Linux, in Windows it loads the server with the IP address of the machine. It makes me think that I have to do some additional configuration for Linux.
server running

You need to replace local IP address in your JDBC URL with real non-local IP address of your server (jdbc:h2:tcp://127.0.1.1:9092/*** -> jdbc:h2:tcp://192.168.1.4:9092/***, for example). H2 listens all network interfaces of the host, it doesn't matter which address was reported in “runnig at …” message.
You also may need to protect ports 8082 and 9092 from connections from untrusted systems if you have them in your internal network and from connections from external network (make sure your router or whatever you have doesn't redirect connections to the host with database server).

Related

How do I use port forwarding to connect to an Oracle database using netsh?

I have a machine(A) that has access to the host running the Oracle listener. I can connect to it fine using SQL Developer.
I have another machine(B) that I would also like to make a connection to the same database but its doesn't have direct access to the host running the listener. However, it does have access to machine (A).
I would like to forward a port on machine A to the Oracle listener host which is listening on port 1521.
Then I should be able to make a database connection from machine B to machine A and then forward a port to the host that has the Oracle listener. Sort of a man in the middle.
However, I'm running into issues. I can't even connect to the Oracle database from machine B using the forwarded port. I have experience using ssh tunnels to do the same thing. In this case, I'm not using ssh and I don't understand why this will not work.
Below is the procedure I'm going through:
Log into machine B - Oracle client is installed. SQL Developer is installed.
Open SQL Developer - Select TNS connection. Enter in credentials. Connection Successful
TNS Names entry:
CRYSTAL=(DESCRIPTION=(ADDRESS_LIST=(LOAD_BALANCE=yes)(FAILOVER=ON)(ADDRESS=(PROTOCOL=TCP)(HOST=testhost.com)(PORT=1521)))(CONNECT_DATA=(service_name=svc1)))
Now setup forwarding:
C:\Windows\system32>netsh interface portproxy add v4tov4 listenaddress=127.0.0.1
listenport=1521 connectaddress=testhost.com connectport=1521
protocol=tcp
If I telnet localhost 1521 , it is able to make a connection.
As a simple test, I make sure I can connect on Machine B using SQL Developer. But this time, I set the connection type to advanced so I can enter the jdbc url.Note, I use 127.0.01 instead of testhost.com because it will be forwarded to testhost.com
jdbc:oracle:thin:#(DESCRIPTION=(ADDRESS_LIST=(LOAD_BALANCE=yes)(FAILOVER=ON)(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521)))(CONNECT_DATA=(service_name=svc1)))
This connection fails with "ORA-12516, TNS: listener could not find available handler with matching protocol stack"
I've checked for firewall issues and made sure port 1521 is open.
Any ideas what I might be missing or has anyone else had success forwarding a port to an Oracle listener?
Thanks to the comment from psaraj12, I knew my approach should work. The problem I was having was due to the host name I was given to connect to the database. It works fine if you enter it into SQL Developer as is. However, when I used it in the port forwarding, I could not connect to the database using SQL Developer using the host name localhost. The host name I was given had "scan" in the name. I think this is a Virtual IP. We use Oracle RAC which is a clustering technology. So I suspected to that the VIP selected a node on the cluster and the forwarder could not handle it. So this is how I solved the problem:
Installed Wireshark.
Start recording packets.
Used SQL Developer To Connect with the VIP - Success
In Wireshark, find the TCP Stream that contains something unique about the connection. In this case, the Oracle service name. You should see the IP of the VIP. Something like:
.........6.,.A ...O........:..............................(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=11.3.68.171)(PORT=1521))(CONNECT_DATA=(CID=(PROGRAM=SQL Developer)(HOST=jdbc)(USER=adpc))(SERVICE_NAME=svc1)(CID=(PROGRAM=SQL Developer)(HOST=jdbc)(USER=adpc)))).................6.,.A ...O........:..............................(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=11.3.68.171)(PORT=1521))(CONNECT_DATA=(CID=(PROGRAM=SQL Developer)(HOST=jdbc)(USER=adpc))(SERVICE_NAME=svc1)(CID=(PROGRAM=SQL Developer)(HOST=jdbc)(USER=adpc)))). .......6.A ........ ..........................
Now, go back to the packet and find out what the "real" address is. Here is an example:
Wire shark packet information. This is the destination IP where Oracle listener is.
Now use this to forward the port.
netsh interface portproxy add v4tov4 listenport=1521 connect address=11.3.68.135 connectport=1521 protocol=tcp
Now, when you log in using SQL Developer Basic Connection Type, you can use localhost instead of the VIP and you should be able to connect.
Now, go to another machine that has access to the machine you just set up and you should be able to connect to the database from there as well using the IP of the machine where you have port forwarding setup.
The same forward connection is working fine in both basic and advanced connection in SQL developer in my machine.
The only difference is machine A,B and Database are in the same network in my case where B can directly connect to the database also
Steps
1)You have to set up forwarding in machine A with machine A ip address as the listen address
2) Use the machine A ipaddress in TNS to connect to Database in machine B
If still, you are facing problems then you can try enabling log and trace files to identify the issue as mentioned in these articles
Oracle Database 10g Debugging Connection problems 1
Oracle Database 10g Debugging Connection problems 2

How to connect to a server that may be on the network/the current computer

I may be using my Java program (which is initialized by the simple Batch code at the bottom, which may or may not help anything). From my main computer which will connect to localhost to get to my MySql server.
I otherwise could be connecting via a laptop, which is on the same Wi-Fi network. Therefore, I believe it would use the IPv4 address. Lastly, I could connect via a completely different network and would use my IP.
How could I use Java to detect where exactly it is and change which host it will connect to while pinging the server?
java -jar thb.jar
pause

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?
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 user#example.com
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");

Cannot connect to couchbase server

I cannot connect to a couchbase server that runs on a different computer of the same local network.
The machine has the IP address 192.168.1.150. I use the official hello-couchbase tutorial for the Java SDK. Thus, my code looks like this:
Cluster cluster = CouchbaseCluster.create("192.168.1.150");
Bucket bucket = cluster.openBucket();
The second line triggers:
com.couchbase.client.deps.io.netty.channel.ConnectTimeoutException: connection timed out: /192.168.1.150:11210
Is there any way to narrow down the reason for the connection failure? The webclient on localhost:8091 (in a browser of the remote machine) tells me that the server is running.
This sounds like a problem with a firewall. Can you telnet from your client machine to the Couchbase server on port 11210? From a command prompt, type telnet 192.168.1.150 11210
If you see a blinking cursor, the test succeeded and the firewall is likely not a problem. You may need to install a telnet client if it isn't already available. Windows Telnet installation: http://www.wikihow.com/Activate-Telnet-in-Windows-7

App Java and hosting mysql

I have a Java application and I have to connect to a MySQL DB host in aruba.it. If I make a connection, aruba.it refuses that. How to solve this?
To start, I assume that you're trying to run this Java application locally, or at least at a different machine than where the MySQL DB runs and that you got a SQLException: Connection Refused.
To fix the particular problem, all routers and firewalls in the complete network pipe between the client (where the Java application runs) and the server (where the MySQL DB runs) needs to be configured to allow/forward the port number which the DB uses. This is by default 3306. If this port is blocked, you cannot reach the DB from outside.
Another solution is just to upload the Java application in flavor of a webapplication and run it by HTTP. You'd normally use JSP/Servlet for this.
Apart from network, routers, firewall issues the reason can be that by default remote access to MySQL database server is disabled for security reasons. Mostly DB is hosted on the same server or on the trusted server. If you run java application from your desktop, you need to configure MySQL so it will accept this connections. See this manual for details how to do it.

Categories