备份( 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 同步延迟,单位:秒