PerlでMongoDBに接続
体調管理に気をつける
風邪を休日にかかるのも大変だが、平日に引きずると通勤地獄に拍車がかかる( TДT)
ご参考
http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/http://docs.mongodb.org/ecosystem/drivers/perl/http://www.kenjiskywalker.org/wp/perl/perlmeetsmongodb.html
UbutuにMongoDBをインストール
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10 echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list sudo apt-get update sudo apt-get install mongodb-10gen
なんかログが意外とでかい
インストール内容
m-shige1979@mshige1979-virtual-machine:~$ ls /etc/mongodb.conf /etc/mongodb.conf m-shige1979@mshige1979-virtual-machine:~$ ls /var/lib/mongodb _tmp local.0 local.ns mongod.lock m-shige1979@mshige1979-virtual-machine:~$ ls /var/log/mongodb mongodb.log m-shige1979@mshige1979-virtual-machine:~$ ls /etc/init.d/mongodb /etc/init.d/mongodb m-shige1979@mshige1979-virtual-machine:~$
サービス起動
m-shige1979@mshige1979-virtual-machine:~$ sudo service mongodb start start: Job is already running: mongodb m-shige1979@mshige1979-virtual-machine:~$ sudo service mongodb stop mongodb stop/waiting m-shige1979@mshige1979-virtual-machine:~$ sudo service mongodb restart stop: Unknown instance: mongodb start/running, process 3440 m-shige1979@mshige1979-virtual-machine:~$
データ投入
m-shige1979@mshige1979-virtual-machine:~$ mongo MongoDB shell version: 2.4.6 connecting to: test Server has startup warnings: Sat Sep 28 11:37:45.166 [initandlisten] Sat Sep 28 11:37:45.166 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary. Sat Sep 28 11:37:45.166 [initandlisten] ** 32 bit builds are limited to less than 2GB of data (or less with --journal). Sat Sep 28 11:37:45.166 [initandlisten] ** Note that journaling defaults to off for 32 bit and is currently off. Sat Sep 28 11:37:45.166 [initandlisten] ** See http://dochub.mongodb.org/core/32bit Sat Sep 28 11:37:45.166 [initandlisten] > > > show dbs; local 0.03125GB > > use sample1; switched to db sample1 > > db.test1.insert({aaa: 111}); > > db.test1.insert({aaa: 222}); > > db.test1.find(); { "_id" : ObjectId("52464158915776a95b134a06"), "aaa" : 111 } { "_id" : ObjectId("5246415c915776a95b134a07"), "aaa" : 222 } > > exit bye m-shige1979@mshige1979-virtual-machine:~$
cpanでクライアントモジュールをインストール
m-shige1979@mshige1979-virtual-machine:~$ cpanm MongoDB --force --> Working on MongoDB Fetching http://www.cpan.org/authors/id/F/FR/FRIEDO/MongoDB-0.702.2.tar.gz ... OK Configuring MongoDB-0.702.2 ... OK Building and testing MongoDB-0.702.2 ... FAIL ! Testing MongoDB-0.702.2 failed but installing it anyway. Successfully installed MongoDB-0.702.2 1 distribution installed m-shige1979@mshige1979-virtual-machine:~$
※testでエラーが発生するので"--force"を付与して強制インストール
※依存モジュールが多いので注意(20~30分位)
サンプルデータによる接続
ソースコード
use strict; use warnings; use Data::Dumper; use MongoDB; # 接続 my $client = MongoDB::MongoClient->new(); # データベースを指定。今回はないので作成となる my $db = $client->get_database('sample1'); # コレクションを取得、ない場合は作成 my $test1 = $db->get_collection('test1'); # データを削除 $test1->remove(); # コレクションにドキュメントを追加 for my $i (1 .. 10){ $test1->insert({"aaa" => $i}); } # コレクションを参照 my $data = $test1->find(); # 確認 while(my $row = $data->next){ print Dumper($row); }
実行
m-shige1979@mshige1979-virtual-machine:~/mongo_test$ perl mongo_sample1.pl $VAR1 = { '_id' => bless( { 'value' => '5246483160b9b46345000000' }, 'MongoDB::OID' ), 'aaa' => 1 }; $VAR1 = { '_id' => bless( { 'value' => '5246483160b9b46345000001' }, 'MongoDB::OID' ), 'aaa' => 2 }; $VAR1 = { '_id' => bless( { 'value' => '5246483160b9b46345000002' }, 'MongoDB::OID' ), 'aaa' => 3 }; $VAR1 = { '_id' => bless( { 'value' => '5246483160b9b46345000003' }, 'MongoDB::OID' ), 'aaa' => 4 }; $VAR1 = { '_id' => bless( { 'value' => '5246483160b9b46345000004' }, 'MongoDB::OID' ), 'aaa' => 5 }; $VAR1 = { 'aaa' => 6, '_id' => bless( { 'value' => '5246483160b9b46345000005' }, 'MongoDB::OID' ) }; $VAR1 = { 'aaa' => 7, '_id' => bless( { 'value' => '5246483160b9b46345000006' }, 'MongoDB::OID' ) }; $VAR1 = { 'aaa' => 8, '_id' => bless( { 'value' => '5246483160b9b46345000007' }, 'MongoDB::OID' ) }; $VAR1 = { '_id' => bless( { 'value' => '5246483160b9b46345000008' }, 'MongoDB::OID' ), 'aaa' => 9 }; $VAR1 = { '_id' => bless( { 'value' => '5246483160b9b46345000009' }, 'MongoDB::OID' ), 'aaa' => 10 }; m-shige1979@mshige1979-virtual-machine:~/mongo_test$
まとめ
とりあえずこんな感じでやることをまとめていく。
外部サイトで確認したいけど、テストサイトとしてだけやりたいのでherokuでできそうか試してみようかな?
- 作者: Randal L. Schwartz,brian d foy,Tom Phoenix,近藤嘉雪
- 出版社/メーカー: オライリージャパン
- 発売日: 2012/07/25
- メディア: 大型本
- 購入: 7人 クリック: 22回
- この商品を含むブログ (15件) を見る