Step-by-Step Guide to Migrating SonarQube from MySQL to PostgreSQL and Upgrading to Version 7.9.1
This guide walks through preparing test data, migrating a SonarQube database from MySQL to PostgreSQL, running the MySQL‑Migrator tool, restarting services, verifying the migration, and finally upgrading SonarQube from 6.7.7 to the 7.9.1 LTS release with JDK 11.
Overall Approach
Prepare test data (optional in production).
Database migration (MySQL → PostgreSQL) – required from SonarQube 7.9 onward.
SonarQube version upgrade (6.7.7 → 7.9.1).
Prepare Test Data
Create User Data
Create Project Data
Install sonar‑scanner (mac)
tar zxf sonar-scanner-4.0.0.1744-macosx.tar.gz -C /usr/local/
export SONAR_HOME=/usr/local/sonar-scanner-4.0.0.1744-macosx
export PATH=$PATH:$M2_HOME/bin:$SONAR_HOME/binGet Maven project
git clone https://github.com/zeyangli/simple-java-maven-app.git
mvn clean package
ls -l target/ | awk '{print $NF}'
classes
maven-archiver
maven-status
my-app-1.1-SNAPSHOT.jar
surefire-reports
test-classesScan Project
Write a simple loop to run the scanner multiple times (the first run may show no issues).
#!/bin/bash
projectName="demo-service"
scanTime=`date +%Y%m%d%H%M%S`
for((i=1;i<=10;i++))
do
sonar-scanner -Dsonar.host.url=http://xxxxxx:9000 \
-Dsonar.projectKey=${projectName}${i} \
-Dsonar.projectName=${projectName}${i} \
-Dsonar.projectVersion=${scanTime} \
-Dsonar.login=admin \
-Dsonar.password=admin \
-Dsonar.ws.timeout=30 \
-Dsonar.projectDescription="my first project!" \
-Dsonar.links.homepage=http://www.baidu.com \
-Dsonar.sources=src \
-Dsonar.sourceEncoding=UTF-8 \
-Dsonar.java.binaries=target/classes \
-Dsonar.java.test.binaries=target/test-classes \
-Dsonar.java.surefire.report=target/surefire-reports
echo "${projectName} scan success!"
doneResult:
Prepare Database Migration
We are migrating from MySQL to PostgreSQL.
Start a SonarQube instance of the same version as the source to perform the migration.
Using a different version will cause errors (see FAQ 2).
Backup MySQL
[root@devops ~]# mysqldump -h 127.0.0.1 -uroot -p sonar >sonar-190803.sql
Enter password:
[root@devops ~]# du -sh sonar-190803.sql
4.6M sonar-190803.sqlConfigure PostgreSQL
# Install PostgreSQL 9.6
yum install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm
yum install postgresql96-server postgresql96-contrib
/usr/pgsql-9.6/bin/postgresql96-setup initdb
systemctl start postgresql-9.6
systemctl enable postgresql-9.6
# Create Sonar database
su - postgres
psql
create user sonarqube with password 'sonarqube';
create database sonarqube owner sonarqube;
grant all on database sonarqube to sonarqube;
\q
# Remote access settings
vi /var/lib/pgsql/9.6/data/postgresql.conf
#listen_addresses = '*'
vi /var/lib/pgsql/9.6/data/pg_hba.conf
# IPv4 local connections:
host all all 0.0.0.0/0 trust
# IPv6 local connections:
host all all ::1/128 ident
host all all 0.0.0.0/0 identStart New Sonar Instance
# Install the same version SonarQube
mkdir pg
unzip sonarqube-6.7.7.zip -d pg/
cp sonarqube-6.7.7/conf/sonar.properties pg/sonarqube-6.7.7/conf/sonar.properties
# Keep all original settings, only change DB configuration to PostgreSQL
sonar.jdbc.username=xxxx
sonar.jdbc.password=xxxx
sonar.jdbc.url=jdbc:postgresql://127.0.0.1/sonarqube
# Start the service (required, otherwise you will see version‑related errors)
./sonar.sh startMySQL Migrator Tool
Project: https://github.com/SonarSource/mysql-migrator
# Download and unzip
wget https://binaries.sonarsource.com/Distribution/mysql-migrator/mysql-migrator-1.1.0.119.zip
unzip mysql-migrator-1.1.0.119.zip && cd bin
# source.properties (MySQL connection)
sonar.jdbc.username=xxxx
sonar.jdbc.password=xxxx
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
# target.properties (PostgreSQL connection)
sonar.jdbc.username=xxxx
sonar.jdbc.password=xxxx
sonar.jdbc.url=jdbc:postgresql://127.0.0.1/sonarqube
# Run migration
./mysql-migrator -source source.properties -target target.properties
[main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table organizations ...
... (output truncated) ...
Migration successful in 2.7 seconds
# Verify data in PostgreSQL
\c sonarqube
\dt
select * from projects;Restart Service
# Remove old ES index
rm -fr data/es5
# Restart SonarQube
./sonar.sh restartValidate Data
Project information:
User information:
Summary
Database migration is complete; next step is to upgrade SonarQube to 7.9.1 LTS.
Upgrade to 7.9.1
Install JDK 11
# Currently Sonar 6.7.7 runs on JDK 8, so we can install JDK 11 without changing the global JAVA_HOME yet.
tar zxf jdk-11_linux-x64_bin.tar.gz -C /usr/local/Configure SonarQube 7.9.1
https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-7.9.1.zip
unzip sonarqube-7.9.1.zip
cp pg/sonarqube-6.7.7/conf/sonar.properties sonarqube-7.9.1/conf/sonar.properties
# SonarQube 7.9.1 requires JDK 11
vim ../../conf/wrapper.conf
wrapper.java.command=/usr/local/jdk-11/bin/javaSwitch Service
# Stop old version
cd pg/sonarqube-6.7.7/bin/linux-x86-64/
./sonar.sh stop
# Start new version
cd sonarqube-7.9.1/bin/linux-x86-64/
./sonar.sh startAfter start you may see an error screen that requires initialization.
Initialize the service at http://119.3.228.122:9000/setup and follow the wizard.
Upgrade completed successfully and no data was lost.
Summary
Upgrade finished; the remaining work is to install required plugins.
FAQ
1. Could not determine SonarQube version of the target database. Could not select version from schema migrations. ERROR: relation "schema migrations" does not exist
A newly created empty PostgreSQL instance cannot be used for migration until a Sonar instance of the same version has been started and created the necessary schema tables.
2. Versions in source and target database don't match: 1838 != 2804
Make sure to run the migration with a SonarQube instance that matches the source version.
3. Migration completed but no data appears – what went wrong?
Delete the Elasticsearch index before starting SonarQube again (see the "Restart Service" section).
4. SonarQube is under maintenance
After an upgrade you need to initialize the instance as described in the "Switch Service" section.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
