LoginSignup
5
4

More than 5 years have passed since last update.

GitBucketで利用しているh2 databaseにターミナルから接続する

Posted at

プロジェクトで利用しているGitBucketが3.8なので、h2は1.4系を利用。(GitBucket2.2で1.4系になった模様)

1.3と1.4ではファイルDBの名前や、jdbcのurlの指定方法などが若干異なっているので注意が必要。

また、異なるバージョンのドライバを利用すると、DBファイル名が合わずDBが参照できない。

GitBucketのissue を参考にして、思考停止でh2をダウンロードしてきたらハマったので、注意(完全に自分の不注意)。

1.4系は以下から取得する。

Linuxで利用したいので、Platform-Independent Zipを取得。

$ unzip h2-2016-01-21.zip

解凍したh2/bin/h2.shを実行するとh2コンソールがブラウザで立ち上がるので、

h2/bin/h2.sh
#!/bin/sh
dir=$(dirname "$0")
java -cp "$dir/h2-1.4.191.jar:$H2DRIVERS:$CLASSPATH" org.h2.tools.Console "$@"

thumb_H2コンソール_1024.jpg

このファイルをコピーしてターミナルで立ち上がるように編集する。
org.h2.tools.Consoleorg.h2.tools.Shellに変更するだけ。

h2/bin/h2_terminal.sh
!/bin/sh
dir=$(dirname "$0")
java -cp "$dir/h2-1.4.191.jar:$H2DRIVERS:$CLASSPATH" org.h2.tools.Shell "$@"

こんな感じで繋がる。

$ sudo sh h2/bin/h2_terminal.sh                                                                                                     

Welcome to H2 Shell 1.4.191 (2016-01-21)
Exit with Ctrl+C
[Enter]   jdbc:h2:~/.gitbucket/data
URL
[Enter]   org.h2.Driver
Driver
[Enter]   sa
User
[Enter]   Hide
Password  sa
Connected
Commands are case insensitive; SQL statements end with ';'
help or ?      Display this help
list           Toggle result list / stack trace mode
maxwidth       Set maximum column width (default is 100)
autocommit     Enable or disable autocommit
history        Show the last 20 statements
quit or exit   Close the connection and exit

sql> select * from account;
USER_NAME | MAIL_ADDRESS   | PASSWORD                                 | ADMINISTRATOR | URL                                    | REGISTERED_DATE         | UPDATED_DATE            | LAST_LOGIN_DATE         | IMAGE | GROUP_ACCOUNT | FULL_NAME | REMOVED
root      | root@localhost | dc76e9f0c0006e8f919e0c515c66dbba3982f785 | TRUE          | https://github.com/gitbucket/gitbucket | 2016-04-13 01:45:44.093 | 2016-04-13 01:45:44.093 | 2016-04-13 01:46:05.268 | null  | FALSE         | root      | FALSE
test      | test@devnull   |                                          | FALSE         | null                                   | 2016-04-13 01:46:27.259 | 2016-04-13 01:46:27.259 | null                    | null  | TRUE          | test      | FALSE
(2 rows, 8 ms)
sql>

なお、h2がserver modeで起動していなくてGitBucketが立ち上がっていると以下のようなエラーが出る。とりあえずGitBucketを止めれば繋がるはず。

Password  sa
SQL Exception: Database may be already in use: "/private/var/root/.gitbucket/data.h2.db". Possible solutions: close all other connection(s); use the server mode [90020-191]
5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4