Centos6.0 64yum安装MongoDB及主从配置

一、Yum安装MongoDB

1、准备工作

运行yum命令查看MongoDB的包信息 [root@localhost~]# yum info mongo-10gen

(提示没有相关匹配的信息,) 说明你的centos系统中的yum源不包含MongoDB的相关资源,所以要在使用yum命令安装MongoDB前需要增加yum源,也就是在 /etc/yum.repos.d/目录中增加 *.repo yum源配置文件

vi /etc/yum.repos.d/10gen.repo,输入下面的语句:

[10gen]

name=10gen  

baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64      

gpgcheck=0

做好yum源的配置后,如果配置正确执行下面的命令便可以查询MongoDB相关的信息:

查看mongoDB的服务器包的信息

[root@localhost geffzhang]# yum info mongo-10gen-server

Loaded plugins: fastestmirror, security

Loading mirror speeds from cached hostfile

* base: mirrors.btte.net

* extras: mirrors.btte.net

* updates: mirrors.btte.net

Installed Packages

Name        : mongo-10gen-server

Arch        : x86_64

Version     : 2.2.3

Release     : mongodb_1

Size        : 17 M

Repo        : installed

From repo   : aaa

Summary     : mongo server, sharding server, and support scripts

URL         : http://www.mongodb.org

License     : AGPL 3.0

Description : Mongo (from "huMONGOus") is a schema-free document-oriented database.

           :

           : This package provides the mongo server software, mongo sharding server

           : softwware, default configuration files, and init.d scripts.

3、安装MongoDB的服务器端和客户端工具  

[root@nagios mongo]# yum -y install  mongo-10gen-server

[root@nagios mongo]# yum -y install  mongo-10gen

安装好后可以查看mongd的命令

[root@nagios mongo]# ls /usr/sbin/mongo

mongo         mongodump     mongofiles    mongorestore  mongosniff    mongotop      

mongod        mongoexport   mongoimport   mongos        mongostat

4、启动Mongodb与登陆

service mongod start

[root@nagios mongo]# mongo

MongoDB shell version: 2.0.4

connecting to: test

> show dbs;

admin(empty)

elaindb0.203125GB

local4.013671875GB

以后有更新了,停掉mongodb,执行yum update mongo-10gen mongo-10gen-server 即可

5、服务器配置: /etc/mongod.conf

# mongo.conf  

#where to log  

logpath=/var/log/mongo/mongod.log  

logappend=true #以追加方式写入日志  

# fork and run in background  

fork = true  

#port = 27017 #端口  

dbpath=/var/lib/mongo #数据库文件保存位置  

directoryperdb=true

# Enables periodic logging of CPU utilization and I/O wait  

#启用定期记录CPU利用率和 I/O 等待  

#cpu = true  

# Turn on/off security.  Off is currently the default  

# 是否以安全认证方式运行,默认是不认证的非安全方式  

#noauth = true  

#auth = true  

# Verbose logging output.  

# 详细记录输出  

#verbose = true  

# Inspect all client data for validity on receipt (useful for  

# developing drivers)用于开发驱动程序时的检查客户端接收数据的有效性  

#objcheck = true  

# Enable db quota management 启用数据库配额管理,默认每个db可以有8个文件,可以用quotaFiles参数设置  

#quota = true  

# 设置oplog记录等级  

# Set oplogging level where n is  

#   0=off (default)  

#   1=W  

#   2=R  

#   3=both  

#   7=W+some reads  

#oplog = 0  

# Diagnostic/debugging option 动态调试项  

#nocursors = true  

# Ignore query hints 忽略查询提示  

#nohints = true  

# 禁用http界面,默认为localhost28017  

# Disable the HTTP interface (Defaults to localhost:27018).这个端口号写的是错的  

#nohttpinterface = true  

# 关闭服务器端脚本,这将极大的限制功能  

# Turns off server-side scripting.  This will result in greatly limited  

# functionality  

#noscripting = true  

# 关闭扫描表,任何查询将会是扫描失败  

# Turns off table scans.  Any query that would do a table scan fails.  

#notablescan = true  

# 关闭数据文件预分配  

# Disable data file preallocation.  

#noprealloc = true  

# 为新数据库指定.ns文件的大小,单位:MB  

# Specify .ns file size for new databases.  

# nssize = <size>  

# Accout token for Mongo monitoring server.  

#mms-token = <token>  

# mongo监控服务器的名称  

# Server name for Mongo monitoring server.  

#mms-name = <server-name>  

# mongo监控服务器的ping 间隔  

# Ping interval for Mongo monitoring server.  

#mms-interval = <seconds>  

# Replication Options 复制选项  

# in replicated mongo databases, specify here whether this is a slave or master 在复制中,指定当前是从属关系  

#slave = true  

#source = master.example.com  

# Slave only: specify a single database to replicate  

#only = master.example.com  

# or  

#master = true  

#source = slave.example.com  

二、MongoDB主从配置

一、mongodb主从服务器

1.172.16.1.170  mongodb主服务器

2.172.16.1.171  mongodb从服务器

二、主从服务器配置

1.172.16.1.170配置

[root@nagios mongo]# cat /etc/mongod.conf

logpath=/var/log/mongo/mongod.log

logappend=true

fork = true

dbpath=/var/lib/mongo

pidfilepath = /var/run/mongodb/mongod.pid

master = true

source = 172.16.1.171

port=27017

2.172.16.1.171配置

[root@171 mongo]# cat /etc/mongod.conf

port=27017

logpath=/var/log/mongo/mongod.log

logappend=true

fork = true

dbpath=/var/lib/mongo

pidfilepath = /var/run/mongodb/mongod.pid

slave = true

source = 172.16.1.170

3.启动主从服务器mongodb

service mongod start

主服务器mongodb日志

[root@nagios mongo]# tail -f /var/log/mongo/mongod.log

Tue Feb 26 13:44:36 [initandlisten] MongoDB starting : pid=14105 port=27017 dbpath=/var/lib/mongo master=1 64-bit host=nagios

Tue Feb 26 13:44:36 [initandlisten] db version v2.2.3, pdfile version 4.5

Tue Feb 26 13:44:36 [initandlisten] git version: f570771a5d8a3846eb7586eaffcf4c2f4a96bf08

Tue Feb 26 13:44:36 [initandlisten] build info: Linux ip-10-2-29-40 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64 BOOST_LIB_VERSION=1_49

Tue Feb 26 13:44:36 [initandlisten] options: { config: "/etc/mongod.conf", dbpath: "/var/lib/mongo", fork: "true", logappend: "true", logpath: "/var/log/mongo/mongod.log", master: "true", pidfilepath: "/var/run/mongodb/mongod.pid", port: 27017, source: "172.16.1.171" }

Tue Feb 26 13:44:36 [initandlisten] journal dir=/var/lib/mongo/journal

Tue Feb 26 13:44:36 [initandlisten] recover : no journal files present, no recovery needed

Tue Feb 26 13:44:37 [websvr] admin web console waiting for connections on port 28017

Tue Feb 26 13:44:37 [initandlisten] waiting for connections on port 27017

Tue Feb 26 13:44:48 [initandlisten] connection accepted from 172.16.1.171:45526 #1 (1 connection now open

从服务器mongodb日志

[root@171 mongo]# tail -f /var/log/mongo/mongod.log

Tue Feb 26 13:47:36 [initandlisten] MongoDB starting : pid=7283 port=27017 dbpath=/var/lib/mongo slave=1 64-bit host=171

Tue Feb 26 13:47:36 [initandlisten] db version v2.2.3, pdfile version 4.5

Tue Feb 26 13:47:36 [initandlisten] git version: f570771a5d8a3846eb7586eaffcf4c2f4a96bf08

Tue Feb 26 13:47:36 [initandlisten] build info: Linux ip-10-2-29-40 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64 BOOST_LIB_VERSION=1_49

Tue Feb 26 13:47:36 [initandlisten] options: { config: "/etc/mongod.conf", dbpath: "/var/lib/mongo", fork: "true", logappend: "true", logpath: "/var/log/mongo/mongod.log", pidfilepath: "/var/run/mongodb/mongod.pid", port: 27017, slave: "true", source: "172.16.1.170" }

Tue Feb 26 13:47:36 [initandlisten] journal dir=/var/lib/mongo/journal

Tue Feb 26 13:47:36 [initandlisten] recover : no journal files present, no recovery needed

Tue Feb 26 13:47:36 [initandlisten] waiting for connections on port 27017

Tue Feb 26 13:47:36 [websvr] admin web console waiting for connections on port 28017

Tue Feb 26 13:47:37 [replslave] repl: syncing from host:172.16.1.170

测试主从同步数据

在主服务器173.16.1.170新建数据库

[root@nagios mongo]# mongo

MongoDB shell version: 2.0.4

connecting to: test

> use zhangfang

switched to db zhangfang

> db.blog.save({title:"new zhangfang"})

从服务器172.16.1.171查看

[root@171 mongo]# mongo

MongoDB shell version: 2.2.3

connecting to: test

> show dbs;

elaindb0.203125GB

local0.203125GB

zhangfang0.203125GB

查看日志

[root@171 mongo]# tail -f /var/log/mongo/mongod.log

Tue Feb 26 13:55:39 [FileAllocator] allocating new datafile /var/lib/mongo/zhangfang.1, filling with zeroes...

Tue Feb 26 13:55:39 [conn1] command admin.$cmd command: { listDatabases: 1.0 } ntoreturn:1 keyUpdates:0 locks(micros) R:6 r:33 reslen:234 1424ms

Tue Feb 26 13:55:41 [replslave] build index zhangfang.blog { _id: 1 }

Tue Feb 26 13:55:42 [replslave] fastBuildIndex dupsToDrop:0

Tue Feb 26 13:55:42 [replslave] build index done.  scanned 1 total records. 1.165 secs

Tue Feb 26 13:55:42 [replslave] resync: done with initial clone for db: zhangfang

Tue Feb 26 13:55:45 [FileAllocator] done allocating datafile /var/lib/mongo/zhangfang.1, size: 128MB,  took 5.423 secs

Tue Feb 26 13:56:23 [conn1] end connection 127.0.0.1:54288 (0 connections now open)

Tue Feb 26 13:56:24 [initandlisten] connection accepted from 127.0.0.1:54290 #2 (1 connection now open)

Tue Feb 26 13:56:39 [conn2] end connection 127.0.0.1:54290 (0 connections now open)

Tue Feb 26 13:56:58 [replslave] repl:   checkpoint applied 9 operations

Tue Feb 26 13:56:58 [replslave] repl:   syncedTo: Feb 26 13:53:59 512c4df7:1

说明主从数据同步成功