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.