RedHat7搭建MongoDB集群

下载RPM安装包
# wget -c -r -N -np -nd -L -nH https://repo.mongodb.org/yum/redhat/7/mongodb-org/stable/x86_64/RPMS/

安装MongoDB

# rpm -ivh mongodb-org-*

开启防火墙

# firewall-cmd --permanent --add-port=27017/tcp

# firewall-cmd --reload

启动MongoDB

# mongod

mongod在没有参数的情况下会使用默认数据目录/data/db。如果数据目录不存在或者不可写,服务器会启动失败。因此,在启动MongoDB前,先创建数据目录(如 mkdir -p /data/db/),以确保对该目录有些权限,这点非常重要。

mongod还会启动一个非常基本的HTTP服务器,监听数字比主端口号高1000的端口,也就是28017端口。通过浏览器访问http://localhost:28017,能获得数据库的管理信息。

解决WARNING: soft rlimits too low.

# /etc/security/limits.d/99-mongodb-nproc.conf 
mongod soft nofile 64000
mongod hard nofile 64000
mongod soft nproc 32000
mongod hard nproc 32000

# service mongod restart

MongoDB自带Javascript shell,可在shell中使用命令行与MongoDB实例交互。

运行shell

$ mongo

shell中的基本操作

1. 创建

> post = {"title" : "My Blog Post",
... "content" : "Here's my blog post.",
... "date" : new Date()}

> db.blog.insert(post)

2. 读取

find和findOne方法可以用于查询集合里的文档。

3. 更新

> post.comments = []

> db.blog.update({title : "My Blog Post"}, post)

4. 删除

> db.blog.remove({title : "My Blog Post"})

基本数据类型

null

{"x" : null}

布尔型

{"x" : true}

数值(默认使用64位浮点型数值)

{"x" : 3.14}

字符串

{"x" : "foobar"}

日期

{"x": new Date()}

正则表达式

{"x" : /foobar/i}

数组

{"x" : ["a", "b", "c"]}

内嵌文档

{"x" : {"foo" : "bar"}}

对象id

{"x" : ObjectId()}

二进制数据

二进制数据是任意字节的字符串

代码

{"x" : function() { /* ... */ }}

在用户主目录创建.mongorc.js文件,这个文件会在启动shell时自动运行

# vi ~/.mongorc.js

EDITOR="/usr/bin/vi";

 

posted @ 2016-01-02 13:44  Edward Guan  阅读(340)  评论(0编辑  收藏  举报