SonarQube安装和使用

一、环境准备

安装环境为centos6.5,需要准备的软件有sonarqube、sonar-scanner、jdk1.8.0、mysql。sonarqube也支持oracle(11g/12c)、postgresql(8.x/9.x)、SqlServer(2008/2012/2014 and SQL Azure)、mysql(5.6以上)多种数据库。此案例选择mysql。

1.1 SonarQube

下载地址:https://www.sonarqube.org/downloads/ 。目前官网最新版本为7.5,BI目前使用的版本为7.5。

1.2 JDK

官网信息,需要用到jdk1.8,如果你的环境已经配置了JAVA_HOME是jdk1.7,没关系我们可以手动指定sonar的运行jdk为1.8(后面配置的时候说明)。

1.3 MySQL

1.检测系统是否自带安装mysql

yum list installed | grep mysql

2.如果发现有系统自带mysql,先卸载

yum -y remove mysql-libs.x86_64

3.下载mysql的yum源

wget http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm

4.接着执行一下命令添加yarn源,在/etc/yum.repos.d/这个目录下多出mysql-community-source.repo和mysql-community.repo

rpm -ivh mysql-community-release-el6-5.noarch.rpm

5.查看一下是否已经有mysql可安装文件

yum repolist all | grep mysql

6.安装mysql服务器命令(一路yes):

yum install mysql-community-server

7.安装成功后

service mysqld start

8.由于mysql刚刚安装完的时候,mysql的root用户的密码默认是空的,所以我们需要及时用mysql的root用户登录(第一次回车键,不用输入密码),并修改密码

mysql -u root;
use mysql;
update user set password=PASSWORD("这里输入root用户密码") where User='root';
flush privileges;

9.查看mysql是否自启动,并且设置开启自启动命令

chkconfig --list | grep mysqld
chkconfig mysqld on

10.mysql 添加sonar用户

CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE USER 'sonar' IDENTIFIED BY 'sonar';
GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';
GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';
FLUSH PRIVILEGES;

二、SonarQube安装

1.新建一个用户

groupadd sonar
useradd sonar -g sonar -p sonar

2.新建sonarqube目录

mkdir -p /usr/local/sonarqube
chown  sonar:sonar /usr/local/sonarqube

3.使用sonar解压软件包到目录/usr/local/sonarqube

su - sonar
unzip -d /usr/local/sonarqube  sonarqube-7.5.zip

4.配置JDK

vi /usr/local/sonarqube/sonarqube-7.5/conf/wrapper.conf 
wrapper.java.command=/usr/java/default
wrapper.java.command=java

5.配置数据库账号

vi /usr/local/sonarqube/sonarqube-7.5/conf/sonar.properties 
sonar.jdbc.username=root
sonar.jdbc.password=root
sonar.jdbc.url=jdbc:mysql://172.17.213.205:3306/sonar?useUnicode=true&characterEncoding=utf8&useConfigs=maxPerformance&rewriteBatchedStatements=true
sonar.web.host=172.17.213.205
sonar.web.context=/sonarqube
sonar.web.port=9000
sonar.login=admin
sonar.password=admin

6.启动SonarQube
/usr/local/sonarqube/sonarqube-7.5/bin/linux-x86-64/sonar.sh start

7.查看启动日志
tail -200f ./logs/sonar.log

<-- Wrapper Stopped
--> Wrapper Started as Daemon
Launching a JVM...
Wrapper (Version 3.2.3) http://wrapper.tanukisoftware.org
  Copyright 1999-2006 Tanuki Software, Inc.  All Rights Reserved.

2019.01.03 14:20:22 INFO  app[][o.s.a.AppFileSystem] Cleaning or creating temp directory /usr/local/sonarqube/sonarqube-7.5/temp
2019.01.03 14:20:22 INFO  app[][o.s.a.es.EsSettings] Elasticsearch listening on /127.0.0.1:9001
2019.01.03 14:20:22 INFO  app[][o.s.a.p.ProcessLauncherImpl] Launch process[[key='es', ipcIndex=1, logFilenamePrefix=es]] from [/usr/local/sonarqube/sonarqube-7.5/elasticsearch]: /usr/local/sonarqube/sonarqube-7.5/elasticsearch/bin/elasticsearch -Epath.conf=/usr/local/sonarqube/sonarqube-7.5/temp/conf/es
2019.01.03 14:20:22 INFO  app[][o.s.a.SchedulerImpl] Waiting for Elasticsearch to be up and running
2019.01.03 14:20:23 INFO  app[][o.e.p.PluginsService] no modules loaded
2019.01.03 14:20:23 INFO  app[][o.e.p.PluginsService] loaded plugin [org.elasticsearch.transport.Netty4Plugin]
2019.01.03 14:20:33 INFO  app[][o.s.a.SchedulerImpl] Process[es] is up
2019.01.03 14:20:33 INFO  app[][o.s.a.p.ProcessLauncherImpl] Launch process[[key='web', ipcIndex=2, logFilenamePrefix=web]] from [/usr/local/sonarqube/sonarqube-7.5]: /usr/java/default/jre/bin/java -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djava.io.tmpdir=/usr/local/sonarqube/sonarqube-7.5/temp -Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError -cp ./lib/common/*:/usr/local/sonarqube/sonarqube-7.5/lib/jdbc/mysql/mysql-connector-java-5.1.46.jar org.sonar.server.app.WebServer /usr/local/sonarqube/sonarqube-7.5/temp/sq-process7626714452449040716properties
2019.01.03 14:20:49 INFO  app[][o.s.a.SchedulerImpl] Process[web] is up
2019.01.03 14:20:49 INFO  app[][o.s.a.p.ProcessLauncherImpl] Launch process[[key='ce', ipcIndex=3, logFilenamePrefix=ce]] from [/usr/local/sonarqube/sonarqube-7.5]: /usr/java/default/jre/bin/java -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djava.io.tmpdir=/usr/local/sonarqube/sonarqube-7.5/temp -Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError -cp ./lib/common/*:/usr/local/sonarqube/sonarqube-7.5/lib/jdbc/mysql/mysql-connector-java-5.1.46.jar org.sonar.ce.app.CeServer /usr/local/sonarqube/sonarqube-7.5/temp/sq-process4454811775570944598properties
2019.01.03 14:20:54 INFO  app[][o.s.a.SchedulerImpl] Process[ce] is up
2019.01.03 14:20:54 INFO  app[][o.s.a.SchedulerImpl] SonarQube is up
2019.01.03 14:26:21 INFO  app[][o.s.a.SchedulerImpl] Process [ce] is stopped
2019.01.03 14:26:24 INFO  app[][o.s.a.SchedulerImpl] Process [web] is stopped
2019.01.03 14:26:24 WARN  app[][o.s.a.p.AbstractProcessMonitor] Process exited with exit value [es]: 143
2019.01.03 14:26:24 INFO  app[][o.s.a.SchedulerImpl] Process [es] is stopped
2019.01.03 14:26:24 INFO  app[][o.s.a.SchedulerImpl] SonarQube is restarting
2019.01.03 14:26:24 WARN  app[][o.s.a.p.AbstractProcessMonitor] Process exited with exit value [es]: 143
2019.01.03 14:26:24 INFO  app[][o.s.a.AppFileSystem] Cleaning or creating temp directory /usr/local/sonarqube/sonarqube-7.5/temp
2019.01.03 14:26:24 INFO  app[][o.s.a.es.EsSettings] Elasticsearch listening on /127.0.0.1:9001
2019.01.03 14:26:24 INFO  app[][o.s.a.p.ProcessLauncherImpl] Launch process[[key='es', ipcIndex=1, logFilenamePrefix=es]] from [/usr/local/sonarqube/sonarqube-7.5/elasticsearch]: /usr/local/sonarqube/sonarqube-7.5/elasticsearch/bin/elasticsearch -Epath.conf=/usr/local/sonarqube/sonarqube-7.5/temp/conf/es
2019.01.03 14:26:24 INFO  app[][o.e.p.PluginsService] no modules loaded
2019.01.03 14:26:24 INFO  app[][o.e.p.PluginsService] loaded plugin [org.elasticsearch.transport.Netty4Plugin]
2019.01.03 14:26:34 INFO  app[][o.s.a.SchedulerImpl] Process[es] is up
2019.01.03 14:26:34 INFO  app[][o.s.a.p.ProcessLauncherImpl] Launch process[[key='web', ipcIndex=2, logFilenamePrefix=web]] from [/usr/local/sonarqube/sonarqube-7.5]: /usr/java/default/jre/bin/java -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djava.io.tmpdir=/usr/local/sonarqube/sonarqube-7.5/temp -Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError -cp ./lib/common/*:/usr/local/sonarqube/sonarqube-7.5/lib/jdbc/mysql/mysql-connector-java-5.1.46.jar org.sonar.server.app.WebServer /usr/local/sonarqube/sonarqube-7.5/temp/sq-process4648446057353788693properties
2019.01.03 14:26:49 INFO  app[][o.s.a.SchedulerImpl] Process[web] is up
2019.01.03 14:26:49 INFO  app[][o.s.a.p.ProcessLauncherImpl] Launch process[[key='ce', ipcIndex=3, logFilenamePrefix=ce]] from [/usr/local/sonarqube/sonarqube-7.5]: /usr/java/default/jre/bin/java -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djava.io.tmpdir=/usr/local/sonarqube/sonarqube-7.5/temp -Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError -cp ./lib/common/*:/usr/local/sonarqube/sonarqube-7.5/lib/jdbc/mysql/mysql-connector-java-5.1.46.jar org.sonar.ce.app.CeServer /usr/local/sonarqube/sonarqube-7.5/temp/sq-process6942946129035618556properties
2019.01.03 14:26:55 INFO  app[][o.s.a.SchedulerImpl] Process[ce] is up
2019.01.03 14:26:55 INFO  app[][o.s.a.SchedulerImpl] SonarQube is up

访问http://IP:9000/sonar 看到欢迎界面即成功。

SonarQube安装和使用_第1张图片
0.png

8.汉化SonarQube
使用admin/admin账号登录,选择Marketplace,找到Chinese Pack,点击install安装语言包,然后重启server,重新加载页面就完成汉化。

SonarQube安装和使用_第2张图片
1.png

三、Maven配置SonarQube

1.安装Maven

mkdir -p /usr/local/maven3
cd /root/software
wget http://mirrors.hust.edu.cn/apache/maven/maven-3/3.5.4/binaries/apache-maven-3.5.4-bin.tar.gz
tar zxf apache-maven-3.5.4-bin.tar.gz
mv apache-maven-3.5.4 /usr/local/maven3

2.配置环境变量。

vi /etc/profile
export M2_HOME=/usr/local/maven3/apache-maven-3.5.4
export PATH=$PATH:$JAVA_HOME/bin:$M2_HOME/bin

3.保存退出后运行下面的命令使配置生效,或者重启服务器生效。

source /etc/profile

4.验证版本

mvn -v

5.安装maven,配置maven环境变量(过程略)。
要实行对maven项目的管理,还需要配置maven的setting.xml文件。
配置如下:

    
     sonar  
       
     true  
       
        
       
   jdbc:mysql://172.17.213.205:3306/sonar  
   com.mysql.jdbc.Driver  
   sonar  
   sonar  
      
   http://172.17.213.205:9000 
   admin
   admin 
       
 

你可能感兴趣的:(SonarQube安装和使用)