I've been using java on a computer (lets call it PC1) to connect to a database on a server, and until recently it was working with no errors.
By working, I mean I could connect to the server using java on PC1, and access the info I needed from the tables using select statements.
The only changes that have been made are the ip addresses on PC1 and the server.
After changing the IP addresses, I then updated the grant table in mysql, and yet, I get the following error:
java.sql.SQLException: Access denied for user 'robot'#'aa-PC' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:885)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3421)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1247)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2775)
at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at WriteToMySql.connection1(WriteToMySql.java:26)
at WriteToMySql.main(WriteToMySql.java:259)
The strange part however, is that I am able to connect to the server's database using the mySQL workbench and access all data on them.
here's the java code:
String host = "jdbc:mysql://PC1IPAdress:3306/users";
String user= "robot";
String password="mypassword";
public void connect()
{
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("worked"); //this gets printed
connect = DriverManager.getConnection(host, user, password);
System.out.println("works"); // this does not get printed due to error
stmt = connect.createStatement();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
note: "users" is the name of the database
Any help will be greatly appreciated.
Thank you.
EDIT:
for testing purposes I tried turning off the firewall, but it did not help.
I think there may be an error in the code sample - it connects to PC1IPAddress when you mention that PC1 should be connecting to a server earlier in the post. Just want to make sure before we continue that it was a typo, as otherwise PC1 would be connecting to itself.
If you have administrative access to the server, connect to MySQL as root and use this query to show configured users and ensure the host field is correct: SELECT user, host FROM mysql.user WHERE user='robot';
If the above checks out, I would suggest looking into Windows user authentication. The fact that MySQL returned the Windows computer name ('aa-PC') and not its IP address seems to indicate it may be attempting to authenticate using Windows domain credentials: http://dev.mysql.com/doc/refman/5.5/en/windows-authentication-plugin.html
If you change the ip address of the client (PC1) make sure that you updated the host field as well when granting new rights to user "robot". Check the table "db", "user" and "host".
Related
I'm trying to connect to my WAMP sever by using its IP address in my LAN. So far I have written the following code, and I don't understand why it does not establish the connection.
public class DBConnect {
public static Connection connect()
{
Connection con=null;
try {
Class.forName("com.mysql.jdbc.Driver");
con=(Connection) DriverManager.getConnection("jdbc:mysql://192.168.1.2:3306/employee101?","root","");
JOptionPane.showMessageDialog(null, "Connection Success");
} catch (Exception e) {
System.out.println("inter.DBConnect.connect()");
}
return con;
}
}
The root mysql account is configured to only allow a connection from the PC running the MySQL Server. For obvious security reasons.
I suggest you go to the WAMPServer machine and login to MySQL as root, then create a new account, that is NOT a Super User account and HAS password. Using phpMyAdmin for this is probably easiest.
Then configure the domain for this account to allow connections from your network only (192.168.1) note only 3 of the 4 quartiles used will allow access from any IP in your network, but only from within your network. Allocate only the required privileges on the specific databases required, to the new account.
Then use this new account in your Java code.
Ok first of all i'm french so sorry for the possible incomprehension or bad translate of error messages.
I have a Raspberry, with a Tomcat server on. Also, i use Postgresql too.
On my website, The server side connect to the localhost to access to the database with the JDBC plugin, all works great.
But now i'm trying to developp a Java Program, which one need to connect to the sql database.
But the program have as target to being deployed. So, I can't use "localhost" to connect to the database. On my computer, i'm trying to connect to my Raspberry's database with the JDBC and the few following code :
<code>
public class DB {
Connection co;
PreparedStatement ps;
public DB() {
try {
Class.forName("org.postgresql.Driver");
co = DriverManager.getConnection("jdbc:postgresql://192.168.1.47:5432/database?sslmode=require", "username", "password");
}
catch (ClassNotFoundException e) {System.out.println(e);}
catch (SQLException e) {System.out.println(e);}
}
}
</code>
But when i try to connect, i get the following error :
" org.postgresql.util.PSQLException: FATAL: aucune entr?e dans pg_hba.conf pour l'h?te << 192.168.1.73 >>, utilisateur << username >>,
base de donn?es << database >>, SSL actif "
Which means "No entries in pg_hba.conf for the host..." but i tried to add entries in pg_hba.conf, like this :
host all postgres 192.168.0.0/24 trust
host databaseName userName 192.168.0.0/24 md5
host all all 0.0.0.0/24 md5
but this didn't worked (maybe bad row ? how can i just authorize the user "username" with a password ?)
I added the listen_addresses='*' in postgres.conf too.
Someone know how i need to configure all ?
My computer is on the same WIFI than my Raspberry. Does i need to connect with an external computer on an external WIFI ?
Maybe i did another mistake ?
Thanks for your attention.
OK I solved it by myself, i added these line to pg_hba.conf :
host all all 0.0.0.0/0 md5
now i have to work on the security. Thanks by the way.
I am trying to connect to a MySQL Server using JDBC tool in java (using eclipse). I was just wondering how to enter 2 user/password combinations. The first one is the one I use to connect to the server(for example when I ssh into the server) and the second one I enter into phpmyadmin. As of now, I am putting in the phpmyadmin password only into the jdbc connection properties and it's not connecting. This is my current statement:
conn = DriverManager.getConnection("jdbc:mysql://[IP of server]:3306/[Database Table name]", "[UserName (same as phpmyadmin)]","[Password (same as phpmyadmin)]");
I am getting a
java.sql.SQLException: null, message from server: "Host '[My computer's full host name]' is not allowed to connect to this MySQL server"
I was just wondering if I needed to enter my server login/password (the one I use for ssh) as well in addition to the phpmyadmin username/pwd. I am new to JDBC and MySQL server, so I would appreciate any tips.
Just for background, I am able to connect successfully through ssh and I can login to the server through phpmyadmin.
Here's how you can create an account that can access your server from another client machine:
CREATE USER 'bobby'#'localhost' IDENTIFIED BY 'some_password';
That creates the user, and says he can connect from localhost.
If he is on the machine 192.168.0.5, you'd do something like this:
CREATE USER 'bobby'#'192.168.0.5' IDENTIFIED BY 'some_password';
Then of course, you have to grant privileges appropriately:
GRANT ALL PRIVILEGES ON databasename.* TO 'bobby'#'localhost' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON databasename.* TO 'bobby'#'192.168.0.5' WITH GRANT OPTION;
That's been my experience anyway.
You're probably better off reading this section on how to specify MySQL accounts.
When you log in from PHPMyAdmin, the web server is located on the same server that hosts the Mysql database (in your case). Mysql, by default, does not allow connections from remote hosts; only the local machine may access the database.
I'll take a shot in the dark and say that the computer you're running the java code on is not the same machine that is hosting the mysql server. You must configure Mysql to allow connections from remote hosts via the config file and then change the Host row of the mysql.users table for the specified user to allow connection from your IP address (or, if security isn't your concern, any IP address.)
To configure mysql to allow connections from remote hosts you must remove the "bind-address=" line from the configuration file.
To allow any host to log on to a specific mysql user, you must set the mysql.users Host` column to "%".
Both of these must be done.
public class MysqlConnect{
public static void main(String[] args) {
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "jdbctutorial";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}
}
}
I am trying jdbc connection with mysql but getting SQLException.
My code is:--
public void createConn() throws MysqlException {
try {
String url ="jdbc:mysql://172.168.1.73:3306/mysql";
Connection con =DriverManager.getConnection(url,"root", "");
System.out.println("URL: " + url);
System.out.println("Connection: " + con);
}
i am getting following exception...
java.sql.SQLException: Access denied for user 'root'#'192.168.1.187'
(using password: NO)
I am trying to connect to 172.168.1.73 but it is trying to coonect to 192.168.1.187...
root user doesnt have any password.
Please tell me how can i solve this issue.
Check whether root user can access the machine with IP 192.168.1.187
It is not trying to connect to 192.168.1.187.
'root'#'192.168.1.187' in the exception means a user with user name "root" in the machine with IP '192.168.1.187' trying to access the database server. Therefore 192.168.1.187 is the client IP.
Database servers restricts the access based on user and the IP of the machine user use to connect. Therefore if you want to connect to the database server from a IP '192.168.1.187', you should grant permission to access the user to access from that specific IP.
Check MySQL GRANT for more details.
To be more secure, you need to assign the root password and try it again. The page below contains Java code to connect to mysql database:
http://www.worldbestlearningcenter.com/index_files/java_database_connect_mysql.htm
Here's how I'm trying to connect:
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception e) {
throw new DbConnectionException();
}
try {
connection = DriverManager.getConnection(url,username,password);
} catch (SQLException e) {
e.printStackTrace();
throw new DbConnectionException();
}
I'm 100% sure that the url, username, password strings are correct. I've already connected successfully using an external tool (MySQL query browser).
This is the error I receive:
com.mysql.jdbc.CommunicationsException:
Communications link failure due to
underlying exception:
** BEGIN NESTED EXCEPTION **
java.net.SocketException MESSAGE:
java.net.ConnectException: Connection
refused
...
Possibly a url issue. If your code is pointing to MySQL localhost, try changing localhost to 127.0.0.1 on your url.
E.g.:
jdbc:mysql://localhost:3306/MY_DB
to
jdbc:mysql://127.0.0.1:3306/MY_DB
And see if this works.
did you run the mysql browser from the same machine where the code is running? What I am getting at is the permissions in mysql can be host-specific, and depending on how you set them up you might not be able to connect from the machine where the code is running.
Also, you might want to double check the url, name, pword again, perhaps with log statements or a debugger to make sure there are no typos, trailing whitespaces, etc...
Double check the format of your url. It should start with "jdbc:mysql:". Make sure you are using a current version for the driver as well.
Check that you can connect to the database from the mysql admin tool, that will drive out whether your mysql is running and that the port is open.
In my case the problem was that I was using a connection from emulator to localhost.
If you use emulator to localhost don't use localhost value in connection String but use 10.0.2.2 instead:
jdbc:mysql://10.0.2.2:3306/MY_DB
Hope this helps.