BlogもどきのWeblog

備忘録と日々の呟きとメモとCGIの実験場とされる何か。
プロフィール

GLANSHE
絵描き担当らしい

Total: 15668
Today: 3
Yesterday: 225
最新の記事

コンテンツ

最新のコメント

最新のトラックバック

カテゴリー

リンク

ニコ動MyListLink

DBI.pmのconnectの使い方(DBD::Oracle)
引数はこう。

$dbh = DBI->connect($dsn, $user, $pass, $attr, $old_driver);

■説明
・$dsn

$dsn ||= $ENV{DBI_DSN} || $ENV{DBI_DBNAME} || '' unless $old_driver;

$old_driverが引数として渡されていない場合、
$dsn、$ENV{DBI_DNS}、$ENV{DBI_DBNAME} の順に評価して、全部偽の場合は空文字が$dsnに入る。

$dsnの書式

dbi:ドライバ名(.*?):その他

最初のdbiは大文字小文字関係なし

ドライバ名は、DBD::mysqlとか、DBD::Oracleを使う場合、
mysql、Oracleというように、モジュール名を大文字小文字をきちんと合わせて指定する。

・$user
DBのユーザ名

・$pass
DBのパスワード

・$attr
ハッシュで指定する。

%attributes = (
  PrintError => 1,
  AutoCommit => 1,
  ref $attr ? %$attr : (),
  # attributes in DSN take precedence over \%attr connect parameter
  $driver_attrib_spec ? (split /\s*=>?\s*|\s*,\s*/, $driver_attrib_spec, -1) : (),
);

$driver_attrib_specにはドライバ名(.*?)の、「.*?」の部分が入っている。
何に使っているのか不明。

こんな風に設定されて、ドライバの接続メソッドに引数として渡される。

$attr = {AutoCommit => 0};
とか書いておくと最初からオートコミットがオフになる。

・$old_driver
多分内部呼び出し用。
外部から呼び出す時は基本的に使ってはいけない…と思う。

ドライバに対しての接続要求が通ると、その戻り値の$dbhが返る。
その際の引数は、($dsn, $user, $pass, $attr)
このとき、$dsnは "dbi:ドライバ名(.*):"より後の文字列。つまり、「その他」

DBD::Oracleの場合
「その他」の書式
$dbname = 「その他」;

$dbnameを何も記述しない場合
接続先はlocalhostになる?
アクセスするDB名をユーザ名の後ろに@を付けて書く必要がある。

$user = "dbuser@SID";

$dbname = $1 if !$dbname && $user && $user =~ s/\@(.*)//s;
こうなってるので後ろじゃなくてもいいのかも…


$dbnameにアクセスするDB名を書く場合
特に何も考えなくていい。接続先はlocalhostとなる?メソッドの記述箇所がわかんえ…
$dsn = "dbi:Oracle:SID";


$dbnameに接続先情報を色々書く場合
「;」をセパレータとして記述する。こんな感じ?

$dsn = "dbi:Oracle:HOST=example.co.jp;SID=SID;PORT=1521;INSTANCE_NAME=INSTANCE"

SERVER、SERVICE_NAMEとかも指定オプションとしてあるけどこれどう使うんだっけ…

外部に接続する場合はこの記述方法でないとだめ。
その際にHOSTは必須となる。
と言うか、「その他」の情報に「;」を書いた時点でHOSTが必須になる…。

こんなところかな。

■警告
ただし未検証。(苦笑)

参考
DBD::Oracle - DBIモジュールのためのOracleデータベース・ドライバ
DBI - Perl用データベース独立インターフェース


time stamp:2010/03/29 00:48:49
トラックバック(0)|コメント(0)
コメントを書く
この記事のトラックバックURL:

DBD::Oracle
PerlからOracleに接続するためのモジュールDBD::Oracleの話。

PerlからDBに接続するためには、基本的にDBIモジュールを使います。
接続するDBによって、 DBD::mysql とか、 DBD::pg とか DBD::DB2 とかがありますが、当然 DBD::Oracle なんてのもあるわけです。

今回、フリーの Oracle 10g Express Edition をインストールしてPerlから接続しようと、DBD::Oracleモジュールをインストールしようとしてまたハマりました。


とりあえずやったこと
# cpan

cpan[1]> install DBI

問題なく終了

cpan[2]> install DBD::Oracle

Trying to find an ORACLE_HOME
Your LD_LIBRARY_PATH env var is set to ''

ORACLE_HOMEが設定されていないと怒られる。
oracle関連の環境変数が設定されてないと駄目らしい。

仕方がないので、環境変数を設定する。

# export ORACLE_HOME=/usr/lib/oracle/xe/app/oracle/product/10.2.0/server
# export ORACLE_SID=XE
# export NLS_LANG=JAPANESE_JAPAN.AL32UTF8

# env
NLS_LANG=JAPANESE_JAPAN.AL32UTF8
ORACLE_SID=XE
ORACLE_HOME=/usr/lib/oracle/xe/app/oracle/product/10.2.0/server
他省略

再度インストール
cpan[1]> install DBD::Oracle

Using Oracle in /usr/lib/oracle/xe/app/oracle/product/10.2.0/server
DEFINE _SQLPLUS_RELEASE = "1002000100" (CHAR)
Oracle version 10.2.0.1 (10.2)

Unable to locate an oracle.mk, proc.mk or other suitable *.mk
file in your Oracle installation. (I looked in
/usr/lib/oracle/xe/app/oracle/product/10.2.0/server/rdbms/demo/demo_rdbms32.mk /usr/share/oracle/xe/app/oracle/product/10.2.0/server/demo.mk /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/precomp/demo/proc/proc.mk /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/precomp/demo/proc/demo_proc.mk /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/proc/lib/proc.mk /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/proc16/lib/proc16.mk /usr/share/oracle/10.2/client/demo.mk /usr/share/oracle/10.2/client64/demo.mk under /usr/lib/oracle/xe/app/oracle/product/10.2.0/server)

The oracle.mk (or demo_rdbms.mk) file is part of the Oracle
RDBMS product. The proc.mk (or demo_proc.mk) file is part of
the Oracle Pro*C product. You need to build DBD::Oracle on a
system which has one of these Oracle components installed.
(Other *.mk files such as the env_*.mk files will not work.)
Alternatively you can use Oracle Instant Client.

こんな感じでまた怒られた。
どうやら、demo*.mkとかいうファイルを探しに行って見つからないらしい。

よくわからないけど、最初に探しに行ってる場所の

/usr/lib/oracle/xe/app/oracle/product/10.2.0/server/rdbms/demo/demo_rdbms32.mk

このファイルと同じディレクトリに、

demo_xe.mk

ってファイルがあったので、試しにdemo_rdbms32.mkと言う名前でコピーしてみる。

# cd /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/rdbms/demo/
# cp demo_xe.mk demo_rdbms32.mk

再度インストール

cpan[1]> install DBD::Oracle

make -- OK

Warningがいっぱい出たけどmakeが通ったっぽい

Warning (usually harmless): 'YAML' not installed, will not store persistent state
Running make test
PERL_DL_NONLAZY=1 /usr/bin/perl "-MExtUtils::Command::MM" "-e" "test_harness(0, 'blib/lib', 'blib/arch')" t/*.t
t/01base................# Test loading DBI, DBD::Oracle and version
t/01base................ok
t/10general.............DBI connect('','scott/tiger',...) failed: ORA-01034: ORACLE not available
ORA-27121: unable to determine size of shared memory segment
Linux Error: 13: Permission denied (DBD ERROR: OCISessionBegin) at t/10general.t line 22
FAILED--Further testing stopped: Unable to connect to Oracle (ORA-01034: ORACLE not available
make: *** [test_dynamic] Error 9
PYTHIAN/DBD-Oracle-1.24a.tar.gz
make test -- NOT OK
//hint// to see the cpan-testers results for installing this module, try:
reports PYTHIAN/DBD-Oracle-1.24a.tar.gz
Warning (usually harmless): 'YAML' not installed, will not store persistent state
Running make install
make test had returned bad status, won't install without force
Failed during this command:
PYTHIAN/DBD-Oracle-1.24a.tar.gz : make_test NO

でもmake_testが失敗。

notest install DBD::Oracle でも問題ないみたいな事をどこかで見たのでそうやってみる

cpan[2]> notest install DBD::Oracle
Running install for module 'DBD::Oracle'
Running make for P/PY/PYTHIAN/DBD-Oracle-1.24a.tar.gz
Has already been unwrapped into directory /root/.cpan/build/DBD-Oracle-1.24-SI0Gyg
Has already been made
Skipping test because of notest pragma
Running make install
make test had returned bad status, won't install without force

まだだめっぽい。
よくわからないけど force でやればいいの?
もうだんだんどうでもよくなってきたのでforceでインストールしてみる

cpan[3]> force install DBD::Oracle

Writing /usr/local/lib/perl/5.10.0/auto/DBD/Oracle/.packlist
Appending installation info to /usr/local/lib/perl/5.10.0/perllocal.pod
PYTHIAN/DBD-Oracle-1.24a.tar.gz
make install -- OK
Warning (usually harmless): 'YAML' not installed, will not store persistent state
Failed during this command:
PYTHIAN/DBD-Oracle-1.24a.tar.gz : make_test FAILED but failure ignored because 'force' in effect

通った…のか?

テストしてみる

[test.pl]
#!/usr/bin/perl

use DBI;

print "Content-type: text/html\n\n";

$ENV{'ORACLE_HOME'} = "/usr/lib/oracle/xe/app/oracle/product/10.2.0/server";
$ENV{'ORACLE_SID'} = "XE";
$ENV{'NLS_LANG'} = "JAPANESE_JAPAN.JA16EUC";

my $dbh = DBI->connect("dbi:Oracle:XE", "user", "pass") or die $@;

$sth = $dbh->prepare("select * from dual");
$nRes = $sth->execute;

while($raRes = $sth->fetchrow_arrayref) {
  print join(", ", @$raRes), "\n";
}

$sth->finish;
$dbh->disconnect;

exit;

出力結果
X

出たー。


■まとめ

DBD::Oracleをインストールする時は、oracle関連の環境変数を設定してからインストールする必要がある。

demo_rdbms32.mk が必要らしい。
あれば問題ないが、ない場合はエラーとなる。
Oracle 10g Express Edition を使用している場合は、

/usr/lib/oracle/xe/app/oracle/product/10.2.0/server/rdbms/demo/demo_xe.mk

で代用できる(っぽい)ので、名前を demo_rdbms32.mk としてコピーする。

まだテストでエラーが起こるので、 force でインストールする。

■警告
Warning読んでないので、何かおかしな事が起こっているかもしれません。

インストールのログを見たい人はこちら


time stamp:2010/03/28 22:02:48
トラックバック(0)|コメント(0)
コメントを書く
この記事のトラックバックURL:

Oracle Database 10g Express Edition
フリーで使えるoracleがあると言うので、その使い方とか制限について調べたメモ

■制限

1. 記憶域
ユーザーデータを格納する記憶域が4GBまでに制限されている。
4G以上データを使うようであれば、シェアウェアの方を使う必要がある。
個人で使うようなシステムで、テキストしか格納しないとかいうなら4Gバイトで十分だと思う。

2. メモリー
利用できるメモリーは最大で1GBに制限されている。
SGAとPGAのサイズを指定することは可能だが、SGAの最大サイズとPGAの合計が、1GBを超えることはできない。

以下引用
SGAはオラクルインスタンスに含まれ、
データベースバッファキャッシュや、 REDOログバッファ、共有プールなどから構成されているメモリ領域のこと。
すなわち、オラクル(インスタンス)を起動した際に確保される領域。
それに対して、PGAは、
ユーザプロセスがDBに接続した際、各サーバプロセス毎に割り当てられるクライアント/サーバ間の処理のためのメモリ領域となる。
すなわち、各ユーザプロセスの接続により確保される領域

・引用元
ORACLE(DB)関連/SGAとPGA

3. CPU
マルチCPUのサーバーでもデータベース・プロセスは1CPUしか使えない。
コアをたくさん乗せてパフォーマンスを上げているマシンでは使いにくい。
しかし、そもそもシングルCPUで動いているマシンならば制限はあってないようなもの。
でも、最近はデュアルコアとかクアッドコアとかが主流だから、影響を受けるのかな。

4. インスタンス
データベース・インスタンスは1つのシステム上で1つのみ。
多数起動してインスタンスをいくつも作ることはできない。
データベースの識別で使われるSIDが「XE」に固定されている。

5. プラットフォーム
Windows、Linuxのみ。ともに32ビット。64ビットは非対応。
…と思ったら出来るらしい?
64ビットでインストール?

6. サポート
当然だが無償なのでサポートは受けられない。

■使い方
Ubuntuにインストールする場合の話

・インストール
/etc/apt/sources.list に、オラクル系のリポジトリを追加

# vi /etc/apt/sources.list

末尾辺りに以下を追加

deb http://oss.oracle.com/debian unstable main non-free

# aptitude update
何か証明書がうんたらとか言われて怒られた。

# aptitude install oracle-xe-universal
途中で信用するかどうかの選択が出たので、Yesと入力した。

とりあえずクライアントも入れてみることに
# aptitude install oracle-xe-client


・設定
# /etc/init.d/oracle-xe configure

Specify the HTTP port that will be used for Oracle Application Express [8080]:
データベース管理を行うアプリケーションのHTTPポート番号
Specify a port that will be used for the database listener [1521]:
データベースリスナーのポート番号

Specify a password to be used for database accounts. Note that the same
password will be used for SYS and SYSTEM. Oracle recommends the use of
different passwords for each database account. This can be done after
initial configuration:
システムが使うパスワードの設定

Do you want Oracle Database 10g Express Edition to be started on boot (y/n) [y]:
oracleを自動起動するか否か


・接続
# su - oracle

$ . /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/bin/oracle_env.sh
$ sqlplus system/(パスワード)@XE

SQL> select * from dual;

とかとか。SQL文の最後に「;」を付けないと、

SQL> select * from dual
  2

とか表示されて意味不明なことになるので注意。


・Web管理画面を外から見えるように

SQL> EXEC DBMS_XDB.SETLISTENERLOCALACCESS(FALSE);

を実行する。

後で接続先のポート番号を変更する場合
Oracle Application Express (APEX) - ポート番号の変更


・インストール時のログ
root@horn:/# aptitude install oracle-xe-universal
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています
状態情報を読み取っています... 完了
Reading extended state information
Initializing package states... 完了
The following NEW packages will be installed:
bc{a} oracle-xe-universal
0 packages upgraded, 2 newly installed, 0 to remove and 7 not upgraded.
Need to get 263MB of archives. After unpacking 451MB will be used.
Do you want to continue? [Y/n/?]
WARNING: untrusted versions of the following packages will be installed!

Untrusted packages could compromise your system's security.
You should only proceed with the installation if you are certain that
this is what you want to do.

oracle-xe-universal

Do you want to ignore this warning and proceed anyway?
To continue, enter "Yes"; to abort, enter "No":
Unrecognized input. Enter either "Yes" or "No".
Do you want to ignore this warning and proceed anyway?
To continue, enter "Yes"; to abort, enter "No": Yes
Writing extended state information... 完了
Get:1 http://jp.archive.ubuntu.com intrepid/main bc 1.06.94-3ubuntu1 [73.1kB]
Get:2 http://oss.oracle.com unstable/non-free oracle-xe-universal 10.2.0.1-1.1 [262MB]
Fetched 263MB in 33s (7937kB/s)
未選択パッケージ bc を選択しています。
(データベースを読み込んでいます ... 現在 27097 個のファイルとディレクトリがインストールされています。)
(.../bc_1.06.94-3ubuntu1_i386.deb から) bc を展開しています...
未選択パッケージ oracle-xe-universal を選択しています。
(.../oracle-xe-universal_10.2.0.1-1.1_i386.deb から) oracle-xe-universal を展開しています...
Processing triggers for man-db ...
bc (1.06.94-3ubuntu1) を設定しています ...

oracle-xe-universal (10.2.0.1-1.1) を設定しています ...
update-rc.d: warning: /etc/init.d/oracle-xe missing LSB style header
Executing Post-install steps...

-e You must run '/etc/init.d/oracle-xe configure' as the root user to configure the database.


パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています
状態情報を読み取っています... 完了
Reading extended state information
Initializing package states... 完了
Writing extended state information... 完了

root@horn:/# aptitude install oracle-xe-client
パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています
状態情報を読み取っています... 完了
Reading extended state information
Initializing package states... 完了
The following NEW packages will be installed:
oracle-xe-client
0 packages upgraded, 1 newly installed, 0 to remove and 7 not upgraded.
Need to get 25.9MB of archives. After unpacking 70.8MB will be used.
WARNING: untrusted versions of the following packages will be installed!

Untrusted packages could compromise your system's security.
You should only proceed with the installation if you are certain that
this is what you want to do.

oracle-xe-client

Do you want to ignore this warning and proceed anyway?
To continue, enter "Yes"; to abort, enter "No": Yes
Writing extended state information... 完了
Get:1 http://oss.oracle.com unstable/non-free oracle-xe-client 10.2.0.1-1.2 [25.9MB]
Fetched 25.9MB in 11s (2339kB/s)
未選択パッケージ oracle-xe-client を選択しています。
(データベースを読み込んでいます ... 現在 29710 個のファイルとディレクトリがインストールされています。)
(.../oracle-xe-client_10.2.0.1-1.2_i386.deb から) oracle-xe-client を展開しています...
Processing triggers for man-db ...
oracle-xe-client (10.2.0.1-1.2) を設定しています ...
Executing Post-install steps..........


パッケージリストを読み込んでいます... 完了
依存関係ツリーを作成しています
状態情報を読み取っています... 完了
Reading extended state information
Initializing package states... 完了
Writing extended state information... 完了
root@horn:/# /etc/init.d/oracle-xe configure

Oracle Database 10g Express Edition Configuration
-------------------------------------------------
This will configure on-boot properties of Oracle Database 10g Express
Edition. The following questions will determine whether the database should
be starting upon system boot, the ports it will use, and the passwords that
will be used for database accounts. Press to accept the defaults.
Ctrl-C will abort.

Specify the HTTP port that will be used for Oracle Application Express [8080]:8080

Specify a port that will be used for the database listener [1521]:1521

Specify a password to be used for database accounts. Note that the same
password will be used for SYS and SYSTEM. Oracle recommends the use of
different passwords for each database account. This can be done after
initial configuration:
Confirm the password:

Do you want Oracle Database 10g Express Edition to be started on boot (y/n) [y]:

Starting Oracle Net Listener...Done
Configuring Database...Done
Starting Oracle Database 10g Express Edition Instance...Done
Installation Completed Successfully.
To access the Database Home Page go to "http://127.0.0.1:8080/apex"


time stamp:2010/03/28 17:18:48
トラックバック(0)|コメント(0)
コメントを書く
この記事のトラックバックURL:
何かしら