Ubuntu下安装MongoDB 7
1. sudo sed -i 's/http:\/\/archive.ubuntu.com/https:\/\/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
2. sudo apt update && sudo apt upgrade -y
3. sudo apt install gnupg wget apt-transport-https ca-certificates software-properties-common
4. wget -qO- https://pgp.mongodb.com/server-7.0.asc | gpg --dearmor | sudo tee /usr/share/keyrings/mongodb-server-7.0.gpg >/dev/null
5. echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/7.0 multiverse" | sudo tee -a /etc/apt/sources.list.d/mongodb-org-7.0.list
6. sudo apt update
7. sudo apt install mongodb-org
8. sudo systemctl enable mongod
9. sudo systemctl restart mongod
10. mongosh --eval 'db.runCommand({ connectionStatus: 1 })'
{
authInfo: { authenticatedUsers: [], authenticatedUserRoles: [] },
ok: 1
}
12. sudo vi /etc/mongod.conf
security:
authorization: enabled
13. sudo systemctl restart mongod
mongosh
Current Mongosh Log ID: 66ab83bcf8edc03d55838725
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.15
Using MongoDB: 7.0.12
Using Mongosh: 2.2.15
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.
test> use admin
switched to db admin
admin> db.createUser(
... {
... user: "terryMongoDBAdmin",
... pwd: passwordPrompt(),
... roles: [
... { role: "userAdminAnyDatabase", db: "admin" },
... { role: "readWriteAnyDatabase", db: "admin" }
... ]
... }
... )
Enter password
*******{ ok: 1 }
admin> quit()
14. mongosh -u terryMongoDBAdmin -p --authenticationDatabase admin
Enter password: *******
Current Mongosh Log ID: 66ab862a4bdc09fa40838725
Connecting to: mongodb://<credentials>@127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&authSource=admin&appName=mongosh+2.2.15
Using MongoDB: 7.0.12
Using Mongosh: 2.2.15
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
test> show users
[]
test> use admin
switched to db admin
admin> show users
[
{
_id: 'admin.terryMongoDBAdmin',
userId: UUID('6e5ca4b1-190c-4b3e-8046-2bef961ac38b'),
user: 'terryMongoDBAdmin',
db: 'admin',
roles: [
{ role: 'userAdminAnyDatabase', db: 'admin' },
{ role: 'readWriteAnyDatabase', db: 'admin' }
],
mechanisms: [ 'SCRAM-SHA-1', 'SCRAM-SHA-256' ]
}
]
参考文档:https://www.mongodb.com/zh-cn/docs/manual/