JDBC Hibernate - Mysql Connection Error

NinjaBoy picture NinjaBoy · Feb 16, 2012 · Viewed 16.6k times · Source

I have a local mysql server on my Ubuntu 11.10 desktop. The hostname = localhost; username = root; password = root; database name = CBS. I am really confused because when I access mysql using terminal, mysql administrator, and mysql query browser I use those authentication I mentioned above and everything is OK. My problem is when I configure my jdbc.properties in my Java App I'm getting this error:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.jdbc.UncategorizedSQLException: Hibernate operation: Cannot open connection; uncategorized SQLException for SQL [???]; SQL state [28000]; error code [1045]; Access denied for user 'root '@'localhost' (using password: YES); nested exception is java.sql.SQLException: Access denied for user 'root '@'localhost' (using password: YES)

My configuration file, jdbc.properties:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/CBS
jdbc.username=root  
jdbc.password=root

By the way, the reason I will be using local server because our main server shutdown so I have to use my local mysql to continue with my project. Please help me... Thanks in advance.

Answer

Perception picture Perception · Feb 16, 2012

More than likely your framework is logging into your local database as 127.0.0.1. Which will create a login problem in MySQL if you have not defined an appropriate domain scoped credential. Try this to verify:

mysql -uroot -proot
SELECT * from mysql.user WHERE user = 'root';

If there is no 'root'@'127.0.0.1' then have found the problem and to remedy it, do one of two things:

  1. Define a domain scoped credential for 'root', at '127.0.0.1'.
  2. Define a wildcard domain scoped credential for 'root', so you can login to your MySQL with those credentials from multiple locations.

Here's an example of the second:

mysql -uroot -proot

CREATE USER 'root'@'%' IDENTIFIED BY 'root';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;

On a side note, I would definitely recommend using something more creative for your user id and password. Especially if you have TCP sockets enabled for your server.