在终端输入GPK码
wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -
添加mongoDB源
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list
sudo apt update
Install MongoDB Server
sudo apt install mongodb-org=4.2.1 mongodb-org-server=4.2.1 mongodb-org-shell=4.2.1 mongodb-org-mongos=4.2.1 mongodb-org-tools=4.2.1
配置启动
sudo vim /etc/systemd/system/mongodb.service
填入以下内容
[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
[Service]
User=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf
[Install]
WantedBy=multi-user.target
如果设置需要登陆验证
[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
[Service]
User=mongodb
ExecStart=/usr/bin/mongod --auth --config /etc/mongod.conf
[Install]
WantedBy=multi-user.target
设置远程访问
sudo vim /etc/mongod.conf
把 bind_ip=127.0.0.1 这一行注释掉或者是修改成 bind_ip=0.0.0.0,最好是直接注释掉
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
bindIp: 0.0.0.0
# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo
#security:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
~
启动
sudo systemctl start mongodb
连接mongo
mongo
添加用户
db.createUser({user: "admin", pwd: "123456",roles: [ { role: "root", db: "admin"} ]})