简体   繁体   中英

Unable to connect to MYSQL database from java

I'm new into MySQL and UNIX, Actually from one UNIX box to another i connected using ssh -i commonprivatekey -p 60041 root@172.16.28.102 . Once it connected then I'm executing mysql -uroot -ppassword mysql mysql prompt open i can able execute select command.

Env Details:

IP               : 172.16.28.102
SSH Port         :  60041
usernames        :  root
passwords        :  password
SSH Privatekey   :  commonprivatekey
mySQL DB Port    :  60040
mySQL DB Name    :  mysql
mySQL DB UserID  :  root
mySQL DB Password:  password

And my Java code is

try {
  Class.forName("com.mysql.jdbc.Driver");
  conn = DriverManager.getConnection("jdbc:mysql://172.16.28.102:60040/mysql", "root", "password");
}  catch (SQLException e) {
  logger.error("Could not connect to the database", e);
}

Exception trace:

Caused by: java.sql.SQLException: null,  message from server: "Unknown error 1130"
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1095)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2031)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:718)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at uk.accenture.fdt.batch.control.ConnectionsHelper.getDatabaseConnection(ConnectionsHelper.java:35)

Is your application on the same server as the mysql database ? Error 1130 sounds like the user is not allowed to connect to the database. Maybe the client IP is not allowed to connect to that database. For example if you have a user root@locahost, you can only connect from the same server. So create a user that is allowed to connect from the IP. I guess that user should have the IP from the box you are trying to connect.

CREATE USER '<username>'@'<ip from the box>' IDENTIFIED BY '<password>';

You need to give him the appropriate rights of course. The following code should give him all rights. You probably don't need that.

GRANT ALL PRIVILEGES ON <database name>.* TO '<username>'@'<ip from the box>';

i)u add a mysql connector jar file in your library folder then right click on lib->buildpath->configurebuildpath then it will show properties for input dialog box then click on library->addexternal jar->add your jar file next clickon order and support->select all the items->click ok. ii)go to ur project rightclick bulidpath->configure buildpath->ok

If those things u do rigth,u check your coloumn name in ur database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM