- Download the installation package and compressed package
- Install for Dummies
- Configure environment variables and start Mongo
1. Download the installation package and compressed package
Official website: Select the required version: https://www.mongodb.com/download-center/community;
2. Perform a fool-proof installation
1. Double-click the downloaded Mongo version file to enter the installation home page;
2. Click to accept the agreement, and then click "next";
3. Click "custom"
4. Customize the installation path;
5. Configure the data and Log directories
6. This step Don't check "Install MongoDB Compass". After this selection, you will download and compass, which takes a lot of time! Click "next";
7. If you need MongoDB Compass, you can directly download it from the official website and install it separately.
MongoDB visualization tool Compass download address: https://www.mongodb.com/download-center/compass?jmp=docs
8. The installation is complete;
3. Configure environment variables and start Mongo
1. Open cmd, execute mongo in the installation directory \bin, and enter the mongo command mode.
2. Enter the [Service] list, find MongoDB Server, and check whether the status is running.
3. Visit Http://localhost:27017
4. Configure environment variables
4. Set the database user name and password (configuration is optional)
1. Create an administrator account
Open cmd, enter mongo, and enter the command page of mongodb
use admin #Enter the admin database
switched to db admin
db.createUser({user:“admin”, pwd:“admin”, roles:[{role:“root”, db:“admin”}]})
db.createUser({user:“admin”, pwd:“ admin”, roles:[“root”]})
2. Modify the configuration file mongod.cfg (location: under the installation directory\bin)
First, change bind_ip to 0.0.0.0 (allow other computers to access it for remote connection, If bind_ip is 127.0.0.1, it can only be accessed locally)
Then find #security: Change it as shown in the figure below to enable security authentication.
security:
authorization: enabled #Pay attention to the indentation, refer to other values to change, if the indentation is wrong, it may cause the subsequent services to fail to restart
3. Restart the service
4. Log in for verification
Enter cmd, enter mongo, and enter the MongoDB shell.
use admin
witched to db admin
db.auth("username", "password")
db.auth(‘admin’,‘admin#passw0rd’)
1
show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
#Successful login
show dbs //展示数据库
use demo //切换数据库
db.createCollection() //创建表/集合
db.users.insert({id:123,name:'chen'}) //新增数据
db.users.find() //展示集合下的所有数据
db.users.findOne() //查询第一条数据
db.users.find({name.name:'chens'}) //按条件查询,多级查询
db.users.find({age:{$gt:20}}) //查找年龄大于20的 $lt小于 $gte大于等于 $lte小于等于
db.users.update({id:1},{$set:{name:'chens'}) //修改数据,第一个参数为修改的条件,第二个参数为修改的内容
db.user.remove({name:'chens'}) //按条件删除
show collections //展示表/集合
db.dropDatabase() //删除数据库
db.user.drop() //删除表/集合
mongoimport -d demo -c users --file /users/duma.json //导入数据
5. Using MongoDB compass tutorial