MongoDB permissions, Backup, restore, de-weight

Source: Internet
Author: User

MongoDB permissions, Backup, restore, de-weight
  1. Permissions

    Data security is a critical part of a database, so here's a general procedure for setting up MongoDB user rights.

    First, create a new database administrator in unlicensed mode:

    Start the database service:

    mongod

    Enable the Named line tool:

    mongouse admindb.createUser({user:"gly",pwd:"[email protected]",roles:[{role:"userAdminAnyDatabase",db:"admin"}]})

    Then, create a new user for the specified database:

    db.createUser({user:"testdb",pwd:"[email protected]",roles:[{role:"readWrite",db:"testdb"},{role:"userAdmin",db:"testdb"},{role:"dbAdmin",db:"testdb"}]})

    Finally, access the data in authorization mode:

    mongod -auth

    Enable the Named line tool:

    mongouse testdbdb.auth("testdb","[email protected]")db.CompanyCards.find({}).limit(1)
    Attention:

    ? When setting a password, do not use ' @ ' as much as possible, otherwise you will not be able to connect when you configure the database connection string, such as Connection field string mongodb://testdb:[email protected][email protected]:27017/testdb?authSource=testdb , ' @ ' as database user name: The delimiter between the password and the database host address, so you cannot use ' @ '; the correct connection string can be configured as: mongodb://testdb:[email protected]:27017/testdb?authSource=testdb. Both the data and user names in the connection string above are testdb.

  2. Backup (Export) command:
    mongoexport -u UName -p [email protected] -h 127.0.0.1:27017 -d DBName -c CName --type csv -o C:\DBak\CName_201708.csv -f Name,Code,Status
    Interpretation:

    Data Export Tool Mongoexport

    Run:

    Results:

    Execution time: about 18 minutes; Export data: 68,668,050; export file: 4,159,994kb; test server: 2 core 8G.

    Example of exported file data:

  3. Restore (Import) command:
      mongoimport-h 127.0.0.1:27017-d dbname-c CName--type csv--file C:\DBak\CName_20170800.csv-u uname-p [email protected]--fields name.string (), code.string (), Status.int32 ()--columnshavetypes  

    When I import, I display fields that specify document, and for the exported data, I don't want the first line because it's the header row. Then there are two ways: one, import directly, then delete, two, delete the title, and then import.

    Method One:

    Method Two:

    Interpretation:

    Data import Tool Mongoexport

    run:

    results:

    Execution time: about 27 minutes; import data: 68,668,050; Import files: 4 , 227,053KB; test server: 2 core 8G.

  4. To re-index:
    use testdbdb.auth("testdb","[email protected]")db.CompanyCards.createIndex( { Name: 1 } )
    Command:
    db.CompanyCards.aggregate([{ $group: { _id: {Name: ‘$Name‘}, count: {$sum: 1}, dups: {$addToSet: ‘$_id‘}}},{ $match: {count: {$gt: 1}}}],{ allowDiskUse: true }).forEach( function(doc){ doc.dups.shift(); db.CompanyCards.remove({_id: {$in: doc.dups}});});

    Note: Disk storage is allowed because of the large collection of data that we operate allowDiskUse:true .

    Run:

    Because it takes a long time, I don't write the result.

    The following data are the results of my previous tests and provide references:

    ? Go to Heavy before: 1,263,765 article
    ? Go to weight after: 1,258,414 article
    ? Total time: 53.151 s
    ? Filter out: 5,351 strips

    ? Notebook: 4-core 8G,I5 processor.

  5. More

    For more information, see MongoDB official online documentation MongoDB Documention.

MongoDB permissions, Backup, restore, de-weight

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.