Install mongodb on Windows system and set username and password
- 1. Download and install
-
- 1.1 Download
- 1.2 Installation
- 2.Set username and password
-
- 2.1 Find the configuration file
- 2.2 Set username and password
-
- 2.2.1 Navicat operation
- 2.2.2 Modify configuration file
- 2.3 Verification
- 3.SpringBoot integration
1. Download and install
1.1 Download
The official website download address will detect the installation package according to the system:
The version installed by Windows is consistent with the version in [Deployment 02].
1.2 Installation
Double-click the downloaded installation package mongodb-windows-x86_64-6.0.10-signed.msi
to start the installation:
MongoDB Windows installers typically provide the following components and options:
-
Server (Server): MongoDB database server, which is the core component of MongoDB. It is responsible for storing and managing data, handling client requests, and providing interaction with the MongoDB database. Installing the server components is a necessary part of installing MongoDB.
-
Router (router, also called mongos): The MongoDB router, often called
mongos
, is the sharding component of MongoDB. It is used to route client requests to the corresponding shards in a MongoDB sharded cluster. It is important to usemongos
in MongoDB deployments with sharded clusters, but installation may not be required in simple non-sharded deployments. -
Miscellaneous Tools (Various tools): This option usually contains various auxiliary tools and utilities for MongoDB. These tools can help you manage the MongoDB database and perform various tasks. These include:
mongoimport
: Tool for importing data into MongoDB database.mongoexport
: Tool for exporting data from MongoDB database.mongodump
: A tool for backing up MongoDB databases.mongorestore
: Tool for restoring MongoDB backups.mongostat
: Tool for monitoring MongoDB server status.- etc…
Depending on your needs and the nature of your MongoDB deployment, you may choose to install these additional tools or not. Typically, if you only need MongoDB’s core database server and don’t need sharding capabilities or other advanced features, then installing just the server component is enough. If you need to perform tasks such as data import and export, backup and restore, then installing various tools will be very useful.
This installation only installs Server
and does not need to install MongoShell
. Click Next to set the data and log directories [can also be modified through the configuration file]:
[Installation is time-consuming. If you have a visualization tool such as Navicat] you can check MongoDB Compass:
Click Next and wait for the installation to be successful:
- After successful installation, you can view the MongoDB service in the system service.
- Or enter
localhost:27017
in the browser to view
2. Set username and password
2.1 Find the configuration file
Find the configuration file according to the instructions on the official website:
For the configuration items in the configuration file, please view the detailed documentation on the official website:
Default configuration document content:
storage: directoryPerDB: true dbPath: D:\Program Files\MongoDB\Server\6.0\data journal: enabled: true systemLog: destination: file logAppend: true path: D:\Program Files\MongoDB\Server\6.0\log net: port: 27017 bindIp: 127.0.0.1
2.2 Set username and password
There are many implementations using mongosh
. Here we use the visualization tool Navicat
to implement it. Do not enable authorization
yet and then enable it after creating the user and password.
2.2.1 Navicat operation
1. Create a database, click Database > Click Roles to view all roles.
Here is a brief description of the permissions and purpose of these roles:
- dbAdmin (database administrator)
Permissions: Allows the user to manage the database, including creating and deleting collections, indexing, and viewing statistics.
Purpose: Suitable for users who need to manage the database structure but do not require global permissions. - dbOwner (database owner):
Permissions: Has all permissions on the database, including read, write and management permissions on all collections in the database.
Purpose: Usually the owner of the database, with the highest level of permissions. - enableSharding (enable sharding permissions):
Permissions: Allows users to enable sharding on the database to distribute data across multiple shard servers.
Purpose: Suitable for users who set up sharded clusters to process large amounts of data. - read (read permission):
Permissions: Allow users to perform read operations on data in the specified database.
Purpose: Allows users to query and read data in the database, but cannot perform write operations. - readWrite (read and write permissions):
Permissions: Allow users to perform read and write operations on data in the specified database.
Purpose: Allows users to perform read and write data operations in the database, including insert, update, and delete operations. - userAdmin (user administrator):
Permissions: Allow users to manage users and roles in the database, including creating, modifying, and deleting users.
Purpose: Suitable for users who need to manage database users.
Please note that the above are general permissions and purpose descriptions of these roles. In fact, MongoDB’s roles and permissions can be configured in a more fine-grained manner to meet specific application needs. Role permissions can also be customized according to the needs of the database. Therefore, in actual use, you can configure and assign roles according to specific access control needs.
- Set username and password
- Set up roles
Click on the script preview to view the SQL:
db.createUser({<!-- --> user: "testadmin", pwd: "123456", roles: [ {<!-- --> role: "dbAdmin", db: "test" } ], authenticationRestrictions: [ ] })
Just save the role after setting it up.
2.2.2 Modify configuration file
Modify the configuration file and add security.authorization
configuration, as shown below:
Restart the service:
2.3 Verification
Reconnect directly to the database:
Instructions require user authentication and connection modification:
The connection test is successful, but:
Close security.authorization
and restart the MongoDB service, set the user’s role to dbOwner
, then enable verification, and then restart MongoDB:
Reconnection successful.
3.SpringBoot integration
rely:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
Configuration:
# can be configured like this spring: data: mongodb: uri: mongodb://testadmin:123456@localhost:27017/test
Another configuration [an error] Command failed with error 18:
com.mongodb.MongoCommandException: Command failed with error 18 (AuthenticationFailed): 'Authentication failed.' on server localhost:27017. The full response is {<!-- -->"ok": 0.0, "errmsg": "Authentication failed.", "code": 18, "codeName": "AuthenticationFailed"}
# can also be configured like this spring: data: mongodb: host: localhost port: 27017 database: test # Correct configuration username: testadmin password: '123456' # Configuration of error reporting [Password without quotation marks] username: testadmin password: 123456
Inject MongoTemplate
to use:
@SpringBootTest public class MongoTemplateTest {<!-- --> @Resource private MongoTemplate mongoTemplate; }
We will continue with the specific methods next time.