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.
Related
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. :)
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
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).
I have already checked the other Stack Overflow and web links on this:
MySQL is on a Linux server
Linux uslx600 2.6.32-358.14.1.el6.x86_64 #1 SMP Mon
Says its IP is 1.2.3.4
I can connect to it from my laptop using MySQL Workbench. I cannot attach image due to Stack Overflow policy, but the dialog is as follows:
Connection Method: tcp/ip
Hostname: <host of the server, so e.g. 1.2.3.4>
Port: 3306 <This is same port as mentioned below for JDBC connection>
username: bugs
Default Schema: bugs
Within that mysql I ran [select user(), current_user()], I get
user() = bugs#<my laptop IP>
current_user() = bugs#%
Now I am trying to connect from the Linux server where the MySQL database is.
Following works, user is user by itself, i.e. no "#%" or something:
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bugs", user, pasword);
Following also works:
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/bugs", user, pasword);
But it does not work, if I give the IP address of the box or hostname. And what I am really working towards is accessing this from another Linux server, which does not work either probably due to same issue.
The error message when it fails is:
Access Denied for user 'bugs'#'<hostname of the server>' (using password=YES)
If there was some access issue on the server, shouldn't the MySQL Workbench have the same issue?
And current_user() says bug#% so does it not imply that user is setup correctly?
I have also tried changing user to 'user'#'%' etc., but in all those cases error message was always:
'bugs'#'%'#'<hostname>'
Make sure you have granted all privileges to all hosts.
Check your my.cnf file and comment out bind-address if it's pointing to 127.0.0.1 only.
MySQL root access from all hosts
The error says that it cannot connect to the server with the given password. May be your password is incorrect or if it is having special characters, you have to escape that string properly.
Another thing, the user bugs could not have privilege to connect to the server from the specified host. Try providing GRANTS to the user from the host you are connecting and then check if you are able to connect to MySQL.
GRANT ALL PRIVILEGES ON *.* TO 'bugs'#'%' IDENTIFIED BY 'password';
or comment the line in my.cnf file
#bind-address = 127.0.0.1
restart mysql service.
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';