mongDB主从

MongoDB【第二篇】MongodDB主从

官方网站:
http://www.mongodb.org/

MongoDB的一些参数

 

 1 --logpath 日志文件路径
 2 --master 指定为主机器
 3 --slave 指定为从机器
 4 --source 指定主机器的IP地址
 5 --pologSize 命令行参数(与--master一同使用)配置用于存储给从节点可用的更新信息占用的磁盘空间(M为单位),如果不指定这个参数,默认大小为当前可用磁盘空间的5%(64位机器最小值为1G,32位机器为50M)。
 6 --logappend 日志文件末尾添加
 7 --port 启用端口号
 8 --fork 在后台运行
 9 --only 指定只复制哪一个数据库
10 --slavedelay 指从复制检测的时间间隔
11 --auth 是否需要验证权限登录(用户名和密码)
12 -h [ --help ]             show this usage information
13 --version                 show version information
14 -f [ --config ] arg       configuration file specifying additional options
15 --port arg                specify port number 指定端口号
16 --bind_ip arg             local ip address to bind listener - all local ips
17                            bound by default
18 -v [ --verbose ]          be more verbose (include multiple times for more
19                            verbosity e.g. -vvvvv)
20 --dbpath arg (=/data/db/) directory for datafiles    指定数据存放目录
21 --quiet                   quieter output   静默模式
22 --logpath arg             file to send all output to instead of stdout   指定日志存放目录
23 
24 --logappend               appnd to logpath instead of over-writing 指定日志是以追加还是以覆盖的方式写入日志文件
25 
26 --fork                    fork server process   以创建子进程的方式运行
27 --cpu                     periodically show cpu and iowait utilization 周期性的显示cpu和io的使用情况
28 --noauth                  run without security 无认证模式运行
29 --auth                    run with security 认证模式运行
30 --objcheck                inspect client data for validity on receipt 检查客户端输入数据的有效性检查
31 --quota                   enable db quota management   开始数据库配额的管理
32 --quotaFiles arg          number of files allower per db, requires --quota 规定每个数据库允许的文件数
33 --appsrvpath arg          root directory for the babble app server 
34 --nocursors               diagnostic/debugging option 调试诊断选项
35 --nohints                 ignore query hints 忽略查询命中率
36 --nohttpinterface         disable http interface 关闭http接口,默认是28017
37 --noscripting             disable scripting engine 关闭脚本引擎
38 --noprealloc              disable data file preallocation 关闭数据库文件大小预分配
39 --smallfiles              use a smaller default file size 使用较小的默认文件大小
40 --nssize arg (=16)        .ns file size (in MB) for new databases 新数据库ns文件的默认大小
41 --diaglog arg             0=off 1=W 2=R 3=both 7=W+some reads 提供的方式,是只读,只写,还是读写都行,还是主要写+部分的读模式
42 --sysinfo                 print some diagnostic system information 打印系统诊断信息
43 --upgrade                 upgrade db if needed 如果需要就更新数据库
44 --repair                  run repair on all dbs 修复所有的数据库
45 --notablescan             do not allow table scans 不运行表扫描
46 --syncdelay arg (=60)     seconds between disk syncs (0 for never) 系统同步刷新磁盘的时间,默认是60s
47 Replication options:
48 --master              master mode 主复制模式
49 --slave               slave mode 从复制模式
50 --source arg          when slave: specify master as <server:port> 当为从时,指定主的地址和端口
51 --only arg            when slave: specify a single database to replicate 当为从时,指定需要从主复制的单一库
52 --pairwith arg        address of server to pair with
53 --arbiter arg         address of arbiter server 仲裁服务器,在主主中和pair中用到
54 --autoresync          automatically resync if slave data is stale 自动同步从的数据
55 --oplogSize arg       size limit (in MB) for op log 指定操作日志的大小
56 --opIdMem arg         size limit (in bytes) for in memory storage of op ids指定存储操作日志的内存大小
57 Sharding options:
58 --configsvr           declare this is a config db of a cluster 指定shard中的配置服务器
59 --shardsvr            declare this is a shard db of a cluster 指定shard服务器
60 --diaglog:创建一个非常详细的故障排除和各种错误的诊断日志记录。默认0。设置为1,为在dbpath目录里生成一个diaglog.开头的日志文件,他的值如下:

 

 例子:diaglog:

1.Value    Setting
2.0    off. No logging.       #关闭。没有记录。
3.1    Log write operations.  #写操作
4.2    Log read operations.   #读操作
5.3    Log both read and write operations. #读写操作
6.7    Log write and some read operations. #写和一些读操作
正确关闭mongo的方法:
kill -2 pid

 

 



MongoDB 安裝,主从配置

一 MongoDB 安装

 

 1 [root@zabbix_server src]# wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.0.0.tgz
 2 [root@zabbix_server src]# tar xzvf mongodb-linux-x86_64-2.0.0.tgz
 3 [root@zabbix_server src]# mv mongodb-linux-x86_64-2.0.0 /usr/local/mongodb
 4 
 5 [root@zabbix_server src]# mkdir /usr/local/mongodb/data
 6 [root@zabbix_server src]# touch /usr/local/mongodb/logs
 7 
 8 [root@zabbix_server bin]# /usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs --logappend --port=27017 --fork
 9 
10 
11 
12 [root@zabbix_server bin]# ./mongo
13 MongoDB shell version: 2.0.0
14 connecting to: test
15 > use test;
16 switched to db test
17 > exit
18 bye
19 
20 
21 [root@zabbix_server bin]# netstat -anlpt | grep mongo
22 tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 11504/mongod 
23 tcp 0 0 0.0.0.0:28017 0.0.0.0:* LISTEN 11504/mongod

 

 

注意:
如果报错
-bash: /usr/local/mongodb/bin/mongod: cannot execute binary file
说明你的服务器和mongodb 的版本不对应, 如果服务器是64位,下载x86_64的mongodb ,如果服务器是32位的, 下载i686的mongodb/


二 MongoDB 主从配置

1)实验环境
主:192.168.0.14
从:192.168.0.64

2)时间同步
两台机器做时间同步
[root@zabbix_server src]# ntpdate time.windows.com

3)启动服务:方法一
master:192.168.0.14
启动命令
/usr/local/mongodb/bin/mongod --master --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs --logappend --port=27017 --fork

slave:192.168.0.64
启动命令
/usr/local/mongodb/bin/mongod --slave --source 192.168.0.14:27017 --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs --logappend --port=27017 --fork

                  方法二(将启动方法写成配置文件的形式)

vi mongod.conf(主库的配置文件)  

port=27017
dbpath=/usr/local/mongodb01/db
logpath=/usr/local/mongodb01/logs/mongodb.log
logappend=true
journal=true
fork = true
master = true

vi mongod.conf(从库的配置文件)                                                                 

port=27017                                                                                                
dbpath=/usr/local/mongodb02/db                                                                      
logpath=/usr/local/mongodb02/logs/mongodb.log                                                               
logappend=true                                                                                           
journal=true                                                                                                
fork = true                                                                                                                                                                                                 
source = 10.143.79.16:27017

./bin/mongd  -f   mongod.conf  执行后mongo启动

4)测试主从
在主上插入数据
测试1:

 1 测试1:
 2 1 在主库上插入数据
 3 [root@cacti bin]# ./mongo
 4 MongoDB shell version: 2.0.0
 5 connecting to: test
 6 > db.foo.save({"id":123456,"name":'lizonggang'})
 7 > db.foo.find({"id":123456})
 8 { "_id" : ObjectId("51c3f4b21399022afd992f39"), "id" : 123456, "name" : "lizonggang" }
 9 >
10 
11 2 在从上查看数据(已有在主上插入的数据,说明主从成功)
12 [root@zabbix_server bin]# ./mongo
13 MongoDB shell version: 2.0.0
14 connecting to: test
15 > db.foo.find({"id":123456})
16 { "_id" : ObjectId("51c3f4b21399022afd992f39"), "id" : 123456, "name" : "lizonggang" }
17 >

进行数据同步是可能出现错误

Error: error: { "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435 } 此错误是当主库插完数据后,在从库查询的时候出现的

MongoDb的“not master and slaveok=false”错误及解决方法

首先这是正常的,因为SECONDARY是不允许读写的, 在写多读少的应用中,使用Replica Sets来实现读写分离。通过在连接时指定或者在主库指定slaveOk,由Secondary来分担读的压力,Primary只承担写操作。
对于replica set 中的secondary 节点默认是不可读的,

 

解决方法:1.在从进库输入>rs.slaveOk();进行强制同步(不推荐)

              2.在主库上设置 slaveok=ok

                 [mongodb@ligh bin]$ mongo 127.0.0.1:33333

                 MongoDB shell version: 2.0.1
                 connecting to: 127.0.0.1:33333/test

 

PRIMARY> db.getMongo().setSlaveOk(); 此命令在从库冲输入

 

然后可以在从库上测试一下。

 

测试2:

 1 1 在主库上入数据
 2 > use elaindb
 3 switched to db elaindb
 4 > db.test.save({title:"This is a test!"})
 5 > db.test.find();
 6 { "_id" : ObjectId("51c3f8f11a90c8da157c7f64"), "title" : "This is a test!" }
 7 >
 8 
 9 2 在从库上验证数据
10 [root@zabbix_server bin]# ./mongo
11 MongoDB shell version: 2.0.0
12 connecting to: test
13 > use elaindb
14 switched to db elaindb
15 > db.test.find();
16 { "_id" : ObjectId("51c3f8f11a90c8da157c7f64"), "title" : "This is a test!" }
17 > db.printReplicationInfo();
18 this is a slave, printing slave replication info.
19 source: 192.168.0.14:27017
20      syncedTo: Fri Jun 21 2013 14:59:10 GMT+0800 (CST)
21          = 9 secs ago (0hrs)
22 >
23 > db.printSlaveReplicationInfo();
24 source: 192.168.0.14:27017
25      syncedTo: Fri Jun 21 2013 14:59:30 GMT+0800 (CST)
26          = 16 secs ago (0hrs)
27 >

 

测试3:

1 在这个状态下,slave 下是不可以插入数据的
2 > db.test.save({title:"This is slave test!"})
3 not master

 

5) 一些查询状态的语句

1)查询库

 

1 > show dbs;
2 elaindb 0.0625GB
3 local 0.125GB
4 test 0.0625GB

 

2)在从库上查询主库的地址

1 > use local;
2 switched to db local
3 > db.sources.find()
4 { "_id" : ObjectId("51c3f5dd568d10529950b10d"), "host" : "192.168.0.14:27017", "source" : "main", "syncedTo" : { "t" : 1371797300000, "i" : 1 } }
5 >

 

3)查看主从复制状态

1 > db.printReplicationInfo();
2 configured oplog size: 47.6837158203125MB
3 log length start to end: 1375secs (0.38hrs)
4 oplog first event time: Fri Jun 21 2013 14:30:35 GMT+0800 (CST)
5 oplog last event time: Fri Jun 21 2013 14:53:30 GMT+0800 (CST)
6 now: Fri Jun 21 2013 14:53:32 GMT+0800 (CST)
7 >

 

4)查看Collection状态

 1 > db.printCollectionStats();
 2 foo
 3 {
 4     "ns" : "test.foo",
 5     "count" : 1,
 6     "size" : 56,
 7     "avgObjSize" : 56,
 8     "storageSize" : 4096,
 9     "numExtents" : 1,
10     "nindexes" : 1,
11     "lastExtentSize" : 4096,
12     "paddingFactor" : 1,
13     "flags" : 1,
14     "totalIndexSize" : 8176,
15     "indexSizes" : {
16         "_id_" : 8176
17     },
18     "ok" : 1
19 }
20 ---
21 system.indexes
22 {
23     "ns" : "test.system.indexes",
24     "count" : 1,
25     "size" : 64,
26     "avgObjSize" : 64,
27     "storageSize" : 4096,
28     "numExtents" : 1,
29     "nindexes" : 0,
30     "lastExtentSize" : 4096,
31     "paddingFactor" : 1,
32     "flags" : 0,
33     "totalIndexSize" : 0,
34     "indexSizes" : {
35         
36     },
37     "ok" : 1
38 }
39 ---
40 >

 

6)应急 
如果在应用中主挂掉了怎么办?
这个情况就需要,停止从库, 把从库以主库的启动命令起来就可以了。

1)停掉从库

kill -9 进程号

2)删除locat下文件

1 rm mongodb/db/locat.* -rf

 

3)启动从库

1 /usr/local/mongodb/bin/mongod --master --dbpath=/usr/local/mongodb/data 
--logpath=/usr/local/mongodb/logs --logappend --port=27017 --fork

 

6) Mongdb Web 查看界面





7) MongoDB 增,删,改,查

 1 [root@cacti bin]# ./mongo
 2 MongoDB shell version: 2.0.0
 3 connecting to: test
 4 > db.test.insert({ "Name" : "ymind", "age" : 8 }); #在test数据库里插入数据 
 5 > 
 6 > db.test.find({ "Name" : "ymind" } ); #查询数据
 7 { "_id" : ObjectId("51c4187090db4e4751a3cd05"), "Name" : "ymind", "age" : 8 }
 8 > db.test.find({ "age" : 8} ); #查询数据 
 9 { "_id" : ObjectId("51c4187090db4e4751a3cd05"), "Name" : "ymind", "age" : 8 }
10 > 
11 > 
12 > db.test.update({"Name" : "ymind"}, { "$set" : { "age" : "5" } } ); #修改数据
13 > db.test.find({ "Name" : "ymind" } );
14 { "Name" : "ymind", "_id" : ObjectId("51c4187090db4e4751a3cd05"), "age" : "5" }
15 > 
16 > db.test.remove({ "Name" : "ymind" } ); #删除数据 
17 > db.test.find({ "Name" : "ymind" } );
18 > 
19 >

 

posted @ 2016-06-28 11:31  屌丝爱篮球  阅读(414)  评论(0编辑  收藏  举报