SonarQube
部署完成后,在浏览器地址栏输入http://IP:9000
可以进入登录页面,以本机运行为例,地址为http://127.0.0.1:9000/
,默认登录名:admin
,登录密码也是admin
。登录后会要求设置密码:
按要求设置新密码即可,系统要求如下:
设置后即可登录系统:
从图中可见,如果用默认的嵌入式数据库(H2)的话,会有仅作测试用提示:
Embedded database should be used for evaluation purposes only. It doesn’t support scaling, upgrading to a new SonarQube Server version, or migration to another database engine.
插件安装
点击配置、应用市场:“Administration->Marketplace
”,可以安装插件,这里我们以“Chinese Pack
”中文语言包为例。
安装后界面变成了中文。此外应用市场Marketplace
还有很多插件可以供我们使用,这个可以在使用过程中逐渐探索。安装完成后系统会要求重启(启用插件)或复原(回退):
点击重启按钮“Restart Server
”后,会自动重启服务:
重启后系统需要重新登录:
登录后界面变成中文的了:
数据库配置
SonarQube
默认是H2
数据库,但事实上它支持Oracle
,PostgreSQL
和 Microsoft SQLServer
,四种数据库,通过修改config
文件夹下sonar.properties
的sonar.jdbc.url
属性,可以指向我们要使用的数据库。
整个sonar.properties
文件的数据库配置部分摘录如下:
#--------------------------------------------------------------------------------------------------
# DATABASE
#
# IMPORTANT:
# - The embedded H2 database is used by default. It is recommended for tests but not for
# production use. Supported databases are 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#----- Oracle 19c/21c
# The Oracle JDBC driver must be copied into the directory extensions/jdbc-driver/oracle/.
# Only the thin client is supported, and we recommend using the latest Oracle JDBC driver. See
# https://jira.sonarsource.com/browse/SONAR-9758 for more details.
# If you need to set the schema, please refer to http://jira.sonarsource.com/browse/SONAR-5000
#sonar.jdbc.url=jdbc:oracle:thin:@localhost:1521/XE#----- PostgreSQL 11 or greater
# By default the schema named "public" is used. It can be overridden with the parameter "currentSchema".
#sonar.jdbc.url=jdbc:postgresql://localhost/sonarqube?currentSchema=my_schema#----- Microsoft SQLServer 2014/2016/2017/2019/2022 and SQL Azure
# A database named sonar must exist and its collation must be case-sensitive (CS) and accent-sensitive (AS)
# Use the following connection string if you want to use integrated security with Microsoft Sql Server
# Do not set sonar.jdbc.username or sonar.jdbc.password property if you are using Integrated Security
# For Integrated Security to work, you have to install the Microsoft SQL JDBC Auth package
# Please refer to the online documentation https://docs.sonarsource.com/sonarqube
# for the exact procedure for this version of SonarQube.
#sonar.jdbc.url=jdbc:sqlserver://localhost;databaseName=sonar;integratedSecurity=true# Use the following connection string if you want to use SQL Auth while connecting to MS Sql Server.
# Set the sonar.jdbc.username and sonar.jdbc.password appropriately.
#sonar.jdbc.url=jdbc:sqlserver://localhost;databaseName=sonar#----- Connection pool settings
# The maximum number of active connections that can be allocated
# at the same time, or negative for no limit.
# The recommended value is 1.2 * max sizes of HTTP pools. For example if HTTP ports are
# enabled with default sizes (50, see property sonar.web.http.maxThreads)
# then sonar.jdbc.maxActive should be 1.2 * 50 = 60.
#sonar.jdbc.maxActive=60# The minimum number of connections that can remain idle in the pool,
# without extra ones being created, or zero to create none.
#sonar.jdbc.minIdle=10# The maximum number of milliseconds that the pool will wait (when there
# are no available connections) for a connection to be returned before
# throwing an exception, or <= 0 to wait indefinitely.
#sonar.jdbc.maxWait=8000
1. 支持的数据库
参考官方文档(Database requirements | SonarQube Community Build Documentation)及前述,我们可以看出,SonarQube支持以下数据库引擎:
数据库引擎 | 安装要求 |
---|---|
PostgreSQL | 版本:13-17(官方文档要求,实际上看配置文件,支持版本11及以上) |
Microsoft SQLServer | 官方文档版本要求:
配置文件实际版本要求:
注:
|
Oracle | 版本要求: 推荐:最新版本的 注:
|
2. 修改数据库连接
以Microsoft SQL Server
为例,我们尝试对SonarQube
连接的数据库进行配置。
(1)数据库创建
需要注意的是,SonarQube
要求大小写和强调差异(case-sensitive
and accent-sensitive
)敏感的排序规则,如:Chinese_PRC_CS_AS
。
ALTER DATABASE sonar COLLATE Chinese_PRC_CS_AS
(2)数据库连接修改
根据config/sonar.properties
配置文件的说明,修改连接指向后,表结构等内容会自动创建(要求具有相应权限)。
首先,我们设置数据库的用户名和密码:
sonar.jdbc.username=your_db_user
sonar.jdbc.password=your_db_password
接着,我们修改数据库连接字符串,对于SQL Server
,其配置参考如下:
#----- Microsoft SQLServer 2014/2016/2017/2019/2022 and SQL Azure
# A database named sonar must exist and its collation must be case-sensitive (CS) and accent-sensitive (AS)
# Use the following connection string if you want to use integrated security with Microsoft Sql Server
# Do not set sonar.jdbc.username or sonar.jdbc.password property if you are using Integrated Security
# For Integrated Security to work, you have to install the Microsoft SQL JDBC Auth package
# Please refer to the online documentation https://docs.sonarsource.com/sonarqube
# for the exact procedure for this version of SonarQube.
#sonar.jdbc.url=jdbc:sqlserver://localhost;databaseName=sonar;integratedSecurity=true# Use the following connection string if you want to use SQL Auth while connecting to MS Sql Server.
# Set the sonar.jdbc.username and sonar.jdbc.password appropriately.
#sonar.jdbc.url=jdbc:sqlserver://localhost;databaseName=sonar
我们可以选择集成安全模式(需要安装配套的包),或使用SQL Server
自己的鉴权方式,本例为了简单我们选择后者,考虑本机连接,Encrypt
属性设置为true
且 trustServerCertificate
属性设置为true
(信任服务器证书,使用自签名证书,否则会报错的):
sonar.jdbc.url=jdbc:sqlserver://localhost;databaseName=sonar;encrypt=true;trustServerCertificate=true
(3)修改后运行效果
重新启动SonarQube服务,我们可以观察到如下变化:
1)数据库相关对象自动创建了
2)密码需要重新设定登录
因为换了数据库,需要重新设置密码并登录。
从上图也可以看出不再提示测试数据库环境(H2
)了。
总结
至此,我本机环境的SonarQube
实现了数据库的更改,当然,在实际的应用中使用Docker
容器安装Sonar
会简便很多,但为了对这个工具有更多了解(踩更多的坑,更改数据库配置过程也遇到一些问题,参见这里),手工操作也是一个不错的体验。
接下来,我将尝试对这个工具做更多的学习、应用与探索。