JDBC / MYSQL: java.sql.SQLException: Access denied for user 'root'#'localhost' (using password: NO) - mysql

With this code
String url = "jdbc:mysql://localhost/mysql";
Connection con = DriverManager.getConnection(url, "root", "");
I get the following exception.
java.sql.SQLException: Access denied for user 'root'#'localhost' (using password: NO)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:943)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4113)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1308)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2336)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2369)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2153)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:381)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at MyTest1.main(MyTest1.java:29)
I logged into the database using the command tool with root and no password. I was able to telnet to the database and I get this message
N
5.5.17-log
7{*Zj%u☻ǧ|tR}=Vz'L,6imysql_native_password"

Even I also faced the same problem. But, in my application, I did one mistake like I forgot to give password. You can try to manually login to your MySQL and check what is the username and password you are using to log in. That same details you can use in the URL. Better to give database names also. As like below
For MySQL:
String url = "jdbc:mysql://localhost:3306/mysql";
Connection con = DriverManager.getConnection(url, "root", "");
In the above, you forgot to give the port number of MySQL. You can check once your MySQL has a port number or not. By default, MySQL port number is 3306. Better to create and use your own database.
If the above one is not working try for GRANT all privileges to user root on your user database. To grant all privileges below is the query,
GRANT ALL PRIVILEGES ON mysql.* TO 'root'#'localhost' WITH GRANT OPTION;
or else
GRANT ALL PRIVILEGES ON mysql.* TO 'root'#'localhost' IDENTIFIED BY 'root';

'root'#'localhost' is different from 'root'#'%'. It may still be a permissions issue.
I suggest that you log in through the console, create a 'test'#'%' user with a password, and give it all permissions. You can learn how to do that here.

It turned out to be some problem with my MYSQL installation. I uninstalled the AMP package I was using and installed XAMPP instead and it works.

Related

Problems connecting to a MariaDB server: java.sql.SQLException: Access denied for user 'root'#'localhost' (using password: YES)

I am trying to connect a Spring Boot Application to my MariaDB server which is up and running. I am getting the following error:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource
...
Caused by: liquibase.exception.DatabaseException: java.sql.SQLException: Access denied for user 'root'#'localhost' (using password: YES)
Now I've seen that similar questions have been asked on Stackoverflow before, and I tried the most common approaches, like granting all privileges:
GRANT ALL PRIVILEGES ON *.* TO 'root'#'localhost' IDENTIFIED BY '%password%' WITH GRANT OPTION;
And removing the password for the root user:
SET PASSWORD FOR root#localhost=PASSWORD('');
None of these approaches worked so far. I cannot really change the .properties file, but from what I can see a password is generated, encrypted and stored every time the application runs:
connection.driver_class = com.mysql.jdbc.Driver
connection.url = jdbc:mysql://127.0.0.1:3306/local02?useSSL=true
connection.username = root
connection.password = ../../config/local/dbkey.txt
connection.pool_size = 200
It's important to highlight that I am experiencing this problem just on MacOS, on Windows (same files, same DB, same IDE) doesn't arise.
Is there anything else I can try?
As far as I know Spring does not allow to use the root user of mariaDB. You must create a new user and grant priviledges.

Exception in thread "main" java.sql.SQLException: Access denied for user 'root'#'localhost' (using password: YES) [duplicate]

The following code:
Class.forName("com.mysql.jdbc.Driver");
Connection m_connection = DriverManager.getConnection("jdbc:mysql://localhost","root","root");
Throws this exception on getConnection():
java.sql.SQLException: Access denied for user 'root'#'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4074)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4006)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:919)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1694)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1244)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2397)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2430)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2215)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:813)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:334)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at db.Database.<init>(Database.java:91)
at db.Main.main(Main.java:10)
How is this caused and how can I solve it?
EDIT:
public static void main(String[] args) throws ClassNotFoundException, ServletException, SQLException
{
try
{
Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/?user=root&password=rootpassword");
Statement s = (Statement) conn.createStatement();
int result = s.executeUpdate("CREATE DATABASE databasename");
}
catch ( Exception e)
{
e.printStackTrace();
}
}
Produces :
java.sql.SQLException: Access denied for user 'root'#'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4074)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4006)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:919)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1694)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1244)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2397)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2430)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2215)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:813)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:334)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at db.Main.main(Main.java:19)
This can help you:
GRANT ALL PRIVILEGES ON *.* TO 'root'#'localhost' IDENTIFIED BY '%password%' WITH GRANT OPTION;
Execute it with command line or some GUI tool.
Don't forget to replace %password% with real password.
This is specific to Ubuntu 18.04 LTS and MySQL 5.x
Followed this link
Follow everything from here onwards:
sudo mysql_secure_installation
sudo mysql
Once logged into MySQL then from the MySQL prompt execute these commands:
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
FLUSH PRIVILEGES;
Now verify that the table has the password for the root
SELECT user,authentication_string,plugin,host FROM mysql.user;
This solved my issue and now i am able to login.
As you are creating a database from scratch, you could use:
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/?user=root&password=rootpassword");
PreparedStatement ps = connection.prepareStatement("CREATE DATABASE databasename");
int result = ps.executeUpdate();
Here is an identical scenario.
I had a similar problem, but the differemce was: I didn't executed my JavaApp from localhost, but from a remote PC. So I got something like java.sql.SQLException: Access denied for user 'root'#'a.remote.ip.adress' (using password: YES)
To solve this, you can simply login to phpMyAdmin, go to Users, click add user and enter the host from which you want to execute your JavaApp (or choose Any Host)
you can use this
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/YOUR_DB_NAME";
static final String USER = "root";
static final String PASS = "YOUR_ROOT_PASSWORD";
Connection conn = DriverManager.getConnection(DB_URL,USER,PASS);
you have to give the right root password .
I had the same issue like below
"java.sql.SQLException: Access denied for user 'root'#'localhost' (using password: YES)". Problem was "WRONG PASSWORD".
Copy and paste the query as-it-is in the shell to check whether it gives the desired output or not. Small errors consumes more time.
This appears to mostly happens when the MySQL username and password are not correct. Check your MySQL username and password.
You should specify the db you are connecting to:
jdbc:mysql://localhost:3306/mydb
I was hitting the same issue. Added mysql service port number(3307), resolved the issue.
conn = DriverManager.getConnection("jdbc:mysql://localhost:3307/?" + "user=root&password=password");
Try it like this....
public static Connection getConnection() throws SQLException{
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/test";
String username = "root";
String password = "vicky"; // Change it to your Password
System.setProperty(driver,"");
return DriverManager.getConnection(url,username,password);
}
Although this may not be the cause of your issue, you'll get the same error if there are two MySQL services running on the same port. You can check on windows by looking at the list of services in the services tab of Task Manager.
This Exception is also caused due to version mismatch of mysql db and your pom.xml/jar.
Make sure your pom.xml/jar version is higher than your mysql db version.
Because higher versions are compatible with lower version's, but the same is not true for the inverse.
Solution's for java project
Replace the Jar with suitable version.
Solution for maven based
Change the dependency Version in pom.xml
Solution for spring boot
- Override the spring boot dependency by adding
5.1.5.Final
I am using Spring Boot 2.2.6(Windows) and faced the same issue when I tried the run the application:
java.sql.SQLException: Access denied for user 'root'#'localhost' (using password: YES)
What solved my problem:
Create a new user (from the MySQL workbench or a similar GUI which you might be using)
Grant DBA priviledges (Tick the DBA checkbox) also from the GUI
Run the spring boot application.
Or follow the #evg solution to grant privilegdes from command line in Linux env.
My application.properties looked something like this
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=root
The only thing worked for me is to maven clean and then maven install.
In my case, I created a new user with name admin and used it...
CREATE USER 'admin'#'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'admin'#'localhost'
WITH GRANT OPTION;
CREATE USER 'admin'#'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'admin'#'%'
WITH GRANT OPTION;
I had to change my SQL setup (in my host, Dreamhost in my case) to allow the username to access the database from other hosts, not just Dreamhost since I'm running a program from the IDE on my computer. I did that for now by adding % to the list of Allowable Hosts. It now works!
The order of declaring:
Driver
Connection string
username
password
If we declaring in order
Connection string
username
password
Driver
the application will fail
use correct jar (with correct version)
give root user host-independent access or create a user
For me the solution worked as by changing the url format to:
con=DriverManager.getConnection("jdbc:mysql://localhost/w3schools?user=###&password=###");
make sure to add proper jar to class path and use the driver
Class.forName("com.mysql.jdbc.Driver");
This resolved issue for me.
ALTER USER 'root'#'%' IDENTIFIED WITH mysql_native_password BY 'password';
GRANT ALL PRIVILEGES ON *.cfe TO 'root'#'%' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
I was also facing the same issue, My connection string is valid and username and password also valid, even user has sufficient privileges to access database.
I solved this issue by deleting temporary folder created by MySQL(Please take backup before deleting any folders so in case if it not works then you can place that files again).
Default folder location for temp and connection files of MySQL in Windows is:
C:\ProgramData\MySQL
I deleted(taken backup) of below folder:
C:\ProgramData\MySQL\MySQL Server \Data\sys
Note: Before deleting any items make sure that MySQL service is not started or else it would not allow to delete any files
And It works for me.
When I'm running a springboot project, the application.yml configuration is like this:
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://localhost:3306/lof?serverTimezone=GMT
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
Notice that there isn't quotation marks around the password. And I can run this project in my windows System.
But when I try to deploy to the server, I have the problem and I fix it by changing the application.yml to:
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://localhost:3306/lof?serverTimezone=GMT
username: root
password: "root"
driver-class-name: com.mysql.cj.jdbc.Driver
Add port number (something like 3306) in:
Connection c = DriverManager.getConnection("jdbc:mysql://localhost:urport","root","root");
It's working fine for me while using same password after executing the following statement:
ALTER USER 'root'#'localhost' IDENTIFIED BY 'root';
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=
spring.datasource.url=jdbc:mysql://localhost:3306/DATABASE_NAME? useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
Go to your phpmyadmin database
Create a database called DATABASE_NAME and go to priviledges and check the users who have priviledges in the database and use them the default is "root"
Due to below reason I created new user and password for mysql, now I am able to connect successfully. if your password is in encoded format please create new password
https://www.digitalocean.com/community/tutorials/how-to-create-a-new-user-and-grant-permissions-in-mysql#:~:text=There%20is%20a,mysql_native_password%20plugin%20instead%3A
There is a known issue with some versions of PHP that causes problems with caching_sha2_password. If you plan to use this database with a PHP application — phpMyAdmin, for example — you may want to create a user that will authenticate with the older, though still secure, mysql_native_password plugin instead:
I encountered this error. The problem was that I had the wrong database name.
If you are connecting a remote mysql server from your local machine using java see the below steps.
Error :
"java.sql.SQLException: Access denied for user 'xxxxx'#'101.123.163.141' (using password: YES) "
for remote access may be cpanel or others grant the remote access for your local ip address.
In the above error message: "101.123.163.141" is my local machine ip. So First we have to give remote access in the Cpanel-> Remote MySQL®. Then run your application to connect.
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://www.xyz.com/abc","def","ghi");
//here abc is database name, def is username and ghi
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from employee");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+"
"+rs.getString(3));
con.close();
Hope it will resolve your issue.
Thanks
I also have this problem, this solved it.
Change the:
Sring url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&user=root&password=password";
Set:
"serverTimezone=UTC" is "Unified standard world time".
"useUnicode=true&characterEncoding=UTF-8" is "Solve Chinese garbled".
Although my database have not any Chinese words. But it is working.
Reference from https://www.cnblogs.com/EasonJim/p/6906713.html
If you are using a MySql workbench, go to the home page of workbench, right click on your database, copy the JDBC connection string, and paste it into the Java program.

MySQL Access denied for user 'User'#'localhost' (using password: YES)

I connected a MySQL database for a project (java web application) in netbeans. I created MySQL server with correct login details as root and created a database. I connected my database to the project, but when I am running the application it was showing error in the browser as:
type Exception report
message Internal Server Error
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: java.sql.SQLException: Access denied for user 'User'#'localhost' (using password: YES)
root cause
java.sql.SQLException: Access denied for user 'User'#'localhost' (using password: YES)
note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 4.1.1 logs.
Entry in the log file is:
java.sql.SQLException: Access denied for user 'User'#'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:925)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1704)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1250)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2465)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2498)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:822)
at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at org.apache.jsp.Login_jsp.cn(Login_jsp.java:114)
at org.apache.jsp.Login_jsp._jspService(Login_jsp.java:589)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
I am tired in few ways as mentioned in some other forms but not use.
You didn't create the user 'bkUser'#'localhost' correctly.
I found solution, When I was using sql statement for grant privileges i used 'user#%' because of that it was not working. Correct sql statement was:
GRANT ALL PRIVILEGES ON dbname.* TO 'User'#'localhost' IDENTIFIED BY 'Pass' WITH GRANT OPTION;

Unable to access Sonar MySQL database Caused by: java.sql.SQLException: Access denied for user 'sonar'#'glassfishdev.ccs.local' (using password: YES)

I am trying to add Sonar to my Continuous Integration build system. I am using ANT as my build script and I am using the sonar-ant-task-1.1.jar for sonar to generate the reports based on my source code in SVN.
Problem
When the build runs and hits the sonar ant task I get the exception as follows:
Caused by: java.sql.SQLException: Access denied for user 'sonar'#'glassfishdev.ccs.local' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3566)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3498)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:919)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4004)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1284)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2312)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2122)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:774)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:49)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:375)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:289)
at org.sonar.jpa.session.DriverProxy.connect(DriverDatabaseConnector.java:160)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at org.sonar.jpa.session.DriverDatabaseConnector.getConnection(DriverDatabaseConnector.java:95)
at org.sonar.jpa.session.AbstractDatabaseConnector.testConnection(AbstractDatabaseConnector.java:185)
... 40 more
What I have done
1. Install MySQL database.
2. Create the sonar database, sonar user, and grant permissions by using the script provided in the sonar installation
**CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE USER 'sonar' IDENTIFIED BY 'sonar';
GRANT ALL ON sonar.* TO 'sonar'#'%' IDENTIFIED BY 'sonar';
GRANT ALL ON sonar.* TO 'sonar'#'localhost' IDENTIFIED BY 'sonar';
FLUSH PRIVILEGES;**
3. Added the following to my build script
4. Added the following external variables in Jenkins Freestyle build project
sonar.jdbc.url=jdbc:mysql://10.120.21.12:3306/sonar?useUnicode=true&characterEncoding=utf8
sonar.jdbc.driverClassName=com.mysql.jdbc.Driver
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar
sonar.host.url=http://10.120.21.12:9000/
5. I was also available to connect to the database through my sql client tool to verify that the database, user and permissions statements were executed correctly.
However when Jenkins runs the build it does not seem to know how to connect to the MySQL database.
Any thoughts?
The solution was to run the following command:
GRANT ALL ON *.* TO 'sonar'#'%';
The right solution is this:
GRANT ALL PRIVILEGES ON `sonar`.* TO 'sonar'#'localhost';
As explained here, connections to localhost occur over a UNIX domain socket. You are likely logging in as an anonymous user to localhost (e.g. ''#localhost).
I just tried it with sonar 4.5.2 with grants only on sonar.* and its definitely not working.
It looks like sonarqube requires additionnal privileges to work, personnaly i haven't identified which ones yet, and i'm still using a grant all on the database.

Error in remote mySQL access

I am trying to remotely access mySQL server. This is the code used by me.
Connection conn = null;
try
{
String url = "jdbc:mysql://172.18.227.237:3306/struts2";
Class.forName ("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection (url,"root","admin");
System.out.println ("Database connection established");
}
But I get the following error:
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Access denied for user 'root'#'172.20.169.174' to database 'struts2'
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1026)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3491)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3423)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:910)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3923)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1273)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2031)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:718)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:298)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.Connect.main(Connect.java:17)
I have been granted access permission to my IP, But yet I get error.
Have you flushed the privileges after granting access to root at that IP? Privileges won't work until they have been loaded by the MySQL engine, so you have to issue a 'FLUSH PRIVILEGES' command.