Getting Access denied for user 'root'#'1-' (using password: YES) in Java when trying to create MySQL connection.
You can see that i set all prvileges to any user in mysql. So why then im getting access error ?
There is connection code in Java :
Class.forName("com.mysql.jdbc.Driver").newInstance();
String ip = InetAddress.getLocalHost().getHostAddress();
Connection connection = DriverManager.getConnection("jdbc:mysql://" + ip + ":3306/forum","root","correct_password_there");
Related
When I'm trying to get connetcion with online mysql database I get this error:
java.sql.SQLException: Access denied for user ''#'89.229.59.50' (using password: NO)
My code is:
public static Statement stmt = null;
public static Connection conn = null;
private static final String dbURL ="jdbc:mysql://db4free.net:3306/dolar;user=neir#localhost&password=password";
private static void createConnection() {
try {
conn = DriverManager.getConnection(dbURL);
} catch (Exception except) {
except.printStackTrace();
}
}
And yes, I've added mysql connector to my project.
You have to make sure the user (neir) has remote access privileges.
If your using a hosting service make sure they allow your remote IP (89.229.59.50) to connect to MySQL Server.
If you're managing the MySQL Server make sure that it's configured to be accessed remotely, that's done in the my.cnf file, search and comment the line:
bind-address = 127.0.0.1
Hope this helps.
From the error you can already see that the user and password parameters are not used. This might give you a clue that there is something wrong with the JDBC connection URL.
You cannot put the user and password parameters in the connection URL.
You should use: getConnection(String url, String user, String password)
See: https://docs.oracle.com/javase/8/docs/api/java/sql/DriverManager.html#getConnection-java.lang.String-java.lang.String-java.lang.String-
This might be for user has not access privileges so you should do this.
CREATE USER 'enterusername'#'xxx.xxx.xxx.xxx' IDENTIFIED BY 'enterpassword';
Where xxx.xxx.xxx.xxx is you IP or if you want to do this for any IP then just write '%' instead 'xxx.xxx.xxx.xxx'
To grant privileges
GRANT ALL PRIVILEGES ON mor.* TO 'enterusername'#'xxx.xxx.xxx.xxx' WITH GRANT OPTIONĀ ;
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".
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
So the problem is that I want to make a connection to a msaccess database that has password every time you open it.
The password works if I directly open the access file.
I can make the connection if I remove the password, which means my code works if there is no password involve
The password was set using Set database Password in the database tools
MS Access 2007(but i used the .mdb)
Here's the code
String dbFile = "db.mdb";
String connectionString = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
String driverID = ";DriverID=22;READONLY=true;pwd=qwer}";
if (CONNECTION == null || CONNECTION.isClosed()) {
dbURL = connectionString + dbFile.trim() + driverID;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
CONNECTION = DriverManager.getConnection(dbURL);
}
----------------------------------------------
Error Code : java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Not a valid password.
----------------------------------------------
I already tried adding parameters to the get connection but it did not work.
Please Help :)
There are 2 types of passwords for MS Access database files:
user passwords
database password
With a user password, you supply the user name and password in the connection string.
;User Id=admin;Password=;
For a database password, you need a different identifier in the connection string to distinguish the password as a database password rather than a user password.
;Database Password=MyDbPassword;
Personally I don't use a database password. That feature doesn't offer much in the way of security, so it seems like it's more trouble than it's worth.
Edit: I don't know if it's possible to supply a database password with an ODBC connection. All the connection examples I found used OLE DB when including a database password.
Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=\\somepath\\mydb.mdb;" & _
"Jet OLEDB:Database Password=MyDbPassword;", "admin", ""
Perhaps it could work to switch from the current Access ODBC driver to the Microsoft OLE DB Provider for ODBC. Or maybe with the current ODBC driver if you switch
Database Password=MyDbPassword;
to
Jet OLEDB:Database Password=MyDbPassword;
I don't know. But seems to me the database password is just getting in your way here. You already know you can connect if you remove the password from the database.