How to create a database and configure a user in mongodb

Source: Internet
Author: User
Tags install mongodb mongodb mongodb commands

1. Install mongodb
Let's skip this step. Let's go to linux/47932.htm"> Centos to install MongoDB.

2. Create a database
Use tt
In this way, a database is created. If you leave the database without any operations, the database will be deleted by the system. Therefore, we need to execute the following command:
Db. usr. insert ({'name': 'tompig '});
Db. usr. insert ({'name': 'tompig1', 'id': 1 });

I just made a total of two tables. It doesn't matter. If you want to import the tables, you can delete them. At present, we just want to keep the database.

Run the following command to check whether the tt database is saved:
Show dbs

3. Configure the user
Use tt
Db. addUser ('mongodb ', '123 ');

Mongodb is the user name and 123456 is the password.

Okay, so that a database and the user who configures the database will be completed.

Common mongodb commands:
1. Help to view command prompts

Help

Db. help ();

Db. yourColl. help ();

Db. youColl. find (). help ();

Rs. help ();

2. Switch to/create a database

Use yourDB; when you create a set (table), the current database is automatically created.

3. Query all databases

Show dbs;

4. Delete the currently used database

Db. dropDatabase ();

5. Clone a database from a specified host

Db. cloneDatabase ("127.0.0.1"); clone the database data on the 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 data of mydb on the local machine to the temp database.

7. Fix the current database

Db. repairDatabase ();

8. View the currently used database

Db. getName ();

Db; the db and getName methods have the same effect. You can query the currently used database.

9. Display the current db status

Db. stats ();

10. Current db version

Db. version ();

11. View the address of the linked machine of the current db

Db. getMongo ();

Collection aggregation set

1. Create an aggregation set (table)

Db. createCollection ("collName", {size: 20, capped: 5, max: 100 });

2. Get the clustering set (table) with the specified name)

Db. getCollection ("account ");

3. Obtain all the aggregation sets of the current db.

Db. getCollectionNames ();

4. Display the status of all clustered indexes of the current db

Db. printCollectionStats ();

User-related

1. Add a user

Db. addUser ("name ");

Db. addUser ("userName", "pwd123", true); add a user, set a password, and check whether the user is read-only

2. Database Authentication and Security mode

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

3. Display all current users

Show users;

4. Delete a user

Db. removeUser ("userName ");

Others

1. Query previous error messages
Db. getPrevError ();
2. Clear error records
Db. resetError ();

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.