MONGODB CREATE DATABASE and configure users

Source: Internet
Author: User
Tags auth mongodb create database

On an article we said MongoDB remote connection configuration, today to you how to create a database MongoDB and configure users, after all, the light has remote connection is not enough, we have to go up and operate and put data.
System: CentOS 5.x
Environment: MongoDB
1. Installing MongoDB
This step will not say, everyone to see the CentOS installation MongoDB.

2. Create a database
Use TT
This creates a database that will be deleted by the system if nothing is left. So we're going to execute the following command:
Db.usr.insert ({' name ': ' Tompig '});
Db.usr.insert ({' name ': ' TOMPIG1 ', ' ID ': 1});

I was casually the whole 2 tables, this does not matter, anyway to import the table, then delete the 2 will be OK, at present we just want to keep the database to stay.

Then use the command to see if there is a database to save the TT:
Show DBS

3. Configure the user
Use TT
Db.adduser (' MongoDB ', ' 123456 ');

MongoDB is the user name, 123456 is the password.

Well, such a database and configuration for this database user is complete.

MongoDB Common commands:
1. Help View command Prompt

Help

Db.help ();

Db.yourColl.help ();

Db.youColl.find (). Help ();

Rs.help ();

2. Switch/CREATE Database

Use Yourdb; The current database is created automatically when a collection (table) is created

3. Query all databases

Show DBS;

4. Delete the currently used database

Db.dropdatabase ();

5. Cloning a database from a specified host

Db.clonedatabase ("127.0.0.1"); Clones data from a database on a specified machine to the current database

6. Copy the specified database data from the specified machine to a database

Db.copydatabase ("MyDB", "temp", "127.0.0.1"), copy the native mydb data to the TEMP database

7. Repairing the current database

Db.repairdatabase ();

8. View the database currently in use

Db.getname ();

db The DB and GetName methods are the same effect and can query the currently used database

9. Display the current DB status

Db.stats ();

10. Current DB version

Db.version ();

11. View the current DB link machine address

Db.getmongo ();

Collection Aggregation Collection

1. Create a Clustered collection (table)

Db.createcollection ("Collname", {size:20, Capped:5, max:100});

2. Get a clustered collection with the specified name (table)

Db.getcollection ("account");

3. Get all the aggregation sets of the current DB

Db.getcollectionnames ();

4. Displays the status of all clustered indexes in the current DB

Db.printcollectionstats ();

User-related

1. Add a user

Db.adduser ("name");

Db.adduser ("UserName", "pwd123", true); Add user, set password, read-only

2, database authentication, security mode

Db.auth ("UserName", "123123");

3. Show all current users

Show Users;

4. Delete users

Db.removeuser ("UserName");

Other

1, the error message before the query
Db.getpreverror ();
2. Clear the Error Record
Db.reseterror ();

~~~~~~~~~~~~~~~~~

The default does not require account password, set auth = True in/etc/mongodb.conf to restart the account access function.

1, create the admin Super Privilege: MONGO

Use admin; Db.adduser (' root ', ' sa ') added root account, this time to execute show collections; prompt does not have permission, must first execute db.auth (' root ', ' sa '), return 1 to verify success! Return 0 description validation failed! When you log in directly to other tables, you must first use Admin;db.auth (' root ', ' sa '), then use Keke;show collections, or you will not have permissions.

2, if you want to access a single table, with a separate user name, you need to build the corresponding users in that table. MONGO Landing,

Use Admin;db.auth (' root ', ' sa ');

Use Keke;db.adduser (' Keke ', ' test '); This time can be directly used Keke account login Keke database.

MONGO use Keke;db.auth ("Keke", "Test"); show collections; right.

MONGODB CREATE DATABASE and configure users

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.