MYSQL Connection: Access Denied - java

These are my codes for the database connection:
Class.forName("com.mysql.jdbc.Driver");
con =DriverManager.getConnection("jdbc:mysql://10.44.222.111/try?"
+ "user=jenny&password=perez");
After running the program, it display this error:
com.mysql.jdbc.exception.jdbc4.MySQLSyntaxErrorException:
Access denied for user 'jenny' # '%' to database 'try'
I am using MySQL Workbench.

Go to MySQL Workbench, then User Privileges, click your username, then check all Administrative Roles then click apply. This will allow connection for user 'jenny'.

As the error says, user 'jenny' does not have access to connect to database from any host(%). On mysql prompt, run the following command to provide privileges to user 'jenny' to connect from any host.
GRANT ALL PRIVILEGES ON try.* TO 'jenny'#'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
You may use specific PRIVILEGES instead of ALL as per your requirement.

This would indicate that your user does not have permission to access that database from any client host ('%').
MySQL uses a four part mechanism for allowing access:
username
password
database
client host
You need to make sure that the user is authorised in MySql to access the 'try' database and is allowed to connect from any client host ('%') and has the correct username and password.
Also, you should ensure you are connecting to port 3306 (unless MySQL is running on a non-standard port).

Related

Access denied when trying to connect to mysql database via eclipse, java

I'm trying to connect to mysql database, but I get one and the same error: javax.servlet.ServletException: java.sql.SQLException: Access denied for user 'user'#'localhost' (using password: YES)
I've already tried the following solutions:
Checked my username and password. MySQL connection is established via username = user1, password = 123. I use the same in my code, when I connect to the database: Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/employee","user1", "123");
Granted privileges to user1 in the following manner:
GRANT ALL PRIVILEGES ON * . * TO 'user1'#'localhost';
FLUSH PRIVILEGES;
By the way I have the same problem when I try to connect with 'root'#'localhost'.
Do you have any ideas why it doesn't work still?
MySQL JDBC is only able to connect via TCP/IP (on Unix, or TCP/IP or named pipe on Windows).
The MySQL JDBC driver cannot establish a connection using the local unix socket.
With MySQL on Unix, localhost has a different meaning that we might expect. It is not a hostname synonym for the 127.0.0.1 TCP/IP loopback address. On Unix, MySQL user
'user1'#'localhost'
specifies a user that can connect only via unix socket file; it's not possible to connect to that user via TCP/IP.
The above explains why MySQL is refusing a connection from JDBC: the connection attempt fails because a matching user does not exist.
To create a MySQL user that connect from TCP/IP loopback address, assuming that MySQL is started with --skip-name-resolve and without --skip-networking, we can specify user as:
'user'#'127.0.0.1'
That user would allow connection from JDBC.
(If MySQL DNS name resolution is enabled, then we would need to use a hostname that resolves to the loopback address; or we can consider using a wildcard '%' for the hostname portion of the user.)
Try this out might help:
1) goto mysql terminal. 2) mysql> use mysql; 3) mysql> select user, host from user; 4) There next to you user set the host as "%" instead of localhost
.
now in the connection instead of localhost tryp specifying the ip address of the server.
Hope this helps you. :)

Access denied for user 'test_user'#'test_host' (using password: YES)

I have a mysql user called test_user
GRANT all privileges on *.* to 'test_user'#'%' identified by 'test_passwd'
I can use this user remotely from my JAVA code. When i run the code in the server, i got
Access denied for user 'test_user'#'test_host' (using password: YES)
I already ran FLUSH PRIVILEGES; after grant all privileges for test_user and restarted the mysql service.
I tried to login mysql in the server using console
mysql -utest_user -ptest_passwd
ERROR 1045 (28000): Access denied for user 'test_user'#'localhost' (using password: YES)
It seems i can only using test_user remotely but not from the server itself. Any suggestions to fix this?
-------New test----------
I tried to do not provide password in the server, it is working
mysql -utest_user
I need to use password for both remotely and locally. Any suggestions?
I have run into a similar problem before.
Having % should allow a user to connect from anywhere including localhost assuming you granted privileges and flushed privileges. However, If you did not make a secure install of mysql you most likely have a ghost localhost left on your machine. So not only would you be able to just run mysql with no user, or password provided from localhost command line but the ghost localhost for some reason conflicts with any other users (besides root) that have been created from logging in on the localhost.
If you do select user, password, host from mysql.user; You will likely see a user with no name, no password and a host localhost.
Delete that user using drop user ''#'localhost;
flush privileges again.
Try to connect using your other user now on localhost and it should work.
It should be:
CREATE USER 'monty'#'%' IDENTIFIED BY 'some_pass';
and then
GRANT ALL PRIVILEGES ON *.* TO 'monty'#'localhost'
-> WITH GRANT OPTION;
If it doesn't work, try logging in with root and see:
SHOW GRANTS FOR 'admin'#'localhost';
Replace your values everywhere
Try this post:
SO solution to this problem

MySQL: Access denied for user 'userName'#'localhost'

I am having an issue with creating and grating permissions to a user using phpMyAdmin. I am having a Java swing application and it need to connect to this database.
How I created the user and granted the permission are below, step by step.
Open phpMyAdmin
Go to 'Users' tab.
Click on Add New User
Give the user name, select Any Host as the host (so the % is displayed in its text box), and mention the password. Any host is because remote access required.
Select Select under Global Privileges - Data
Click on Go
Now I am in the Users tab starting page again.
Click on Edit Privileges on my newly created user.
Select the database under Database-specific privileges
Tick everything under Database-specific privileges, Data section.
Click on Go
Now, whenever my Java application connects to this, it gives the below error
java.sql.SQLException: Access denied for user 'userName'#'localhost' (using password: YES)
This is how I connect to the database, in my Java application
con = DriverManager.getConnection("jdbc:mysql://"+ip+":3306/databaseName","user","password");
Here,for the variable ip, I tried both localhost and 127.0.0.1 but still no good. What have I done wrong?
I noticed the connection works fine if I select Localhost instead of Any Host in step 4.
After you have made your user.
Click edit priviliges.
Change Any Host for Localhost.
Apply your priviliges.
Scroll to the bottom and
make sure that "keep old one" is selected and press Go.
You need to grant permissions to the userName#localhost in mysql.
GRANT ALL PRIVILEGES ON database_name TO userName#localhost IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
sometimes, mysql server doesn't get a 'localhost' from a local connection, you should try :
GRANT ALL PRIVILEGES ON database_name TO 'userName'#'theHostName' IDENTIFIED BY 'password';
or in a more risky way:
GRANT ALL PRIVILEGES ON database_name TO 'userName'#'%' IDENTIFIED BY 'password';
and finally
FLUSH PRIVILEGES;
Have you set any password for the phpmyadmin. if yes please include that in the password field.or you can try lik this
con = DriverManager.getConnection("jdbc:mysql://"+ip+":3306/databaseName","localhost","");

Connecting to MySQL database error in Java

I have a java code and I am connecting to the mysql database with the following connection string.
String userName = "admin";
String password = "pass";
String url = "jdbc:mysql://<my IP>/dbase"; //not localhost
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
conn = DriverManager.getConnection (url, userName, password);
When I make a JAR (Runnable JAR through eclipse), and take it to another machine in the same network I get an error
Access denied for user 'admin'#'<another machine IP' (using password: YES) //not localhost
The IP magically changed to another machine IP when I take the JAR to another machine. admin user has all privileges possible.
Whats wrong? Please help !!
The IP address listed in the error message is the IP address of the machine your program is running on. It's telling you that you're not allowed to connect to MySQL from that IP address as root
You will need to talk to the person who configures/administers your MySQL database. More than likely this is an intentional security measure.
Maybe you should run the below command.
grant all privileges on *.* to 'admin'%'#' identified by 'pass';
flush privilges;
I think There is no problem with your Java code. Also try to connect the mysql server from command prompt
mysql -u root -h <ip address> -p
If you got the same error then the possible reason would be
1. In correct password
2. Permission is denied to access via network
You can give permission using below command
GRANT ALL ON db.* to root#'ip' IDENTIFIED BY 'PASSWORD';

Connect to mysql database java

I am trying to connect to MySQL database from Java (MySQL is hosted in WAMP server)
String userName = "root";
String password = "pass";
String url = "jdbc:mysql://localhost/dbase";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
conn = DriverManager.getConnection (url, userName, password);
The connection is fine when I am running from localhost. However when I run this code from another computer replacing localhost with my computer's IP (within the same network), I get the error,
message from server: "Host '<name>' is not allowed to connect to this MySQL server"
I have tried with port 3306 too. Whats wrong?
That's a permission issue on the database side; you need to grant permissions to user root to connect from your specific IP address.
Something like this should work:
GRANT ALL ON foo.* TO root#'1.2.3.4' IDENTIFIED BY 'PASSWORD';
On the other hand; I wouldn't use root for access to the database; you should use a regular user account for this.
You should not use the root account for development. To solve your problem lets create a hypothetical user called dbasedev.
CREATE USER 'dbasedev'#'%' IDENTIFIED BY 'passw0rd!';
GRANT ALL PRIVILEGES ON *.* TO 'dbasedev'#'%';
FLUSH PRIVILEGES;
This will allow you to connect to the MySQL server with user id: dbasedev, password: passw0rd!, from any host.
This is a complement to #Icarus's answer, I couldn't post all the code in a comment.

Categories

Resources