• SonarQube LTS 7.9.1升级指南


    SonarQube LTS 7.9版之后,放弃了对于MySQL数据库的支持,同时Java的版本已经升至Java 11. 由于此两项依赖均为强制性依赖,所以从老版本上升上来的SonarQube至少需要完成Java版本的升级和数据库的移植才能实现SonarQube的版本升级。这篇文章以容器化的SonarQube服务升级为例,介绍如何实现从SonarQube LTS 6.7.1版本 + MySQL 5.7.16版本 升至SonarQube 7.9.1版本 + PostgreSQL 12.1版本。

    一、事前准备

    1、SonarQube环境搭建

    准备LTS 6.7.1版本 + MySQL 5.7.16的环境,详细可参看:

    • https://liumiaocn.blog.csdn.net/article/details/102691455

    PS:在做数据库迁移的时候,先运行一个同版本的新sonar进行迁移,所以先要搭建一个基于 Postgres 的 SonarQube  LTS 6.7.1

    其实此工具应该是建立在对应的数据库结构创建完成的基础上,所以才会要求相同版本。因为本文使用容器化的方式,所以只需要修改docker-compose.yml文件做如下设定即可:

    LTS 6.7.1版本 + MySQL 5.7.16

    [root@localhost ~]# docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=111111 -e MYSQL_DATABASE=sonar liumiaocn/mysql:5.7.16
    [root@localhost ~]# docker run -d --name sonar-mysql -p 9001:9000 -e SONARQUBE_JDBC_USERNAME=root -e SONARQUBE_JDBC_PASSWORD=111111 -e SONARQUBE_JDBC_URL="jdbc:mysql://172.17.0.4:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance" liumiaocn/sonarqube:6.7.1

    LTS 6.7.1版本 + postgres12 

    [root@localhost ~]# docker run -d --name postgres -e POSTGRES_USER=sonar -e POSTGRES_PASSWORD=sonar -e PGDATA=/var/lib/postgresql/data/pgdata postgres:12-alpine
    [root@localhost ~]# docker run -d --name sonar-pos -p 9002:9000 --link=postgres -e SONARQUBE_JDBC_URL=jdbc:postgresql://postgres:5432/sonar liumiaocn/sonarqube:6.7.1

    运行后结构如下:

    • LTS 6.7.1版本 + MySQL 5.7.16   ,启动端口:9001
    • LTS 6.7.1版本 + postgres12 , 启动端口:  9002

    2、准备测试数据

    创建项目数据

    安装sonar-scanner

    [root@localhost ~]# wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.6.0.2311-linux.zip
    [root@localhost ~]# unzip  sonar-scanner-cli-4.6.0.2311-linux.zip

    获取maven项目

    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-classes 

    扫描项目

    [root@localhost ~]# cd simple-java-maven-app/
    [root@localhost simple-java-maven-app]# /usr/local/sonar-scanner-4.5.0/bin/sonar-scanner  -Dsonar.host.url=http://192.168.130.133:9001 -Dsonar.sources=.  -Dsonar.login=admin  -Dsonar.password=admin -Dsonar.projectKey=test

    二、数据迁移

    SonarQube提供了从MySQL将数据移出的工具,项目详细可以参看:  https://github.com/SonarSource/mysql-migrator

    • 下载地址:https://binaries.sonarsource.com/Distribution/mysql-migrator/
    • 当前最新版本zip文件名称:mysql-migrator-1.1.0.119.zip

    1、下载数据迁移工具

    进入源SonarQube容器中(服务端口9001)

    使用wget命令下载此文件至本地。

    sonarqube@178c1767d4c9:/opt/sonarqube$ wget https://binaries.sonarsource.com/Distribution/mysql-migrator/mysql-migrator-1.1.0.119.zip
    --2021-01-27 12:17:05--  https://binaries.sonarsource.com/Distribution/mysql-migrator/mysql-migrator-1.1.0.119.zip
    Resolving binaries.sonarsource.com (binaries.sonarsource.com)... 91.134.125.245
    Connecting to binaries.sonarsource.com (binaries.sonarsource.com)|91.134.125.245|:443... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 2632311 (2.5M) [application/zip]
    Saving to: ‘mysql-migrator-1.1.0.119.zip’
    
    mysql-migrator-1.1.0.119.zip                        100%[=================================================================================================================>]   2.51M   391KB/s    in 7.1s
    
    2021-01-27 12:17:13 (364 KB/s) - ‘mysql-migrator-1.1.0.119.zip’ saved [2632311/2632311]
    
    sonarqube@178c1767d4c9:/opt/sonarqube$ unzip mysql-migrator-1.1.0.119.zip
    Archive:  mysql-migrator-1.1.0.119.zip
       creating: mysql-migrator-1.1.0.119/
       creating: mysql-migrator-1.1.0.119/lib/
      inflating: mysql-migrator-1.1.0.119/lib/mysql-migrator-1.1.0.119.jar
      inflating: mysql-migrator-1.1.0.119/lib/jsr305-1.3.9.jar
      inflating: mysql-migrator-1.1.0.119/lib/slf4j-simple-1.7.26.jar
      inflating: mysql-migrator-1.1.0.119/lib/slf4j-api-1.7.26.jar
      inflating: mysql-migrator-1.1.0.119/lib/postgresql-42.2.5.jar
      inflating: mysql-migrator-1.1.0.119/lib/mysql-connector-java-5.1.47.jar
      inflating: mysql-migrator-1.1.0.119/lib/mssql-jdbc-6.2.2.jre8.jar
       creating: mysql-migrator-1.1.0.119/bin/
      inflating: mysql-migrator-1.1.0.119/bin/mysql-migrator
      inflating: mysql-migrator-1.1.0.119/bin/mysql-migrator.bat
    sonarqube@178c1767d4c9:/opt/sonarqube$ cd mysql-migrator-1.1.0.119
    sonarqube@178c1767d4c9:/opt/sonarqube/mysql-migrator-1.1.0.119$ ls
    bin  lib
    sonarqube@178c1767d4c9:/opt/sonarqube/mysql-migrator-1.1.0.119$ cd bin/
    sonarqube@178c1767d4c9:/opt/sonarqube/mysql-migrator-1.1.0.119/bin$ ls
    mysql-migrator  mysql-migrator.bat
    sonarqube@178c1767d4c9:/opt/sonarqube/mysql-migrator-1.1.0.119/bin$ cat >mysql.properties <<EOF
    sonar.jdbc.username=root
    sonar.jdbc.password=111111
    sonar.jdbc.url=jdbc:mysql://172.17.0.4:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
    EOF
    sonarqube@178c1767d4c9:/opt/sonarqube/mysql-migrator-1.1.0.119/bin$
    sonarqube@178c1767d4c9:/opt/sonarqube/mysql-migrator-1.1.0.119/bin$ cat > postgres.properties <<EOF
    sonar.jdbc.username=sonar
    sonar.jdbc.password=sonar
    sonar.jdbc.url=jdbc:postgresql://172.17.0.2:5432/sonar
    EOF

    2、数据移行实施

    sonarqube@178c1767d4c9:/opt/sonarqube/mysql-migrator-1.1.0.119/bin$ sh mysql-migrator -source mysql.properties -target postgres.properties
    Wed Jan 27 12:18:50 UTC 2021 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table organizations ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table organization_members ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table groups_users ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table rules_parameters ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table rules_profiles ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table org_qprofiles ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table default_qprofiles ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table project_qprofiles ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table qprofile_edit_users ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table qprofile_edit_groups ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table groups ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table snapshots ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table group_roles ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table rule_repositories ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table deprecated_rule_keys ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table rules ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table rules_metadata ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table events ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table quality_gates ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table quality_gate_conditions ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table org_quality_gates ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table properties ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table project_links ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table duplications_index ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table live_measures ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table project_measures ...
    
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table internal_properties ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table projects ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table manual_measures ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table active_rules ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table notifications ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table user_roles ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table active_rule_parameters ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table users ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table metrics ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table issues ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table issue_changes ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table permission_templates ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table perm_tpl_characteristics ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table perm_templates_users ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table perm_templates_groups ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table qprofile_changes ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table file_sources ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table ce_queue ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table ce_activity ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table ce_task_characteristics ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table ce_task_input ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table ce_scanner_context ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table ce_task_message ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table user_tokens ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table es_queue ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table plugins ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table project_branches ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table analysis_properties ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table webhooks ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table webhook_deliveries ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table alm_app_installs ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table project_alm_bindings ...
    [main] INFO org.sonarsource.sqdbmigrator.migrator.ContentCopier - copying table project_mappings ...
    Tables                    Records  Seconds
    ------------------------  -------  -------
    organizations                   1      0.3
    organization_members          137      0.1
    groups_users                  138      0.2
    rules_parameters              400      0.2
    rules_profiles                 21      0.1
    org_qprofiles                  21      0.1
    default_qprofiles              14      0.0
    project_qprofiles               0      0.0
    qprofile_edit_users             0      0.1
    qprofile_edit_groups            0      0.0
    groups                          2      0.1
    snapshots                    9590      0.6
    group_roles                   164      0.0
    rule_repositories              39      0.0
    deprecated_rule_keys            0      0.1
    rules                       10013      1.7
    rules_metadata                  1      0.1
    events                      13284      0.7
    quality_gates                   3      0.1
    quality_gate_conditions         9      0.0
    org_quality_gates               3      0.1
    properties                     34      0.0
    project_links                   2      0.3
    duplications_index              0      0.2
    live_measures              341769     24.6
    project_measures          2024189     97.6
    internal_properties             3      0.2
    projects                    15373      1.6
    manual_measures                 0      0.2
    active_rules                16165      0.3
    notifications                   0      0.1
    user_roles                      0      0.1
    active_rule_parameters        233      0.1
    users                         137      0.2
    metrics                       169      0.2
    issues                     289760     19.9
    issue_changes             2452287     76.8
    permission_templates            1      0.1
    perm_tpl_characteristics        0      0.1
    perm_templates_users            0      0.1
    perm_templates_groups           5      0.0
    qprofile_changes            16194      0.5
    file_sources                 5802      4.4
    ce_queue                        0      0.3
    ce_activity                  9774      0.8
    ce_task_characteristics         0      0.1
    ce_task_input                   0      0.0
    ce_scanner_context           1726      0.7
    ce_task_message                 0      0.1
    user_tokens                     3      0.1
    es_queue                        0      0.0
    plugins                        20      0.1
    project_branches               52      0.0
    analysis_properties             0      0.0
    webhooks                        0      0.1
    webhook_deliveries              0      0.1
    alm_app_installs                0      0.1
    project_alm_bindings            0      0.5
    project_mappings                0      0.1
    
    Migration successful in 238.4 seconds

    3、删除ES索引并重启SonarQube服务

    进入postgres12 对应的SonarQube容器

    此时如果重启会发现,似乎从MySQL导入的数据并没有起作用,这是因为还需要清一下之前的ES的数据。

    #清除es索引
    rm -fr data/es5

    重启SonarQube容器

    [root@localhost simple-java-maven-app]# docker restart 11aed04a7bd6

    此时即可看到数据已经导入的效果

    4、升级SonarQube

     如果非容器方式的升级,则需要升级SonarQube所使用的JDK以及SonarQube自身。而容器化则非常简单,只需要将上述镜像的tag修改一下即可:

    删除 postgres 12  原先对于的 sonarqube容器,执行以下升级的容器命令:

    docker run -d --name sonarqube7.9 -p 9002:9000 --link=postgres -e SONARQUBE_JDBC_URL=jdbc:postgresql://postgres:5432/sonar sonarqube:7.9.1-community

    再次通过  9002 端口进行访问可以看到如下信息:

     需要重新进行初始化服务,访问地址为  http://localhost:9002/setup

    点击Upgrade按钮,并重启SonarQube服务,即可确认升级后的信息了. 如果重启时发现无法启动,并提示如下错误信息

    可通过尝试删除es6的内容后重启SonarQube服务来解决

    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

    新创建的空的PG是不能进行迁移的,需要同版本的soanr使用后才会找到和这个表!

    2.Versions in source and target database don't match: 1838 != 2804

    此问题一定要注意在做数据库迁移的时候,先运行一个同版本的新sonar进行迁移哦!

    3.迁移完成没有数据怎么办? 是不是我操作错误了?

    需要先删除es索引再启动sonarqube,详见切换服务部分。

    4.出现SonarQube is under maintenance

    升级后需要初始化详见切换服务部分。

    转载于:  https://blog.csdn.net/weixin_38168728/article/details/101732099

                  https://blog.csdn.net/liumiaocn/article/details/102788571

  • 相关阅读:
    HTML5来了,7个混合式移动开发框架
    React Native
    Cordova
    Java Next: Groovy、Scala or Clojure?
    分页 Database Frist
    Kindeditor编译器
    sql内连接
    layer删除
    实现用户注册,要求输入用户名之后,焦点离开判断用户名是否存在,如果存在给出提示,不存在允许注册改用户。要求使用AJAX
    实现根据用户名查询用户信息,要求用AJAX
  • 原文地址:https://www.cnblogs.com/deny/p/14338564.html
Copyright © 2020-2023  润新知