table of Contents
[MongoDB detailed tutorial] First, Mac install MongoDB
[MongoDB detailed tutorial】 Second, MongoDB basic operation
[MongoDB detailed tutorial] Third, the advanced query
[MongoDB detailed tutorial] four, python operation MongoDB
[MongoDB detailed tutorial] five , MongoDB database management
1, the database security
Add user and password for the database
1.1, create an administrator account and password
- Login without password database
- Create (or switch to) admin Library
- Create an administrator account and password
- Administrator account and password verification
Example:
# 1
cbowendeMacBook-Pro:~ cbowen$ mongo
MongoDB shell version v4.0.9
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
...
>
# 2
> use admin
switched to db admin
# 3
> db.createUser({user:'admin',pwd:'123',roles:["root"]})
Successfully added user: { "user" : "admin", "roles" : [ "root" ] }
# 4
> db.auth('admin','123')
1
"Roles": [ "root"] is intended to assign the super administrator privileges
1.2, setting the service state to authenticate the user needs
Normal start the service does not require the user to verify the information, even if the user has been created.
Here we must first shut down the database service, and start the service needs to verify the user's mode.
- Close Service
- Restart restart the service, and set to require authentication
# 终端中输入:
mongod --dbpath data --logpath log/mongod.g --logappend --auth
Normally open behind the statement with --auth.
(For the windows system, if you have to join the service, you need to uninstall rejoin)
1.3, create a user account and password
- To connect to the database administrator
- Switch to the account you want to create a database, and create a user account and password
- Before switching to log in to the library, then landing
Example:
# 1、以管理员身份连接数据库
cbowendeMacBook-Pro:~ cbowen$ mongo
MongoDB shell version v4.0.9
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("5316d74a-2148-43e9-a9e8-f501cc1d9781") }
MongoDB server version: 4.0.9
> use admin # 验证账号前要先切换到admin库
switched to db admin
> db.auth('admin','123')
1
# 2、切换到要创建账号的库,并创建账号和密码,以及设置权限roles
> use mymongo
switched to db mymongo
> db.createUser({user:'mymongo',pwd:'123',roles:[{role:'dbOwner',db:'mymongo'}]})
Successfully added user: {
"user" : "mymongo",
"roles" : [
{
"role" : "dbOwner",
"db" : "mymongo"
}
]
}
# 3、退出管理员账号,用刚创建的用户登陆
> exit
bye
cbowendeMacBook-Pro:~ cbowen$ mongo
MongoDB shell version v4.0.9
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("47cb777a-d000-41be-a61c-1041faa31dc0") }
MongoDB server version: 4.0.9
> use mymongobu # 先切换数据库
switched to db mymongobu
> db.auth('mymongobu','123') # 再验证账号
1
Note:
. 1, Roles: [{Role: 'dbowner', DB: 'mymongo'}] is intended only for the account database mymongo effective.
2, the user's information is stored as data in a database, which database before the switch to create the user information, user information created in which the library will be saved, then landing time also first to switch to the library, before verification is successful. If you create mymongo user, admin is in the library, you must first switch to the verification database admin, after verifying the same effect, mymongo account remains valid only for mymongo library.
1.4, forgotten password / change password
After the super administrator to log in to or connect to the database without having to authenticate a user mode,
change the user name, use the following command:
db.changeUserPassword('用户名','新密码');
2, from the master server
From the server as the primary server redundancy, improve data availability and ensure data security, recover data from hardware failures and service interruption in.
Is responsible for writing the master server, from the server is responsible for reading, after the primary server goes down, the server is automatically switched from the primary server.
2.1, Creating a server directory for storing primary data from the server, respectively
Create a folder in the LordSlave MongoDB directory, and create a lord and slave folders LordSlave in.
(MongoDB directory need to enter in finder press shift + command + G to query / usr / local and go.)
2.2, start the service
grammar
# 终端中输入 (IP地址、集群名称要一致):
mongod --bind_ip IP地址 --port 端口1 --dbpath 主服务器目录 --replSet 集群名称
mongod --bind_ip IP地址 --port 端口2 --dbpath 从服务器目录 --replSet 集群名称
Examples
mongod --bind_ip 127.0.0.1 --port 27017 --dbpath /usr/local/MongoDB/LordSlave/lord --replSet rps
mongod --bind_ip 127.0.0.1 --port 27018 --dbpath /usr/local/MongoDB/LordSlave/slave --replSet rps
Terminal window open services do not quit.
2.3, and connected to one of the servers as the master server
Enter the following command in the new terminal window, landing port 27017
mongo --host 127.0.0.1 --port 27017
Use "rs.initiate ()" command to initialize and display the result:
> rs.initiate()
{
"info2" : "no configuration specified. Using a default configuration for the set",
"me" : "127.0.0.1:27017",
"ok" : 1,
"operationTime" : Timestamp(1572401660, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1572401660, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
2.4, on the master server to add / remove from another server
Continue setting up the current input terminal of the primary server:
rs.add('127.0.0.1:27018')
Show results:
rps:PRIMARY> rs.add('127.0.0.1:27018')
{
"ok" : 1,
"operationTime" : Timestamp(1572402066, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1572402066, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
Use "rs.status ()" query server status:
To delete from the server is used
rs.remove('127.0.0.1:27018')
2.5, and activate the landing from the server
Landing another server in the new terminal:
mongo --host 127.0.0.01 --port 27018
Use "rs.slaveOk ()" activation from the server
rps:SECONDARY> rs.slaveOk()
At this point, from the main server to complete the configuration, you can configure multiple servers from.
3, backup and restore
3.1, database backup
grammar:
mongodump -h dbhost -d dbname -o dbdirectory
# 如果账号有密码的话加上 -u 和 -p 属性
mongodump -h dbhost -u user -p pass -d dbdirectory
- -h: server address, you can also specify the port number
- -d: database name to be backed up
- -o: Backup data storage location, this directory contains the data back out of
- -u: user name, in quotes
- -p: password, in quotes
Example:
- First open database service
- Create a backup folder directory / usr / local / MongoDB / bu
- Input terminal:
mongodump -h 127.0.0.1:27017 -d mymongo -o /usr/local/MongoDB/bu
- The following message appears backup success
cbowendeMacBook-Pro:~ cbowen$ mongodump -h 127.0.0.1:27017 -d mymongo -o /usr/local/MongoDB/bu
2019-10-29T15:43:55.109+0800 writing mymongo.singer to
2019-10-29T15:43:55.109+0800 writing mymongo.students to
2019-10-29T15:43:55.114+0800 done dumping mymongo.singer (15 documents)
2019-10-29T15:43:55.115+0800 done dumping mymongo.students (9 documents)
- Backup files as follows, the system automatically creates a database file with the same name as the folder mymongo
3.2, database restore
grammar:
mongorestore -h dbhost -d dbname --dir dbdirectory
# 如果账号有密码的话加上 -u 和 -p 属性
mongorestore -h dbhost -d dbname -u user -p pass --dir dbdirectory
- -h: Server Address
- -d: database instance needs to be restored (if using a new name, it will re-create a library)
- --dir: the location of the backup data can be omitted --dir keywords, write directly address.
(Automatically creates a backup when the backup directory specified in a file with the same name as the library folder backup, restore to add this folder in the path, look at the following specific examples.)
Example:
- Open the database service
In a terminal
mongorestore -h 127.0.0.1 -d mymongobu --dir /usr/local/MongoDB/bu/mymongo
Note: The
address of the backup is / usr / local / MongoDB / bu
with the same name as the library file is automatically created when mymongo backup folder, you need to add back / mymongo restore- The following message appears the restore success
cbowendeMacBook-Pro:~ cbowen$ mongorestore -h 127.0.0.1 -d mymongobu --dir /usr/local/MongoDB/bu/mymongo
2019-10-29T16:03:32.386+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
2019-10-29T16:03:32.386+0800 building a list of collections to restore from /usr/local/MongoDB/bu/mymongo dir
2019-10-29T16:03:32.390+0800 reading metadata for mymongobu.singer from /usr/local/MongoDB/bu/mymongo/singer.metadata.json
2019-10-29T16:03:32.390+0800 reading metadata for mymongobu.students from /usr/local/MongoDB/bu/mymongo/students.metadata.json
2019-10-29T16:03:32.440+0800 restoring mymongobu.singer from /usr/local/MongoDB/bu/mymongo/singer.bson
2019-10-29T16:03:32.498+0800 no indexes to restore
2019-10-29T16:03:32.498+0800 finished restoring mymongobu.singer (15 documents)
2019-10-29T16:03:32.498+0800 restoring mymongobu.students from /usr/local/MongoDB/bu/mymongo/students.bson
2019-10-29T16:03:32.500+0800 restoring indexes for collection mymongobu.students from metadata
2019-10-29T16:03:32.538+0800 finished restoring mymongobu.students (9 documents)
2019-10-29T16:03:32.538+0800 done
3.3, a set of backup and restore
# 备份单个集合
mongodump -h dbhost -d dbname -c collectionname -o dbdirectory
# 还原单个集合
mongorestore -h dbhost -d dbname --dir dbdirectory
Here is to be noted that, when dbdirectory backup directory, but when dbdirectory reduction of .bson want to assign to specific files.
Example:
# 备份集合
mongodump -h 127.0.0.1:27017 -d mymongo -c singer -o /usr/local/MongoDB/bu/collection
#还原集合
mongorestore -h 127.0.0.1:27017 -d mymongobu --dir /usr/local/MongoDB/bu/collection/mymongo/singer.bson