Quantcast
Channel: 碳基体
Viewing all articles
Browse latest Browse all 75

大数据之MongoDB的安装配置、基本操作及Perl操作MongoDB

$
0
0
大数据,数据挖掘,数据可视化成为了热点词汇,也给安全领域带了新(其实不算新)而有趣的方向,例如用数据分析来识别恶意攻击,垃圾邮件,webshell等。同时数据存储方式也发生了大的变化,SQL存储结构转化为了NoSQL,其中MongoDB无疑是NoSQL中最受欢迎(资料最多的),本篇文章就介绍MongoDB的安装及基本使用,为数据挖掘预热下。

一、安装

1. CentOS上安装

vim /etc/yum.repos.d/mongodb.repo

64位

[mongodb] name=MongoDB Repository baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64/ gpgcheck=0 enabled=1

32位

[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


2. Debian/Ubuntu上安装

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

安装成功后,会在系统中生成如下重要文件
默认配置文件/etc/mongodb.conf 
默认日志文件/var/log/mongodb/mongodb.log
默认数据库文件/var/lib/mongodb

常用命令行

mongod :MonoDB 服务端
mongo :MongoDB 客户端
mongoimport: 数据库导入工具
mongoexport : 数据导出工具
mongodump : 数据库备份工具    
mongorestore: 数据库恢复工具
mongofiles : GridFS管理工具,可实现对二进制文件的存取
mongostat : 性能分析工具,类似与vmstat
mongotop  :性能分析工具,查看mongodb实例读写时间花费
mongoperf    :性能分析工具 ,磁盘性能检查工具
mongos : MongoDB集群工具 


         

二、启动与基本配置

1. 启动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

Mon 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:

由上可知,journal文件的存储空间不够,我们可以选择关闭journal

vim /etc/mongod.conf

# Disables write-ahead journaling
nojournal = true

然后就可以正常启动了

mongod -f /etc/mongod.conf



2. 添加身份认证

(1)添加全局帐号
在未开启身份认证之前(即/etc/mongod.conf noauth = true),添加帐号
mongo --port xxxx --host xxx.xxx.xxx admin
> db.addUser("admin","xxxx")
修改配置文件开启身份认证

vim /etc/mongod.conf

auth = true

重启mongod

pkill mongod

mongod -f /etc/mongod.conf



(2)添加指定数据库帐号

mongo --port xxxx --host xxx.xxx.xxx.xxx admin

> db.auth("admin","xxxx") #身份认证

> use test #切换到指定数据库

> db.addUser("tanjiti","xxx")


3.修改db存放路径

mkdir /home/tanjiti/mongod

chown mongod /home/tanjiti/mongod


vim /etc/mongod.conf

修改

dbpath=/home/tanjiti/mongod



4. 常见配置项
最为一个新手,我选择修改默认的port,开启身份认证,关闭HTTP接口,关闭服务端脚本来增加安全性。

vim /etc/mongodb

设置mongodb 存储空间

dbpath=/home/tanjiti/mongod

设置mongodb 连接端口

port = 27317

开启/或关闭身份认证

auth = true

noauth = true

看情况是否关闭HTTP接口

nohttpinterface = true

关闭服务端脚本

noscripting = true


三、数据库常见操作
可以对比SQL数据库来学习NoSQL,mongoDB官方文档给出了常用操作的对比http://docs.mongodb.org/manual/reference/sql-comparison/

1. 创建操作
MongoDB没有专门的创建数据库的命令,但运行use DBName命令,在做一些操作(例如添加数据库用户)就可以(1)创建数据库

> use test
switched to db test

> db.addUser("tanjiti","xxxx")

(2)创建collection(等同于SQL中的table)

> use test
switched to db test

> db.createCollection("test")




2.插入操作

> db.test.insert(...)



3.查询操作
(1)查询MongoDB中存在的数据库

> show dbs;


(2)查询当前数据库名

>db.getName()


(3)查询指定数据库中存在的collections

> use test
switched to db test
> show collections

或者

> db.getCollectionNames()


(4)查看指定数据库的用户表

> use test
switched to db test
> db.system.users.find()
{ "_id" : ObjectId("52f9c8d169ec0909845453df"), "user" : "tanjiti", "readOnly" : false, "pwd" : "bxf768cc1xxxxxxxxxxxxxxx8a59fb" }


4.更新操作
(1)更新表

> db.test.update(...)



5.删除操作
(1)删除数据库

> use test
switched to db test
> db.dropDatabase()
{ "dropped" : "test", "ok" : 1 }

(2)删除表

> db.test.drop()

(3)删除表中记录

> db.test.remove()


6.用户相关
(1.)查询当前所有用户

> use test
switched to db test
> show users

(2)添加用户

> use test
switched to db test
>db.addUser("tanjiti","xxxx")

(3)删除用户

> use test
switched to db test
>db.removeUser("tanjiti")

(4)用户认证

> use test
switched to db test
>db.auth("admin","xxxx")


四、perl与MongoDB的结合
1. 安装MongoDB驱动
参考:http://docs.mongodb.org/ecosystem/drivers/perl/
(1)安装依赖包
将依赖包写入文件,然后批量安装

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

然后

cpan App::cpanminus #安装cpanm

cat packages.txt | cpanm

(2)安装MongoDB perl驱动

wget http://cpan.metacpan.org/authors/id/F/FR/FRIEDO/MongoDB-0.702.2.tar.gz
tar zxvf MongoDB-0.702.2.tar.gz
cd MongoDB-0.702.2
perl Makefile.PL
make
make test #需要启动Mongodb
make install 


2. perl mongo 基本操作
下面演示连接数据库与在集合中插入数据,查询数据

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";
}
}

更多操作,可以查看perl文档

perldoc MongoDB

perldoc MongoDB::MongoClient #实现了mongo命令功能的模块

perldoc MongoDB::Database #database操作模块

perldoc MongoDB::Collection #collection操作模块

perldoc MongoDB::Cursor #查询结果集操作模块


Viewing all articles
Browse latest Browse all 75

Trending Articles