JDBC tries to connect to "localhost" regardless of the hostname - java

I'm banging my head hard.
This simple line of code:
DriverManager.getConnection("jdbc:mysql://machine1:3306/information_schema", "user1", "password");
Running on machine1 results in an error:
1045 Access denied for user 'user1'#'localhost' (using password: YES)
However there's no problem running on machine2 or any other machine for that matter.
user1 actually has two different passwords, one is password if the connection is made to machine1 (which is the machine name), one is password1 if the connection is made to localhost.
That sounds crazy but our IT department insists that it is for security purposes. So if you can ignore the madness of that, the problem is obvious: I asked JDBC to connect to machine1 with username user1 and password password, which is correct, but it somehow tries to connect to localhost and hence the error.
I suspect that on machine1, there's some sort of configuration that delegates all internal connection from machine1 to localhost. That's why the error only occurs when I run that command from machine1, and never occurs if I run it from elsewhere.
Modify the code to connect to localhost will work but the framework does not allow me to do that.
Any idea?

I hate this when I bang my head against the wall for 5 hours and the moment after I post the question on SO I find the answer.
The problem is that in /etc/host, machine1 points to the same address as localhost (127.0.0.1). Modify the file to have machine1 point to the real IP address (e.g. 10.240.3.167) will solve the problem.

Are you running this code on machine1? In that case the error is correct because localhost is itself. Have you tried connecting via the MySQL command line from the same machine running your code? I suspect the credentials aren't set up in MySQL to allow connections from localhost.
I just re-read. You need to use the localhost password when connecting from machine1. MySQL uses #localhost as where the connection is coming from. What value jdbc used for dns is not relevant.

Related

What exactly is my database URL for mysql server connecting remotely?

Trying to connect to a mysql database using JDBC automatic driver loading with syntax that requires the following: jdbc:subProtocolName:databaseURL
I know how to work it with a local host that's providing the mysql server, but I haven't done this remotely, and I want to get it right the first time, rather than coding my application and not being able to set breakpoints in the .xml file etc and confusing the source of other issues I might be having.
So far I have: "jdbc:mysql:[url???]:3306/dbname?autoReconnect=true"
Is it simply the ip address of the server, or is it myusername#[ipAddressofServer], or something else entirely? It doesn't sit with me that it would be name#localhost:[port#] because that would indicate the server resides locally. I've seen things online that say to use the 'server name' here. Is that equivalent to the hostname? Example would be great that doesn't involve mysql running on localhost. Thanks!
(Warning: Here be some slight oversimplifications.)
As you may be aware, computers (directly) connected to the Internet can be identified in two ways:
an "IP address", historically of the form 151.101.1.69 (but may be longer and scarier nowadays), or
a "DNS (Domain Name Service) name", e.g. stackoverflow.com, sometimes called a "host name".
When an application wants to connect to its own computer it can use two special cases of the above. 127.0.0.1 and localhost both mean "this here computer".
So if you want to connect to a remote machine you can use either its DNS name or its IP address as the "server name" in the connection string. For example, if there was a MySQL Server running on the machine in the examples above then your connection string could be
jdbc:mysql://151.101.1.69:3306/databasename?useUnicode=true&characterEncoding=utf8
or
jdbc:mysql://stackoverflow.com:3306/databasename?useUnicode=true&characterEncoding=utf8
Notes:
You don't actually need to specify port 3306 because that is the default port for MySQL. If you omit the port number from your connection string the driver will try to use 3306.
useUnicode=true&characterEncoding=utf8 are just examples of common attributes that are added to the connection string. There are lots of them, and you can read more about them here.

Java, MySql -- connecting from another computer

I created a java program that uses MySql. Now I want to give the app to someone else, and I'm using the URL as: jdbc:mysql://ipAddress:3306/tableName, but it doesn't work, showing the error of connection failure.
What should I do?
It's a troubleshooting stuff, do the following.
Check the Firewall and Port access on MySQL Server computer.
Check the MySQL Service and the DB User is doing good.
Follow the following thread
How to allow remote connection to mysql

mysql cpanel netbeans remote connection

hello guys i have seen similar threads concerning my question but couldn't solve my problem. i am trying to remotely connect to the mysql database hosted on cpanel through my netbeans. i have added my ip address as a host to be allowed remote access. the cpanel ip address was given as https:197.211.45.2:2087. the username given as 'root' and the password also given. in my netbeans i try connecting to the database using the stated parameters and the database name but it wouldnt just connect. i have also tried using the default mysql port it still wouldnt work. if there is anything wrong i have done please put me aright. thanksthe image.. i have attached a snapshot. thanks
It's seems that MySQL port is not added in server firewall and due to that you are getting this issues. Please add port 3306 in firewall and check again.
Under cPanel there is usually 'Remote MySQL' tool which allows you to enter the IP addresses that are allowed to connect into the host.

How to make any ip address connect to the remote MySQL server?

Reason for asking the question:
For my third year minor project I am making an instant messenger type of application. The MySQL server for which will reside on my college's server. I have made the application in java and I am using mySQL for the back-end database.
Problem/Question:
My problem is that when I will distribute the application to my fellow students, the mysql server will be transferred to the college's server(currently everything is on my home computer, since I am developing right now), but then what I am supposed to do so that to every application remotely will be able to access this db on my college server.
I will be more specific:
This the basic code I am using currently:
Connection o =
DriverManager.getConnection("jdbc:mysql://localhost/db","root","pass");
I know when i will finally deploy the mysql db onto the server the localhost shall be replaced by the server's ip address(in each of the applications), but today when I tried to do this, I always got the error that:
"null message from server host 'theremoteip' is not allowed to connect to this mysql server"
So i went to the mysql server and applied:
GRANT ALL PRIVILEGES
ON database.*
TO 'user'#'%'
IDENTIFIED BY '%';
then i got this error:
Access denied for user 'root'#'theremoteipaddress' (using password: YES)
So, i figured that in the 'mysql' database root user may be assigned to only certain host addresses, i may be wrong but i think this is the problem.
So, I humbly request all of the veterans present on this community to please help me, I am new to all of this and I apologize if this question has an obvious answer and I didn't knew it.
Thank you for reading and please help me.
Again, the question is how I can make all the remote applications that I distribute among students to connect to the MYSQL database.
Probably for security reasons, root is allowed to login only from the same machine on which mysql server is running(or from listed ip addresses). If you ssh to your server and open mysql from there you should be able to change the permissions for your user.

How to connect XAMPP MySQL local DB using JDBC?

I have this Tetris game written in Java, which uses DB to record high scores. It worked ok as long as I was using remote MySQL DB, but now I'm trying to set up localhost DB using XAMPP MySQL and it keeps going like "SQLException: Communications link failure" at command:
con = java.sql.DriverManager.getConnection("jdbc:mysql://localhost/score", user, psw);
I guess it's either wrong URL or DB configuration, but I really don't know what to check. Any ideas?
EDIT: My friend has fixed my problem by replacing "localhost" in URL by "127.0.0.1" (which was quite embarrassing as you can surely imagine :P ).
So question is: Why is XAMPP not able to translate "localhost" into IP address and how to fix it?
Why is XAMPP not able to translate "localhost" into IP address and how to fix it?
This is not a XAMPP problem nor a programming problem. This is more a DNS problem.
To start, do you have a %SystemRoot%/system32/drivers/etc/hosts file with the following line as first line? (thus, after all comments, but before any other host declarations)
127.0.0.1 localhost
Update: as per the comments I've Googled a bit and it look like that the MySQL JDBC driver doesn't eat IPv6 addresses at all. In other words, you'll need to change ::1 to 127.0.0.1. But I also found this topic which mentions that you can use the following JVM argument to fix this problem:
java -Djava.net.preferIPv4Stack=true
I tried and got a successful connection. First create a database in phpmyadmin - eg. 'mydb'
and then in code put connection.url with this value
'jdbc:mysql://localhost:3306/mydb'
If you don't create a database first it wont connect
In MySql you have to allow access for your user from localhost explicitly. Here is an example (taken from here):
mysql> grant usage on *.* to amarokuser#localhost identified by 'amarokpasswd';
mysql> grant all privileges on amarokdb.* to amarokuser#localhost ;

Categories