一、mysql服务部署
1、添加mysql用户
useradd -s /sbin/nologin -M mysql
2、下载mysql
cd /home/tools
wget https://downloads.mysql.com/archives/get/file/mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz
3、解压mysql二进制包
cd /home/tools
tar xf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz
4、把mysql移动到 /application
mkdir -p /application
mv /home/tools/mysql-5.6.36-linux-glibc2.5-x86_64 /application/mysql-5.6.36
5、创建软连接
ln -s /application/mysql-5.6.36/ /application/mysql
6、让mysql用户管理/application/mysql目录
chown -R mysql.mysql /application/mysql
7、初始化数据库
cd /application/mysql && /application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/application/mysql/data --user=mysql
8、复制启动脚本并授权
cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
9、修改启动脚本和mysql命令中的路径
sed -i 's#/usr/local/mysql#/application/mysql#g' /application/mysql/bin/mysqld_safe /etc/init.d/mysqld
10、复制默认的配置文件
\cp -f /application/mysql/support-files/my-default.cnf /etc/my.cnf
11、启动mysql服务
/etc/init.d/mysqld start
12、将mysql命令加入系统环境变量
echo 'export PATH=/application/mysql/bin:$PATH' >>/etc/profile
source /etc/profile
13、加入开机自启动
chkconfig --add mysqld
chkconfig mysqld on
14、给mysql的root用户设置密码
mysqladmin -uroot password '要设置的密码'
15、重新登录mysql
mysql -uroot -p123456
16、创建sonar数据库
mysql> CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
mysql> GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar@pw';
mysql> GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar@pw';
mysql> FLUSH PRIVILEGES;
二、配置并启动Sonar
1、安装jdk
yum install -y java-1.8.0
2、下载sonar
cd /usr/local/src
wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-6.7.6.zip
3、解压并配置sonar
unzip sonarqube-6.7.6.zip
mv sonarqube-6.7.6 /usr/local/
ln -s /usr/local/sonarqube-6.7.6/ /usr/local/sonarqube
说明:Sonar的配置文件主要是配置相关启动参数和数据库的信息,数据库不需要初始化,Sonar启动的过程中会自动初始化。
cd /usr/local/sonarqube/conf/
ls
sonar.properties wrapper.conf
4、修改数据库配置
vim sonar.properties
sonar.jdbc.username=sonar
sonar.jdbc.password=sonar@pw
sonar.jdbc.url=jdbc:mysql://192.168.1.100:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance
说明:你可以在Sonar的配置文件来配置Sonar Web监听的IP地址和端口,默认是9000端口
vim sonar.properties
sonar.web.host=0.0.0.0
sonar.web.port=9000
5、启动Sonar
说明:必须使用普通用户启动sonar服务端/usr/local/sonarqube/bin/linux-x86-64/sonar.sh start
三、sonar客户端部署
6.1、下载sonar客户端
cd /usr/local/src
wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-3.3.0.1492-linux.zip
6.2、解压sonar客户端
unzip sonar-scanner-cli-3.3.0.1492-linux.zip
mv sonar-scanner-cli-3.3.0.1492 /usr/local
ln -s /usr/local/sonar-scanner-cli-3.3.0.1492-linux /usr/local/sonar-scanner
6.3、配置sonar客户端
cd /usr/local/sonar-scanner/conf/
cat sonar-scanner.properties
#Configure here general information about the environment, such as SonarQube server connection details for example
#No information about specific project should appear here
#----- Default SonarQube server
sonar.host.url=http://192.168.1.201:9000 #填写sonar服务端的ip和port
#----- Default source code encoding
sonar.sourceEncoding=UTF-8 #打开此行的注释即可
6.4、sonar客户端使用
6.4.1、在项目的根目录中创建配置文件:sonar -project.properties
cat sonar-project.properties
# must be unique in a given SonarQube instance
sonar.projectKey=icp-service-interface #填写sonar唯一的key,可随意填写,不能与其它项目重复
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=icp-service-interface #sonar客户端识别的用户名,可随意填写,唯一即可
sonar.projectVersion=1.0 #sonar的版本(保持不变即可)
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# This property is optional if sonar.modules is set.
sonar.sources=. #源码路径(默认即可)
sonar.java.binaries=. #java路径(默认接口)
# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8 #字符集(默认即可)
6.4.2、运行以下命令以启动分析
/usr/local/sonar-scanner/bin/sonar-scanner
6.4.3、分析完成后使用浏览器查看分析结果
浏览器输入192.168.1.201:9000