1

I am using maven's sonar:sonar goal to generate Sonar reports in one of my Jenkins job.

My Jenkins host name is : jenhost.tst.com and My Sonar host is sonhost.tst.com and My Sonar jdbc url is : jdbc:mysql://sonhost.tst.com:3306/sonar, this database has a user names sonar created with proper permissions.

Now While running the Maven goal, I am getting the error:

Cannot open connection to database: Access denied for user 'sonar'@jenhost.tst.xxxx.com' (using password: YES)

The weird thing in the above error is that the sonar user is trying to access my Jenkins host as a database name and not the sonar host.

I have checked my Maven settings.xml and the database URL of Sonar is mentioned correctly there, and it is mentioned correctly in Jenkins too.

Does any one have any clue regarding this one?

Dchucks
  • 1,189
  • 5
  • 22
  • 48
Manish Joshi
  • 3,550
  • 2
  • 21
  • 29
  • Hi, I rechecked the sonar.properties file present on my sonarhost and it looks fine to me: `sonar.jdbc.url: jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8 sonar.jdbc.driverClassName: com.mysql.jdbc.Driver sonar.jdbc.validationQuery: select 1 ` – Manish Joshi Jul 24 '13 at 06:48

2 Answers2

6

If the Sonar plugin is connecting to localhost that's an indication that it's using the default settings.

Using Maven, there are several ways to configure Sonar.

Jenkins plugin

The sonar plugin for jenkins is the simplest way to enable Sonar. Sonar properties are managed centrally

Maven settings

Add the following profile to your Maven settings file ($HOME/.m2/settings.xml)

<settings>
    ..
    <profiles>
        <profile>
            <id>sonar</dev>
            <activeByDefault>true</activeByDefault>
            <properties>
                <sonar.host.url>XXXXX</onar.host.url>
                <sonar.jdbc.url>YYYYY</sonar.jdbc.url>
                ..
                ..
            </properties>
        </profile>
        ..
    </profiles>
    ..
</settings>

Read the following answer for an easy way to manage Maven settings files across multiple Jenkins projects:

Maven properties

You can set the Sonar properties within your POM as follows:

<properties>
    <sonar.host.url>XXXXX</onar.host.url>
    <sonar.jdbc.url>YYYYY</sonar.jdbc.url>
    ..
    ..
</properties>

Or import your sonar.properties file into your build using the properties plugin

I would favour one of the first two approaches. This option requires changing files within your project. Items like passwords should never be committed into revision control.

Community
  • 1
  • 1
Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
  • ` sonar true jdbc:mysql://sonhost:3306/sonar sonar sonar com.mysql.jdbc.Driver http://sonhost:9000/ ` – Manish Joshi Jul 24 '13 at 08:02
  • Then perhaps this settings file is not being picked up. Run the Maven build using the "-s" option to be certain. Don't assume that it's going to pick the file up. Remember your $HOME directory may not be the same as Jenkins. – Mark O'Connor Jul 24 '13 at 13:04
  • Just ran it with -s option, but still it is giving out the same error. – Manish Joshi Jul 24 '13 at 13:13
  • @user2362314 I find it interesting that when you use the Sonar plugin for Jenkins it also does not work..... Very odd. Perhaps run Maven in debug mode and perhaps you'll see the property assignments and their values. Otherwise there seems little more I can advise (you're not combining the above approaches? Wonder if one property assignment might be overriding another) – Mark O'Connor Jul 24 '13 at 13:16
  • Hi Mark, Can you tell me how this sonar:sonar goal works? Does it take the database url of sonar only from maven's settings.xml or does it check this somewhere else too? Does this sonar:sonar goal makes any http conection also with the sonar frontpage or something like this? – Manish Joshi Jul 24 '13 at 14:58
  • The reason I am asking this is that: If it makes http call to sonar, then while accessing the sonar database,which is mentioned as localhost:3306...in Sonarhost, will this localhost resolve to sonarhost or whether this will resolve to jenkins host from where we are running Maven? If this is going to resolve to jenkins host, because his is where Maven is geting executed then more worringly whether while accessing the sonar database, which is MySql, will I have to install MySql in my jenkinshost also? – Manish Joshi Jul 24 '13 at 15:14
  • I did changed the database url in sonar host from localhost to sonhost, but still it is not working :( Sill Maven is connecting to sonar@jenhost rather than connecting to sonar@sonhost – Manish Joshi Jul 24 '13 at 20:02
  • 1
    @user2362314 Sonar uses the URL to make REST API calls to retrieve operational settings (analysis tool rules, etc). The database URL is used to upload metric results after completing source code analysis. The default value of the db url is "localhost". If it's resolving to Jenhost that would indicate Maven is picking up an overriding value for that property somewhere in your environment. – Mark O'Connor Jul 24 '13 at 20:52
0

Looking at the similar issue over here, getting

...
[DEBUG]  To prevent a memory leak, the JDBC Driver [com.mysql.jdbc.Driver] has been forcibly deregistered
[DEBUG]  Delete temporary directories 
...

and later

[ERROR] Failed to execute goal org.codehaus.mojo:sonar-maven-plugin:2.0:sonar (default-cli) on project commons-mdh: Can not execute Sonar: Fail to connect to database: Cannot create PoolableConnectionFactory (Communications link failure
[ERROR] 
[ERROR] The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.): Connection refused

just to find out, connecting with telnet to mysql, that mysql is refusing connection.

telnet: Unable to connect to remote host: Connection refused

Therefore, make sure you can connect from jenkins to both sonar and mysql.

zziga
  • 96
  • 1
  • 6