美文网首页
Centos安装和配置MongoDB4.2

Centos安装和配置MongoDB4.2

作者: 鹊南飞_ | 来源:发表于2020-11-18 11:00 被阅读0次

1. 配置yum源

  • 编辑文件
vim /etc/yum.repos.d/mongodb-org-4.2.repo
  • 新增以下内容
[mongodb-org-4.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc

2. 安装

  • 安装
yum install -y mongodb-org
  • 安装成功后,查看是否有以下目录,没有则新建
/var/lib/mongo (数据目录)
/var/log/mongodb (日志目录)

3. 运行

  • 启动
systemctl start mongod
  • 停止
systemctl stop mongod
  • 重启
systemctl restart mongod
  • 设置为开机自启
chkconfig mongod on

4. 设置远程访问

  • 查看配置文件位置
whereis mongod
# mongod: /usr/bin/mongod /etc/mongod.conf /usr/share/man/man1/mongod.1
  • 编辑文件
vim /etc/mongod.conf
  • 将bindIp:127.0.0.1 修改为:0.0.0.0
  bindIp: 0.0.0.0
  • 重启服务
systemctl restart mongod

5. 设置账户密码

  • 连接mongo
mongo
  • 切换数据库
use admin
  • 新增用户
db.createUser({
  user: 'user',  // 用户名
  pwd: '123456',  // 密码
  roles:[{
    role: 'root',  // 角色
    db: 'admin'  // 数据库
  }]
})
  • 查看用户是否添加成功
show users
  • 编辑配置文件
vim /etc/mongod.conf
  • 修改以下内容
#security:

修改为

security:
  authorization: enabled
  • 重启服务
systemctl restart mongod
  • 现在连接需要用户名和密码
// 方式一
mongo
use admin
db.auth('user', '123456')

// 方式二
mongo admin -u admin -p 123456

相关文章

网友评论

      本文标题:Centos安装和配置MongoDB4.2

      本文链接:https://www.haomeiwen.com/subject/mdaqiktx.html