1.环境

使用VM VirtualBox + Vagrant专门为SonarQube启动的虚拟机,具体环境如下。

linux版本

[root@sonarqube ~]# cat /proc/version
Linux version 3.10.0-1127.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) ) #1 SMP Tue Mar 31 23:36:51 UTC 2020
[root@sonarqube ~]# cat /etc/redhat-release
CentOS Linux release 7.8.2003 (Core)

docker版本

[root@sonarqube ~]# docker --version
Docker version 20.10.21, build baeda1f

内存

[root@sonarqube ~]# cat /proc/meminfo | grep MemTotal
MemTotal:        2914280 kB

2.运行SonarQube

选择7.8版本的SonarQube,这是支持Java8的最高版本。

docker pull sonarqube:7.8-community
docker run -d --name sonarqube sonarqube:7.8-community

创建想要挂载的目录

mkdir -p sonarqube/{conf,logs,data,extensions}
docker cp sonarqube:/opt/sonarqube/conf /mydata/sonarqube
docker cp sonarqube:/opt/sonarqube/data /mydata/sonarqube
docker cp sonarqube:/opt/sonarqube/logs /mydata/sonarqube
docker cp sonarqube:/opt/sonarqube/extensions /mydata/sonarqube

3.配置MySQL持久存储

查看配置文件,开头就是有关数据库的部分。

vim /mydata/sonarqube/conf/sonar.properties

# Property values can:
# - reference an environment variable, for example sonar.jdbc.url= ${env:SONAR_JDBC_URL}
# - be encrypted. See https://redirect.sonarsource.com/doc/settings-encryption.html

#--------------------------------------------------------------------------------------------------
# DATABASE
#
# IMPORTANT:
# - The embedded H2 database is used by default. It is recommended for tests but not for
#   production use. Supported databases are MySQL, Oracle, PostgreSQL and Microsoft SQLServer.
# - Changes to database connection URL (sonar.jdbc.url) can affect SonarSource licensed products.

# User credentials.
# Permissions to create tables, indices and triggers must be granted to JDBC user.
# The schema must be created first.
#sonar.jdbc.username=
#sonar.jdbc.password=

#----- Embedded Database (default)
# H2 embedded database server listening port, defaults to 9092
#sonar.embeddedDatabase.port=9092

#----- DEPRECATED 
#----- MySQL >=5.6 && <8.0
# Support of MySQL is dropped in Data Center Editions and deprecated in all other editions
# Only InnoDB storage engine is supported (not myISAM).
# Only the bundled driver is supported. It can not be changed.
#sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false

安装MySQL

sudo docker pull mysql:5.7
sudo docker run -p 3306:3306 --name mysql \
-v /mydata/mysql/log:/var/log/mysql \
-v /mydata/mysql/data:/var/lib/mysql \
-v /mydata/mysql/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7

修改MySQL配置

vim /mydata/mysql/conf/my.conf

[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve

docker restart mysql

创建数据库和用户

docker exec -it mysql /bin/bash
mysql -u root -p
create database sonar;
CREATE USER 'sonar'@'%' IDENTIFIED WITH mysql_native_password BY 'sonar';
GRANT ALL PRIVILEGES ON *.* TO 'sonar'@'%';

重新启动

docker run -d --name sonarqube \
-p 9000:9000 -p 9092:9092 \
-e SONARQUBE_JDBC_URL="jdbc:mysql://192.168.56.100:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false" \
-v /mydata/sonarqube/logs:/opt/sonarqube/logs \
-v /mydata/sonarqube/conf:/opt/sonarqube/conf \
-v /mydata/sonarqube/data:/opt/sonarqube/data \
-v /mydata/sonarqube/extensions:/opt/sonarqube/extensions \
sonarqube:7.8-community

4.报错:Process exited with exit value [es]: 1

这是因为挂载的目录权限不够导致es启动失败,修改权限重新启动容器。

chmod -R 777 /mydata/sonarqube/
docker restart sonarqube

5.报错:Process exited with exit value [es]: 143

查看日志可知max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

vim /etc/sysctl.conf 
vm.max_map_count=262144
sysctl -p
docker restart sonarqube

6.中文插件

中文插件版本与SonarQube版本有着严格的对应关系,将插件放置到/mydata/sonarqube/extensions/plugins目录下重启容器即可。

SonarQube9.09.19.29.39.49.59.69.7
sonar-l10n-zh9.09.19.29.39.49.59.69.7
SonarQube8.08.18.28.38.48.58.68.78.88.9
sonar-l10n-zh8.08.18.28.38.48.58.68.78.88.9
SonarQube7.07.17.27.37.47.57.67.77.87.9
sonar-l10n-zh1.201.211.221.231.241.251.261.271.281.29
SonarQube6.06.16.26.36.46.56.66.7
sonar-l10n-zh1.121.131.141.151.161.171.181.19
SonarQube 5.45.55.6
sonar-l10n-zh 1.91.101.11
SonarQube4.04.1
sonar-l10n-zh1.71.8
SonarQube 3.13.23.33.43.53.63.7
sonar-l10n-zh 1.01.11.21.31.41.51.6

7.报错:Failed to upload report - An error has occurred. Please contact your administrator

在进行项目分析时报错,查看日志可知

Caused by: com.mysql.jdbc.PacketTooBigException: Packet for query is too large (7079751 > 4194304). You can change this value on the server by setting the max_allowed_packet' variable.
        at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3681)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2512)
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2683)
        at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2486)
        at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)
        at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2079)
        at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2013)
        at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5104)
        at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1998)
        at org.apache.commons.dbcp2.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:136)
        at org.apache.commons.dbcp2.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:136)
        at org.sonar.db.ce.CeTaskInputDao.insert(CeTaskInputDao.java:53)

解决方案:修改数据库配置重启容器即可。

8.设置自启

docker update --restart=always mysql
docker update --restart=always sonarqube 
Last modification:December 10th, 2022 at 04:55 pm
如果觉得我的文章对你有用,请随意赞赏