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: 

你可能感兴趣的:(CentOS服务器环境搭建,centos,mongodb,linux)