2MongoDB

把/etc/passwd导入mongodb里的db5库下的user集合。

# cp /etc/passwd /mdb/

# head -2 /mdb/passwd 查看文件前2行的内容
root:x:0:0:root:/root:/bin/bash 它用:冒号来分隔
bin:x:1:1:bin:/bin:/sbin/nologin

# sed -i 's/:/,/g' /mdb/passwd 用,逗号替换原来的:冒号。也就是用,都逗号来分隔不同字段

# vim /mdb/passwd
name,password,uid,gid,comment,homedir,shell 添加这一行,即字段名
root,x,0,0,root,/root,/bin/bash
... ...
#############################################################################################
# /etc/mongodb/bin/mongoimport --host 192.168.4.50 --port 27050 -d db5 -c user --type=csv --headerline --drop /mdb/passwd
显示如下:
2018-12-05T09:32:40.112+0800 connected to: 192.168.4.50:27050
2018-12-05T09:32:40.112+0800 dropping: db5.user 先删除db5.user,如果它存在的话
2018-12-05T09:32:40.281+0800 imported 43 documents 一共导入43行

或者可以
# /etc/mongodb/bin/mongoimport --host 192.168.4.50 --port 27050 -d db5 -c usertab --type=csv -f name,password,uid,gid,comment,homedir,shell --drop /mdb/passwd
2018-12-05T09:41:36.784+0800 connected to: 192.168.4.50:27050
2018-12-05T09:41:36.785+0800 dropping: db5.usertab
2018-12-05T09:41:36.936+0800 imported 44 documents
#############################################################################################
# linkm
MongoDB shell version v3.6.3
connecting to: mongodb://192.168.4.50:27050/
MongoDB server version: 3.6.3
Server has startup warnings:
2018-12-05T09:06:46.889+0800 I CONTROL [initandlisten]
2018-12-05T09:06:46.889+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-12-05T09:06:46.889+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-12-05T09:06:46.889+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-12-05T09:06:46.889+0800 I CONTROL [initandlisten]
2018-12-05T09:06:46.890+0800 I CONTROL [initandlisten]
2018-12-05T09:06:46.890+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-12-05T09:06:46.890+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-12-05T09:06:46.890+0800 I CONTROL [initandlisten]
2018-12-05T09:06:46.890+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-12-05T09:06:46.890+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-12-05T09:06:46.890+0800 I CONTROL [initandlisten]

> show dbs
admin 0.000GB
config 0.000GB
db1 0.000GB
db2 0.000GB
db5 0.000GB
local 0.000GB

> use db5
switched to db db5

> show tables
user
#############################################################################################
> db.user.find()
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d75"), "name" : "root", "password" : "x", "uid" : 0, "gid" : 0, "comment" : "root", "homedir" : "/root", "shell" : "/bin/bash" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d76"), "name" : "bin", "password" : "x", "uid" : 1, "gid" : 1, "comment" : "bin", "homedir" : "/bin", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d77"), "name" : "daemon", "password" : "x", "uid" : 2, "gid" : 2, "comment" : "daemon", "homedir" : "/sbin", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d78"), "name" : "adm", "password" : "x", "uid" : 3, "gid" : 4, "comment" : "adm", "homedir" : "/var/adm", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d79"), "name" : "lp", "password" : "x", "uid" : 4, "gid" : 7, "comment" : "lp", "homedir" : "/var/spool/lpd", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d7a"), "name" : "sync", "password" : "x", "uid" : 5, "gid" : 0, "comment" : "sync", "homedir" : "/sbin", "shell" : "/bin/sync" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d7b"), "name" : "shutdown", "password" : "x", "uid" : 6, "gid" : 0, "comment" : "shutdown", "homedir" : "/sbin", "shell" : "/sbin/shutdown" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d7c"), "name" : "halt", "password" : "x", "uid" : 7, "gid" : 0, "comment" : "halt", "homedir" : "/sbin", "shell" : "/sbin/halt" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d7d"), "name" : "mail", "password" : "x", "uid" : 8, "gid" : 12, "comment" : "mail", "homedir" : "/var/spool/mail", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d7e"), "name" : "operator", "password" : "x", "uid" : 11, "gid" : 0, "comment" : "operator", "homedir" : "/root", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d7f"), "name" : "games", "password" : "x", "uid" : 12, "gid" : 100, "comment" : "games", "homedir" : "/usr/games", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d80"), "name" : "ftp", "password" : "x", "uid" : 14, "gid" : 50, "comment" : "FTP User", "homedir" : "/var/ftp", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d81"), "name" : "nobody", "password" : "x", "uid" : 99, "gid" : 99, "comment" : "Nobody", "homedir" : "/", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d82"), "name" : "systemd-network", "password" : "x", "uid" : 192, "gid" : 192, "comment" : "systemd Network Management", "homedir" : "/", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d83"), "name" : "dbus", "password" : "x", "uid" : 81, "gid" : 81, "comment" : "System message bus", "homedir" : "/", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d84"), "name" : "polkitd", "password" : "x", "uid" : 999, "gid" : 998, "comment" : "User for polkitd", "homedir" : "/", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d85"), "name" : "libstoragemgmt", "password" : "x", "uid" : 998, "gid" : 996, "comment" : "daemon account for libstoragemgmt", "homedir" : "/var/run/lsm", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d86"), "name" : "rpc", "password" : "x", "uid" : 32, "gid" : 32, "comment" : "Rpcbind Daemon", "homedir" : "/var/lib/rpcbind", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d87"), "name" : "colord", "password" : "x", "uid" : 997, "gid" : 995, "comment" : "User for colord", "homedir" : "/var/lib/colord", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d88"), "name" : "saslauth", "password" : "x", "uid" : 996, "gid" : 76, "comment" : "Saslauthd user", "homedir" : "/run/saslauthd", "shell" : "/sbin/nologin" }
Type "it" for more
#############################################################################################
> it
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d89"), "name" : "abrt", "password" : "x", "uid" : 173, "gid" : 173, "comment" : "", "homedir" : "/etc/abrt", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d8a"), "name" : "rtkit", "password" : "x", "uid" : 172, "gid" : 172, "comment" : "RealtimeKit", "homedir" : "/proc", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d8b"), "name" : "radvd", "password" : "x", "uid" : 75, "gid" : 75, "comment" : "radvd user", "homedir" : "/", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d8c"), "name" : "chrony", "password" : "x", "uid" : 995, "gid" : 993, "comment" : "", "homedir" : "/var/lib/chrony", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d8d"), "name" : "tss", "password" : "x", "uid" : 59, "gid" : 59, "comment" : "Account used by the trousers package to sandbox the tcsd daemon", "homedir" : "/dev/null", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d8e"), "name" : "usbmuxd", "password" : "x", "uid" : 113, "gid" : 113, "comment" : "usbmuxd user", "homedir" : "/", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d8f"), "name" : "geoclue", "password" : "x", "uid" : 994, "gid" : 991, "comment" : "User for geoclue", "homedir" : "/var/lib/geoclue", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d90"), "name" : "qemu", "password" : "x", "uid" : 107, "gid" : 107, "comment" : "qemu user", "homedir" : "/", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d91"), "name" : "rpcuser", "password" : "x", "uid" : 29, "gid" : 29, "comment" : "RPC Service User", "homedir" : "/var/lib/nfs", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d92"), "name" : "nfsnobody", "password" : "x", "uid" : 65534, "gid" : 65534, "comment" : "Anonymous NFS User", "homedir" : "/var/lib/nfs", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d93"), "name" : "setroubleshoot", "password" : "x", "uid" : 993, "gid" : 990, "comment" : "", "homedir" : "/var/lib/setroubleshoot", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d94"), "name" : "pulse", "password" : "x", "uid" : 171, "gid" : 171, "comment" : "PulseAudio System Daemon", "homedir" : "/var/run/pulse", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d95"), "name" : "gdm", "password" : "x", "uid" : 42, "gid" : 42, "comment" : "", "homedir" : "/var/lib/gdm", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d96"), "name" : "gnome-initial-setup", "password" : "x", "uid" : 992, "gid" : 987, "comment" : "", "homedir" : "/run/gnome-initial-setup/", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d97"), "name" : "sshd", "password" : "x", "uid" : 74, "gid" : 74, "comment" : "Privilege-separated SSH", "homedir" : "/var/empty/sshd", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d98"), "name" : "avahi", "password" : "x", "uid" : 70, "gid" : 70, "comment" : "Avahi mDNS/DNS-SD Stack", "homedir" : "/var/run/avahi-daemon", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d99"), "name" : "postfix", "password" : "x", "uid" : 89, "gid" : 89, "comment" : "", "homedir" : "/var/spool/postfix", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d9a"), "name" : "ntp", "password" : "x", "uid" : 38, "gid" : 38, "comment" : "", "homedir" : "/etc/ntp", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d9b"), "name" : "tcpdump", "password" : "x", "uid" : 72, "gid" : 72, "comment" : "", "homedir" : "/", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d9c"), "name" : "lisi", "password" : "x", "uid" : 1000, "gid" : 1000, "comment" : "lisi", "homedir" : "/home/lisi", "shell" : "/bin/bash" }
Type "it" for more
#############################################################################################
> it
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d9d"), "name" : "mysql", "password" : "x", "uid" : 27, "gid" : 27, "comment" : "MySQL Server", "homedir" : "/var/lib/mysql", "shell" : "/bin/false" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d9e"), "name" : "apache", "password" : "x", "uid" : 48, "gid" : 48, "comment" : "Apache", "homedir" : "/usr/share/httpd", "shell" : "/sbin/nologin" }
{ "_id" : ObjectId("5c072ab8d3fc2b2629325d9f"), "name" : "lucy", "password" : "x", "uid" : 1001, "gid" : 1001, "comment" : "", "homedir" : "/home/lucy", "shell" : "/bin/bash" }
>


db.user.find(一次只能显示20行)
it
#############################################################################################
分片信息
config配置信息

不指定文件夹,就在当前目录生成同名目录dump
# cd /mdb
# /etc/mongodb/bin/mongodump --host 192.168.4.50 --port 27050
显示如下:
2018-12-05T09:53:47.187+0800 writing admin.system.version to
2018-12-05T09:53:47.189+0800 done dumping admin.system.version (1 document)
2018-12-05T09:53:47.189+0800 writing db5.usertab to
2018-12-05T09:53:47.189+0800 writing db5.user to
2018-12-05T09:53:47.189+0800 writing db1.t1 to
2018-12-05T09:53:47.190+0800 writing db2.t2 to
2018-12-05T09:53:47.192+0800 done dumping db5.usertab (44 documents)
2018-12-05T09:53:47.192+0800 writing db1.t2 to
2018-12-05T09:53:47.192+0800 done dumping db5.user (43 documents)
2018-12-05T09:53:47.194+0800 done dumping db1.t2 (1 document)
2018-12-05T09:53:47.195+0800 done dumping db1.t1 (17 documents)
2018-12-05T09:53:47.195+0800 done dumping db2.t2 (17 documents)


# ls /mdb
dump passwd t1.csv t1.json 在当前目录生成同名目录dump

# cd dump/
# ls
admin db1 db2 db5 会发现它所有库都备份了,所以生成同名库的目录

# cd db5
# ls
user.bson user.metadata.json usertab.bson usertab.metadata.json 这个是备份的文件user.bson,不能直接用cat查看

# mkdir /mymdb
# /etc/mongodb/bin/mongodump --host 192.168.4.50 --port 27050 -d db5 -c user -o /mydb
2018-12-05T09:54:44.896+0800 writing db5.user to
2018-12-05T09:54:44.897+0800 done dumping db5.user (43 documents)

# cd /mydb
# ls
db5

# cd db5/
# ls
user.bson user.metadata.json

# /etc/mongodb/bin/bsondump user.bson
{"_id":{"$oid":"5c072ab8d3fc2b2629325d75"},"name":"root","password":"x","uid":0,"gid":0,"comment":"root","homedir":"/root","shell":"/bin/bash"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d76"},"name":"bin","password":"x","uid":1,"gid":1,"comment":"bin","homedir":"/bin","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d77"},"name":"daemon","password":"x","uid":2,"gid":2,"comment":"daemon","homedir":"/sbin","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d78"},"name":"adm","password":"x","uid":3,"gid":4,"comment":"adm","homedir":"/var/adm","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d79"},"name":"lp","password":"x","uid":4,"gid":7,"comment":"lp","homedir":"/var/spool/lpd","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d7a"},"name":"sync","password":"x","uid":5,"gid":0,"comment":"sync","homedir":"/sbin","shell":"/bin/sync"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d7b"},"name":"shutdown","password":"x","uid":6,"gid":0,"comment":"shutdown","homedir":"/sbin","shell":"/sbin/shutdown"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d7c"},"name":"halt","password":"x","uid":7,"gid":0,"comment":"halt","homedir":"/sbin","shell":"/sbin/halt"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d7d"},"name":"mail","password":"x","uid":8,"gid":12,"comment":"mail","homedir":"/var/spool/mail","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d7e"},"name":"operator","password":"x","uid":11,"gid":0,"comment":"operator","homedir":"/root","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d7f"},"name":"games","password":"x","uid":12,"gid":100,"comment":"games","homedir":"/usr/games","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d80"},"name":"ftp","password":"x","uid":14,"gid":50,"comment":"FTP User","homedir":"/var/ftp","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d81"},"name":"nobody","password":"x","uid":99,"gid":99,"comment":"Nobody","homedir":"/","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d82"},"name":"systemd-network","password":"x","uid":192,"gid":192,"comment":"systemd Network Management","homedir":"/","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d83"},"name":"dbus","password":"x","uid":81,"gid":81,"comment":"System message bus","homedir":"/","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d84"},"name":"polkitd","password":"x","uid":999,"gid":998,"comment":"User for polkitd","homedir":"/","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d85"},"name":"libstoragemgmt","password":"x","uid":998,"gid":996,"comment":"daemon account for libstoragemgmt","homedir":"/var/run/lsm","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d86"},"name":"rpc","password":"x","uid":32,"gid":32,"comment":"Rpcbind Daemon","homedir":"/var/lib/rpcbind","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d87"},"name":"colord","password":"x","uid":997,"gid":995,"comment":"User for colord","homedir":"/var/lib/colord","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d88"},"name":"saslauth","password":"x","uid":996,"gid":76,"comment":"Saslauthd user","homedir":"/run/saslauthd","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d89"},"name":"abrt","password":"x","uid":173,"gid":173,"comment":"","homedir":"/etc/abrt","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d8a"},"name":"rtkit","password":"x","uid":172,"gid":172,"comment":"RealtimeKit","homedir":"/proc","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d8b"},"name":"radvd","password":"x","uid":75,"gid":75,"comment":"radvd user","homedir":"/","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d8c"},"name":"chrony","password":"x","uid":995,"gid":993,"comment":"","homedir":"/var/lib/chrony","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d8d"},"name":"tss","password":"x","uid":59,"gid":59,"comment":"Account used by the trousers package to sandbox the tcsd daemon","homedir":"/dev/null","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d8e"},"name":"usbmuxd","password":"x","uid":113,"gid":113,"comment":"usbmuxd user","homedir":"/","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d8f"},"name":"geoclue","password":"x","uid":994,"gid":991,"comment":"User for geoclue","homedir":"/var/lib/geoclue","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d90"},"name":"qemu","password":"x","uid":107,"gid":107,"comment":"qemu user","homedir":"/","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d91"},"name":"rpcuser","password":"x","uid":29,"gid":29,"comment":"RPC Service User","homedir":"/var/lib/nfs","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d92"},"name":"nfsnobody","password":"x","uid":65534,"gid":65534,"comment":"Anonymous NFS User","homedir":"/var/lib/nfs","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d93"},"name":"setroubleshoot","password":"x","uid":993,"gid":990,"comment":"","homedir":"/var/lib/setroubleshoot","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d94"},"name":"pulse","password":"x","uid":171,"gid":171,"comment":"PulseAudio System Daemon","homedir":"/var/run/pulse","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d95"},"name":"gdm","password":"x","uid":42,"gid":42,"comment":"","homedir":"/var/lib/gdm","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d96"},"name":"gnome-initial-setup","password":"x","uid":992,"gid":987,"comment":"","homedir":"/run/gnome-initial-setup/","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d97"},"name":"sshd","password":"x","uid":74,"gid":74,"comment":"Privilege-separated SSH","homedir":"/var/empty/sshd","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d98"},"name":"avahi","password":"x","uid":70,"gid":70,"comment":"Avahi mDNS/DNS-SD Stack","homedir":"/var/run/avahi-daemon","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d99"},"name":"postfix","password":"x","uid":89,"gid":89,"comment":"","homedir":"/var/spool/postfix","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d9a"},"name":"ntp","password":"x","uid":38,"gid":38,"comment":"","homedir":"/etc/ntp","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d9b"},"name":"tcpdump","password":"x","uid":72,"gid":72,"comment":"","homedir":"/","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d9c"},"name":"lisi","password":"x","uid":1000,"gid":1000,"comment":"lisi","homedir":"/home/lisi","shell":"/bin/bash"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d9d"},"name":"mysql","password":"x","uid":27,"gid":27,"comment":"MySQL Server","homedir":"/var/lib/mysql","shell":"/bin/false"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d9e"},"name":"apache","password":"x","uid":48,"gid":48,"comment":"Apache","homedir":"/usr/share/httpd","shell":"/sbin/nologin"}
{"_id":{"$oid":"5c072ab8d3fc2b2629325d9f"},"name":"lucy","password":"x","uid":1001,"gid":1001,"comment":"","homedir":"/home/lucy","shell":"/bin/bash"}
2018-12-05T09:55:27.275+0800 43 objects found
#############################################################################################
导入到备份文件到某库下的某个集合
# /etc/mongodb/bin/mongorestore --host 192.168.4.50 --port 27050 -d db5 -c user /mydb/db5/user.bson
2018-12-05T10:20:51.947+0800 checking for collection data in /mydb/db5/user.bson
2018-12-05T10:20:51.949+0800 reading metadata for db5.user from /mydb/db5/user.metadata.json
2018-12-05T10:20:52.227+0800 restoring db5.user from /mydb/db5/user.bson
2018-12-05T10:20:52.290+0800 no indexes to restore
2018-12-05T10:20:52.290+0800 finished restoring db5.user (43 documents)
2018-12-05T10:20:52.290+0800 done


# linkm
> use db5
switched to db db5
> show tables
usertab
> db.usertab.count()
44 原本有44行
> exit

用备份恢复到集合中,如果该集合已经存在且有内容,那么会追加。
# /etc/mongodb/bin/mongorestore --host 192.168.4.50 --port 27050 -d db5 -c usertab /mydb/db5/user.bson
# linkm
> use db5
switched to db db5
> show tables
user
usertab
> db.usertab.count()
87 现在追加后,已经有87行了
################################################################################################
50客户端,51-53运行mongodb服务,里面只有3个原有的库

51-53操作:

> show dbs 51-53里面只有3个原有的库
admin 0.000GB
config 0.000GB
local 0.000GB

# mstop 停止服务
# vim /etc/mongodb/etc/mongodb.conf 改配置
bind_ip=192.168.4.51
port=27051
logpath=/etc/mongodb/log/mongodb.log
logappend=true
dbpath=/etc/mongodb/data/db
fork=true
replSet=rs1 追加这行启动集群,等号后面是集群成功。是rs1数字1

# mstart 成功启动就是配置正确!
# netstat -tunlp | grep mongod 能查看到IP和端口号
tcp 0 0 192.168.4.51:27051 0.0.0.0:* LISTEN 2890/mongod
#############################################################################################
> config = {
... _id:"rs1",
... members:[
... {_id:0,host:"192.168.4.51:27051"},
... {_id:1,host:"192.168.4.52:27052"},
... {_id:2,host:"192.168.4.53:27053"}
... ]
... }; 回车,出现下面提示就是成功了!
{
"_id" : "rs1",
"members" : [
{
"_id" : 0,
"host" : "192.168.4.51:27051"
},
{
"_id" : 1,
"host" : "192.168.4.52:27052"
},
{
"_id" : 2,
"host" : "192.168.4.53:27053"
}
]
}
>
##############################################################################
> config = {
... _id:"rs1",
... members:[
... {_id:0,host:"192.168.4.51:27051",priority:11},
... {_id:1,host:"192.168.4.52:27052",priority:11},
... {_id:2,host:"192.168.4.53:27053",priority:10} 优先级,指定为主,数字10最小
... ]
... }; 回车,出现下面提示就是成功了!
{
#############################################################################
> rs.initiate(config)
{
"ok" : 1,
"operationTime" : Timestamp(1543979379, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1543979379, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
rs1:SECONDARY> 一开始每个主机都认为自己是从库
rs1:PRIMARY> 等待一会儿,51就会知道自己是主库了
##############################################################################

健康是1

从库连读、写权限都没有
#############################################################################
51

rs1:PRIMARY> rs.status()
{
"set" : "rs1",
"date" : ISODate("2018-12-05T03:51:46.290Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1543981901, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1543981901, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1543981901, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1543981901, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "192.168.4.51:27051",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 3994,
"optime" : {
"ts" : Timestamp(1543981901, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-12-05T03:51:41Z"),
"electionTime" : Timestamp(1543979390, 1),
"electionDate" : ISODate("2018-12-05T03:09:50Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "192.168.4.52:27052",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 2527,
"optime" : {
"ts" : Timestamp(1543981901, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1543981901, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-12-05T03:51:41Z"),
"optimeDurableDate" : ISODate("2018-12-05T03:51:41Z"),
"lastHeartbeat" : ISODate("2018-12-05T03:51:44.387Z"),
"lastHeartbeatRecv" : ISODate("2018-12-05T03:51:44.387Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "192.168.4.51:27051",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.4.53:27053",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 2527,
"optime" : {
"ts" : Timestamp(1543981901, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1543981901, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-12-05T03:51:41Z"),
"optimeDurableDate" : ISODate("2018-12-05T03:51:41Z"),
"lastHeartbeat" : ISODate("2018-12-05T03:51:44.397Z"),
"lastHeartbeatRecv" : ISODate("2018-12-05T03:51:44.469Z"),
"pingMs" : NumberLong(1),
"syncingTo" : "192.168.4.51:27051",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1543981901, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1543981901, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
#############################################################################################
rs1:PRIMARY> rs.isMaster()
{
"hosts" : [
"192.168.4.51:27051",
"192.168.4.52:27052",
"192.168.4.53:27053"
],
"setName" : "rs1",
"setVersion" : 1,
"ismaster" : true,
"secondary" : false,
"primary" : "192.168.4.51:27051",
"me" : "192.168.4.51:27051",
"electionId" : ObjectId("7fffffff0000000000000001"),
"lastWrite" : {
"opTime" : {
"ts" : Timestamp(1543981921, 1),
"t" : NumberLong(1)
},
"lastWriteDate" : ISODate("2018-12-05T03:52:01Z"),
"majorityOpTime" : {
"ts" : Timestamp(1543981921, 1),
"t" : NumberLong(1)
},
"majorityWriteDate" : ISODate("2018-12-05T03:52:01Z")
},
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 100000,
"localTime" : ISODate("2018-12-05T03:52:06.548Z"),
"logicalSessionTimeoutMinutes" : 30,
"minWireVersion" : 0,
"maxWireVersion" : 6,
"readOnly" : false,
"ok" : 1,
"operationTime" : Timestamp(1543981921, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1543981921, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
######################################################################################
52


rs1:SECONDARY> show dbs
2018-12-05T11:40:45.203+0800 E QUERY [thread1] Error: listDatabases failed:{
"operationTime" : Timestamp(1543981241, 1),
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk",
"$clusterTime" : {
"clusterTime" : Timestamp(1543981241, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:65:1
shellHelper.show@src/mongo/shell/utils.js:816:19
shellHelper@src/mongo/shell/utils.js:706:15
@(shellhelp2):1:1
#############################################################################################
rs1:SECONDARY> db.getMongo().setSlaveOk() 只要是从库,打这条命令,才能查看到主库的数据
#############################################################################################
rs1:SECONDARY> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
#############################################################################################
rs1:SECONDARY> rs.isMaster()
{
"hosts" : [
"192.168.4.51:27051",
"192.168.4.52:27052",
"192.168.4.53:27053"
],
"setName" : "rs1",
"setVersion" : 1,
"ismaster" : false,
"secondary" : true,
"primary" : "192.168.4.51:27051",
"me" : "192.168.4.52:27052",
"lastWrite" : {
"opTime" : {
"ts" : Timestamp(1543981321, 1),
"t" : NumberLong(1)
},
"lastWriteDate" : ISODate("2018-12-05T03:42:01Z"),
"majorityOpTime" : {
"ts" : Timestamp(1543981321, 1),
"t" : NumberLong(1)
},
"majorityWriteDate" : ISODate("2018-12-05T03:42:01Z")
},
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 100000,
"localTime" : ISODate("2018-12-05T03:42:04.096Z"),
"logicalSessionTimeoutMinutes" : 30,
"minWireVersion" : 0,
"maxWireVersion" : 6,
"readOnly" : false,
"ok" : 1,
"operationTime" : Timestamp(1543981321, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1543981321, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
#############################################################################################
rs1:SECONDARY> rs.status()
{
"set" : "rs1",
"date" : ISODate("2018-12-05T03:54:25.534Z"),
"myState" : 2,
"term" : NumberLong(1),
"syncingTo" : "192.168.4.51:27051",
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1543982061, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1543982061, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1543982061, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1543982061, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "192.168.4.51:27051",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 2684,
"optime" : {
"ts" : Timestamp(1543982061, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1543982061, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-12-05T03:54:21Z"),
"optimeDurableDate" : ISODate("2018-12-05T03:54:21Z"),
"lastHeartbeat" : ISODate("2018-12-05T03:54:24.296Z"),
"lastHeartbeatRecv" : ISODate("2018-12-05T03:54:24.296Z"),
"pingMs" : NumberLong(0),
"electionTime" : Timestamp(1543979390, 1),
"electionDate" : ISODate("2018-12-05T03:09:50Z"),
"configVersion" : 1
},
{
"_id" : 1,
"name" : "192.168.4.52:27052",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 4162,
"optime" : {
"ts" : Timestamp(1543982061, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-12-05T03:54:21Z"),
"syncingTo" : "192.168.4.51:27051",
"configVersion" : 1,
"self" : true
},
{
"_id" : 2,
"name" : "192.168.4.53:27053",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 2684,
"optime" : {
"ts" : Timestamp(1543982061, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1543982061, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-12-05T03:54:21Z"),
"optimeDurableDate" : ISODate("2018-12-05T03:54:21Z"),
"lastHeartbeat" : ISODate("2018-12-05T03:54:24.296Z"),
"lastHeartbeatRecv" : ISODate("2018-12-05T03:54:24.388Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "192.168.4.51:27051",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1543982061, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1543982061, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}

##############################################################################################
53

rs1:SECONDARY> show dbs 从库默认没有权限,无法查看所有库的信息
2018-12-05T11:57:24.853+0800 E QUERY [thread1] Error: listDatabases failed:{
"operationTime" : Timestamp(1543982241, 1),
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk",
"$clusterTime" : {
"clusterTime" : Timestamp(1543982241, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:65:1
shellHelper.show@src/mongo/shell/utils.js:816:19
shellHelper@src/mongo/shell/utils.js:706:15
@(shellhelp2):1:1
#############################################################################################
rs1:SECONDARY> db.getMongo().setSlaveOk() 只要是从库,打这条命令,才能查看到主库的数据
#############################################################################################
rs1:SECONDARY> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
#############################################################################################
rs1:SECONDARY> rs.isMaster() 查看是否是主库
{
"hosts" : [
"192.168.4.51:27051",
"192.168.4.52:27052",
"192.168.4.53:27053"
],
"setName" : "rs1",
"setVersion" : 1,
"ismaster" : false,
"secondary" : true,
"primary" : "192.168.4.51:27051",
"me" : "192.168.4.53:27053",
"lastWrite" : {
"opTime" : {
"ts" : Timestamp(1543982451, 1),
"t" : NumberLong(1)
},
"lastWriteDate" : ISODate("2018-12-05T04:00:51Z"),
"majorityOpTime" : {
"ts" : Timestamp(1543982451, 1),
"t" : NumberLong(1)
},
"majorityWriteDate" : ISODate("2018-12-05T04:00:51Z")
},
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 100000,
"localTime" : ISODate("2018-12-05T04:00:51.409Z"),
"logicalSessionTimeoutMinutes" : 30,
"minWireVersion" : 0,
"maxWireVersion" : 6,
"readOnly" : false,
"ok" : 1,
"operationTime" : Timestamp(1543982451, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1543982451, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
#############################################################################################
rs1:SECONDARY> rs.status() 查看集群状态
{
"set" : "rs1",
"date" : ISODate("2018-12-05T04:01:22.719Z"),
"myState" : 2,
"term" : NumberLong(1),
"syncingTo" : "192.168.4.51:27051",
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1543982481, 1),
"t" : NumberLong(1)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1543982481, 1),
"t" : NumberLong(1)
},
"appliedOpTime" : {
"ts" : Timestamp(1543982481, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1543982481, 1),
"t" : NumberLong(1)
}
},
"members" : [
{
"_id" : 0,
"name" : "192.168.4.51:27051",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 3102,
"optime" : {
"ts" : Timestamp(1543982481, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1543982481, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-12-05T04:01:21Z"),
"optimeDurableDate" : ISODate("2018-12-05T04:01:21Z"),
"lastHeartbeat" : ISODate("2018-12-05T04:01:22.683Z"),
"lastHeartbeatRecv" : ISODate("2018-12-05T04:01:22.615Z"),
"pingMs" : NumberLong(0),
"electionTime" : Timestamp(1543979390, 1),
"electionDate" : ISODate("2018-12-05T03:09:50Z"),
"configVersion" : 1
},
{
"_id" : 1,
"name" : "192.168.4.52:27052",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 3102,
"optime" : {
"ts" : Timestamp(1543982481, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1543982481, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-12-05T04:01:21Z"),
"optimeDurableDate" : ISODate("2018-12-05T04:01:21Z"),
"lastHeartbeat" : ISODate("2018-12-05T04:01:22.683Z"),
"lastHeartbeatRecv" : ISODate("2018-12-05T04:01:22.614Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "192.168.4.51:27051",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.4.53:27053",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 4585,
"optime" : {
"ts" : Timestamp(1543982481, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2018-12-05T04:01:21Z"),
"syncingTo" : "192.168.4.51:27051",
"configVersion" : 1,
"self" : true
}
],
"ok" : 1,
"operationTime" : Timestamp(1543982481, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1543982481, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
#############################################################################################
51

rs1:PRIMARY> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB

rs1:PRIMARY> use db5 新建并进入db5库
switched to db db5

rs1:PRIMARY> db.t5.save({name:"tom"}) 建立新集合
WriteResult({ "nInserted" : 1 })

rs1:PRIMARY> db.t5.find() 查看集合的信息
{ "_id" : ObjectId("5c074e49e7da3fb39ecbc1ea"), "name" : "tom" }
#############################################################################################
50


# /etc/mongodb/bin/mongo --host 192.168.4.53 --port 27053 客户端连接从库53
#############################################################################################
rs1:SECONDARY> show dbs 从库默认没有权限,无法查看所有库的信息
显示如下:
2018-12-05T11:57:24.853+0800 E QUERY [thread1] Error: listDatabases failed:{
"operationTime" : Timestamp(1543982241, 1),
"ok" : 0,
"errmsg" : "not master and slaveOk=false",
"code" : 13435,
"codeName" : "NotMasterNoSlaveOk",
"$clusterTime" : {
"clusterTime" : Timestamp(1543982241, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
Mongo.prototype.getDBs@src/mongo/shell/mongo.js:65:1
shellHelper.show@src/mongo/shell/utils.js:816:19
shellHelper@src/mongo/shell/utils.js:706:15
@(shellhelp2):1:1
#############################################################################################
rs1:SECONDARY> db.getMongo().setSlaveOk() 只要是从库,打这条命令,才能查看到主库的数据
#############################################################################################
rs1:SECONDARY> show dbs 查看所有库的信息
admin 0.000GB
config 0.000GB
db5 0.000GB 能发现主库刚才新建的库
local 0.000GB
#############################################################################################
rs1:SECONDARY> use db5 进入库db5
switched to db db5
#############################################################################################
rs1:SECONDARY> show tables
t5 能发现主库刚才新建的集合
#############################################################################################
rs1:SECONDARY> db.t5.find() 能查看主库建立的集合的内容
{ "_id" : ObjectId("5c074e49e7da3fb39ecbc1ea"), "name" : "tom" }
#####################################################################################
如果配置错了,就把所有的主库和从库的local库下的集合全部删掉!停了服务,然后重启!
#####################################################################################
51

# mstop
#####################################################################################
50

# /etc/mongodb/bin/mongo --host 192.168.4.52 --port 27052


rs1:PRIMARY> rs.isMaster()
{
"hosts" : [
"192.168.4.51:27051",
"192.168.4.52:27052",
"192.168.4.53:27053"
],
"setName" : "rs1",
"setVersion" : 1,
"ismaster" : true,
"secondary" : false,
"primary" : "192.168.4.52:27052",
"me" : "192.168.4.52:27052",
"electionId" : ObjectId("7fffffff0000000000000003"),
"lastWrite" : {
"opTime" : {
"ts" : Timestamp(1543993830, 1),
"t" : NumberLong(3)
},
"lastWriteDate" : ISODate("2018-12-05T07:10:30Z"),
"majorityOpTime" : {
"ts" : Timestamp(1543993830, 1),
"t" : NumberLong(3)
},
"majorityWriteDate" : ISODate("2018-12-05T07:10:30Z")
},
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 100000,
"localTime" : ISODate("2018-12-05T07:10:31.227Z"),
"logicalSessionTimeoutMinutes" : 30,
"minWireVersion" : 0,
"maxWireVersion" : 6,
"readOnly" : false,
"ok" : 1,
"operationTime" : Timestamp(1543993830, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1543993830, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
#############################################################################################
报错:

rs1:PRIMARY> rs.status
function () {
return db._adminCommand("replSetGetStatus");
}
#############################################################################################
rs1:PRIMARY> rs.status()
{
"set" : "rs1",
"date" : ISODate("2018-12-05T07:11:29.744Z"),
"myState" : 1,
"term" : NumberLong(3),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1543993880, 1),
"t" : NumberLong(3)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1543993880, 1),
"t" : NumberLong(3)
},
"appliedOpTime" : {
"ts" : Timestamp(1543993880, 1),
"t" : NumberLong(3)
},
"durableOpTime" : {
"ts" : Timestamp(1543993880, 1),
"t" : NumberLong(3)
}
},
"members" : [
{
"_id" : 0,
"name" : "192.168.4.51:27051",
"health" : 0, 0代表不健康状态
"state" : 8,
"stateStr" : "(not reachable/healthy)",
"uptime" : 0,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDurable" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2018-12-05T07:11:29.354Z"),
"lastHeartbeatRecv" : ISODate("2018-12-05T07:07:41.577Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "Connection refused",
"configVersion" : -1
},
{
"_id" : 1,
"name" : "192.168.4.52:27052",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY", 52成为主了
"uptime" : 15986,
"optime" : {
"ts" : Timestamp(1543993880, 1),
"t" : NumberLong(3)
},
"optimeDate" : ISODate("2018-12-05T07:11:20Z"),
"electionTime" : Timestamp(1543993739, 1),
"electionDate" : ISODate("2018-12-05T07:08:59Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 2,
"name" : "192.168.4.53:27053",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 152,
"optime" : {
"ts" : Timestamp(1543993880, 1),
"t" : NumberLong(3)
},
"optimeDurable" : {
"ts" : Timestamp(1543993880, 1),
"t" : NumberLong(3)
},
"optimeDate" : ISODate("2018-12-05T07:11:20Z"),
"optimeDurableDate" : ISODate("2018-12-05T07:11:20Z"),
"lastHeartbeat" : ISODate("2018-12-05T07:11:29.217Z"),
"lastHeartbeatRecv" : ISODate("2018-12-05T07:11:29.110Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "192.168.4.52:27052", 53从52同步
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1543993880, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1543993880, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
#############################################################################################
rs1:PRIMARY> show dbs
admin 0.000GB
config 0.000GB
db5 0.000GB
local 0.000GB

rs1:PRIMARY> use db5
switched to db db5

rs1:PRIMARY> show tables
t2
t5

rs1:PRIMARY> db.t5.find()
{ "_id" : ObjectId("5c074e49e7da3fb39ecbc1ea"), "name" : "tom" }
#############################################################################################
rs1:PRIMARY> db.t5.save({_id:1,name="伊一"})
2018-12-05T15:15:23.130+0800 E QUERY [thread1] SyntaxError: missing : after property id @(shell):1:22

rs1:PRIMARY> db.t5.save({_id:1,name:"伊一"})
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 1 })

rs1:PRIMARY> db.t5.find()
{ "_id" : ObjectId("5c074e49e7da3fb39ecbc1ea"), "name" : "tom" }
{ "_id" : 1, "name" : "伊一" }

rs1:PRIMARY> db.t5.save({_id:2,name:"小二"})
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 2 })

rs1:PRIMARY> db.t5.find()
{ "_id" : ObjectId("5c074e49e7da3fb39ecbc1ea"), "name" : "tom" }
{ "_id" : 1, "name" : "伊一" }
{ "_id" : 2, "name" : "小二" }

rs1:PRIMARY> db.t5.save({_id:3,name:"张三"})
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 3 })

rs1:PRIMARY> db.t5.find()
{ "_id" : ObjectId("5c074e49e7da3fb39ecbc1ea"), "name" : "tom" }
{ "_id" : 1, "name" : "伊一" }
{ "_id" : 2, "name" : "小二" }
{ "_id" : 3, "name" : "张三" }

rs1:PRIMARY> db.t5.save({_id:3,name:"蜡笔小新"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

rs1:PRIMARY> db.t5.find()
{ "_id" : ObjectId("5c074e49e7da3fb39ecbc1ea"), "name" : "tom" }
{ "_id" : 1, "name" : "伊一" }
{ "_id" : 2, "name" : "小二" }
{ "_id" : 3, "name" : "蜡笔小新" }

rs1:PRIMARY> db.t5.insert({_id:4,name:"李四"})
WriteResult({ "nInserted" : 1 })

rs1:PRIMARY> db.t5.find()
{ "_id" : ObjectId("5c074e49e7da3fb39ecbc1ea"), "name" : "tom" }
{ "_id" : 1, "name" : "伊一" }
{ "_id" : 2, "name" : "小二" }
{ "_id" : 3, "name" : "蜡笔小新" }
{ "_id" : 4, "name" : "李四" }

rs1:PRIMARY> db.t5.insert({_id:5,name:"王五"})
WriteResult({ "nInserted" : 1 })

rs1:PRIMARY> db.t5.find()
{ "_id" : ObjectId("5c074e49e7da3fb39ecbc1ea"), "name" : "tom" }
{ "_id" : 1, "name" : "伊一" }
{ "_id" : 2, "name" : "小二" }
{ "_id" : 3, "name" : "蜡笔小新" }
{ "_id" : 4, "name" : "李四" }
{ "_id" : 5, "name" : "王五" }

rs1:PRIMARY> db.t5.insert({_id:5,name:"外星人"})
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "E11000 duplicate key error collection: db5.t5 index: _id_ dup key: { : 5.0 }"
}
})
#############################################################################################
rs1:PRIMARY> db.t5.insert({_id:6,name:"赵六"},{_id:7,name:"齐七"})
WriteResult({ "nInserted" : 1 })
rs1:PRIMARY> db.t5.find()
{ "_id" : ObjectId("5c074e49e7da3fb39ecbc1ea"), "name" : "tom" }
{ "_id" : 1, "name" : "伊一" }
{ "_id" : 2, "name" : "小二" }
{ "_id" : 3, "name" : "蜡笔小新" }
{ "_id" : 4, "name" : "李四" }
{ "_id" : 5, "name" : "王五" }
{ "_id" : 6, "name" : "赵六" }
#############################################################################################
rs1:PRIMARY> db.t5.insertMany([{_id:8,name:"八路"},{_id:9,name:"九九"}])
{ "acknowledged" : true, "insertedIds" : [ 8, 9 ] }
rs1:PRIMARY> db.t5.find()
{ "_id" : ObjectId("5c074e49e7da3fb39ecbc1ea"), "name" : "tom" }
{ "_id" : 1, "name" : "伊一" }
{ "_id" : 2, "name" : "小二" }
{ "_id" : 3, "name" : "蜡笔小新" }
{ "_id" : 4, "name" : "李四" }
{ "_id" : 5, "name" : "王五" }
{ "_id" : 6, "name" : "赵六" }
{ "_id" : 8, "name" : "八路" }
{ "_id" : 9, "name" : "九九" }
#############################################################################################
rs1:PRIMARY> db.t5.findOne()
{ "_id" : ObjectId("5c074e49e7da3fb39ecbc1ea"), "name" : "tom" }

rs1:PRIMARY> db.t5.find({name:"tom"},{name:1})
{ "_id" : ObjectId("5c074e49e7da3fb39ecbc1ea"), "name" : "tom" }

rs1:PRIMARY> db.t5.remove({ "_id" : ObjectId("5c074e49e7da3fb39ecbc1ea"), "name" : "tom" })
WriteResult({ "nRemoved" : 1 })
rs1:PRIMARY> db.t5.find()
{ "_id" : 1, "name" : "伊一" }
{ "_id" : 2, "name" : "小二" }
{ "_id" : 3, "name" : "蜡笔小新" }
{ "_id" : 4, "name" : "李四" }
{ "_id" : 5, "name" : "王五" }
{ "_id" : 6, "name" : "赵六" }
{ "_id" : 8, "name" : "八路" }
{ "_id" : 9, "name" : "九九" }
#############################################################################################
rs1:PRIMARY> db.t5.find({name:"tom"},{_id:0,name:1})
{ "name" : "tom" }

rs1:PRIMARY> db.t5.find({},{_id:0,name:1})
{ "name" : "tom" }
{ "name" : "伊一" }
{ "name" : "小二" }
{ "name" : "蜡笔小新" }
{ "name" : "李四" }
{ "name" : "王五" }
{ "name" : "赵六" }
{ "name" : "八路" }
{ "name" : "九九" }

rs1:PRIMARY> db.t5.find().limit(3)
{ "_id" : 1, "name" : "伊一" }
{ "_id" : 2, "name" : "小二" }
{ "_id" : 3, "name" : "蜡笔小新" }
rs1:PRIMARY> db.t5.find().skip(2)
{ "_id" : 3, "name" : "蜡笔小新" }
{ "_id" : 4, "name" : "李四" }
{ "_id" : 5, "name" : "王五" }
{ "_id" : 6, "name" : "赵六" }
{ "_id" : 8, "name" : "八路" }
{ "_id" : 9, "name" : "九九" }
#############################################################################################
rs1:PRIMARY> db.t5.find().sort({_id:1})
{ "_id" : 1, "name" : "伊一" }
{ "_id" : 2, "name" : "小二" }
{ "_id" : 3, "name" : "蜡笔小新" }
{ "_id" : 4, "name" : "李四" }
{ "_id" : 5, "name" : "王五" }
{ "_id" : 6, "name" : "赵六" }
{ "_id" : 8, "name" : "八路" }
{ "_id" : 9, "name" : "九九" }

rs1:PRIMARY> db.t5.find().sort({_id:-1})
{ "_id" : 9, "name" : "九九" }
{ "_id" : 8, "name" : "八路" }
{ "_id" : 6, "name" : "赵六" }
{ "_id" : 5, "name" : "王五" }
{ "_id" : 4, "name" : "李四" }
{ "_id" : 3, "name" : "蜡笔小新" }
{ "_id" : 2, "name" : "小二" }
{ "_id" : 1, "name" : "伊一" }

rs1:PRIMARY> db.t5.find({_id:{$in:[1,5,9]}})
{ "_id" : 1, "name" : "伊一" }
{ "_id" : 5, "name" : "王五" }
{ "_id" : 9, "name" : "九九" }

rs1:PRIMARY> db.t5.find({_id:{$nin:[1,5,9]}})
{ "_id" : 2, "name" : "小二" }
{ "_id" : 3, "name" : "蜡笔小新" }
{ "_id" : 4, "name" : "李四" }
{ "_id" : 6, "name" : "赵六" }
{ "_id" : 8, "name" : "八路" }

rs1:PRIMARY> db.t5.find({$or:[{name:"伊一"},{_id:6}]})
{ "_id" : 1, "name" : "伊一" }
{ "_id" : 6, "name" : "赵六" }
#############################################################################################
rs1:PRIMARY> db.t1.find()
{ "_id" : ObjectId("5c0784d5f0c50c147e82b28d"), "num" : 2, "name" : 2 }
{ "_id" : ObjectId("5c0784fef0c50c147e82b28f"), "num" : 3, "name" : 3 }
{ "_id" : ObjectId("5c078503f0c50c147e82b290"), "num" : 4, "name" : 4 }
{ "_id" : ObjectId("5c078507f0c50c147e82b291"), "num" : 5, "name" : 5 }
{ "_id" : ObjectId("5c07850cf0c50c147e82b292"), "num" : 6, "name" : 6 }
{ "_id" : ObjectId("5c078511f0c50c147e82b293"), "num" : 7, "name" : 7 }
{ "_id" : ObjectId("5c078516f0c50c147e82b294"), "num" : 8, "name" : 8 }
{ "_id" : ObjectId("5c07851bf0c50c147e82b295"), "num" : 9, "name" : 9 }
#############################################################################################
rs1:PRIMARY> db.t1.find({num:{$lt:6}},{_id:0,num:1,name:1})
{ "num" : 2, "name" : 2 }
{ "num" : 3, "name" : 3 }
{ "num" : 4, "name" : 4 }
{ "num" : 5, "name" : 5 }

rs1:PRIMARY> db.t1.update({num:{$lt:6}},{$inc:{num:1}},false,true)
WriteResult({ "nMatched" : 4, "nUpserted" : 0, "nModified" : 4 })

rs1:PRIMARY> db.t1.find({num:{$lt:6}},{_id:0,num:1,name:1})
{ "num" : 3, "name" : 2 }
{ "num" : 4, "name" : 3 }
{ "num" : 5, "name" : 4 }

###########################################################################
rs1:PRIMARY> db.t5.find({})
{ "_id" : 1, "name" : "伊一" }
{ "_id" : 2, "name" : "小二" }
{ "_id" : 3, "name" : "蜡笔小新" }
{ "_id" : 4, "name" : "李四" }
{ "_id" : 5, "name" : "王五" }
{ "_id" : 6, "name" : "赵六" }
{ "_id" : 8, "name" : "八路" }
{ "_id" : 9, "name" : "九九" }
############################################################################
rs1:PRIMARY> db.t5.update({_id:{$lte:3}},{$set:{shell:"/bin/bash"}},false,true) 如果要改的字段不存在,则会追加写入
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })

rs1:PRIMARY> db.t5.find()
{ "_id" : 1, "name" : "伊一", "shell" : "/bin/bash" }
{ "_id" : 2, "name" : "小二", "shell" : "/bin/bash" }
{ "_id" : 3, "name" : "蜡笔小新", "shell" : "/bin/bash" }
{ "_id" : 4, "name" : "李四" }
{ "_id" : 5, "name" : "王五" }
{ "_id" : 6, "name" : "赵六" }
{ "_id" : 8, "name" : "八路" }
{ "_id" : 9, "name" : "九九" }
###################################################################################
rs1:PRIMARY> db.t5.update({shell:"/bin/bash"},{$set:{shell:"/sbin/nologin"}},false,true)
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })

rs1:PRIMARY> db.t5.find()
{ "_id" : 1, "name" : "伊一", "shell" : "/sbin/nologin" }
{ "_id" : 2, "name" : "小二", "shell" : "/sbin/nologin" }
{ "_id" : 3, "name" : "蜡笔小新", "shell" : "/sbin/nologin" }
{ "_id" : 4, "name" : "李四" }
{ "_id" : 5, "name" : "王五" }
{ "_id" : 6, "name" : "赵六" }
{ "_id" : 8, "name" : "八路" }
{ "_id" : 9, "name" : "九九" }
###################################################################################
rs1:PRIMARY> db.t5.update({_id:{$gt:6}},{$set:{shell:"/bin/login"}}) 不加,false,true的话,只会改一行
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

rs1:PRIMARY> db.t5.find()
{ "_id" : 1, "name" : "伊一", "shell" : "/sbin/nologin" }
{ "_id" : 2, "name" : "小二", "shell" : "/sbin/nologin" }
{ "_id" : 3, "name" : "蜡笔小新", "shell" : "/sbin/nologin" }
{ "_id" : 4, "name" : "李四" }
{ "_id" : 5, "name" : "王五" }
{ "_id" : 6, "name" : "赵六" }
{ "_id" : 8, "name" : "八路", "shell" : "/bin/login" }
{ "_id" : 9, "name" : "九九" }
###################################################################################
不用$set,不加false,true的话,只会改一行且只留shell字段,其他的字段都被删除了!

rs1:PRIMARY> db.t5.update({_id:{$gt:6}},{shell:"/bin/login"})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

rs1:PRIMARY> db.t5.find()
{ "_id" : 1, "name" : "伊一", "shell" : "/sbin/nologin" }
{ "_id" : 2, "name" : "小二", "shell" : "/sbin/nologin" }
{ "_id" : 3, "name" : "蜡笔小新", "shell" : "/sbin/nologin" }
{ "_id" : 4, "name" : "李四" }
{ "_id" : 5, "name" : "王五" }
{ "_id" : 6, "name" : "赵六" }
{ "_id" : 8, "shell" : "/bin/login" }
{ "_id" : 9, "name" : "九九" }
###################################################################################
rs1:PRIMARY> db.user.save({name:null,uid:null})
WriteResult({ "nInserted" : 1 })

rs1:PRIMARY> db.user.find()
{ "_id" : ObjectId("5c079d2881c7aa2f69faff14"), "name" : null, "uid" : null }

rs1:PRIMARY> db.user.find({name:null})
{ "_id" : ObjectId("5c079d2881c7aa2f69faff14"), "name" : null, "uid" : null }

rs1:PRIMARY> db.user.find({shell:null}) 表示此条文档没有shell字段
{ "_id" : ObjectId("5c079d2881c7aa2f69faff14"), "name" : null, "uid" : null }
#####################################################################################
rs1:PRIMARY> db.t2.save({_id:0,name:"tom",likes:["a","b","c"]})
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 0 })

rs1:PRIMARY> db.t2.update({name:"tom"},{$push:{likes:"d"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

rs1:PRIMARY> db.t2.find()
{ "_id" : 0, "name" : "tom", "likes" : [ "a", "b", "c", "d" ] }

rs1:PRIMARY> db.t2.update({name:"tom"},{$push:{likes:"d"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

rs1:PRIMARY> db.t2.find()
{ "_id" : 0, "name" : "tom", "likes" : [ "a", "b", "c", "d", "d" ] }
#############################################################################################
rs1:PRIMARY> db.t2.update({name:"tom"},{$addToSet:{likes:"d"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })

rs1:PRIMARY> db.t2.find()
{ "_id" : 0, "name" : "tom", "likes" : [ "a", "b", "c", "d", "d" ] }

rs1:PRIMARY> db.t2.update({name:"tom"},{$addToSet:{likes:"d"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })

rs1:PRIMARY> db.t2.find()
{ "_id" : 0, "name" : "tom", "likes" : [ "a", "b", "c", "d", "d" ] }
#############################################################################################
rs1:PRIMARY> db.t2.find()
{ "_id" : 0, "name" : "tom", "likes" : [ "a", "b", "c", "d", "d" ] }

rs1:PRIMARY> db.t2.update({name:"tom"},{$pop:{likes:1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

rs1:PRIMARY> db.t2.find()
{ "_id" : 0, "name" : "tom", "likes" : [ "a", "b", "c", "d" ] }
#############################################################################################
rs1:PRIMARY> db.t2.update({name:"tom"},{$pop:{likes:-1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

rs1:PRIMARY> db.t2.find()
{ "_id" : 0, "name" : "tom", "likes" : [ "b", "c", "d" ] }
#############################################################################################
rs1:PRIMARY> db.t2.update({name:"tom"},{$pull:{likes:"c"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

rs1:PRIMARY> db.t2.find()
{ "_id" : 0, "name" : "tom", "likes" : [ "b", "d" ] }
#####################################################################################
常见报错:

rs1:PRIMARY> db.t2.update({name:"tom"},{$pop:{likes:"c"}})
WriteResult({
"nMatched" : 0,
"nUpserted" : 0,
"nModified" : 0,
"writeError" : {
"code" : 9,
"errmsg" : "Expected a number in: likes: \"c\""
}
})
###########################################################################
正数1删除尾1个,负数1删除头1个
写指定的值,就删除所有同名的值
删除与条件匹配的行,不匹配就删除所有

rs1:PRIMARY> show tables
t1
t2
t5
user

rs1:PRIMARY> db.t2.drop()
true

rs1:PRIMARY> show tables
t1
t5
user
#####################################################################################
rs1:PRIMARY> db.user.remove()
2018-12-05T20:06:46.380+0800 E QUERY [thread1] Error: remove needs a query :
DBCollection.prototype._parseRemove@src/mongo/shell/collection.js:357:1
DBCollection.prototype.remove@src/mongo/shell/collection.js:382:18
@(shell):1:1
#####################################################################################
rs1:PRIMARY> db.user.remove({})
WriteResult({ "nRemoved" : 1 })

rs1:PRIMARY> show tables
t1
t5
user
#####################################################################################
数据库用DAS直连存储。因为共享存储要通过网络传播,可能会延迟。

 

posted @ 2019-04-30 22:35  安于夏  阅读(215)  评论(0编辑  收藏  举报