Unable to connect to mysql jdbc database - java

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

Related

Cannot connect mysql workbench to remote database

I have a remote mysql database on mydomain.com power by goDaddy and have a database name "arsenal101" with several tables and I have assigned the user "arsenal101" with password "22216888" with full privilege to that database.
In my local MySQl workbench in Macbook, I try connecting my MySQL workbench to that remote database.
Here is the screenshot
The hostname I enter mydomain.com
The username is arsenal101
The password is 22216888
The default schema is blank.
After I pressed test connection, an error message showed up " Failed to Connect to MySQL at mydomain.com: 3306 with user arsenal101
access denied for user arsenal101#xxxxxxx (using password Yes)
You should check if GoDaddy allows you to do a remote connection to the database because a lot of hosting companies don't allow that. Their MySQL has to be configured to receive external connections.
According to here: https://in.godaddy.com/help/connect-remotely-to-databases-4978
If you want to connect remotely to a database, you must enable Direct Database Access when setting it up1 — you cannot enable it later.

Connecting to MySql using Java - SSL Connection

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.

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.

java.sql.SQLException: Single-Sign-On is only supported on Windows. Please specify a user name

I am using the jTDS driver in order to connect to an SQL Server database from my Android application, which uses the Windows Authentication. As advised in the FAQs, I read the READMESSO file and as told, I placed the native SPPI library (ntlmauth.dll) in the system path (defined by the PATH system variable)
However, when I try to connect to the database using the following code:
String driver = "net.sourceforge.jtds.jdbc.Driver";
Class.forName(driver).newInstance();
String connString = "jdbc:jtds:sqlserver://192.168.56.1/MyMovies;";
Conncection conn = DriverManager.getConnection(connString);
I get the following exception:
java.sql.SQLException: Single-Sign-On is only supported on Windows. Please specify a user name.
Since you are connecting from an android device, you would not be able to get the SSO credentials required by the driver to connect to SQL server. The setting you referred to works only if the java program trying to connect to the DB is on a windows machine, which is clearly mentioned by the error message.
Unless your application has authorization based on the SSO user connecting to the DB, you should have an SQL Server user-based authentication mechanism to connect to the server and all authorization procedures should be tied to this user.
You might have to give the username also.
"jdbc:jtds:sqlserver://192.168.56.1/MyMovies;instance=SQLEXPRESS;user=foo"

Connecting to a MySQL Database with Java

I want to connect to my MySQL database with Java.
I am using JDBC and I have the driver installed. (com.mysql.jdbc.Driver)
The only problem is that I keep getting an error:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException The last
packet sent successfully to the server was 0 milliseconds ago.
Here is my code:
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql:/mydomain.com/mydatabase", "username", "password");
I am not positive how to compose the URL (and where I get my username and password) but I have done A LOT of research.
I am the only person with acess to my database and domain, so there's no use asking the admin.
I use phpMyAdmin to create the database(s) and manage them. Do I use my phpMyAdmin username and password or what?
By the way, my site is hosted on Yahoo! Small Business.
So my questions are:
How do I make the connection URL?
What is my username and password?
I would say you are missing a forward slash on your URL.
Connection connection = DriverManager.getConnection("jdbc:mysql://mydomain.com/mydatabase", "username", "password");
Or I have a feeling that there is something wrong with your access privileges. This same thing happened to me also and it was a problem of Firewall blocking the port on the server. So verify this is not the case.
How do I make the connection URL?
Are you missing a forward slash in your URL? I would've assumed it would be something like:
jdbc:mysql://server/database
Load the drivers for mysql
Class.forName("com.mysql.jdbc.Driver");
connect with the data base
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/widget_corp","dbuser","dbpassword");
.....jdbc:mysql://this is use as it is
.....127.0.0.1 this is the address of localhost You can use the word "localhost" insted of "127.0.0.1"
.....dbuser is the data base user
.....dbpassword is the password of the dbuser
.....3306 is the default port used for database

Categories