Connecting to MySQL database error in Java - 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';

Related

Java MariaDB connection error: Access denied for user 'root'#'localhost' [duplicate]

This question already has answers here:
MySQL ERROR 1045 (28000): Access denied for user 'bill'#'localhost' (using password: YES)
(43 answers)
Closed 3 years ago.
I wrote some function used by a php webpage, in order to interact with a mysql database. When I test them on my server I get this error:
"Connect failed: Access denied for user 'root'#'localhost' (using password: YES)"
I am able to use them on my pc (using XAMPP) and I can navigate through the tables of the database using the command line in the server. However, the webpage fails to connect. I've checked the password but with no results. It's correct (otherwise I could not log in to mysql from the command line).
The call of the function is the following:
$conn = new mysqli("localhost", "root", "password", "shop");
Do I have to set something in my server? Thanks
Edit:
PHP version 5.3.3-7+squeeze1
mysql version: 5.1.49-3
both on debian
I solved in this way:
I logged in with root username
mysql -u root -p -h localhost
I created a new user with
CREATE USER 'francesco'#'localhost' IDENTIFIED BY 'some_pass';
then I created the database
CREATE DATABASE shop;
I granted privileges for new user for this database
GRANT ALL PRIVILEGES ON shop.* TO 'francesco'#'localhost';
Then I logged out root and logged in new user
quit;
mysql -u francesco -p -h localhost
I rebuilt my database using a script
source shop.sql;
And that's it.. Now from php works without problems with the call
$conn = new mysqli("localhost", "francesco", "some_pass", "shop");
Thanks to all for your time :)
Is there a user account entry in the DB for root#localhost? In MySQL you can set different user account permissions by host. There could be several different accounts with the same name combined with the host they are connecting from. The most common are root#127.0.0.1 and root#localhost. These can have different passwords and permissions. Make sure root#localhost exist and has the settings you expect.
I am willing to bet, based on your explanation, that this is the problem. Connecting from another PC uses a different account than root#localhost and the command line I think connects using root#127.0.0.1.
From what you've said so far, it sounds like a problem where the MySQL driver in PHP5.3 has trouble connecting to the older MySQL version 4.1. Have a look on http://www.bitshop.com/Blogs/tabid/95/EntryId/67/PHP-mysqlnd-cannot-connect-to-MySQL-4-1-using-old-authentication.aspx
There's a similar question here with some useful answers, Cannot connect to MySQL 4.1+ using old authentication
SELECT `User`, `Host`, Length(`Password`) FROM mysql.user
This will return 16 for accounts with old passwords and 41 for accounts with new passwords (and 0 for accounts with no password at all, you might want to take care of those as well).
Either use the user managements tools of the MySQL front end (if there are any) or
SET PASSWORD FOR 'User'#'Host'=PASSWORD('yourpassword');
FLUSH Privileges
I had this problem too. But this was because of another reason.
My password began with character $... e.g. $MyPassword
I've changed it to #MyPassword and the problem is solved.
Easy.
open xampp control panel -> Config -> my.ini edit with notepad. now add this below [mysqld]
skip-grant-tables
Save. Start apache and mysql.
I hope help you
Here maybe?
I believe that the code should be:
$connect = new mysqli("host", "root", "", "dbname");
because root does not have a password. the (using password: YES) is saying "you're using a password with this user"
TIP:
comment out pid and socket file, if you can't connect to server..
mysql could be connecting with incorrect pid and/or socket file,
so when you try to connect to server trough command-line it "looks"
at the incorrect pid/socket file...
### comment out pid and socket file, if you can't connect to server..#pid-file=/var/run/mysql/mysqld.pid
#socket=/var/lib/mysql/mysql.sock
Most likely it is not identifying your user properly. root#localhost.
Select the Mysql version your MAMP is using.
/Applications/MAMP/Library/bin/mysqldump -uroot -p db_name > test_db_dump.sql
I actually wrote an alias on my computer so I don't have to remember how to get to the bin directory.
alias mysqldumpMAMP='/Applications/MAMP/Library/bin/mysqldump'
Which allows me to run this:
mysqldumpMAMP -uroot -p db_name > test_db_dump.sql
You can save an alias in your bash profile ~/.bash_profile or ~/.bash_rc
May be there password is not set and you are passing password as argument.
Try ommitting password argument during connection..
Edit your privileges on PHPmyAdmin (WAMP), Note that your password and user name has not been created. So do create or edit it, that it might work with your sql connection in your php.
Hope it works
Try initializing your variables and use them in your connection object:
$username ="root";
$password = "password";
$host = "localhost";
$table = "shop";
$conn = new mysqli("$host", "$username", "$password", "$table");

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. :)

MYSQL Connection: Access Denied

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).

JDBC Connection error to mysql Access Denied for user <user>#<host>

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.

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