环境准备
# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
vim /etc/security/limits.conf
* soft nproc 655350
* hard nproc 655350
* soft nofile 655350
* hard nofile 655350
https://www.mongodb.com/docs/manual/tutorial/install-mongodb-on-red-hat/
yum源配置
vim /etc/yum.repos.d/mongodb-org-6.0.repo
[mongodb-org-6.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/6.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc
sudo yum install -y mongodb-org
# to install a specific release of MongoDB
sudo yum install -y mongodb-org-6.0.0 mongodb-org-database-6.0.0 mongodb-org-server-6.0.0 mongodb-mongosh-6.0.0 mongodb-org-mongos-6.0.0 mongodb-org-tools-6.0.0
mongod 服务管理
systemctl enable mongod
systemctl start mongod
systemctl status mongod
修改监听地址
vim /etc/mongod.conf
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
systemctl restart mongod
用户授权认证
2、用户管理
# 进入mongo shell
# mongosh
#查看用户
show users
db.system.users.find().pretty()
#创建用户
test> use admin
switched to db admin
db.createUser({
"user" : "liam",
"pwd" : "Liam2022",
"roles" : [{
role: "root",
db: "admin"
}]
})
修改密码
use admin
db.changeUserPassword('root','Leiwang6')
db.auth("root","abc123") // 修改完密码需要重新运行db.auth验证授权
删除用户
use admin
db.dropUser('admin') //删除用户
4、配置文件开启认证
vim /etc/mongod.conf
security:
authorization: enabled
==========================
auth = true
5、用认证用户登录
# 方法一:
mongo
use admin
db.auth("admin","admin888")
#方法二:
# mongo admin -u admin -p admin888
# mongo 127.0.0.1:27017/admin -u admin -p admin888
3、数据库操作
# show dbs # 查看数据量
> use admin #切换数据库
switched to db admin
> use mydb # 创建数据库 或者切换数据库
> db # 查看当前所属的数据库
> show dbs # 这个时候查不到刚创建的数据 需要插入数据
> db.mydb.insert("name":"durotar")
> use mydb
> db.dropDatabase() # 删除数据库
> db.shutdownServer(); # 关闭数据库 类似systemctl stop mongodb
标签:use,admin,mongodb,配置,db,6.0,org,安装
From: https://www.cnblogs.com/wl30564/p/16541946.html