Skip to content

数据库管理工具

mongoexport

创建可读用户

sh
use admin
db.createUser(
	{
		user: "readUser",
		pwd: "passwd",
		roles: ["readAnyDatabase"]
	}
)

操作

sh
# csv
mongoexport --db test --collection user_info --type=csv --fields name,website --out /opt/backups/accounts.csv -u readUser -p passwd --authenticationDatabase admin

# 导出内嵌文档
mongoexport --db test --collection user_info --type=csv --fields name.firtname,name.lastname,website --out /opt/backups/accounts.csv -u readUser -p passwd --authenticationDatabase admin


# json
mongoexport --db test --collection user_info --type=json --fields name,website --out /opt/backups/accounts.json -u readUser -p passwd --authenticationDatabase admin

# 导出所有字段
mongoexport --db test --collection user_info --type=json --out /opt/backups/accounts.json -u readUser -p passwd --authenticationDatabase admin


#使用查询语句导出文档 --query 
mongoexport --db test --collection user_info --type=json --fields name,website --out /opt/backups/accounts.json -u readUser -p passwd --authenticationDatabase admin --query '{balance: {$gte: 100}}'

# --sort --limit --skip
mongoexport --db test --collection user_info --type=json --fields name,website --out /opt/backups/accounts.json -u readUser -p passwd --authenticationDatabase admin --query '{balance: {$gte: 100}}' --sort '{balance: 1}' --limit 3 --skip 1

mongoimport

创建可读写用户

sh
use admin
db.createUser(
	{
		user: "writeUser",
		pwd: "passwd",
		roles: ["readWriteAnyDatabase"]
	}
)

操作

sh
# import csv, --headline 申明第一行不是数据
mongoimport --db test --collection importAccounts --type csv --headerline  --file /opt/backups/accounts.csv -u writeUser -p passwd --authenticationDatabase admin

# 查看
mongo  -u writeUser -p passwd --authenticationDatabase admin --quiet --eval 'db.importAccounts.find()'

# --drop
mongoimport --db test --collection importAccounts --type csv --headerline  --file /opt/backups/accounts.csv --drop -u writeUser -p passwd --authenticationDatabase admin


# --fields 替换头字段,去掉--headerline, 同时删除首行,否则会将首行字段也写入
mongoimport --db test --collection importAccounts --type csv --fields firstName,lastName,balance  --file /opt/backups/accounts.csv --drop -u writeUser -p passwd --authenticationDatabase admin

# 更新就文档--upsertFields
mongoimport --db test --collection importAccounts --type csv --headerline  --file /opt/backups/accounts.csv --drop -u writeUser -p passwd --authenticationDatabase admin --upsertFields name.firstName,balance


#json, 不能有 --headerline --fields
mongoimport --db test --collection importAccounts --drop --type json --file /opt/backups/accounts.json --drop -u writeUser -p passwd --authenticationDatabase admin
	
# 更新就文档--upsertFields
mongoimport --db test --collection importAccounts --drop --type json --file /opt/backups/accounts.json --drop -u writeUser -p passwd --authenticationDatabase admin --upsertFields name.firstName,balance

# --stopOnError
# --maintainInsertionOrder 保持导入顺序

mongostat

显示数据库服务器进程状态,需要有clusterMonitor角色权限

创建clusterMonitor用户

sh
use admin
db.createUser(
	{
		user: "monitorUser",
		pwd: "passwd",
		roles: ["clusterMonitor"]
	}
)

操作

sh
mongostat -u monitorUser -p passwd --authenticationDatabase admin 

# command: 每秒执行的命令书
# dirty,used: 数据库引擎缓存的使用量百分比
# vsize: 虚拟内存使用量M
#res: 常驻内存使用量M
#conn: 连接数
insert query update delete getmore command dirty used flushes vsize  res qrw arw net_in net_out conn                time
    *0    *0     *0     *0       0     1|0  0.0% 0.0%       0 1.50G 141M 0|0 1|0   169b   34.4k    2 Feb  7 16:42:52.764
    *0    *0     *0     *0       0     0|0  0.0% 0.0%       0 1.50G 141M 0|0 1|0   111b   34.2k    2 Feb  7 16:42:53.765
    *0    *0     *0     *0       0     0|0  0.0% 0.0%       0 1.50G 141M 0|0 1|0   110b   33.9k    2 Feb  7 16:42:54.774
    *0    *0     *0     *0       0     1|0  0.0% 0.0%       0 1.50G 141M 0|0 1|0   113b   34.6k    2 Feb  7 16:42:55.764
    *0    *0     *0     *0       0     1|0  0.0% 0.0%       0 1.50G 141M 0|0 1|0   112b   34.3k    2 Feb  7 16:42:56.763
    *0    *0     *0     *0       0     0|0  0.0% 0.0%       0 1.50G 141M 0|0 1|0   111b   34.2k    2 Feb  7 16:42:57.765
    *0    *0     *0     *0       0     1|0  0.0% 0.0%       0 1.50G 141M 0|0 1|0   112b   34.3k    2 Feb  7 16:42:58.764
    *0    *0     *0     *0       0     1|0  0.0% 0.0%       0 1.50G 141M 0|0 1|0   112b   34.3k    2 Feb  7 16:42:59.763
    
    
    
# 每隔3s
mongostat -u monitorUser -p passwd --authenticationDatabase admin 3

# 每隔3s,抓取5次
mongostat -u monitorUser -p passwd --authenticationDatabase admin --rowcount 5 3

#选择监控字段
mongostat -u monitorUser -p passwd --authenticationDatabase admin -o "command,dirty,userd,vsize,res,conn,time"

mongotop

显示各个集合上的读写时间,需要有clusterMonitor角色权限

sh
mongotop -u monitorUser -p passwd --authenticationDatabase admin 

                    ns    total    read    write    2025-02-07T16:50:58Z
    admin.system.roles      0ms     0ms      0ms
    admin.system.users      0ms     0ms      0ms
  admin.system.version      0ms     0ms      0ms
config.system.sessions      0ms     0ms      0ms
   config.transactions      0ms     0ms      0ms
  local.system.replset      0ms     0ms      0ms
         test.accounts      0ms     0ms      0ms
   test.importAccounts      0ms     0ms      0ms
        test.user_info      0ms     0ms      0ms
        
 # 每隔3s
mongotop -u monitorUser -p passwd --authenticationDatabase admin 3

# 每隔3s,抓取5次
mongotop -u monitorUser -p passwd --authenticationDatabase admin --rowcount 5 3