先日、コマンドラインツールで IBM Cloud 上の Db2 サービスインスタンスに接続する、というエントリを書きました:
Db2 コマンドラインインターフェースから IBM Cloud 上のリモート Db2 インスタンスに接続する
接続するまでの設定項目は上記エントリに書いてあるのですが、実際に接続したあとに SQL を実行する上でコマンドを実行する際のチートシート的なまとめを書いておきます。
まずは前回も書いた DB2 のスタート。db2inst1 ユーザーになってから db2start コマンドを実行します(青字は実行結果です):
データベースの一覧を確認する場合は list db directory を実行します:
上記コマンドでデータベースの一覧を確認した上でどこかのデータベース(下の例では mydb)に接続します:
データベースに接続した上で、テーブルの一覧を照会する場合は list tables を実行します:
DB2 コマンドをそのまま実行するとインタラクティブモードになります:
インタラクティブモードでは接続中のデータベースに対して、SQL を直接実行することができます:
インタラクティブモードから抜けるには quit を実行します:
最後に DB2 を停止します:
管理系コマンドについてはノータッチでしたが、とりあえずデベロッパーである自分が最低限使いそうなコマンドはこんなもんでしょうかね。。
Db2 コマンドラインインターフェースから IBM Cloud 上のリモート Db2 インスタンスに接続する
接続するまでの設定項目は上記エントリに書いてあるのですが、実際に接続したあとに SQL を実行する上でコマンドを実行する際のチートシート的なまとめを書いておきます。
まずは前回も書いた DB2 のスタート。db2inst1 ユーザーになってから db2start コマンドを実行します(青字は実行結果です):
$ su - db2inst1
$ db2start
05/27/2018 20:13:33 0 0 SQL1063N DB2START processing was successful.
SQL1063N DB2START processing was successful.
データベースの一覧を確認する場合は list db directory を実行します:
$ db2 list db directory
System Database Directory
Number of entries in the directory = 3
Database 1 entry:
Database alias = REMOTEDB
Database name = BLUDB
Node name = DASHDB
Database release level = 14.00
Comment =
Directory entry type = Remote
Catalog database partition number = -1
Alternate server hostname =
Alternate server port number =
Database 2 entry:
Database alias = JSONDB
Database name = JSONDB
Local database directory = /home/db2inst1
Database release level = 14.00
Comment =
Directory entry type = Indirect
Catalog database partition number = 0
Alternate server hostname =
Alternate server port number =
Database 3 entry:
Database alias = MYDB
Database name = MYDB
Local database directory = /home/db2inst1
Database release level = 14.00
Comment =
Directory entry type = Indirect
Catalog database partition number = 0
Alternate server hostname =
Alternate server port number =
上記コマンドでデータベースの一覧を確認した上でどこかのデータベース(下の例では mydb)に接続します:
$ db2 connect to mydb
Database Connection Information
Database server = DB2/LINUXX8664 11.1.2.2
SQL authorization ID = DB2INST1
Local database alias = MYDB
データベースに接続した上で、テーブルの一覧を照会する場合は list tables を実行します:
$ db2 list tables
Table/View Schema Type Creation time
------------------------------- --------------- ----- --------------------------
GOKON DB2INST1 T 2018-05-23-19.09.07.748477
1 record(s) selected.
DB2 コマンドをそのまま実行するとインタラクティブモードになります:
$ db2
(c) Copyright IBM Corporation 1993,2007
Command Line Processor for DB2 Client 11.1.2.2
You can issue database manager commands and SQL statements from the command
prompt. For example:
db2 => connect to sample
db2 => bind sample.bnd
For general help, type: ?.
For command help, type: ? command, where command can be
the first few keywords of a database manager command. For example:
? CATALOG DATABASE for help on the CATALOG DATABASE command
? CATALOG for help on all of the CATALOG commands.
To exit db2 interactive mode, type QUIT at the command prompt. Outside
interactive mode, all commands must be prefixed with 'db2'.
To list the current command option settings, type LIST COMMAND OPTIONS.
For more detailed help, refer to the Online Reference Manual.
db2 =>
インタラクティブモードでは接続中のデータベースに対して、SQL を直接実行することができます:
db2 => select * from gokon
GIRL_ID GNAME AGE GOKON_DATE
----------- ---------- ----------- ----------
2 Beth 19 2016-01-01
3 Karmen 34 1990-10-01
4 Hanako 69 2009-05-01
3 record(s) selected.
インタラクティブモードから抜けるには quit を実行します:
db2 => quit
DB20000I The QUIT command completed successfully.
最後に DB2 を停止します:
$ db2stop
2018-05-27 20:10:25 0 0 SQL1064N DB2STOP processing was successful.
SQL1064N DB2STOP processing was successful.
管理系コマンドについてはノータッチでしたが、とりあえずデベロッパーである自分が最低限使いそうなコマンドはこんなもんでしょうかね。。

コメント