简体   繁体   中英

SonarQube analysis via Maven fails with database access denied to MySQL

I have instances of MySQL, SonarQube, and TeamCity all running on the same Linux host. All are very vanilla installations (out of the box / insecure / local / behind company firewall).

These are my Maven settings ... where ${fullyQualifiedDomainName} is something like build.company.com

<profile>
  <id>env-dev</id>

  <properties>
    <downloadSources>true</downloadSources>
    <downloadJavadocs>false</downloadJavadocs>
    <sonar.jdbc.url>jdbc:mysql://${fullyQualifiedDomainName}:3306/sonar?useUnicode=true&amp;characterEncoding=utf8</sonar.jdbc.url>
    <sonar.jdbc.username>sonar</sonar.jdbc.username>
    <sonar.jdbc.password>sonar</sonar.jdbc.password>
    <sonar.host.url>http://${fullyQualifiedDomainName}:9000</sonar.host.url>
  </properties>

  <activation>
    <activeByDefault>true</activeByDefault>
  </activation>
</profile>

I run the command mvn sonar:sonar to perform and upload SonarQube analysis.

The Maven settings above work OK from my Windows PC. However, if I run from the Linux server via TeamCity, I see this error: (giant Java stacktrace + ...):

[ERROR] Failed to execute goal org.codehaus.mojo:sonar-maven-plugin:2.5:sonar (default-cli) on project IMQuant: 
Fail to connect to database:
Cannot create PoolableConnectionFactory
(Access denied for user 'sonar'@'${fullyQualifiedDomainName}'
(using password: YES)) -> [Help 1]

If I switch to use localhost in the Maven settings it runs OK on the Linux server. I am guessing this is one of the following issues:

  1. MySQL config (I am a MySQL newbie)
  2. Linux server config (network config / admin stuff beyond my control)
  3. MySQL JDBC driver bug ( localhost != fqdn from localhost)

For now I am using a second Maven profile in my settings for TeamCity / Linux with localhost .

Is there a solution for this issue? Has anyone seen this issue?

Update

Here are the commands to create my MySQL database for SonarQube. As noted by @Mithfindel, it may be the root cause.

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;

Ref: https://github.com/SonarSource/sonar-examples/blob/master/scripts/database/mysql/create_database.sql

It looks like a MySQL configuration issue: for MySQL, a GRANT on localhost is different from a GRANT on localhost's FQDN. You might want to check which permissions are granted to users 'sonar'@'localhost' and 'sonar'@'fqdn-of-localhost' or 'sonar'@'%' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM