MongoDB connection security

Source: Internet
Author: User

In the past, when using relational databases, we could not access data without a user name or password. However, in MongDB, you can access the database without the user name or password by default and perform various operations. This is not safe for beginners. So let's talk about MongDB connection security today.

 

First, we can connect without using the user name and password because we didn't set permission authentication when starting the MongoDB service.

1. the startup method that can be accessed without the user name or password:

Mongod -- dbpath c: \ mongo_data -- logpath c: \ mongo \ logs \ logs.txt

You only need to provide -- dbpath and logpath.

2. Start mode with the user name and password:

Mongod -- dbpath c: \ mongo_data -- logpath c: \ mongo \ logs \ logs.txt-auth

-Auth (cateauthentication authentication) must be added)

 

To log on with a user name and password, you must have the user name and password. Where should we set the user name and password?

First, log on to the system in non-authenticated mode (no user name or password is required). Then we can see that the default system provides several libraries, including localhost and admin. Of course, we can also create a new database.

Each database has its own user table, and the admin database has all other permissions. Therefore, in the beginner stage, we use to add users to the admin database. When users are actually used, we use the users of each database.

Add User Images

 

Two different database connection methods

1. Do not use the user name and password:

Mongo m = new Mongo ("localhost", 27017); // access the local port 27017 by default

2. Use the user name and password:

Mongo m = new Mongo ("localhost", 27017 );

Booleanauth = db. authenticate (userName, password );

 

In this way, we probably learned how to secure our MongoDB connection. In the future, we will introduce MongDB's usage of the connection pool to learn about efficient connections.

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.