参考链接:

PS:

  • Sonarqube自带了轻量级的H2数据库,测试和跑少量项目是没有问题的。基于后期的性能、运维、扩展等考虑,我们选择使用Mysql数据库。
  • Sonarqube自带了web server,也可以使用Tomcat来进行加载。基于以往的使用经验默认的web server足够使用,我们选择默认。

搭建环境

  • OS: Windows Server 2012 R2
  • JDK: 1.8.0_71
  • DB: Mysql 5.6.26
  • Sonar: 6.3.1

搭建步骤

  1. 安装JDK

    1
    2
    3
    4
    C:\Users\Administrator>java -version
    java version "1.8.0_71"
    Java(TM) SE Runtime Environment (build 1.8.0_71-b15)
    Java HotSpot(TM) 64-Bit Server VM (build 25.71-b15, mixed mode)
  2. 安装Mysql

    1
    2
    C:\Users\Administrator>mysql -V
    mysql Ver 14.14 Distrib 5.6.26, for Win64 (x86_64)
  3. 创建数据库和用户

    1
    2
    3
    4
    5
    6
    7
    8
    # 创建sonar数据库
    CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
    # 创建sonar用户
    CREATE USER 'sonar' IDENTIFIED BY 'passwd';
    # 赋予sonar用户对sonar数据库权限
    GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'passwd';
    GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'passwd';
    FLUSH PRIVILEGES;
  4. 安装Sonarqube

    • 解压缩sonarqube-6.3.1.zip到D:\scm\sonarqube-6.3.1

    • 修改配置D:\scm\sonarqube-6.3.1\conf\sonar.properties

      • sonar.jdbc.username=sonar
      • sonar.jdbc.password=passwd
      • sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
      • sonar.web.host=本机IP
      • sonar.web.context=
      • sonar.web.port=9000
      • sonar.web.http.maxThreads=500
    • 配置Sonarqube windows服务

      1. 以管理员身份运行D:\scm\sonarqube-6.3.1\bin\windows-x86-64\InstallNTService.bat创建服务
        logo
      2. 配置Administrator账户运行Sonarqube服务
        logo
      3. 启动Sonarqube服务
        • 建议使用上面配置的windows service启动
        • 也可以直接运行D:\scm\sonarqube-6.3.1\bin\windows-x86-64\StartSonar.bat
  5. 网页端登陆配置Sonarqube

    • http://搭建机器ip:9000
    • 用户名/密码:admin

PS1:

在安装的过程中遇到问题:

  • 运行InstallNTService.bat生成系统服务的时候没有使用管理员身份;
  • Sonarqube服务创建成功,但是启动的时候失败;
    1
    2
    3
    4
    2017.06.20 14:34:50 ERROR web[][o.a.c.c.C.[.[.[/]] Exception sending context initialized event to listener instance of class org.sonar.server.platform.web.PlatformServletContextListener
    org.sonar.api.utils.MessageException: Current version is too old. Please upgrade to Long Term Support version firstly.
    2017.06.20 14:34:50 ERROR web[][o.a.c.c.StandardContext] One or more listeners failed to start. Full details will be found in the appropriate container log file
    2017.06.20 14:34:50 ERROR web[][o.a.c.c.StandardContext] Context [] startup failed due to previous errors
  • 查看发现mysql数据库sonar已经有了很多表;

解决方法:

  1. 删除sonar数据库里面的表,删除Sonarqube服务
  2. 安装上面过程:先以管理员身份运行InstallNTService.bat,然后配置Administrator账户运行Sonarqube服务
  3. 重新启动Sonarqube服务即可。

PS2:

如果Mysql数据库启用了binarylog的话,请设置为row格式。否则在启动Sonarqube的时候创建数据库表会报下面错误:

1
2
Error updating database.  Cause: java.sql.SQLException: Cannot execute statement: impossible to write to binary log since BINLOG_FORMAT = STATEMENT and at least one table uses a storage engine limited to row-based logging. InnoDB is limited to row-logging when transaction isolation level is READ COMMITTED or READ UNCOMMITTED.