mongodb安装使用

前言

MongoDB是一个基于分布式文件存储的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。

  MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。他支持的数据结构非常松散,是类似jsonbjson格式,因此可以存储比较复杂的数据类型。Mongo最大的特点是他支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引

MongoDB服务端可运行在LinuxWindowsOS X平台,支持32位和64位应用,默认端口为27017。推荐运行在64位平台,因为MongoDB32位模式运行时支持的最大文件尺寸为2GB

目的

写此文档是方便运维人员在以后的运维维护过程中了解学习,如果想深入了解mongdb,可自行参考网上资料学习。

系统框架

shard服务器:使用Replica Sets确保每个数据都具有备份、自动容错转移、自动恢复的能力。

 

当主monogdb服务器出现故障停止服务时,切换方式如下:

 

Mongdb安装部署

安装包:wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-2.6.1.tgz

#tar zxvf mongodb-linux-x86_64-2.6.1.tgz

#mv mongodb-linux-x86_64-2.6.1 /opt/mongodb

4.1 配置monogdb副本集

注:配置文件中参数解释请参考下文中参考资料模块

10.15.201.191

#mkdir -p /opt/mongodb/data/rs1_1/

#mkdir -p /opt/mongodb/log/

#mkdir -p /opt/mongodb/bin/configFile

 

rs1_1.conf(手动创建的配置文件名字)

#vi /opt/mongodb/bin/configFile/rs1_1.conf

dbpath = /opt/mongodb/data/rs1_1/

logpath = /opt/mongodb/log/sharding1_1.log

logappend = true

port = 27017 

rest = true

shardsvr = true

replSet = shard1 

fork = true

oplogSize = 4000

directoryperdb = true

nohttpinterface = true

profile = 0 

 

10.15.201.192

#mkdir -p /opt/mongodb/data/rs1_2/

#mkdir -p /opt/mongodb/log/

#mkdir -p /opt/mongodb/bin/configFile

 

rs1_2.conf (手动创建的配置文件名字)

#vi /opt/mongodb/bin/configFile/rs1_2.conf

dbpath = /opt/mongodb/data/rs1_2/

logpath = /opt/mongodb/log/sharding1_2.log

logappend = true

port = 27017 

rest = true

shardsvr = true

replSet = shard1 

fork = true

oplogSize = 4000

directoryperdb = true

nohttpinterface = true

profile = 0

 

10.15.201.193

#mkdir -p /opt/mongodb/data/rs1_3/

#mkdir -p /opt/mongodb/log/

#mkdir -p /opt/mongodb/bin/configFile

 

rs1_3.conf (手动创建的配置文件名字)

#vi /opt/mongodb/bin/configFile/rs1_3.conf

dbpath = /opt/mongodb/data/rs1_3

logpath = /opt/mongodb/log/sharding1_3.log

logappend = true

port = 27017 

rest = true

shardsvr = true

replSet = shard1 

fork = true

oplogSize = 4000

directoryperdb = true

nohttpinterface = true

profile = 0

 

4.1.1 添加副本集及初始化

启动mongodb(启动文件在/opt/mongodb/bin/下)

10.15.201.191

#./mongod --config /opt/mongodb/bin/configFile/rs1_1.conf

启动后需要时间初始化

2014-05-14T13:33:08.279+0800 ** WARNING: Should not specify both --rest and --nohttpinterface

about to fork child process, waiting until server is ready for connections.

forked process: 1591

child process started successfully, parent exiting

查看日志信息,可以看到控制台上显示副本集还没有配置初始化信息

 2014-05-14T13:33:25.075+0800 [initandlisten]command local.$cmd command: create { create: "startup_log", size: 10485760, capped: true } ntoreturn:1 keyUpdates:0 numYields:0  reslen:37 455ms

2014-05-14T13:33:25.076+0800 [initandlisten] waiting for connections on port 27017

2014-05-14T13:33:26.082+0800 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)

2014-05-14T13:33:27.083+0800 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)

2014-05-14T13:33:28.086+0800 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)

依次启动10.15.201.19210.15.201.193

10.15.201.192

#./mongod --config /opt/mongodb/bin/configFile/rs1_2.conf

10.15.201.193

#./mongod --config /opt/mongodb/bin/configFile/rs1_3.conf

注册副本集

10.15.201.191

./mongo  10.15.201.191:27017   进入数据库

#使用admin数据库

use admin

#配置configpriority参数为优先级

>config = {_id: 'shard1', members: [{_id: 0, host: '10.15.201.191:27017 ', priority: 90},{_id: 1, host: '10.15.201.192:27017 ', priority: 80},{_id: 2, host: '10.15.201.193:27017 ', priority: 70}]}

 

{

        "_id" : "shard1",

        "members" : [

                {

                        "_id" : 0,

                        "host" : "10.15.201.191:27017 ",

                        "priority" : 90

                },

                {

                        "_id" : 1,

                        "host" : "10.15.201.192:27017 ",

                        "priority" : 80

                },

                {

                        "_id" : 2,

                        "host" : "10.15.201.193:27017 ",

                        "priority" : 70

                }

        ]

}

 

>rs.initiate(config)                      #初始化配置

{

"info" : "Config now saved locally. Should come online in about a minute.",

"ok" : 1

}

>rs.conf()      #查看配置信息

{

        "_id" : "shard1",

        "version" : 1,

        "members" : [

                {

                        "_id" : 0,

                        "host" : "10.15.201.191:27017",

                        "priority" : 90

                },

                {

                        "_id" : 1,

                        "host" : "10.15.201.192:27017",

                        "priority" : 80

                },

                {

                        "_id" : 2,

                        "host" : "10.15.201.193:27017",

                        "priority" : 70

                }

        ]

}

>rs.status()   #查看副本集

{

        "set" : "shard1",

        "date" : ISODate("2014-05-14T05:49:34Z"),

        "myState" : 1,

        "members" : [

                {

                        "_id" : 0,

                        "name" : "10.15.201.191:27017",

                        "health" : 1,

                        "state" : 1,

                        "stateStr" : "PRIMARY",

                        "uptime" : 986,

                        "optime" : Timestamp(1400046545, 1),

                        "optimeDate" : ISODate("2014-05-14T05:49:05Z"),

                        "electionTime" : Timestamp(1400046564, 1),

                        "electionDate" : ISODate("2014-05-14T05:49:24Z"),

                        "self" : true

                },

                {

                        "_id" : 1,

                        "name" : "10.15.201.192:27017",

                        "health" : 1,

                        "state" : 5,

                        "stateStr" : "STARTUP2",

                        "uptime" : 28,

                        "optime" : Timestamp(0, 0),

                        "optimeDate" : ISODate("1970-01-01T00:00:00Z"),

                        "lastHeartbeat" : ISODate("2014-05-14T05:49:32Z"),

                        "lastHeartbeatRecv" : ISODate("2014-05-14T05:49:32Z"),

                        "pingMs" : 1465

                },

                {

                        "_id" : 2,

                        "name" : "10.15.201.193:27017",

                        "health" : 1,

                        "state" : 5,

                        "stateStr" : "STARTUP2",

                        "uptime" : 28,

                        "optime" : Timestamp(0, 0),

                        "optimeDate" : ISODate("1970-01-01T00:00:00Z"),

                        "lastHeartbeat" : ISODate("2014-05-14T05:49:34Z"),

                        "lastHeartbeatRecv" : ISODate("2014-05-14T05:49:34Z"),

                        "pingMs" : 0

                }

        ],

        "ok" : 1

}

以上内容显示mongdb副本集注册成功。

4.1.2 模拟故障

假设主mongodb停止服务,故障模拟过程中,我们手动关闭主mongdb,当主mongdb服务停止时,观察从mongdb变化。

进入mongdb数据库中关闭mongodb

shard1:PRIMARY> use admin;

switched to db admin

shard1:PRIMARY> db.shutdownServer();

2014-05-14T15:07:09.834+0800 DBClientCursor::init call() failed

server should be down...

2014-05-14T15:07:09.838+0800 trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed

2014-05-14T15:07:09.839+0800 reconnect 127.0.0.1:27017 (127.0.0.1) ok

shard1:SECONDARY> ^C

或者直接杀死主mongdb进程。(官方建议用kill -2杀死进程,不建议用kill -9

在其余两台从mongodb上运行rs.status()

shard1:PRIMARY> rs.status();

{

        "set" : "shard1",

        "date" : ISODate("2014-05-15T00:28:34Z"),

        "myState" : 1,

        "members" : [

                {

                        "_id" : 0,

                        "name" : "10.15.201.191:27017",

                        "health" : 0,      #0为下线

                        "state" : 8,

                        "stateStr" : "(not reachable/healthy)",

                        "uptime" : 0,

                        "optime" : Timestamp(1400113499, 1),

                        "optimeDate" : ISODate("2014-05-15T00:24:59Z"),

                        "lastHeartbeat" : ISODate("2014-05-15T00:28:34Z"),

                        "lastHeartbeatRecv" : ISODate("2014-05-15T00:27:26Z"),

                        "pingMs" : 0

                },

                {

                        "_id" : 1,

                        "name" : "10.15.201.192:27017",

                        "health" : 1,

                        "state" : 1,

                        "stateStr" : "PRIMARY",

                        "uptime" : 68069,

                        "optime" : Timestamp(1400113499, 1),

                        "optimeDate" : ISODate("2014-05-15T00:24:59Z"),

                        "electionTime" : Timestamp(1400113655, 1),

                        "electionDate" : ISODate("2014-05-15T00:27:35Z"),

                        "self" : true

                },

                {

                        "_id" : 2,

                        "name" : "10.15.201.193:27017",

                        "health" : 1,

                        "state" : 2,

                        "stateStr" : "SECONDARY",

                        "uptime" : 67158,

                        "optime" : Timestamp(1400113499, 1),

                        "optimeDate" : ISODate("2014-05-15T00:24:59Z"),

                        "lastHeartbeat" : ISODate("2014-05-15T00:28:34Z"),

                        "lastHeartbeatRecv" : ISODate("2014-05-15T00:28:34Z"),

                        "pingMs" : 0,

                        "syncingTo" : "10.15.201.192:27017"

                }

        ],

        "ok" : 1

}

实验结论:

可以看出10.15.201.192:27017自动升级为主,同时在10.15.201.193:27017已经切换10.15.201.192为主服务。

在以上模拟环境基础上继续操作,当10.15.201.191:27017恢复后

启动之前手动关闭的主mongdb程序

[root@localhost bin]# ./mongod --config /opt/mongodb/bin/configFile/rs1_1.conf

2014-05-15T08:35:29.627+0800 ** WARNING: Should not specify both --rest and --nohttpinterface

about to fork child process, waiting until server is ready for connections.

forked process: 13187

child process started successfully, parent exiting

[root@localhost bin]# ./mongo

MongoDB shell version: 2.6.1

connecting to: test

Server has startup warnings: 

2014-05-15T08:35:29.627+0800 ** WARNING: Should not specify both --rest and --nohttpinterface

shard1:SECONDARY

实验结论:

进入刚启动的mongdb数据库,通过前缀可发现已经自动以10.15.201.192为主库,10.15.201.191自动降为从库。

经过一段时间的同步后,3个数据库完成数据一致性,再查看数据库状态。

[root@localhost bin]# ./mongo

MongoDB shell version: 2.6.1

connecting to: test

Server has startup warnings: 

2014-05-15T08:35:29.627+0800 ** WARNING: Should not specify both --rest and --nohttpinterface

shard1:SECONDARY> ^C

bye

[root@localhost bin]# ./mongo

MongoDB shell version: 2.6.1

connecting to: test

Server has startup warnings: 

2014-05-15T08:35:29.627+0800 ** WARNING: Should not specify both --rest and --nohttpinterface

shard1:PRIMARY> ^C

bye

[root@localhost bin]# ./mongo

MongoDB shell version: 2.6.1

connecting to: test

Server has startup warnings: 

2014-05-15T08:35:29.627+0800 ** WARNING: Should not specify both --rest and --nohttpinterface

shard1:PRIMARY> rs.status;rs.status;

function () { return db._adminCommand("replSetGetStatus"); }

shard1:PRIMARY> rs.status();rs.status();

{

        "set" : "shard1",

        "date" : ISODate("2014-05-15T00:39:03Z"),

        "myState" : 1,

        "members" : [

                {

                        "_id" : 0,

                        "name" : "10.15.201.191:27017",

                        "health" : 1,

                        "state" : 1,

                        "stateStr" : "PRIMARY",

                        "uptime" : 214,

                        "optime" : Timestamp(1400113499, 1),

                        "optimeDate" : ISODate("2014-05-15T00:24:59Z"),

                        "electionTime" : Timestamp(1400114137, 1),

                        "electionDate" : ISODate("2014-05-15T00:35:37Z"),

                        "self" : true

                },

                {

                        "_id" : 1,

                        "name" : "10.15.201.192:27017",

                        "health" : 1,

                        "state" : 2,

                        "stateStr" : "SECONDARY",

                        "uptime" : 214,

                        "optime" : Timestamp(1400113499, 1),

                        "optimeDate" : ISODate("2014-05-15T00:24:59Z"),

                        "lastHeartbeat" : ISODate("2014-05-15T00:39:01Z"),

                        "lastHeartbeatRecv" : ISODate("2014-05-15T00:39:02Z"),

                        "pingMs" : 0,

                        "syncingTo" : "10.15.201.191:27017"

                },

                {

                        "_id" : 2,

                        "name" : "10.15.201.193:27017",

                        "health" : 1,

                        "state" : 2,

                        "stateStr" : "SECONDARY",

                        "uptime" : 214,

                        "optime" : Timestamp(1400113499, 1),

                        "optimeDate" : ISODate("2014-05-15T00:24:59Z"),

                        "lastHeartbeat" : ISODate("2014-05-15T00:39:01Z"),

                        "lastHeartbeatRecv" : ISODate("2014-05-15T00:39:01Z"),

                        "pingMs" : 0,

                        "syncingTo" : "10.15.201.192:27017"

                }

        ],

        "ok" : 1

}

实验结果:

现在数据库状态为:

10.15.201.191为主库,可读可写。

shard1:PRIMARY> db.testdb.insert({"test5":"testval5"})

WriteResult({ "nInserted" : 1 })

10.15.201.192为从库,只读,其主库为10.15.201.191

shard1:SECONDARY> db.testdb.insert({"test6":"testval6"})

WriteResult({ "writeError" : { "code" : undefined, "errmsg" : "not master" } })

10.15.201.193为从库,只读,其主库为10.15.201.192

shard1:SECONDARY> db.testdb.insert({"test6":"testval6"})

WriteResult({ "writeError" : { "code" : undefined, "errmsg" : "not master" } })

 

4.2 配置monogdb主从

注:mongdb主从配置和上文中的副本集部署互不影响,可以把以上操作清空从新配置

以下配置文件中参数解释可参考下文资料参考模块

10.15.201.191

#mkdir -p /opt/mongodb/data/

#mkdir -p /opt/mongodb/log/

#mkdir -p /opt/mongodb/bin/configFile

runMaster.conf (手动创建master server 配置文件)

#vi /opt/mongodb/bin/configFile/runMaster.conf

##MongoDB Server config ---Master---

 

bind_ip = 10.15.201.191

port = 27017

fork = true 

master = true

logappend = true

journal = true

dbpath = ../data/

logpath = ../log/mongodb_Master.log 

directoryperdb = true

oplogSize = 4096

 

10.15.201.192

#mkdir -p /opt/mongodb/data/

#mkdir -p /opt/mongodb/log/

#mkdir -p /opt/mongodb/bin/configFile

 

runSlave.conf (手动创建slave server 配置文件)

#vi /opt/mongodb/bin/configFile/runSlave.conf

##MongoDB Server config ---Slave---

 

bind_ip = 10.15.201.192

port = 27017

fork = true 

slave = true

source = 10.15.201.191:27017

logappend = true

journal = true

dbpath = ../data/

logpath = ../log/mongodb.log 

directoryperdb = true

oplogSize = 4096

 

启动mongodb

10.15.201.191 master

#./mongod --config /opt/mongodb/bin/configFile/runMaster.conf

显示以下信息启动成功:

[root@localhost bin]# ./mongod --config /opt/mongodb/bin/configFile/runMaster.conf 

about to fork child process, waiting until server is ready for connections.

forked process: 14079

child process started successfully, parent exiting

初始化数据库日志:

[root@localhost log]# more mongodb_Master.log 

2014-05-15T09:52:00.886+0800 [initandlisten] MongoDB starting : pid=13519 port=27017 dbpath=/opt/mongodb/bin/../data/ master=1 64-bit host=localhost.localdomain

2014-05-15T09:52:00.886+0800 [initandlisten] db version v2.6.1

2014-05-15T09:52:00.886+0800 [initandlisten] git version: 4b95b086d2374bdcfcdf2249272fb552c9c726e8

2014-05-15T09:52:00.886+0800 [initandlisten] build info: Linux build14.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSI

ON=1_49

2014-05-15T09:52:00.886+0800 [initandlisten] allocator: tcmalloc

2014-05-15T09:52:00.886+0800 [initandlisten] options: { config: "/opt/mongodb/bin/configFile/runMaster.conf", master: true, net: { bindIp: "10.15.201.191", port: 27017

 }, processManagement: { fork: true }, replication: { oplogSizeMB: 4096 }, storage: { dbPath: "../data/", directoryPerDB: true, journal: { enabled: true } }, systemLog

: { destination: "file", logAppend: true, path: "../log/mongodb_Master.log" } }

2014-05-15T09:52:00.896+0800 [initandlisten] journal dir=/opt/mongodb/bin/../data/journal

2014-05-15T09:52:00.896+0800 [initandlisten] recover : no journal files present, no recovery needed

2014-05-15T09:52:01.301+0800 [initandlisten] preallocateIsFaster=true 7.8

2014-05-15T09:52:01.719+0800 [initandlisten] preallocateIsFaster=true 7.92

2014-05-15T09:52:03.140+0800 [initandlisten] preallocateIsFaster=true 7.94

2014-05-15T09:52:03.140+0800 [initandlisten] preallocating a journal file /opt/mongodb/bin/../data/journal/prealloc.0

2014-05-15T09:52:08.304+0800 [initandlisten] preallocating a journal file /opt/mongodb/bin/../data/journal/prealloc.1

2014-05-15T09:52:15.292+0800 [initandlisten] preallocating a journal file /opt/mongodb/bin/../data/journal/prealloc.2

2014-05-15T09:52:21.561+0800 [FileAllocator] allocating new datafile /opt/mongodb/bin/../data/local/local.ns, filling with zeroes...

2014-05-15T09:52:21.562+0800 [FileAllocator] creating directory /opt/mongodb/bin/../data/local/_tmp

2014-05-15T09:52:21.646+0800 [FileAllocator] done allocating datafile /opt/mongodb/bin/../data/local/local.ns, size: 16MB,  took 0.074 secs

2014-05-15T09:52:21.651+0800 [FileAllocator] allocating new datafile /opt/mongodb/bin/../data/local/local.0, filling with zeroes...

2014-05-15T09:52:21.973+0800 [FileAllocator] done allocating datafile /opt/mongodb/bin/../data/local/local.0, size: 64MB,  took 0.32 secs

2014-05-15T09:52:21.974+0800 [initandlisten] build index on: local.startup_log properties: { v: 1, key: { _id: 1 }, name: "_id_", ns: "local.startup_log" }

2014-05-15T09:52:21.975+0800 [initandlisten]     added index to empty collection

2014-05-15T09:52:21.975+0800 [initandlisten] command local.$cmd command: create { create: "startup_log", size: 10485760, capped: true } ntoreturn:1 keyUpdates:0 numYie

lds:0  reslen:37 413ms

2014-05-15T09:52:21.975+0800 [initandlisten] ******

2014-05-15T09:52:21.976+0800 [initandlisten] creating replication oplog of size: 4096MB...

2014-05-15T09:52:21.980+0800 [FileAllocator] allocating new datafile /opt/mongodb/bin/../data/local/local.1, filling with zeroes...

2014-05-15T09:52:34.573+0800 [FileAllocator] done allocating datafile /opt/mongodb/bin/../data/local/local.1, size: 2047MB,  took 12.593 secs

2014-05-15T09:52:34.574+0800 [initandlisten] ExtentManager took 12 seconds to open: /opt/mongodb/bin/../data/local/local.1

2014-05-15T09:52:34.574+0800 [FileAllocator] allocating new datafile /opt/mongodb/bin/../data/local/local.2, filling with zeroes...

 

启动从mongdb

10.15.201.192 slave

#./mongod --config /opt/mongodb/bin/configFile/runSlave.conf

启动数据库后,初始化完毕,已连接到主库上。

2014-05-15T09:57:39.205+0800 [replslave] build index on: local.me properties: { v: 1, key: { _id: 1 }, name: "_id_", ns: "local.me" }

2014-05-15T09:57:39.205+0800 [replslave]         added index to empty collection

2014-05-15T09:57:39.206+0800 [replslave] build index on: local.sources properties: { v: 1, key: { _id: 1 }, name: "_id_", ns: "local.sources" }

2014-05-15T09:57:39.206+0800 [replslave]         added index to empty collection

2014-05-15T09:57:39.207+0800 [replslave] repl: syncing from host:10.15.201.191:27017

2014-05-15T09:58:37.630+0800 [clientcursormon] mem (MB) res:31 virt:434

2014-05-15T09:58:37.630+0800 [clientcursormon]  mapped (incl journal view):160

2014-05-15T09:58:37.630+0800 [clientcursormon]  connections:0

 

查看建立连接后,主库日志

2014-05-15T09:52:46.223+0800 [initandlisten] ******

2014-05-15T09:52:46.324+0800 [initandlisten] waiting for connections on port 27017

2014-05-15T09:53:42.249+0800 [clientcursormon] mem (MB) res:31 virt:8612

2014-05-15T09:53:42.249+0800 [clientcursormon]  mapped (incl journal view):8348

2014-05-15T09:53:42.249+0800 [clientcursormon]  connections:0

2014-05-15T09:57:39.208+0800 [initandlisten] connection accepted from 10.15.201.192:35586 #1 (1 connection now open)

2014-05-15T09:57:40.215+0800 [slaveTracking] build index on: local.slaves properties: { v: 1, key: { _id: 1 }, name: "_id_", ns: "local.slaves" }

2014-05-15T09:57:40.215+0800 [slaveTracking]     added index to empty collection

[root@localhost log]# tailf mongodb_Master.log 

2014-05-15T09:52:45.751+0800 [FileAllocator] done allocating datafile /opt/mongodb/bin/../data/local/local.2, size: 2047MB,  took 11.176 secs

2014-05-15T09:52:45.751+0800 [initandlisten] ExtentManager took 11 seconds to open: /opt/mongodb/bin/../data/local/local.2

 

4.2.1 Mongdb数据库内基本操作

/mongo  10.15.201.191:27017   进入数据库

查看当前数据是主还是从

10.15.201.191

> db.runCommand({"isMaster":1})

{

        "ismaster" : true,

        "maxBsonObjectSize" : 16777216,

        "maxMessageSizeBytes" : 48000000,

        "maxWriteBatchSize" : 1000,

        "localTime" : ISODate("2014-05-15T02:04:55.771Z"),

        "maxWireVersion" : 2,

        "minWireVersion" : 0,

        "ok" : 1

}

10.15.201.192

> db.runCommand({"isMaster":1})db.runCommand({"isMaster":1})

{

        "ismaster" : false,

        "maxBsonObjectSize" : 16777216,

        "maxMessageSizeBytes" : 48000000,

        "maxWriteBatchSize" : 1000,

        "localTime" : ISODate("2014-05-15T02:05:22.109Z"),

        "maxWireVersion" : 2,

        "minWireVersion" : 0,

        "ok" : 1

}

简单测试主从使用是否正常

#在主库10.15.201.191 上连接到终端:

mongo 10.15.201.191:27017

#建立test 数据库。

use test;

testdb表插入数据。

> db.testdb.insert({"test1":"testval1"})

 

#在从本库 10.15.201.191 上连接到mongodb查看数据是否复制过来。

mongo 10.15.201.191:27017

#使用test 数据库。

> use test;

> show tables;

> db.testdb.find();

{ "_id" : ObjectId("537421d1b984120be13fdb94"), "test1" : "testval1" }

 

4.2.2 测试从库读写性

向从库插入数据(日志反映,只读权限)

> db.testdb.insert({"test2":"testval2"})

WriteResult({ "writeError" : { "code" : undefined, "errmsg" : "not master" } })

4.2.3 新添加一个从库

10.15.201.193

#mkdir -p /opt/mongodb/data/

#mkdir -p /opt/mongodb/log/

#mkdir -p /opt/mongodb/bin/configFile

 

runSlave.conf

#vi /opt/mongodb/bin/configFile/runSlave.conf

##MongoDB Server config ---Slave---

 

bind_ip = 10.15.201.193

port = 27017

fork = true 

slave = true

logappend = true

journal = true

dbpath = ../data/

logpath = ../log/mongodb.log 

directoryperdb = true

oplogSize = 4096

 

启动后手动添加主库

>use  local 

>db.sources.insert ( { "host":  "10.15.201.193:27017" } ); 

WriteResult({ "nInserted" : 1 })

 

添加从库之后查看同步情况

[root@localhost bin]# ./mongo 10.15.201.193:27017

MongoDB shell version: 2.6.1

connecting to: 10.15.201.193:27017/test

> db.sources.find ( ); 

> show tables;

system.indexes

testdb

>  db.testdb.find(); db.testdb.find();

{ "_id" : ObjectId("537421d1b984120be13fdb94"), "test1" : "testval1" }

结果显示:

新添加的从库中信息已经和10.15.201.191主库中测试信息同步成功。

4.2.4 模拟故障

模拟主库宕机,从库我们要手动设置。

关闭从库10.15.201.192

>db.shutdownServer();db.shutdownServer();

修改配置文件

#vi /opt/mongodb/bin/configFile/runSlave.conf

##MongoDB Server config ---Slave---

 

bind_ip = 10.15.201.192

port = 27017

fork = true 

slave = true     #改为master = true 

source = 10.15.201.191:27017   #删除

logappend = true

journal = true

dbpath = ../data/

logpath = ../log/mongodb.log 

directoryperdb = true

oplogSize = 4096

 

删除 10.15.201.192 数据目录中的 local.*

rm  -rf  /opt/mongodb /data /local. * 

重新启动10.15.201.192上的mongodb

从库10.15.201.19切换source

db.sources.insert ( { "host":  "10.15.201.192:27017" } ); 

db.sources.remove ( { "host":  "10.15.201.193:27017" } ); 

登陆10.15.201.19210.15.201.193重新查看哪个是主哪个是从

进入数据库之后执行以下命令:

db.runCommand({"isMaster":1})

> db.runCommand({"isMaster":1})db.runCommand({"isMaster":1})

{

        "ismaster" : true,

        "maxBsonObjectSize" : 16777216,

        "maxMessageSizeBytes" : 48000000,

        "maxWriteBatchSize" : 1000,

        "localTime" : ISODate("2014-05-16T03:28:06.107Z"),

        "maxWireVersion" : 2,

        "minWireVersion" : 0,

        "ok" : 1

}

 

> db.runCommand({"isMaster":1})db.runCommand({"isMaster":1})

{

        "ismaster" : false,

        "maxBsonObjectSize" : 16777216,

        "maxMessageSizeBytes" : 48000000,

        "maxWriteBatchSize" : 1000,

        "localTime" : ISODate("2014-05-16T03:28:09.481Z"),

        "maxWireVersion" : 2,

        "minWireVersion" : 0,

        "ok" : 1

}

 

参考资料

参数说明

Mongodb启动命令mongod参数说明

 

mongod的主要参数有:

 基本配置

--quiet # 安静输出 

--port arg # 指定服务端口号,默认端口27017 

--bind_ip arg # 绑定服务IP,若绑定127.0.0.1,则只能本机访问,不指定默认本地所有IP 

--logpath arg # 指定MongoDB日志文件,注意是指定文件不是目录 

--logappend # 使用追加的方式写日志 

--pidfilepath arg # PID File 的完整路径,如果没有设置,则没有PID文件 

--keyFile arg # 集群的私钥的完整路径,只对于Replica Set 架构有效 

--unixSocketPrefix arg # UNIX域套接字替代目录,(默认为 /tmp) 

--fork # 以守护进程的方式运行MongoDB,创建服务器进程 

--auth # 启用验证 

--cpu # 定期显示CPUCPU利用率和iowait 

--dbpath arg # 指定数据库路径 

--diaglog arg # diaglog选项 0=off 1=W 2=R 3=both 7=W+some reads 

--directoryperdb # 设置每个数据库将被保存在一个单独的目录 

--journal # 启用日志选项,MongoDB的数据操作将会写入到journal文件夹的文件里 

--journalOptions arg # 启用日志诊断选项 

--ipv6 # 启用IPv6选项 

--jsonp # 允许JSONP形式通过HTTP访问(有安全影响) 

--maxConns arg # 最大同时连接数 默认2000 

--noauth # 不启用验证 

--nohttpinterface # 关闭http接口,默认关闭27018端口访问 

--noprealloc # 禁用数据文件预分配(往往影响性能

--noscripting # 禁用脚本引擎 

--notablescan # 不允许表扫描 

--nounixsocket # 禁用Unix套接字监听 

--nssize arg (=16) # 设置信数据库.ns文件大小(MB) 

--objcheck # 在收到客户数据,检查的有效性, 

--profile arg # 档案参数 0=off 1=slow, 2=all 

--quota # 限制每个数据库的文件数,设置默认为

--quotaFiles arg # number of files allower per db, requires --quota 

--rest # 开启简单的rest API 

--repair # 修复所有数据库run repair on all dbs 

--repairpath arg # 修复库生成的文件的目录,默认为目录名称dbpath 

--slowms arg (=100) # value of slow for profile and console log 

--smallfiles # 使用较小的默认文件 

--syncdelay arg (=60) # 数据写入磁盘的时间秒数(0=never,不推荐

--sysinfo # 打印一些诊断系统信息 

--upgrade # 如果需要升级数据库 

 * Replicaton 参数

 

--------------------------------------------------------------------------------

 

 

--fastsync # 从一个dbpath里启用从库复制服务,该dbpath的数据库是主库的快照,可用于快速启用同步 

--autoresync # 如果从库与主库同步数据差得多,自动重新同步, 

--oplogSize arg # 设置oplog的大小(MB) 

 * /从参数

 

--------------------------------------------------------------------------------

 

 

--master # 主库模式 

--slave # 从库模式 

--source arg # 从库 端口号 

--only arg # 指定单一的数据库复制 

--slavedelay arg # 设置从库同步主库的延迟时间 

 * Replica set(副本集)选项:

 

--------------------------------------------------------------------------------

 

 

--replSet arg # 设置副本集名称 

 * Sharding(分片)选项

 

--------------------------------------------------------------------------------

 

--configsvr # 声明这是一个集群的config服务,默认端口27019,默认目录/data/configdb 

--shardsvr # 声明这是一个集群的分片,默认端口27018 

--noMoveParanoia # 关闭偏执为moveChunk数据保存 

 

参考网址:

主从:

http://blog.chinaunix.net/uid-90618-id-4082567.html

 

副本集:

http://www.csdn.net/article/2014-04-09/2819221-build-high-avialable-mongodb-cluster-part-1/2

 

启动说明:

http://blog.csdn.net/fdipzone/article/details/7442162

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值