mongodb install backup and restore

Unzip the file, create a database folders, log files folders, configure folder

tar -zxf mongodb-linux-x86_64-4.0.10.tgz

cd mongodb-linux-x86_64-4.0.10

mkdir -p data logs etc

Create a configuration file in the etc directory

vim mongodb.conf

dbpath = /u01/app/mongodb-linux-x86_64-4.0.10/data
logpath = /u01/app/mongodb-linux-x86_64-4.0.10/mongodb.log
port = 27017
fork = true
bind_ip = 10.10.10.14

Mongodb start command mongod Parameters

The main parameters mongod are:

 basic configuration

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

--quiet # Quiet output
--port arg # Specify the service port number, the default port 27017
--bind_ip arg # Bind IP services, if binding 127.0.0.1, you can only access the machine, do not specify a default for all local IP
--logpath arg # Specify the MongoDB log file, note that the specified file is not a directory
--logappend # Use additional way to write the log
--pidfilepath arg # Full path to PID File, if not set, no PID file
--keyFile arg # Full path to the private key cluster, is only valid for the Replica Set Architecture
--unixSocketPrefix arg # UNIX domain sockets alternative directory (default is / tmp)
--fork # Way daemon running MongoDB, create a server process
--auth # Enable verification
--cpu # Periodic display of CPU utilization and iowait CPU
--dbpath arg # Specify the database path
--diaglog arg # diaglog选项 0=off 1=W 2=R 3=both 7=W+some reads
--directoryperdb # Settings on each database will be stored in a separate directory
--journal # Enable logging options, MongoDB data manipulation will be written to a file folder in the journal
--journalOptions arg # Enable diagnostic logging options
--ipv6 # Enable IPv6 option
--jsonp # JSONP form allows access through HTTP (there are safety implications)
--maxConns arg # 2000 default maximum number of simultaneous connections
--noauth # No authentication is enabled
--nohttpinterface # Http interface to shut down, turned off by default 27018 port access
--noprealloc # Disable data file pre-allocated (often affect performance)
--noscripting # Disable script engine
--notablescan # Does not allow table scan
--nounixsocket # Disable Unix socket to listen
--nssize arg (=16) # Set letter .ns database file size (MB)
--objcheck # Receipt of the effectiveness of customer data, check,
--profile arg Parameter File # 0 = off 1 = slow, 2 = all
--quota # Limit the number of files per database, setting defaults to 8
--quotaFiles arg # number of files allower per db, requires --quota
--rest # Enable simple rest API
--repair # Repair all databases run repair on all dbs
--repairpath arg Catalog # repair library generated files, the default directory name 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 配置文档里例如:

启动MongoDB

 ./mongod --config /u01/app/mongodb-linux-x86_64-4.0.10/etc/mongodb.conf

 

指定端口号(用以单机区分多实例),使用mongo命令链接MongoDB服务器端

./mongo --host 10.10.0.114 --port 27017

 

 

连接MongoDB

MongoDB备份

 

一,mongodump备份数据库

1,常用命令格

  1. mongodump -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -o 文件存在路径  

如果没有用户谁,可以去掉-u和-p。
如果导出本机的数据库,可以去掉-h。
如果是默认端口,可以去掉--port。
如果想导出所有数据库,可以去掉-d。

2,导出所有数据库

  1. [root@localhost mongodb]# mongodump -h 127.0.0.1 -o /u01/app/mongodb-linux-x86_64-4.0.10/dump/
  2. connected to: 127.0.0.1  
  3. Tue Dec  3 06:15:55.448 all dbs  
  4. Tue Dec  3 06:15:55.449 DATABASE: test     to     /u01/app/mongodb-linux-x86_64-4.0.10/test  
  5. Tue Dec  3 06:15:55.449     test.system.indexes to /u01/app/mongodb-linux-x86_64-4.0.10/test/system.indexes.bson  
  6. Tue Dec  3 06:15:55.450          1 objects  
  7. Tue Dec  3 06:15:55.450     test.posts to /u01/app/mongodb-linux-x86_64-4.0.10//test/posts.bson  
  8. Tue Dec  3 06:15:55.480          0 objects  

3,导出指定数据库

  1. [root@localhost mongodb]# mongodump -h 192.168.1.108 -d tank -o /u01/app/mongodb-linux-x86_64-4.0.10//  
  2. connected to: 192.168.1.108  
  3. Tue Dec  3 06:11:41.618 DATABASE: tank     to     /u01/app/mongodb-linux-x86_64-4.0.10/tank  
  4. Tue Dec  3 06:11:41.623     tank.system.indexes to /u01/app/mongodb-linux-x86_64-4.0.10/tank/system.indexes.bson  
  5. Tue Dec  3 06:11:41.623          2 objects  
  6. Tue Dec  3 06:11:41.623     tank.contact to /u01/app/mongodb-linux-x86_64-4.0.10/tank/contact.bson  
  7. Tue Dec  3 06:11:41.669          2 objects  
  8. Tue Dec  3 06:11:41.670     Metadata for tank.contact to /u01/app/mongodb-linux-x86_64-4.0.10/tank/contact.metadata.json  
  9. Tue Dec  3 06:11:41.670     tank.users to /u01/app/mongodb-linux-x86_64-4.0.10/tank/users.bson  
  10. Tue Dec  3 06:11:41.685          2 objects  
  11. Tue Dec  3 06:11:41.685     Metadata for tank.users to /u01/app/mongodb-linux-x86_64-4.0.10/tank/users.metadata.json  

三,mongorestore还原数据库

1,常用命令格式

  1. mongorestore -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 --drop 文件存在路径  

--drop的意思是,先删除所有的记录,然后恢复。

2,恢复所有数据库到mongodb中

  1. [root@localhost mongodb]# mongorestore /u01/app/mongodb-linux-x86_64-4.0.10//   #这里的路径是所有库的备份路径  

3,还原指定的数据库

  1. [root@localhost mongodb]# mongorestore -d tank /u01/app/mongodb-linux-x86_64-4.0.10//tank/    #tank这个数据库的备份路径  
  2.   
  3. [root@localhost mongodb]# mongorestore -d tank_new  /u01/app/mongodb-linux-x86_64-4.0.10//tank/    #将tank还有tank_new数据库中  

这二个命令,可以实现数据库的备份与还原,文件格式是json和bson的。无法指写到表备份或者还原。

四,mongoexport导出表,或者表中部分字段

1,常用命令格式

  1. mongoexport -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -c 表名 -f 字段 -q 条件导出 --csv -o 文件名  

上面的参数好理解,重点说一下:

-f    导出指字段,以字号分割,-f name,email,age导出name,email,age这三个字段
-q    可以根查询条件导出,-q '{ "uid" : "100" }' 导出uid为100的数据
--csv 表示导出的文件格式为csv的,这个比较有用,因为大部分的关系型数据库都是支持csv,在这里有共同点

2,导出整张表

  1. [root@localhost mongodb]# mongoexport -d tank -c users -o /u01/app/mongodb-linux-x86_64-4.0.10//tank/users.dat  
  2. connected to: 127.0.0.1  
  3. exported 4 records  

3,导出表中部分字段

  1. [root@localhost mongodb]# mongoexport -d tank -c users --csv -f uid,name,sex -o tank/users.csv  
  2. connected to: 127.0.0.1  
  3. exported 4 records  

4,根据条件敢出数据

  1. [root@localhost mongodb]# mongoexport -d tank -c users -q '{uid:{$gt:1}}' -o tank/users.json  
  2. connected to: 127.0.0.1  
  3. exported 3 records  

五,mongoimport导入表,或者表中部分字段

1,常用命令格式

1.1,还原整表导出的非csv文件

  1. mongoimport -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -c 表名 --upsert --drop 文件名  

重点说一下--upsert,其他参数上面的命令已有提到,--upsert 插入或者更新现有数据

1.2,还原部分字段的导出文件

  1. mongoimport -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -c 表名 --upsertFields 字段 --drop 文件名  

--upsertFields根--upsert一样

1.3,还原导出的csv文件

  1. mongoimport -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -c 表名 --type 类型 --headerline --upsert --drop 文件名  

上面三种情况,还可以有其他排列组合的。

2,还原导出的表数据

  1. [root@localhost mongodb]# mongoimport -d tank -c users --upsert tank/users.dat  
  2. connected to: 127.0.0.1  
  3. Tue Dec  3 08:26:52.852 imported 4 objects  

3,部分字段的表数据导入

  1. [root@localhost mongodb]# mongoimport -d tank -c users  --upsertFields uid,name,sex  tank/users.dat  
  2. connected to: 127.0.0.1  
  3. Tue Dec  3 08:31:15.179 imported 4 objects  

4,还原csv文件

    1. [root@localhost mongodb]# mongoimport -d tank -c users --type csv --headerline --file tank/users.csv  
    2. connected to: 127.0.0.1  
    3. Tue Dec  3 08:37:21.961 imported 4 objects 

Guess you like

Origin www.cnblogs.com/wushenghfut/p/11206413.html