You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

安装MongoDb

cd /usr/local
rz -be  选择mongodb-linux-x86_64-rhel80-4.2.24.tgz

tar zxvf mongodb-linux-x86_64-rhel80-4.2.24.tgz -C /usr/local/
mv mongodb-linux-x86_64-rhel80-4.2.24 mongodb
rm -rf mongodb-linux-x86_64-rhel80-4.2.24.tgz

#添加环境变量
vi /etc/profile
export PATH=/usr/local/mongodb/bin:$PATH
#生效
source /etc/profile

cd /usr/local/mongodb
mkdir db
mkdir log

#设置文件夹权限
cd /usr/local/mongodb
chmod 777 db  
chmod 777 log 

# 上传配置文件
rz -be  
mongodb.conf
# 数据库目录
dbpath = /usr/local/mongodb/db

# 绑定IP
bind_ip = 0.0.0.0

# 端口
port = 27017

# 日志文件
logpath = /usr/local/mongodb/log/mongod.log

# 日志追加方式
logappend = true

# write操作首先写入“日记”是一个数据安全的设置
journal = true

# 后台运行
fork = true

# 是否以安全认证方式运行,默认是不认证的非安全方式
noauth = true
# auth = true

# 是不是每个数据库一个目录
directoryperdb = true

# 最大连接数
maxConns = 10000

启动MongoDB

/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/mongodb.conf

关闭MongdoDB

# 关闭数据库
/usr/local/mongodb/bin/mongod --shutdown --dbpath /usr/local/mongodb/db

分配管理员密码

mongo

use admin

#创建管理员账号
db.createUser({ user: "admin", pwd: "DsideaL147258369", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] })

# 关闭不用登录的实例,然后重新启动一次
# noauth = true
  auth = true

# 重启 MongoDB 服务
mongo
db.updateUser('root',{pwd:'DsideaL147258369',roles:[{role:'root',db:'admin'}]})
db.updateUser('root',{pwd:'DsideaL147258369',roles:[{role:'root',db:'ycharge'}]})

MongoDB4.28开启权限认证配置用户密码登录功能

https://www.jb51.net/article/234416.htm

常用命令

yum install net-tools -y
netstat -lanp | grep "27017"

mongo  #进入MongoDB控制台

show dbs #查看默认数据库

use admin  #切换到admin数据库

exit #退出MongoDB控制台
[root@hadoop-master bin]# mongo
use admin

#db.dropUser("root")
#db.dropUser("admin")

db.createUser({user:"root",pwd:"DsideaL147258369",roles:["root"]})
 show users

use yltcharge
db.createUser({user:"yltcharge",pwd:"yltcharge",roles:[{role:"readWrite",db:"yltcharge"}]})

#认证模式(true代表开启认证登录false代表未开启认证登录)
auth=true

重启动
/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/mongodb.conf

use admin
db.auth("root","DsideaL147258369")

use yltcharge
db.auth("yltcharge","yltcharge");