vim /etc/yum.repos.d/mongodb.repo
[mongodb] name=MongoDB Repository baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/ gpgcheck=0 enabled=1
[mongodb]
name=MongoDB Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/i686/
gpgcheck=0
enabled=1
yum install mongo-10gen mongo-10gen-server
apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
vim /etc/apt/sources.list.d/mongodb.list
deb http://downloads-distro.mongodb.org/repo/debian-sysvinit dist 10gen
安装成功后,会在系统中生成如下重要文件apt-get update
apt-get install mongodb-10gen
mongod :MonoDB 服务端mongo :MongoDB 客户端mongoimport: 数据库导入工具mongoexport : 数据导出工具mongodump : 数据库备份工具mongorestore: 数据库恢复工具mongofiles : GridFS管理工具,可实现对二进制文件的存取mongostat : 性能分析工具,类似与vmstatmongotop :性能分析工具,查看mongodb实例读写时间花费mongoperf :性能分析工具 ,磁盘性能检查工具mongos : MongoDB集群工具
mongod -f /etc/mongod.conf或者service mongod start(CentOS) 或者service mongodb start (Debian)或者/etc/init.d/mongod start (CentOS) 或者 /etc/init.d/mongodb start (Debian)
我们可以先查看日志,看发生了什么service mongod start
Starting mongod: about to fork child process, waiting until server is ready for connections.
forked process: 31346
all output going to: /var/log/mongo/mongod.log
ERROR: child process failed, exited with error number 100
more /var/log/mongo/mongod.log
由上可知,journal文件的存储空间不够,我们可以选择关闭journalMon Feb 10 19:38:26.199 [initandlisten] ERROR: Insufficient free space for journal files
Mon Feb 10 19:38:26.199 [initandlisten] Please make at least 3379MB available in /var/lib/mongo/journal or use --smallfiles
Mon Feb 10 19:38:26.199 [initandlisten]
Mon Feb 10 19:38:26.199 [initandlisten] exception in initAndListen: 15926 Insufficient free space for journals, terminating
Mon Feb 10 19:38:26.199 dbexit:
然后就可以正常启动了vim /etc/mongod.conf
# Disables write-ahead journaling
nojournal = true
mongod -f /etc/mongod.conf
mongo --port xxxx --host xxx.xxx.xxx admin
> db.addUser("admin","xxxx")
重启mongodvim /etc/mongod.conf
auth = true
pkill mongod
mongod -f /etc/mongod.conf
mongo --port xxxx --host xxx.xxx.xxx.xxx admin
> db.auth("admin","xxxx") #身份认证
> use test #切换到指定数据库
> db.addUser("tanjiti","xxx")
mkdir /home/tanjiti/mongod
chown mongod /home/tanjiti/mongod
vim /etc/mongod.conf
修改
dbpath=/home/tanjiti/mongod
vim /etc/mongodb
设置mongodb 存储空间
dbpath=/home/tanjiti/mongod
设置mongodb 连接端口
port = 27317
开启/或关闭身份认证
auth = true
noauth = true
看情况是否关闭HTTP接口
nohttpinterface = true
关闭服务端脚本
noscripting = true
(2)创建collection(等同于SQL中的table)> use test
switched to db test> db.addUser("tanjiti","xxxx")
> use test
switched to db test> db.createCollection("test")
> db.test.insert(...)
> show dbs;
>db.getName()
> use testswitched to db test> show collections
> db.getCollectionNames()
> use testswitched to db test> db.system.users.find(){ "_id" : ObjectId("52f9c8d169ec0909845453df"), "user" : "tanjiti", "readOnly" : false, "pwd" : "bxf768cc1xxxxxxxxxxxxxxx8a59fb" }
> db.test.update(...)
> use testswitched to db test> db.dropDatabase(){ "dropped" : "test", "ok" : 1 }
(3)删除表中记录> db.test.drop()
> db.test.remove()
> use testswitched to db test> show users
> use testswitched to db test>db.addUser("tanjiti","xxxx")
> use testswitched to db test>db.removeUser("tanjiti")
> use testswitched to db test>db.auth("admin","xxxx")
然后vim packages.txt
添加
Data::Types
DateTime
DateTime::Tiny
ExtUtils::MakeMaker
File::Slurp
File::Temp
FileHandle
JSON
Test::Exception
Test::Warn
Tie::IxHash
Try::Tiny
boolean
ExtUtils::MakeMaker
Class::Method::Modifiers
DateTime
Digest::MD5
Moose
Tie::IxHash
XSLoader
(2)安装MongoDB perl驱动cpan App::cpanminus #安装cpanm
cat packages.txt | cpanm
wget http://cpan.metacpan.org/authors/id/F/FR/FRIEDO/MongoDB-0.702.2.tar.gztar zxvf MongoDB-0.702.2.tar.gzcd MongoDB-0.702.2perl Makefile.PLmakemake test #需要启动Mongodbmake install
更多操作,可以查看perl文档use MongoDB;
use MongoDB::OID;
#mongoDB 配置
my $database_mo = "YOURDB";
my $host_mo = "YOUR_HOST";
my $port_mo = YOUR_PORT;
my $collection = "YOUR_COLLECTION";
my $user_mo = "YOUR_USERNAME";
my $password_mo = "YOUR_PASSWORD";
#MongoDB连接
my $conn = MongoDB::MongoClient->new("host" => $host_mo, "username" => $user_mo, "password" => $password_mo, "db_name" => $database_mo, "port" => $port_mo);
#指定数据库
my $db = $conn->get_database($database_mo);
#指定collections(类似SQL的table)
my $coll = $db->get_collection($collection);
#在collections集合中插入数据
$coll->insert({Data1 => $Data1, Data2 => $Data2});
#查询单行document(类似SQL的row)数据,
my $objects = $coll->find_one({Data1=>"1",Data2=>$Data2});
#查看该行document的各个字段field(类似SQL的column)的值,perl将查询结果存储为一个对hash结构的引用
my %tmp = %$objects;
foreach (keys %tmp){
print $_," : ",$tmp{$_},"\n";
}
#查询多行documents数据
my $cursor = $coll->find({Status=>"1",Level=>$Level});
#提取结果集的内容存储在数组objects中,该数组由指向具体document数据的引用组成
my @objects = $cursor->all;
#查看结果集合各个字段field的值
foreach (@objects){
my %tmp = %$_;
foreach (keys %tmp){
print $_," : ",$tmp{$_},"\n";
}
}
perldoc MongoDB
perldoc MongoDB::MongoClient #实现了mongo命令功能的模块
perldoc MongoDB::Database #database操作模块
perldoc MongoDB::Collection #collection操作模块
perldoc MongoDB::Cursor #查询结果集操作模块