How to connect with remote MySQL database? - java

so I tried to connect to my local host with the 3306 port, and it works fine. Now, I'm using my second computer to connect to the database remotely.
I use the jdbc:mysql://192.168.15.25:3306/yourdatabase type.
My two computers are actually using the same IP.
The trace says communication failure. I followed the problem, and it happens when I try to get the connection from the DriverManager.
I did not touch the firewall at all.
I did not write any permissions to users, as there are no users, just the admin.

Two computers can't have same IP. Check the second for IP address using ifconfig command (if you have *nix system).

Follow this instructions:
Client PC:(sample ip: 192.168.0.105 want yo connect to mysql server on 192.168.0.172)
- Java app: user: dbuser, pass: dbpass, host: 192.168.0.172
Mysql Server PC:
- On mysql console: GRANT ALL PRIVILEGES ON *.* TO dbuser#192.168.0.105 identified by "dbpass";
FLUSH PRIVILEGES;
Now you have all set to make a remote conection from your client to your mysql server.

Related

Spring Boot, using oracle-ldap url through ssh tunnel on local machine

There are 3 machines:
local -> some remote server -> oracle db server (via ldap)
I want to set up datasource connection (in my spring boot app) to the oracle db.
There is no direct connectivity between local machine and the one with oracle db.
So, i'm using the ssh tunnel through remote server:
ssh -L 127.0.0.1:8081:some.ldap.host:389 user#remote.server.host
In application.yml file i'm using further url:
spring:
datasource:
url: jdbc:oracle:thin:#ldap://127.0.0.1:8081//srvcnm,cn=OracleContext,dc=yy,dc=xx,dc=com
And when my app trying to get db connection, im getting the following error:
Caused by: oracle.net.nt.TimeoutInterruptHandler$IOReadTimeoutException: Socket read timed out
at oracle.net.nt.TimeoutSocketChannel.handleInterrupt(TimeoutSocketChannel.java:254)
at oracle.net.nt.TimeoutSocketChannel.connect(TimeoutSocketChannel.java:103)
at oracle.net.nt.TimeoutSocketChannel.<init>(TimeoutSocketChannel.java:77)
at oracle.net.nt.TcpNTAdapter.connect(TcpNTAdapter.java:192)
... 126 common frames omitted
Whenever i'm deploying app on the remote server and enter "direct" url in application.yml the connection is being obtained without any timeouts, and the app works well.
jdbc:oracle:thin:#ldap://some.ldap.host:389//srvcnm,cn=OracleContext,dc=yy,dc=xx,dc=com
Does anyone know how to handle this? How to get connection from local machine?
I may do some thing like this, I am going to create file call ~/.ssh/config then add following
Host remoteserver1
User usermane
Hostname ip or host name
ForwardAgent yes
Host oracleserver
User username
Hostname some.ldap.host
Port 22
# ForwardAgent yes if you need to forward one more instance
LocalForward 8081 some.ldap.host:389
ProxyCommand ssh -q -W %h:%p remoteserver1
What this does is that when I attempt to connect to ssh oracleserver from remoteserver1, it connects to hopper and then proxies the SSH connection to port 22 on overthere (ie: SSH on oracleserver).
now to connect via ssh do following ssh oracleserver , as it will make ssh tunnel between your machine and oracleserver via remoteserver1. along with port forwarding.
The problem was in redirecting source connection request to another machine with oracle db itself (after ldap auth).
So, the request's path looked like:
1.local -> 2.remote server -> 3.ldap server -> 4.oracle db server
There wasn't connectivity between 1st and 4th machine as the tunnel was only between 1th and 3rd one.
So, you if you faced this issue, you may add one more ssh tunnel (First tunnel is for ldap server, second one for oracle db) and enrich your "etc/hosts" with oracle server's routing.
In my case the issue was in access restrictions. The oracle server is filtering sockets somehow and grants access to certain machines.

Access denied when trying to connect to mysql database via eclipse, java

I'm trying to connect to mysql database, but I get one and the same error: javax.servlet.ServletException: java.sql.SQLException: Access denied for user 'user'#'localhost' (using password: YES)
I've already tried the following solutions:
Checked my username and password. MySQL connection is established via username = user1, password = 123. I use the same in my code, when I connect to the database: Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/employee","user1", "123");
Granted privileges to user1 in the following manner:
GRANT ALL PRIVILEGES ON * . * TO 'user1'#'localhost';
FLUSH PRIVILEGES;
By the way I have the same problem when I try to connect with 'root'#'localhost'.
Do you have any ideas why it doesn't work still?
MySQL JDBC is only able to connect via TCP/IP (on Unix, or TCP/IP or named pipe on Windows).
The MySQL JDBC driver cannot establish a connection using the local unix socket.
With MySQL on Unix, localhost has a different meaning that we might expect. It is not a hostname synonym for the 127.0.0.1 TCP/IP loopback address. On Unix, MySQL user
'user1'#'localhost'
specifies a user that can connect only via unix socket file; it's not possible to connect to that user via TCP/IP.
The above explains why MySQL is refusing a connection from JDBC: the connection attempt fails because a matching user does not exist.
To create a MySQL user that connect from TCP/IP loopback address, assuming that MySQL is started with --skip-name-resolve and without --skip-networking, we can specify user as:
'user'#'127.0.0.1'
That user would allow connection from JDBC.
(If MySQL DNS name resolution is enabled, then we would need to use a hostname that resolves to the loopback address; or we can consider using a wildcard '%' for the hostname portion of the user.)
Try this out might help:
1) goto mysql terminal. 2) mysql> use mysql; 3) mysql> select user, host from user; 4) There next to you user set the host as "%" instead of localhost
.
now in the connection instead of localhost tryp specifying the ip address of the server.
Hope this helps you. :)

Connection to <hostname>:5432 refused

I'm not a pro in communication networks, so I have no idea what should be my "hostname" and where can I find it?
I created a postgreSQL server on ubuntu 14.04, I can connect to it from the same computer without problem by setting hostname to 127.0.0.1(localhost), or even 192.168.1.42 (my private ip). But I can not connect to my server from any other computer, even if they are on local network or not. I always get this message:
"Connection to <hostname>:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections."
where is either 127.0.1.1, 127.0.0.1, 192.168.1.42, 98.765.432.123(public ip) or anything else, it never works.
What is the right hostname?
I already set listen_addresses to '*', already edited pg_hba.conf to accept 0.0.0.0/0 IP-s, and disabled ubuntu firewall.
This could be caused by many things.
The server has an IP address. That is the address that the JDBC connection string should use for <hostname>, unless you have some local DNS solution. To rule out problems with the latter, use the IP address of the Ubuntu server. On Ubuntu type ifconfig to see it; it probably is 192.168.1.42 like you mentioned. So from the client with the Java app, you use the IP address of the server.
If you have a standard PostgreSQL installation, it should use port 5432, you can check the setting in postgresql.conf. In the JDBC connection string you should also indicate the port: "jdbc:postgresql://192.168.1.42:5432/...", just to be on the safe side.
You need to have a database that you can connect to. Note that by default only the owner of the database (probably the user postgres in your case) can connect to it. See further down.
In pg_hba.conf you need to create an entry for your new database so it can be connected to (typical settings, check your network setup). The IP address (range) you specify here is that of the clients connecting to the database!
host my_db all 192.168.0.0/16 md5
You must restart your PostgreSQL server after modifying pg_hba.conf. (Typically on Ubuntu, do sudo ./etc/init.d/postgresql restart)
To create database my_db (or whatever name you prefer), go to your Ubuntu box and enter psql as the postgres user:
vekszor#ubuntu:~$ sudo -u postgres psql
[sudo] password for user:
psql (9.3.5)
Type "help" for help
postgres=#
In order to connect to the database, you should have a database:
postgres=# CREATE DATABASE my_db;
CREATE DATABASE
You also want a user (role) that is less powerful than the postgres superuser, but still able to manipulate the database. So create this user role and assign ownership of the database:
postgres=# CREATE ROLE vekszor LOGIN PASSWORD 'secret123' CREATEROLE;
CREATE ROLE
postgres=# ALTER DATABASE my_db OWNER TO vekszor;
ALTER
Now you can go back to your client computer with the Java app and finish the JDBC connection string with the name of the database, the user and password.
Note that if you want to access your database from the internet, you should set up a NAT rule in your router to point traffic on port 5432 to the IP of your Ubuntu server.So long as the internal address of the router is in the same address range of the client computers that you indicated in pg_hba.conf this should be easy to set up, otherwise add a new entry in pg_hba.conf.
Reading your comments to the correct answer:
This is how you add a rule to iptables:
iptables -A INPUT -s 0/0 -p tcp --dport 5432 -j ACCEPT
0/0: If you want anybody to access it. You can change it to a specific ip address or range of ip addresses.
If you are connected to the server using UI you can run the below query to get the IP Address of server using below query to verify if you are using the correct IP.
Select inet_server_addr();

JDBC Connection error to mysql Access Denied for user <user>#<host>

I have already checked the other Stack Overflow and web links on this:
MySQL is on a Linux server
Linux uslx600 2.6.32-358.14.1.el6.x86_64 #1 SMP Mon
Says its IP is 1.2.3.4
I can connect to it from my laptop using MySQL Workbench. I cannot attach image due to Stack Overflow policy, but the dialog is as follows:
Connection Method: tcp/ip
Hostname: <host of the server, so e.g. 1.2.3.4>
Port: 3306 <This is same port as mentioned below for JDBC connection>
username: bugs
Default Schema: bugs
Within that mysql I ran [select user(), current_user()], I get
user() = bugs#<my laptop IP>
current_user() = bugs#%
Now I am trying to connect from the Linux server where the MySQL database is.
Following works, user is user by itself, i.e. no "#%" or something:
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bugs", user, pasword);
Following also works:
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/bugs", user, pasword);
But it does not work, if I give the IP address of the box or hostname. And what I am really working towards is accessing this from another Linux server, which does not work either probably due to same issue.
The error message when it fails is:
Access Denied for user 'bugs'#'<hostname of the server>' (using password=YES)
If there was some access issue on the server, shouldn't the MySQL Workbench have the same issue?
And current_user() says bug#% so does it not imply that user is setup correctly?
I have also tried changing user to 'user'#'%' etc., but in all those cases error message was always:
'bugs'#'%'#'<hostname>'
Make sure you have granted all privileges to all hosts.
Check your my.cnf file and comment out bind-address if it's pointing to 127.0.0.1 only.
MySQL root access from all hosts
The error says that it cannot connect to the server with the given password. May be your password is incorrect or if it is having special characters, you have to escape that string properly.
Another thing, the user bugs could not have privilege to connect to the server from the specified host. Try providing GRANTS to the user from the host you are connecting and then check if you are able to connect to MySQL.
GRANT ALL PRIVILEGES ON *.* TO 'bugs'#'%' IDENTIFIED BY 'password';
or comment the line in my.cnf file
#bind-address = 127.0.0.1
restart mysql service.

Couldn't connect to a MySQL DB via JWS app

Could someone help me with establishing connection with my DB on non-localhost server?
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://"+SERVER+"/javadb?user=javadb&password=*****");
How could I set-up the SERVER String for my domain www.lmntstudio.cz?
All required files are stored in one folder on the server.
I did it first on my localhost server and everything works well. But with the connection to another server is trouble.
I would recommend not to open MySQL connection from the client/user machine to the server. Because this would make your DB server open and prone to attacks.
Rather consider using web service. call that web service from your JWS application from client side and get the data.
Another point to consider is that opening a connection on a port say 3360 may be blocked by the firewall. If my firewall is set to block any out bound connection on port 3306 the connection could not be established.
Please ensure the MySQL is running in default port 3306 else you need change the port number accordingly.
Connection connection = DriverManager.getConnection(
"jdbc:mysql://www.lmntstudio.cz:3306/javadb", "username", "password");
instead of www.lmntstudio.cz you can use ip of the domain.
run the following in the command line to get the ip
cmd prompt> nslookup www.lmntstudio.cz
The Url syntax as follows
jdbc:mysql://(host/ip):port/databasename", "username", "password"

Categories

Resources