Five: MongoDB import, export, backup, restore, user authorization
One: Data export, data import
As a DBA (administrator), you often encounter the need to import and export data, as described in the following utilities (bring-your-own):
1: Data import Mongoexport
Export files in CSV format
./mongoexport-d test-c User1-o/tmp/user1.out--Specify the location and filename of the export file
Database collection (table)
./mongoexport-d my_mongodbtest-c User--csv-f Uid,name,age-o User_csv.dat
Parameter description:
-D is the database
-C is a collection
-O Output filename
-csv refers to export to CSV format
-F indicates that those columns (fields) need to be exported
More detailed usage can be viewed mongoexport-h
2: Data import Mongoimport
./mongoimport-d test-c User1/tmp/user1.out--Import the/tmp/user1.out file into the test database User1 collection
Second: Data backup, data recovery--backup files are a bunch of bson files
As a DBA (administrator), it is very important to have a backup of the data needed to import and export the data, as described in the following utilities (bring your Own):
1: Data backup Mongodump (a large collection will have a Bson file)
./mongodump-d Test-o/tmp/--Back up the test database to/tmp/, you will have a/tmp/test/xxx.bson ... File
-D Database
-H View Detailed parameters
* If you do not specify a directory, the default is to create a dump directory under the current file directory (BIN) to hold the backed up files
You can specify a directory: where to go after adding-o/tmp/my_mongodb_dump backup
2: Data Recovery Mongorestore
./mongorestore-d test/tmp/test--Restore/tmp/test data to test database
III: User security and authentication (user authorization)
MongoDB administrator: Super Administrator, administrator for each database
There are many users in the database in each instance of the MongoDB instance, and if security authentication is enabled, only users with database authentication can read and write.
MongoDB default startup is not to verify the user name and password, start MongoDB can be used directly with the MONGO link, all the data have root permissions.
So when you start specifying parameters, you can block client access and links.
Simply specify the--auth parameter at startup
Detailed operation steps: (The first time comes in the time by default is the Super administrator, the login time adds the--auth to enter when can set the permission)
1->./mongod--auth--dbpath=/usr/local/mongodb/data/--logpath=/usr/local/mongodb/dblogs--fork start MongoDB
1-> Show DBS
2-> Use admin--enter MongoDB, show DBS to see the admin database
3-> db.adduser ("root", "123456"); Enter--Create a Super Administrator, enter a value readonly:false indicates a readable writable
4-> Exit--Set you'll find nothing to do with birds, quit logging in again
5->./mongo-h--Take a look at the parameters-u-p
6->./mongo localhost:27017/test Anonymous user logged into the test database, you will find that you cannot manipulate
7->./mongo-uroot-p123456 Localhost:27017/test--you will find this is not possible, here is the test, must enter the admin database validation
--because we have just added the root user in the admin database Plus, is the admin library and the entire station super user, to be authenticated under the admin login
8->/mongo-uroot-p123456 Localhost:27017/admin--that's it.
Go to test to add a user (as is the case with other created libraries)
* Note: Users added in test can only have permission to test data, can not operate other libraries, admin inside is the Super administrator can
Use test
Db.adduser ("user1", "123456");
./mongo-uuser1-p123456 Localhost:27017/test--Can be verified
After Setup: The next time you use MongoDB, you have to:
1: Start MongoDB
./mongod--auth--dbpath=/usr/local/mongodb/data/--logpath=/usr/local/mongodb/dblogs--fork
2: User login:
./mongo-uroot-p123456 Localhost:27017/admin
MongoDB import, export, backup, restore, user authorization (iv)