MongoDB安装入门

Posted by Kaka Blog on January 17, 2019

安装

ubuntu版本:18.04 MongoDB版本:4.0.5

1、下载安装包

下载地址:https://www.mongodb.com/download-center/community,下载tgz包。

wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1804-4.0.5.tgz

2、解压到目录

tar -zxvf mongodb-linux-x86_64-ubuntu1804-4.0.5.tgz
sudo mv mongodb-linux-x86_64-ubuntu1804-4.0.5 /usr/local/mongodb/

配置MongoDB

创建数据和日志目录:

sudo mkdir -p /data/mongodb/data
sudo mkdir -p /data/mongodb/logs
sudo mkdir -p /data/mongodb/pid
sudo mkdir -p /usr/local/mongodb/mongodb-linux-x86_64-ubuntu1804-4.0.5/config

创建配置文件:

sudo vi mongod.cnf

配置文件内容:

systemLog:
  destination: file
  logAppend: true
  path: /data/mongodb/logs/mongod.log
storage:
  dbPath: /data/mongodb/data
  journal:
    enabled: true
processManagement:
  fork: true
  pidFilePath: /data/mongodb/pid/mongod.pid
net:
  port: 28123
  bindIp: 0.0.0.0
security:
  authorization: enabled

启动

sudo /usr/local/mongodb/bin/mongod -f /usr/local/mongodb/config/mongod.c
nf --fork

可能出现问题:./mongod: error while loading shared libraries: libcurl.so.4: cannot open shared object file: No such file or directory ,在启动./mongod 的时候缺少 libcurl.so 库。

解决:apt-get install libcurl4-openssl-dev

访问

mongo localhost:28123

创建管理账号

use admin
db.createUser(
... {user:"root",
... pwd:"Lsfu4BsSG3SG3Sg4",
... roles:[{role:"root",db:"admin"}]
... }
... )

重启服务即可。

参考

Install using .tgz Tarball on Ubuntu