DebianにMongoDBをインストールする

CouchDBについて調べていると、たまたまMongoDBというデータベースを見つけました。

MongoDB

なんとなくCouchDBと同じような仕様のようです。
インストールして動かしてみます。


※2014/7/22追記

apt-getでのインストールはこちら。
Debian 7にMongoDB 2.0.6 をapt-getでインストールする




MongoDBのインストール



http://www.mongodb.org/display/DOCS/Quickstart
こちらを参考にインストールしました。

どうやら、ビルド済みのモジュールが提供されいるようで、
configureやmakeの必要はありませんでした。

以下、インストールしたときの手順です。



### インストールするディレクトリに移動
### ※/opt/のほうが良かったかも。
# cd /usr/local

### データフォルダを作成
# mkdir -p /data/db

### バイナリを取得
# wget http://downloads.mongodb.org/linux/mongodb-linux-i686-latest.tgz

### 展開
# tar xzf mongodb-linux-i686-latest.tgz

### ディレクトリ名にバイナリが作成された年月が入ります。
### mongodbという名前にリネームしました。
### これで、/usr/local/mongodbにバイナリが配置されたことになります。
# mv mongodb-linux-i686-yyyy-MM-dd mongodb






MongoDBの起動



mongodbのbinに移動し、「./mongod &」としてやれば起動しました。



# cd /usr/local/mongodb/bin
# ./mongod &
[1] 6268
root@firebird:/usr/local/mongodb/bin# ./mongod --help for help and startup options
Sat Jan 2 19:57:41 Mongo DB : starting : pid = 6268 port = 27017 dbpath = /data/db/ master = 0 slave = 0 32-bit

** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data
**     see http://blog.mongodb.org/post/137788967/32-bit-limitations for more

Sat Jan 2 19:57:41 db version v1.3.1-, pdfile version 4.5
Sat Jan 2 19:57:41 git version: 9b1d3f135d70fc30f7c85c50e088b2f59068c708
Sat Jan 2 19:57:41 sys info: Linux domU-12-31-39-01-70-B4 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_37
Sat Jan 2 19:57:41 waiting for connections on port 27017





MongoDBの動作確認



クライアントツールが付属していますので、それを使用して動作確認してみます。


# cd /usr/local/mongodb/bin
# ./mongo
MongoDB shell version: 1.3.1-
url: test
connecting to: test
Sat Jan 2 19:59:07 connection accepted from 127.0.0.1:45845 #1
type "help" for help




データを登録してみます。


> db.foo.save( { a : 1} )
Sat Jan 2 19:59:24 allocating new datafile /data/db/test.ns, filling with zeroes...
Sat Jan 2 19:59:24 done allocating datafile /data/db/test.ns, size: 16MB, took 0.114 secs
Sat Jan 2 19:59:24 allocating new datafile /data/db/test.0, filling with zeroes...
Sat Jan 2 19:59:25 done allocating datafile /data/db/test.0, size: 64MB, took 0.527 secs
Sat Jan 2 19:59:25 building new index on { _id: ObjId(000000000000000000000000) } for test.foo...
Sat Jan 2 19:59:25 Buildindex test.foo idxNo:0 { name: "_id_", ns: "test.foo", key: { _id: ObjId(000000000000000000000000) } }
Sat Jan 2 19:59:25 done for 0 records 0secs
Sat Jan 2 19:59:25 insert test.foo32646ms




データを検索してみます。


> db.foo.findOne()
{ "_id" : ObjectId("4b3f270c114f5cca93dc6a89"), "a" : 1 }



ちゃんと登録したデータが表示されました。

ツールの終了は、exit


> exit
bye





MongoDBの終了



MongoDBを終了するには、2つの方法があるようです。

クライアントツールからのシャットダウンは、以下の手順で行えました。


# cd /usr/local/mongodb/bin
# ./mongo
> use admin
switched to db admin
> db.shutdownServer()
Sat Jan 2 20:15:50 terminating, shutdown command received
Sat Jan 2 20:15:50 dbexit:
Sat Jan 2 20:15:50     shutdown: going to flush oplog...
Sat Jan 2 20:15:50     shutdown: going to close sockets...
Sat Jan 2 20:15:50     shutdown: waiting for fs...
Sat Jan 2 20:15:50     shutdown: closing all files...
Sat Jan 2 20:15:50     closeAllFiles() finished
Sat Jan 2 20:15:50     shutdown: removing fs lock...
Sat Jan 2 20:15:50 dbexit: really exiting now
server should be down...
Sat Jan 2 20:15:50 trying reconnect to 127.0.0.1
Sat Jan 2 20:15:50 connection accepted from 127.0.0.1:56597 #3
Sat Jan 2 20:15:50 Listener on port 27017 aborted
Sat Jan 2 20:15:50 reconnect 127.0.0.1 ok
ERROR: Client::shutdown not called!
Sat Jan 2 20:15:50 JS Error: Error: error doing query: failed (anon):1119





もうひとつ、killコマンドでPIDを指定しての終了も行えます。


kill -2 <PID>




PIDは、MongoDBの起動時に表示された値を指定しました。





とりあえず、データベースの起動と終了、データの登録が行えました。

クライアントツールを使用して、使い方をもう少し詳しく調べてみます。





関連記事

コメント

プロフィール

Author:symfo
blog形式だと探しにくいので、まとめサイト作成中です。
https://symfo.web.fc2.com/

PR

検索フォーム

月別アーカイブ