Connecting to MySql using Java - SSL Connection - java

I have been trying to to connect to MySql data base which uses ssl connection with java and having trouble, if any one can help me will be of great help.
Manual Connection to MySql:
We use MySQL Workbench , parameters - Hostname - "test-db1-ro-xxxxxx.net" , Port - 3306 , User Name , Password.
There is a SSL CA File - mysql-ssl-ca-cert.pem , the manual connection is successful.
I tried the same in java (in windows system)
My code:
String url = "jdbc:mysql://test-db1-ro.apdev.XXXXXX.net:3306/database";
String driver = "com.mysql.jdbc.Driver";
String userName = "XXXXXXXX";
String password = "XXXXXXXX";
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(url,userName,password);
when i run the code i get an error as "Access Denied"
I am thinking it is due to the SSL Connection.
I am not able to know how to use the .pem file to make a secure connection to database in this case.
Can some one help, have done various things but always get the same access denied error.
Full StackTrace:
java.sql.SQLException: Access denied for user 'xxxxxxxx' (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:571)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at TestDBConnection.main(TestDBConnection.java:34)

You have trouble authenticating.
On the MySql terminal:
GRANT ALL ON *.* TO 'user'#'%' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
If problem still persists:
GRANT ALL ON *.* TO 'user'#'%' WITH GRANT OPTION;
This command grants you superuser privileges.
FLUSH PRIVILEGES;
To make sure your privileges are loaded.
NB: Do not use this account on websites with public access.

Related

caching_sha2_password Connection closed by foreign host [duplicate]

This question already has answers here:
How to resolve Unable to load authentication plugin 'caching_sha2_password' issue
(18 answers)
Closed 4 years ago.
I create the mysql container in docker with url:
jdbc:mysql://172.17.0.2:3306/'databaseName'
172.17.0.2 is the IP address of the docker container.
I create the user with:
CREATE USER 'saman'#'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'saman'#'%' WITH GRANT OPTION;
when i want to connect from intllje Ide i connect successfully, but when i want to connect from my java application i got the exception :
Caused by: com.mysql.cj.core.exceptions.WrongArgumentException: Unable
to load authentication plugin 'caching_sha2_password'
also i changed the default authentication password from caching_sha2_password to mysql_native_password in mysql users.
for more information when i use telnet command:
telnet 172.17.0.2 3306 return:
caching_sha2_passwordConnection closed by foreign host.
You got this issue because all new MySQL version came with added authentication plugin, called "caching_sha2_password" but you can bypass this by using below statement.
GRANT ALL PRIVILEGES ON *.* TO 'saman'#'%' IDENTIFIED WITH mysql_native_password WITH GRANT OPTION;
Finally i found the solution for my question.
I connect to mysql command line and run the following query:
alter user 'saman'#'%' identified with mysql_native_password by 'password';
Then in phpmyadmin the exception gone away.
You can check the result of above query with:
select user,host,authentication_string,plugin from mysql.user;
and you must see the mysql_native_password in front of 'saman' user.

Error in creating the connection. java.sql.sqlException

i am trying to create a database connection , but database is not in my localhost, so i provided the IP instead of localhost in
Connection con= DriverManager.getConnection(conStr,user,pass);
where
constr="jdbc:mysql://10.0.0.1/Sigma"
but its giving
java.sql.SQLException: Access denied for user 'root'#'15.3.0.4' (using password: NO)
but i login with the same user name and password
That's probably because the machine where you're running that code is has not that 15.3.0.4 IP, although you can connect from your machine which potentially has that IP.
You can try to create a 'root'#'%' user who will be granted to access regardless where you are connecting from.

Unable to connect to mysql jdbc database

I am frustrated seeing this error and not knowing the solution. I am trying to connect to a mysql db with a server url but it is giving my mysqlException(stacktrace below). The code works fine till here:
String dbUrl = "jdbc:mysql://server_url/db_name";
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver).newInstance();
String user = "user";
String password = "password";
conn = DriverManager.getConnection(dbUrl,user,password);
This is the error I'm getting
java.sql.SQLException: null, message from server: "Host '172.23.251.154' is not allowed to connect to this MySQL server"
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1070)
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:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
Is this because I'm using a different version of mysql-connector jar?
Please help me.
From Chapter 6. SQL Questions: How Do I Enable TCP Connections to MySQL?,
By default, MySQL won't allow ANY users access to any of the databases if they connect over a TCP connection. In order to permit the connection, you must create a entry in the user table of the mysql database (make sure you select the PASSWORD function to encrypt your password). In particular, the Host field needs to indicate which host(s) are permitted to connect. If you specify % (which I would not recommend), then a user would be able to connect from any host.
What about firewall on port 3306?
May be the user you are trying to access doesn't have enough privileges to access the database from the given server.
So try providing GRANTS to the user.
GRANT ALL PRIVILEGES ON database_name TO 'user'#'hostname' IDENTIFIED BY PASSWORD <password>;
FLUSH PRIVILEGES;
here hostname can be your host address "172.23.251.154". Dont forget to flush privileges and then try connecting the server.
If your MySql server is located on your host provider there is option in your control panel where you can set ip addresses from which you can connect to mysql server in your case 172.23.251.154 if this is your ip address

Remote Connection to MySQL Server [duplicate]

This question already has answers here:
Enable remote MySQL connection: ERROR 1045 (28000): Access denied for user
(13 answers)
Closed 8 years ago.
hello there I am new to MySQL Server and I want to connect to MySQL DataBase remotely from my computer other than database placed on! My IP on machine on which I have MySQL server is:
39.32.58.13
and I am using this code to connect:
private Connection connect = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
private void Connect(){
try{
// this will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// setup the connection with the DB.
connect = DriverManager
.getConnection("jdbc:mysql://39.32.222.77:3306/test","myname", "mypass");
}
}
But it gives me an error all the time:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Is it me doing something wrong because I'm doing it for the first time and need instructions on how to do it ! so please dont mind my mistakes thanks in advance
You need to grant permission for using mysql fro remote location.
GRANT ALL PRIVILEGES ON *.* TO 'user'#'%.example.com'
IDENTIFIED BY PASSWORD 'Password'
WITH GRANT OPTION;
FLUSH PRIVILEGES;
where user can be root or any other user
see this for more info on grant
First,check if the remote mysql database is running.
Second,check if your mysql accound has rights to access the database
hope to help you !
You have to put this as root:
GRANT ALL PRIVILEGES ON *.* TO 'USERNAME'#'IP' IDENTIFIED BY 'PASSWORD';
where IP is the IP you want to allow acess and USERNAME is the user you use to connect
If you want to allow access from any IP just put % instead of your IP
and then you only have to put
FLUSH PRIVILEGES
or restart mysql server and that's it.
Here's Answer
Another Tutorial

MySQL not able to connect from remote client

I have my MySQL server running on machine with IP address 181.2.3.5, and I have a database called affablebean running on it. I am able to connect via from same machine. But when I try to access it remotely to deploy java application but I am getting below error:
Internal Exception: java.sql.SQLException: Error in allocating a connection. Cause: Connection could not be allocated because: Access denied for user 'root'#'181.2.3.4' (using password: YES)
Both systems are on LAN. I tried below queries to grant privellages:
GRANT ALL ON *.* to 'root'#'%' IDENTIFIED BY 'sql123';
flush privileges;
also below thing:
GRANT ALL ON *.* to 'root'#'181.2.3.4' IDENTIFIED BY 'sql123';
flush privileges;
But still I am getting same error, I tried restarting mysql service but still it's not accepting connections.
Btw Server is running Windows XP and client is running on Windows 7. I also tried below query:
mysql> use mysql;
mysql> SELECT host FROM user WHERE user = 'root';
it returns my ip address correctly. But still same error. Can anyone guide me? I tried above solutions from various SO questions.
Note: Above IP address and user name are just a place holder to illustrate.
Edit: I am able to access from Workbench, but I am not able to access from Java program.
You have to specify the port name with your server IP.
Like hostIP:portNumber
Workbench and your java programs are like any other clients. So what you need to see if there are any settings difference in between these two clients, maybe wrong credentials.

Categories