dshimizu/blog

アルファ版

Ubuntu 16.04 LTSに Redash をインストールして MySQL に接続してみる

はじめに

話題になっている Redash を使ってみたく、自サーバへインストールして WordpressMySQL へ接続して簡単なデータソースの操作を実施してみるまでをやってみました。

Redash とは

Redash は OSSのデータコラボレーション・可視化ツールです。

インストールとセットアップ

環境

OS Ubuntu 16.04 LTS(4.4.0-21-generic)
Redash バージョン 0.10.1.b1834

インストール

インストールの方法はいくつかありますが、以下にセットアップ用のスクリプトがありますので、今回はUbuntu 14.04 のものを取得して実行します。 ※Ubuntu 16.04 LTSでも実行は出来ました。

Redashをgitリポジトリからcloneします。

$ wget -P /tmp https://raw.githubusercontent.com/getredash/redash/master/setup/ubuntu/bootstrap.sh

bootstrap.sh を実行すると PostgreSQL(クエリやデータ保存)、Redis(キュー処理)、Nginxが自動でセットアップされます。

そのままだとRedisがソースビルドされたりPostgreSQLPostgreSQL公式リポジトリの9.3だったので、今回は各種Ubuntu標準リポジトリから最新取得するように、とりあえず簡単に修正します。

$ diff -u /tmp/bootstrap.sh{.org,}
--- /tmp/bootstrap.sh.org   2016-yy-dd hh:mm:ss.117005000 +0900
+++ /tmp/bootstrap.sh       2016-yy-dd hh:mm:ss.593005000 +0900
@@ -39,10 +39,8 @@
 pg_available=0
 psql --version || pg_available=$?
 if [ $pg_available -ne 0 ]; then
-    wget $FILES_BASE_URL"postgres_apt.sh" -O /tmp/postgres_apt.sh
-    bash /tmp/postgres_apt.sh
     apt-get update
-    apt-get -y install postgresql-9.3 postgresql-server-dev-9.3
+    apt-get -y install postgresql-9.5 postgresql-server-dev-9.5
 fi

 add_service() {
@@ -70,30 +68,14 @@
 redis_available=0
 redis-cli --version || redis_available=$?
 if [ $redis_available -ne 0 ]; then
-    wget http://download.redis.io/releases/redis-2.8.17.tar.gz
-    tar xzf redis-2.8.17.tar.gz
-    rm redis-2.8.17.tar.gz
-    (cd redis-2.8.17
-    make
-    make install
-
+    apt-get install -y redis-server redis-tools
+    (
     # Setup process init & configuration
-
     REDIS_PORT=6379
     REDIS_CONFIG_FILE="/etc/redis/$REDIS_PORT.conf"
     REDIS_LOG_FILE="/var/log/redis_$REDIS_PORT.log"
     REDIS_DATA_DIR="/var/lib/redis/$REDIS_PORT"
-
-    mkdir -p "$(dirname "$REDIS_CONFIG_FILE")" || die "Could not create redis config directory"
-    mkdir -p "$(dirname "$REDIS_LOG_FILE")" || die "Could not create redis log dir"
-    mkdir -p "$REDIS_DATA_DIR" || die "Could not create redis data directory"
-
-    wget -O /etc/init.d/redis_6379 $FILES_BASE_URL"redis_init"
-    wget -O $REDIS_CONFIG_FILE $FILES_BASE_URL"redis.conf"
-
-    add_service "redis_$REDIS_PORT"
     )
-    rm -rf redis-2.8.17
 fi

 # Directories

スクリプトを実行します。

$ sudo sh bootstrap.sh

以下のようなエラーが出たら、 pip を更新します。

 :
AttributeError: 'Requirement' object has no attribute 'project_name'
You are using pip version 8.1.1, however version 8.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

pip をバージョンアップします。

$ sudo pip install --upgrade pip

バージョンを確認します。

$ pip -V
pip 8.1.2 from /home/${USERNAME}/.local/lib/python2.7/site-packages (python 2.7)

再度インストールスクリプトを実行します。

$ sudo sh bootstrap.sh

スクリプトが正常終了すればインストールは完了です。

管理画面へのログイン

上記スクリプトでインストール後は redash サービスや PotgreSQL, Redis Nginxが起動しており、http(80番ポート) で管理画面へアクセスできるようになっています。

なお、 redash サービスは以下で起動/停止できます。

# systemctl start redash_supervisord.service
# systemctl stop redash_supervisord.service

初期id/passはadmin/adminです。 デフォルトのadminユーザでログイン後、パスワードを変更します。

WordPressMySQLデータベースへ接続

とりあえずWordPressMySQLデータベースに接続してみます。

管理画面から追加しても良いですがコマンドで対応してみます。

$ cd /opt/redash/current
$ sudo -u redash ./bin/run ./manage.py ds new

対話的に以下のように設定します。

Name: WordPress []
Select type:
1. bigquery_gce
2. google_spreadsheets
3. redshift
4. url
5. mongodb
6. presto
7. hive
8. bigquery
9. mssql
10. influxdb
11. vertica
12. kibana
13. graphite
14. pg
15. mysql
16. elasticsearch
17. dynamodb_sql
18. impala
19. sqlite
20. treasuredata
[1-20]: [15] (#<- MySQL)
Path to CA certificate file to verify peer against (SSL) (optional):
Host (optional): [MySQLのホスト名]
Path to client certificate file (SSL) (optional):
User (optional): [MySQLのDBユーザ名]
Path to private key file (SSL) (optional):
Password (optional): [MySQLのDBパスワード]
Use SSL (optional):
Database name (required): [MySQLのDB名]
Port (optional): 
Creating mysql data source (データソース名) with options:
{"passwd": "[MySQLのDBパスワード]", "host": "[MySQLのホスト名]", "db": "[MySQLのDB名]", "user": "[MySQLのユーザ名]", "port": 3306}
Id: 2

追加されたデータソースを確認します。

$ sudo -u redash ./bin/run ./manage.py ds list
[2016-08-22 00:31:30,982][PID:5422][INFO][root] Latest version: 0.11.1 (newer: True)
Id: 1
Name: re:dash metadata
Type: pg
Options: 
--------------------
Id: 2 
Name: WordPress
Type: mysql
Options: 

画面上のメニューの Queries から New Query を選択します。

画面中央の Data Source から先ほど作成した データソース WordPress を選択します。

タグごとの記事数を取得するSQLを記述します。

+ NEW VISUALIZATION をクリックして各種設定を行うと以下のようなグラフも表示できます。

おわりに

以上、 Redash のセットアップと簡単な操作でした。 UIはまだわかりにくいところがありますが、SQLを保存できたり結果をグラフ化できたりというのは便利そうです。