CentOS 7.x 安装MongoDB

1.配置yum源:

vim /etc/yum.repos.d/mongodb-org-4.4.repo

配置文件内容

[mongodb-org-4.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc

2.安装MongoDB:

yum install -y mongodb-org-4.4.1 mongodb-org-server-4.4.1 mongodb-org-shell-4.4.1 mongodb-org-mongos-4.4.1 mongodb-org-tools-4.4.1

3.MongoDB常用命令

# 查看mongod状态
systemctl status mongod.service
# 启动
systemctl start mongod.service
# 停止
systemctl stop mongod.service
# 自启
systemctl enable mongod.service

4.配置MongoDB

4.1创建MongoDB账号

# 命令行登录
mongo
# 查看数据库列表:
show dbs
# 启用身份验证:
use admin
# 创建管理员账号
db.createUser({user:'root', pwd:'root@2023', customData: {description: "管理员用户"}, roles:[ {role:'root' , db:'admin'}]})
# 验证账号 返回结果 1 则表示验证成功
db.auth("root","root@2023")
# 删除指定账号 root 为账号名
db.dropUser("root")
# 查看当前数据库下的账号
show  users

注意:上方的命令必须登录MongoDBshell中执行,mongo 命令的用法和 mysql 命令的用法差不多;

4.2.开启账号认证

MongoDB默认配置文件路径:/etc/mongod.conf

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.

# security config
security:
  authorization: enabled
  • 完整配置文件
# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# Where and how to store data.
storage:
  dbPath: /var/lib/mongo
  journal:
    enabled: true
#  engine:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

# 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.

# security config
security:
  authorization: enabled

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options

#auditLog:

#snmp:
# 开放防火墙端口:
firewall-cmd --zone=public --add-port=27017/tcp --permanent
firewall-cmd --reload
firewall-cmd --query-port=27017/tcp

# 重启Mongo
systemctl restart mongo

内网自用 我直接把防火墙关了 所以不需要配置 开放防火墙端口 索性我就直接重启Mongo

# 执行如下命令 输入密码就可以成功登录了
[root@test212 ~]# mongo -u root
MongoDB shell version v4.4.1
Enter password: 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值