mongod linux上常用操作

  1. 使用下面命令可以查看服务的启动参数
 mongod --help  或者mongod -h 
  1. 其他命令参数
 * 基本配置 

 --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                            # 定期显示CPU的CPU利用率和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                          # 限制每个数据库的文件数,设置默认为8 
 --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数据保存? 

 

#上述参数都可以写入mongod.conf配置文档里例如:

dbpath = /data/mongodb

logpath = /data/mongodb/mongodb.log

logappend = true

port = 27017

fork = true

auth = true

 

监控(mongostat)

Use the mongostat utility to quickly view statistics on a running mongod instance.

Run mongostat --help for help.

Fields:
   insert       - # of inserts per second (* means replicated op)
   query        - # of queries per second
   update       - # of updates per second
   delete       - # of deletes per second
   getmore      - # of get mores (cursor batch) per second
   command      - # of commands per second (on a slave, it's local|replicated)
   flushes      - # of fsync flushes per second
   mapped       - amount of data mmaped (total data size) megabytes
   vsize        - virtual size of process in megabytes
   res          - resident size of process in megabytes
   faults       - # of pages faults/sec (linux only)
   locked       - percent of time in global write lock
   idx miss     - percent of btree page misses (sampled)
   qr | qw      - queue lengths for
 clients waiting (read|write)
   ar | aw      - active clients (read|write)
   netIn        - network traffic in - bits 
   netOut       - network traffic out - bits
   conn         - number of open connections
   set          - replica set name 
   repl         - replication type 
                    M    - master
                    SEC  - secondary 
                    REC  - recovering
                    UNK  - unknown
                    SLV  - slave

multiple servers:

mongostat --host a,b,c

find all connected servers (added in 1.7.2):

mongostat --discover (--host optional)

 

 

备份(mongodump

 用法 :

[root@web3 3]# mongodump --help

options:

--help produce help message

-v [ --verbose ] be more verbose (include multiple times for more

verbosity e.g. -vvvvv)

-h [ --host ] arg mongo host to connect to ("left,right" for pairs)

-d [ --db ] arg database to use

-c [ --collection ] arg collection to use (some commands)

-u [ --username ] arg username

-p [ --password ] arg password

--dbpath arg directly access mongod data files in the given path,

instead of connecting to a mongod instance - needs

to lock the data directory, so cannot be used if a

mongod is currently accessing the same path

--directoryperdb if dbpath specified, each db is in a separate

directory

-o [ --out ] arg (=dump) output directory

 


例子:

[root@web3 ~]# mongodump -h 192.168.1.103 -d citys -o /backup/mongobak/3

connected to: 192.168.1.103

DATABASE: citys to /backup/mongobak/3/citys

citys.building to /backup/mongobak/3/citys/building.bson

13650 objects

citys.system.indexes to /backup/mongobak/3/citys/system.indexes.bson

1 objects

 

备份出来的数据是二进制的,已经经过压缩。

 

恢复(mongorestore

[root@web3 3]# mongorestore --help

usage: mongorestore [options] [directory or filename to restore from]

options:

--help produce help message

-v [ --verbose ] be more verbose (include multiple times for more

verbosity e.g. -vvvvv)

-h [ --host ] arg mongo host to connect to ("left,right" for pairs)

-d [ --db ] arg database to use

-c [ --collection ] arg collection to use (some commands)

-u [ --username ] arg username

-p [ --password ] arg password

--dbpath arg directly access mongod data files in the given path,

instead of connecting to a mongod instance - needs to

lock the data directory, so cannot be used if a

mongod is currently accessing the same path

--directoryperdb if dbpath specified, each db is in a separate

directory

--drop drop each collection before import

--objcheck validate object before inserting

 

--drop参数可以在导入之前把collection先删掉。


例子:

[root@web3 3]# mongorestore -h 127.0.0.1 --directoryperdb /backup/mongobak/3/

connected to: 127.0.0.1

/backup/mongobak/3/citys/building.bson

going into namespace [citys.building]

13667 objects

/backup/mongobak/3/citys/system.indexes.bson

going into namespace [citys.system.indexes]

1 objects

 

另外mongodb还提供了mongoexport和mongoimport这两个命令来导出或导入数据,导出的数据是json格式的。也可以实现备份和恢复的功能。

例:

mongoexport -d mixi_top_city_prod -c building_45 -q '{ "uid" : "10832545" }' > mongo_10832545.bson

mongoimport -d mixi_top_city -c building_45 --file mongo_10832545.bson

 

关闭

kill -9可能导致瘫痪

建议killall mongod

 

修复
首先停止mongod服务,删除mongodb.log,也可以备份一下
# rm -rf /data/mongodb/mongodb.log

删除mongodb进程文件
# rm -rf /mongodb/mongod.lock

进行修复
# mongod --repair --dbpath /data/mongodb --repairpath /data/mongodb_repair --port=27017

这时mongodb进程会在/mongodb/repair/目录下储存临时的修复数据库文件,文件目录为“$tmp_repairDatabase_0”所以此目录空间要足够大。
生产环境数据库为100G,修复进行了大约3个半小时,在“/mongodb/repair /$tmp_repairDatabase_0"目录下产生了近30G的数据库临时文件,修复完成后数据库临时文件自动清除。

主从同步

启动主服务器(192.168.61.200

./mongod -dbpath /data/jobcnresume -port 5555 -master

 

启动从服务器

./mongod -slave -source=192.168.61.200:5555 -dbpath=/data/db2 -port 6666 -slavedelay 5

./mongod -slave -source=192.168.61.200:5555 -dbpath=/data/db3 -port 7777 -slavedelay 5

参数:

--source主服务器ip和端口

--autoresync当发现从服务器的数据不是最新时,开始从主服务器请求同步数据

--slavedelay同步延迟,单位:秒

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值