AWSã§ä»ããå§ããmongodb(1) ã¤ã³ã¹ãã¼ã«ãããµã¼ãæ¥ç¶ã¾ã§
ã©ãã©ããä»æ¥ã¯ã¹ã¿ãã§ãã³ãã£é£²ãã ãè ¹ãã¿ãã¿ãããã®ã§ãã
ããããã°ãmongodbã«ã¤ãã¦æ¸ãã¦ãªããªãã¨æãã¾ãã¦ãèªåã®ç¥èã®å®çã®æå³ãå«ãã¦ã¡ãã£ã¨ãã°ããæ¸ãã¦ããããã¨æã£ã¦ãã¾ãã
ä»æ¥ã®ãã¼ã
ã¤ã³ã¹ãã¼ã«ããã¯ã©ã¤ã¢ã³ãã§ã®æ¥ç¶ã«ã¤ãã¦ã§ãã
mongodbã¨ã¯ï¼
ã¾ããmongodbã§ãã
mongodbã¯ããã¥ã¡ã³ãæåãã¼ã¿ãã¼ã¹ã§ãã
ããã¥ã¡ã³ãæåã¨ãããã¨ã§ããã¼ã¿å½¢å¼ã¯JSONã§ãã¹ã¦è¡¨ç¾ããã¾ãã
PRIMARY> db.TestUser.findOne()
{
"_id" : "aaaaaaaaaaaaa",
"userid" : "akuwano",
"lastLoginTime" : NumberLong("1326287399283"),
"username" : "Akihiro Kuwano"
}
ãããªæããã¾ããã¹ãã¼ãã¬ã¹ãªã®ã§éä¸ã§ã«ã©ã ãå¢ãããããã¨ããMySQLãªã©ã®ããã«ALTER TABLEã§ã®å®ç¾©å¤æ´ãªã©ã¯å¿ è¦ããã¾ããã
詳ããã¯この辺ãã©ããï¼
EC2ã¤ã³ã¹ã¿ã³ã¹ã®launchæ¹æ³
å²æã
quick-launch楽ã¡ãã§ããã
ã¤ã³ã¹ãã¼ã«
ã¤ã³ã¹ãã¼ã«ã¯ã以ä¸ã®ããã«mongodbのダウンロードサイトãããã¤ããªããã¦ã³ãã¼ããã¦ã¿ãã®ãä¸çªç°¡åã§ãã
ãã£ãããªã®ã§EC2ã¤ã³ã¹ã¿ã³ã¹ã«ã¤ã³ã¹ãã¼ã«ãã¦ã¿ã¾ãããã
$ ssh -i akuwano.pem [email protected]
__| __|_ )
_| ( / Amazon Linux AMI
___|\___|___|
[ec2-user@ip-hogehoge ~]$
See /usr/share/doc/system-release/ for latest release notes.
There are 18 security update(s) out of 26 total update(s) available
$ cd /usr/local
$ sudo wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.0.2.tgz
$ sudo tar zxvf mongodb-linux-x86_64-2.0.2.tgz
$ cd mongodb-linux-x86_64-2.0.2
[root@ip-hogehoge mongodb-linux-x86_64-2.0.2]$ ls -R
.:
GNU-AGPL-3.0 README THIRD-PARTY-NOTICES bin./bin:
bsondump mongo mongod mongodump mongoexport mongofiles mongoimport mongorestore mongos mongosniff mongostat mongotop
ã¤ã³ã¹ãã¼ã«ããããã¡ã¤ã«ã¯ãããªæãã
è¨å®ãèµ·å
ã§ã¯æ¬¡ã¯ã¤ã³ã¹ãã¼ã«å¾ã®è¨å®ããèµ·åã¾ã§ã
cd /usr/local/mongodb-linux-x86_64-2.0.2
sudo mkdir -p /usr/local/mongodb-linux-x86_64-2.0.2/{logs,conf,data}
sudo su -c "cat <<'EOF' > /usr/local/mongodb-linux-x86_64-2.0.2/conf/mongod.conf
port = 27017
logpath=/usr/local/mongodb-linux-x86_64-2.0.2/logs/mongod.log
pidfilepath=/var/run/mongod1.pid
logappend=true
fork = true
dbpath=/usr/local/mongodb-linux-x86_64-2.0.2/data
directoryperdb=true
noauth = true# Replication Options
#replSet = replSetTest001
rest = true# Sharding Options
#shardsvr=true# Journal Option
journal = true
EOF"
ããããèµ·åï¼
sudo /usr/local/mongodb-linux-x86_64-2.0.2/bin/mongod -f /usr/local/mongodb-linux-x86_64-2.0.2/conf/mongod.conf
$ vmstat 1
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 3 0 7112 4236 534524 0 0 37 426 26 27 0 0 95 4 0
0 3 0 7076 4236 534684 0 0 0 29876 462 836 0 4 0 93 4
0 3 0 7072 4240 534700 0 0 0 31024 509 869 0 4 0 93 4
0 2 0 7068 4240 534484 0 0 0 27240 394 171 0 4 0 88 8
0 3 0 6596 4240 535644 0 0 0 22948 408 505 0 4 0 95 1
ãã°ããã¯ãã¼ã¿ãã¡ã¤ã«ã®ãã¡ã¤ã«ç¢ºä¿ã§DISKãã¬ãªã¬ãªæ¸ãã¾ããããã°ããçµã¦ã°çµããã¾ãã
æ¥ç¶
次ã«ãã¼ã«ã«ããmongoã¯ã©ã¤ã¢ã³ãã§æ¥ç¶ãã¦ã¿ã¾ãããã
$ /usr/local/mongodb-linux-x86_64-2.0.2/bin/mongo
MongoDB shell version: 2.0.2
connecting to: test
>
OK
ãã¼ã¿ã®ã¤ã³ãµã¼ã
ã¡ããã¨ãã¼ã¿ãå
¥ãããããã®ç¢ºèªãã¯ã©ã¤ã¢ã³ããã
testdbãã¼ã¿ãã¼ã¹ã®ãtestcolã³ã¬ã¯ã·ã§ã³ã«ã1ãªãã¸ã§ã¯ã追å ãã¦ã¿ã¾ãã
ãã¼ã¿è¿½å ã
mongo> doc = { cdname: 'cdname001',
... cdtitle : 'Example CD Name'
... }
{ "cdname" : "cdname001", "cdtitle" : "Example CD Name" }
mongo>
mongo> db.testcol.insert(doc);
findã§ç¢ºèªã
> db.testcol.find()
{ "_id" : ObjectId("4f25e4d097a5113670430159"), "cdname" : "cdname001", "cdtitle" : "Example CD Name" }
OKã§ããã
"_id" : ObjectId("4f25e4d097a5113670430159")ã¯mongodbå´ã§èªåã§ã¤ãã¦ããã¾ããMySQLãªã©ã®RDBã§ããã°PK(PrimaryKey)ã«ããããsharding*1ã®æã®ãã¼ã«ãªãã¾ãã
DBã¸ã®ã¢ã¯ã»ã¹å¶é
ããã ã¨ã誰ã§ãã¢ã¯ã»ã¹ã§ãã¦ãã¾ãã®ã§ãã¦ã¼ã¶ããã¹ã¯ã¼ããã¤ãã¦ã¢ã¯ã»ã¹å¶éãã¦ã¿ã¾ãããã
$ /usr/local/mongodb-linux-x86_64-2.0.2/bin/mongo
MongoDB shell version: 2.0.2
connecting to: test
> use testdb
switched to db testdb
> db.addUser('user', 'password')
{
"updatedExisting" : true,
"n" : 1,
"connectionId" : 6,
"err" : null,
"ok" : 1
}
{
"_id" : ObjectId("4f25e51097a511367043015b"),
"user" : "user",
"readOnly" : false,
"pwd" : "3bcfc22a1cd6be41bc7814c13d3ce94c"
}
> use admin
switched to db admin
> db.addUser('user', 'password')
{
"updatedExisting" : true,
"n" : 1,
"connectionId" : 6,
"err" : null,
"ok" : 1
}
{
"_id" : ObjectId("4f25e50197a511367043015a"),
"user" : "user",
"readOnly" : false,
"pwd" : "3bcfc22a1cd6be41bc7814c13d3ce94c"
}
> exit
db.addUser('user', 'password') - ã¦ã¼ã¶ããã¹ã¯ã¼ãã®è¨å®ã®ããã®ã³ãã³ãã«ãªãã¾ãã
$ ./mongo ec2-hoge.ap-northeast-1.compute.amazonaws.com:27017/testdb -u user -p HOGEHOGE
MongoDB shell version: 2.0.2
connecting to: ec2-hoge.ap-northeast-1.compute.amazonaws.com:27017/testdb
Mon Jan 30 09:46:51 uncaught exception: login failed
exception: login failed
ã¢ã¯ã»ã¹ä¸å¯ãããããã
$ ./mongo ec2-hoge.ap-northeast-1.compute.amazonaws.com:27017/testdb -u user -p password
MongoDB shell version: 2.0.2
connecting to: ec2-hoge.ap-northeast-1.compute.amazonaws.com:27017/testdb
>
ã¢ã¯ã»ã¹ã§ãã¾ããã¼ï¼
ããã§ã»ãã¥ãªãã£ãOKã§ãã
EC2ã®ã»ãã¥ãªãã£ã°ã«ã¼ãã®è¨å®
ããã¾ã§OKã«ããããAWSã®ManagementConsoleããEC2ã®ã»ãã¥ãªãã£ã°ã«ã¼ãè¨å®ãå¤æ´ãã¾ãããã
27017ãã¼ãã¸ã®ã¢ã¯ã»ã¹ãæå¹ã«ãã¾ããç»ååç
§ã
[Apply Rule Changes]ãããããæå¹ã«ãªãã¾ãã
ããã§å®äºã
ããã§mongodbãã©ãããã§ã使ããç¶æ
ã«ãªãã¾ããã
ã¨ããã®ããã¾ã«é²åé²çã«ã¾ã¨ãã¦ããããã¨æãã¾ãããããããé¡ããã¾ãã
*1:å¾è¿°äºå®ï¼æ±ï¼