Sonar uses a Derby or H2 as default database. When running Sonar, it says that these databases may only be used for evaluation. Now, in this article, I will tell you how to change the default database and how to configure Maven to use it.
Installation
First, download Sonar from its website at http://www.sonarqube.org/downloads/. Then, extract the zip file and the we can start configuring. Note that I will demonstrate the configuration with a MySQL database. You can use any other database you prefer.
Configuration
In the sonar folder, go to the conf-folder and open sonar.properties. You will see many commented lines. First comment line 48. That’s the connection String to the default database Sonar uses.
# Comment the following line to deactivate the default embedded database.
# sonar.jdbc.url: jdbc:h2:tcp://localhost:9092/sonar
Then, I will change line 40 and 41. These are the login credentials to my database. For MySQL, it will be:
sonar.jdbc.username: root
sonar.jdbc.password:
After that, we need to uncomment the MySQL connection string. (Note that other database may uncomment more properties).
#----- MySQL 5.x
# Comment the embedded database and uncomment the following line to use MySQL
sonar.jdbc.url: jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true
When that’s finished, we can start Sonar, but before we do that, make sure your database is running and that the Sonar database is created!. To start sonar, go to the bin-folder, select your operating system folder and click on StartSonar.xxx. For me, it will be windows-x86-32 and StartSonar.bat. After starting up, a command box will appear, create the needed tales in your database and start up. To see if Sonar is up and running, got to http://localhost:9000
Eclipse plugin
Now sonar is up and running, we need to configure Eclipse and Maven to use sonar. First, install the Eclipse plugin for Sonar. You can find it at http://dist.sonar-ide.codehaus.org/eclipse/. After installing the plugin, we need a little configuration in Maven to use the plugin.
Maven configuration
When you run Sonar in Maven without a configuration, Maven will still use the default database. We need to add a profile in the Maven settings. In the .m2 folder, open the settings.xml. Then add the code below. You also need to do that in the settings.xml in your Maven conf folder. You can find other database profiles at http://mojo.codehaus.org/sonar-maven-plugin/examples/use-enterprise-database.html
<profile>
<id>sonar</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<sonar.jdbc.url>jdbc:mysql://localhost:3306/sonar</sonar.jdbc.url>
<sonar.jdbc.username>root</sonar.jdbc.username>
<sonar.jdbc.password></sonar.jdbc.password>
<sonar.jdbc.driver>com.mysql.jdbc.Driver</sonar.jdbc.driver>
</properties>
</profile>
Update Maven in Eclipse
Now sonar and Maven are configured, we need to update your project in Eclipse. Right click on your project in Eclipse, go to Maven en click on Update Project. Then the Maven settings will become active.
Running Sonar in Eclipse
Now we are ready to run Sonar in Eclipse. Right click on your root project, select run as and click on run configurations. Create a new Maven build and insert into your goal: clean install sonar:sonar. Then click on run and your project will show up in Sonar. Enjoy!
“…In the .m2 folder, open the settings.xml. Than add the code below. You also need to do that in the settings.xml in your Maven conf folder…”
I would say it is enough to update one OR the other, but not both.
If you put the config in .m2/settings.xml, it will only apply to yourself.
If you put it in conf/settings.xml, it will apply to all users using the same maven-installation.
If you’re the only user on your machine, it doesn’t matter which you chose.
Ta-Da! Works well for me. Many thanks! You’re a star! 🙂
actually, you have to do the same configuration in the sonar runner too 🙂
Heeeeeeey thank you very much!
What changes need to be made to the sonar runner as I have tried the above but when I go to start sonar in the bin folder, it comes to the end of the CMD screen and says it is stopping and then it is stopped.
[…] trying alter default database mysql. used tutorials like https://laurenthinoul.com/how-to-change-sonar-default-database/. mysql database created , […]
Hi , We are currently using the default H2 database . We are planning to migrate to mysql . Can you please let me know how can we do that ?
Commenting the “H2” line from “sonar.properties” doesn’t fix it for me. After a restart the database is still H2. Is there another file somewhere else? SonarQube 7.9.1