一、安装MongoDB
忽略
二、创建账号和授权
- 在新的MongoDB上创建用户管理员。先切换到admin库,然后通过命令创建数据库管理员账号和密码。最后再验证账号密码是否创建成功!
bash
use admin
db.createUser({user:"root",pwd:"123456Ab",roles:["userAdminAnyDatabase"]})
db.auth("root","123456Ab") #返回1表示登录成功
过程记录
bash
C:\Users\administrator>mongo
MongoDB shell version v3.6.23
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("9c8640d6-7b58-4220-b2a5-286938940a38") }
MongoDB server version: 3.6.23
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
> use admin
switched to db admin
> db.createUser({user:"root",pwd:"123456Ab",roles:["userAdminAnyDatabase"]})
Successfully added user: { "user" : "root", "roles" : [ "userAdminAnyDatabase" ] }
> db.auth("root","123456Ab")
1
> exit
bye
C:\Users\administrator>
- 在指定数据库上创建用户。
先切换到admin库,然后验证刚刚创建的管理root账号,再切换到新的ramsey数据库,通过命令创建账号和授权。
bash
use admin
db.auth("root","123456Ab")
use ramsey
db.createUser({user:"ss",pwd:"secret",roles:[{role:"dbOwner", db:"ramsey"}]})
过程记录
bash
C:\Users\administrator>mongo
MongoDB shell version v3.6.23
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("930e2c76-530e-4345-8840-4705075e2646") }
MongoDB server version: 3.6.23
>
> use admin
switched to db admin
> db.auth("root","123456Ab")
1
> use ramsey
switched to db ramsey
> db.createUser({user:"ss",pwd:"secret",roles:[{role:"dbOwner", db:"ramsey"}]})
Successfully added user: {
"user" : "ss",
"roles" : [
{
"role" : "dbOwner",
"db" : "ramsey"
}
]
}
>
> exit
bye
C:\Users\administrator>
三、恢复 Mongodb数据
通过下面命令恢复数据
bash
-- restore
mongorestore -h 127.0.0.1 --port 27017 -u ss -p secret -d ramsey C:\Mongodump\ramsey >> C:\Mongodump\ramsey\restore.log
过程记录
bash
C:\Users\administrator>mongorestore -h 127.0.0.1 --port 27017 -u ss -p secret -d ramsey C:\Mongodump\ramsey >> C:\Mongodump\ramsey\restore.log
2024-07-16T15:17:24.818+0800 the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead
2024-07-16T15:17:24.820+0800 building a list of collections to restore from C:\Mongodump\ramsey dir
2024-07-16T15:17:24.827+0800 don't know what to do with file "C:\Mongodump\ramsey\restore.log", skipping...
2024-07-16T15:17:24.833+0800 reading metadata for ramsey.sent_item from C:\Mongodump\ramsey\sent_item.metadata.json
2024-07-16T15:17:24.838+0800 reading metadata for ramsey.sent_item_archive from C:\Mongodump\ramsey\sent_item_archive.metadata.json
2024-07-16T15:17:24.839+0800 reading metadata for ramsey.failed_item from C:\Mongodump\ramsey\failed_item.metadata.json
2024-07-16T15:17:24.904+0800 reading metadata for ramsey.failed_item_archive from C:\Mongodump\ramsey\failed_item_archive.metadata.json
2024-07-16T15:17:24.915+0800 restoring ramsey.sent_item from C:\Mongodump\ramsey\sent_item.bson
2024-07-16T15:17:24.950+0800 restoring ramsey.failed_item from C:\Mongodump\ramsey\failed_item.bson
2024-07-16T15:17:24.964+0800 restoring ramsey.sent_item_archive from C:\Mongodump\ramsey\sent_item_archive.bson
2024-07-16T15:17:25.733+0800 restoring ramsey.failed_item_archive from C:\Mongodump\ramsey\failed_item_archive.bson
2024-07-16T15:17:28.184+0800 [........................] ramsey.sent_item 47.7MB/6.46GB (0.7%)
2024-07-16T15:17:28.186+0800 [######..................] ramsey.failed_item 49.0MB/176MB (27.7%)
2024-07-16T15:17:28.191+0800 [........................] ramsey.sent_item_archive 42.6MB/1.30GB (3.2%)
2024-07-16T15:17:28.192+0800 [###############.........] ramsey.failed_item_archive 48.6MB/73.9MB (65.8%)
2024-07-16T15:17:28.193+0800
2024-07-16T15:17:29.870+0800 [########################] ramsey.failed_item_archive 73.9MB/73.9MB (100.0%)
2024-07-16T15:17:29.879+0800 no indexes to restore
2024-07-16T15:17:29.886+0800 finished restoring ramsey.failed_item_archive (7526 documents)
2024-07-16T15:17:29.892+0800 reading metadata for ramsey.sending from C:\Mongodump\ramsey\sending.metadata.json
2024-07-16T15:17:29.934+0800 restoring ramsey.sending from C:\Mongodump\ramsey\sending.bson
2024-07-16T15:17:29.940+0800 no indexes to restore
2024-07-16T15:17:29.942+0800 finished restoring ramsey.sending (0 documents)
2024-07-16T15:17:30.808+0800 [........................] ramsey.sent_item 108MB/6.46GB (1.6%)
2024-07-16T15:17:30.820+0800 [##############..........] ramsey.failed_item 107MB/176MB (60.4%)
2024-07-16T15:17:30.847+0800 [#.......................] ramsey.sent_item_archive 104MB/1.30GB (7.9%)
2024-07-16T15:17:30.850+0800
2024-07-16T15:17:33.748+0800 [........................] ramsey.sent_item 169MB/6.46GB (2.6%)
2024-07-16T15:17:33.750+0800 [#####################...] ramsey.failed_item 156MB/176MB (88.3%)
2024-07-16T15:17:33.753+0800 [###.....................] ramsey.sent_item_archive 179MB/1.30GB (13.5%)
2024-07-16T15:17:33.758+0800
2024-07-16T15:17:34.348+0800 [########################] ramsey.failed_item 176MB/176MB (100.0%)
2024-07-16T15:17:34.348+0800 no indexes to restore
2024-07-16T15:17:34.355+0800 finished restoring ramsey.failed_item (18145 documents)
2024-07-16T15:17:36.757+0800 [........................] ramsey.sent_item 263MB/6.46GB (4.0%)
2024-07-16T15:17:36.757+0800 [#####...................] ramsey.sent_item_archive 303MB/1.30GB (22.9%)
2024-07-16T15:17:36.761+0800
2024-07-16T15:17:39.749+0800 [#.......................] ramsey.sent_item 313MB/6.46GB (4.7%)
2024-07-16T15:17:39.753+0800 [#######.................] ramsey.sent_item_archive 431MB/1.30GB (32.5%)
2024-07-16T15:17:39.758+0800
2024-07-16T15:17:42.748+0800 [#.......................] ramsey.sent_item 361MB/6.46GB (5.5%)
2024-07-16T15:17:42.748+0800 [#########...............] ramsey.sent_item_archive 548MB/1.30GB (41.3%)
2024-07-16T15:17:42.755+0800
2024-07-16T15:17:45.748+0800 [#.......................] ramsey.sent_item 426MB/6.46GB (6.4%)
2024-07-16T15:17:45.749+0800 [############............] ramsey.sent_item_archive 675MB/1.30GB (50.9%)
2024-07-16T15:17:45.755+0800
2024-07-16T15:17:48.748+0800 [#.......................] ramsey.sent_item 490MB/6.46GB (7.4%)
2024-07-16T15:17:48.749+0800 [###############.........] ramsey.sent_item_archive 842MB/1.30GB (63.4%)
2024-07-16T15:17:48.750+0800
2024-07-16T15:17:51.748+0800 [##......................] ramsey.sent_item 616MB/6.46GB (9.3%)
2024-07-16T15:17:51.750+0800 [##################......] ramsey.sent_item_archive 1014MB/1.30GB (76.4%)
2024-07-16T15:17:51.753+0800
2024-07-16T15:17:54.853+0800 [##......................] ramsey.sent_item 719MB/6.46GB (10.9%)
2024-07-16T15:17:54.854+0800 [####################....] ramsey.sent_item_archive 1.13GB/1.30GB (87.1%)
2024-07-16T15:17:54.877+0800
2024-07-16T15:17:57.748+0800 [##......................] ramsey.sent_item 812MB/6.46GB (12.3%)
2024-07-16T15:17:57.752+0800 [#######################.] ramsey.sent_item_archive 1.28GB/1.30GB (98.9%)
2024-07-16T15:17:57.758+0800
2024-07-16T15:17:58.115+0800 [########################] ramsey.sent_item_archive 1.30GB/1.30GB (100.0%)
2024-07-16T15:17:58.115+0800 no indexes to restore
2024-07-16T15:17:58.120+0800 finished restoring ramsey.sent_item_archive (135502 documents)
2024-07-16T15:18:00.748+0800 [###.....................] ramsey.sent_item 940MB/6.46GB (14.2%)
2024-07-16T15:18:03.770+0800 [####....................] ramsey.sent_item 1.09GB/6.46GB (16.9%)
2024-07-16T15:18:06.748+0800 [####....................] ramsey.sent_item 1.23GB/6.46GB (19.0%)
2024-07-16T15:18:09.770+0800 [#####...................] ramsey.sent_item 1.43GB/6.46GB (22.1%)
2024-07-16T15:18:12.747+0800 [#####...................] ramsey.sent_item 1.58GB/6.46GB (24.5%)
2024-07-16T15:18:15.749+0800 [######..................] ramsey.sent_item 1.76GB/6.46GB (27.3%)
2024-07-16T15:18:18.756+0800 [######..................] ramsey.sent_item 1.87GB/6.46GB (29.0%)
2024-07-16T15:18:21.747+0800 [#######.................] ramsey.sent_item 2.06GB/6.46GB (31.8%)
2024-07-16T15:18:24.800+0800 [########................] ramsey.sent_item 2.25GB/6.46GB (34.8%)
2024-07-16T15:18:27.747+0800 [#########...............] ramsey.sent_item 2.44GB/6.46GB (37.8%)
2024-07-16T15:18:30.747+0800 [#########...............] ramsey.sent_item 2.62GB/6.46GB (40.5%)
2024-07-16T15:18:33.747+0800 [##########..............] ramsey.sent_item 2.77GB/6.46GB (42.8%)
2024-07-16T15:18:36.748+0800 [##########..............] ramsey.sent_item 2.95GB/6.46GB (45.6%)
2024-07-16T15:18:39.747+0800 [###########.............] ramsey.sent_item 3.13GB/6.46GB (48.4%)
2024-07-16T15:18:42.747+0800 [############............] ramsey.sent_item 3.34GB/6.46GB (51.7%)
2024-07-16T15:18:45.748+0800 [#############...........] ramsey.sent_item 3.52GB/6.46GB (54.5%)
2024-07-16T15:18:48.758+0800 [#############...........] ramsey.sent_item 3.70GB/6.46GB (57.3%)
2024-07-16T15:18:51.750+0800 [##############..........] ramsey.sent_item 3.87GB/6.46GB (59.9%)
2024-07-16T15:18:54.747+0800 [###############.........] ramsey.sent_item 4.09GB/6.46GB (63.3%)
2024-07-16T15:18:57.747+0800 [################........] ramsey.sent_item 4.31GB/6.46GB (66.8%)
2024-07-16T15:19:00.784+0800 [################........] ramsey.sent_item 4.53GB/6.46GB (70.1%)
2024-07-16T15:19:03.760+0800 [#################.......] ramsey.sent_item 4.71GB/6.46GB (72.9%)
2024-07-16T15:19:06.747+0800 [##################......] ramsey.sent_item 4.94GB/6.46GB (76.4%)
2024-07-16T15:19:09.747+0800 [###################.....] ramsey.sent_item 5.13GB/6.46GB (79.4%)
2024-07-16T15:19:12.747+0800 [###################.....] ramsey.sent_item 5.34GB/6.46GB (82.7%)
2024-07-16T15:19:15.844+0800 [####################....] ramsey.sent_item 5.57GB/6.46GB (86.2%)
2024-07-16T15:19:18.748+0800 [#####################...] ramsey.sent_item 5.79GB/6.46GB (89.7%)
2024-07-16T15:19:21.747+0800 [######################..] ramsey.sent_item 6.00GB/6.46GB (92.9%)
2024-07-16T15:19:24.747+0800 [#######################.] ramsey.sent_item 6.21GB/6.46GB (96.2%)
2024-07-16T15:19:27.816+0800 [#######################.] ramsey.sent_item 6.40GB/6.46GB (99.1%)
2024-07-16T15:19:28.762+0800 [########################] ramsey.sent_item 6.46GB/6.46GB (100.0%)
2024-07-16T15:19:28.762+0800 restoring indexes for collection ramsey.sent_item from metadata
2024-07-16T15:20:13.627+0800 finished restoring ramsey.sent_item (676453 documents)
2024-07-16T15:20:13.627+0800 done
C:\Users\administrator>