Mongodb Ubuntu安装
1.更新软件源+导入MongoDB的GPG密钥
sudo apt update
sudo apt install -y dirmngr wget gnupg apt-transport-https ca-certificates software-properties-common gnupgwget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
2.安装mongodb
sudo apt update
sudo apt install -y mongodb-orgsudo apt install mongodb-org-shell mongodb-org-tools
3.为mongodb添加权限,否则有可能启动不了
sudo chown -R mongodb:mongodb /var/lib/mongodb
sudo chown mongodb:mongodb /tmp/mongodb-27017.sock
然后Mongodb重启
sudo service mongod restart
4.启动mongodb服务
sudo systemctl enable mongod --now
sudo systemctl status mongod #查看服务状态
mogodb开启身份验证
1.编辑mongod.conf
sudo vim /etc/mongod.conf
2.在文件中添加
security:authorization: enabled
若需要开启远程连接
修改 /etc/mongod.conf中 bindIP为0.0.0.0
net:port: 27017bindIp: 0.0.0.0
进入mongosh
创建用户
db.createUser({user: "root",pwd: "password",roles: [ { role: "userAdminAnyDatabase", db: "mydatabase" } ]}
)
为数据库添加用户
db.grantRolesToUser("root", [ { role:"dbOwner", db:"mydatabase"} ]) ;
查询语句
索引ts字段大于2016-01-24T12:52:33.341Z 小于 2023-07-21T12:52:33.341Z
{ "ts": { $gt: ISODate("2016-01-24T12:52:33.341Z").valueOf(), $lt: ISODate("2023-07-21T12:52:33.341Z").valueOf()} }