概要
コード解析ツールのSonarQubeをCentOSに構築する為の備忘録です
SonarQubeはデフォルトで組み込みDBで構築するので、今回はMySQLでやってみようかと思います。
MariaDBはサポートしてないのが残念・・・・
環境
- CentOS 7.4
- Oracle Java 1.8.0_144
- SonarQube 6.5
- MySQL 5.7.19
事前準備
Oracle Javaのダウンロードとインストール
Oracle Javaのダウンロード
$wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u144-b01/090f390dda5b47b9b721c7dfaa008135/jre-8u144-linux-x64.rpm"
Oracle Javaのインストール
今回はyumでインストールします
$sudo yum localinstall jre-8u144-linux-x64.rpm
Oracle Javaの確認
$ java -version
java version "1.8.0_144"
Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)
MySQL のインストールと起動
MySQLのyumでインストールする為に、レポジトリにMySQLを追加します
$ sudo yum install http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
MySQLをインストール
$ sudo yum install mysql-community-server
MySQLのバージョン確認
$ mysql --version
mysql Ver 14.14 Distrib 5.7.19, for Linux (x86_64) using EditLine wrapper
MySQLのサービスを起動
$ sudo systemctl start mysqld
初期のrootパスワードは、
/var/log/mysqld.log
に出力されるので、パスワード変更するときに、忘れないようにしましょう
MySQLの初期設定
最低限のセキュリティ設定をしてくれるmysql_secure_installationを実行
$ mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root: 【初期パスワードを入力】
The existing password for the user account root has expired. Please set a new password.
New password: 【新しいパスワードを入力】
Re-enter new password: 【再度新しいパスワードを入力】
Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y
Success.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y
Success.
All done!
/etc/my.cnf の設定を追加
# 追加
default-storage-engine=InnoDB
character-set-server = utf8
# ユーザのパスワードはデフォルトで360日なので、無期限に設定
default_password_lifetime = 0
[mysql]
default-character-set=utf8
MySQLサービスを再起動
$ sudo systemctl restart mysqld
SonarQube用のDBとユーザを作成
$ mysql -u root -p
mysql> create database sonar;
mysql> create user 'sonar'@'localhost' identified by '【パスワード】';
mysql> grant all privileges on sonar.* to 'sonar'@'localhost';
mysql> flush privileges;
SonarQubeのダウンロードと配置
SonarQubeをダウンロードし、/usr/local/sonarqubeに配置する
$ wget https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-6.5.zip
$ unzip sonarqube-6.5.zip
$ sudo mkdir -p /usr/local/sonarqube
$ sudo mv sonarqube-6.5 /usr/local/sonarqube/
$ sudo ln -s /usr/local/sonarqube/sonarqube-6.5 /usr/local/sonarqube/sonar
SonarQubeの設定ファイルにDB接続情報を記述する
$ sudo vi /usr/local/sonarqube/sonar/conf/
# User credentials.
# Permissions to create tables, indices and triggers must be granted to JDBC user.
# The schema must be created first.
sonar.jdbc.username=【SonarQube用のユーザ名】
sonar.jdbc.password=【SonarQube用のパスワード】
#----- Embedded Database (default)
# H2 embedded database server listening port, defaults to 9092
#sonar.embeddedDatabase.port=9092
#----- MySQL 5.6 or greater
# Only InnoDB storage engine is supported (not myISAM).
# Only the bundled driver is supported. It can not be changed.
# ↓コメントを外す
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
SonarQubeを起動
$ sudo /usr/local/sonarqube/sonar/bin/linux-x86-64/sonar.sh start
$ sudo /usr/local/sonarqube/sonar/bin/linux-x86-64/sonar.sh status
SonarQube is running (29898).
デフォルトだと9000ポートでブラウザ上にwebコンソールが表示されます
右上のLoginのリンクを押すと、ログイン画面が表示されます。
admin/admin でログインすることができます。
自動起動の設定
sonarユーザで自動起動するように設定します
$ sudo useradd sonar
$ sudo chown -R sonar. /usr/local/sonarqube/
$ sudo vi /etc/systemd/system/sonar.service
[Unit]
Description=SonarQube
After=network.target network-online.target
Wants=network-online.target
[Service]
ExecStart=/usr/local/sonarqube/sonar/bin/linux-x86-64/sonar.sh start
ExecStop=/usr/local/sonarqube/sonar/bin/linux-x86-64/sonar.sh stop
ExecReload=/usr/local/sonarqube/sonar/bin/linux-x86-64/sonar.sh restart
Type=forking
User=sonar
[Install]
WantedBy=multi-user.target
$ sudo systemctl daemon-reload
$ sudo systemctl enable sonar
$ sudo systemctl start sonar
以上