【环境配置】Ubuntu20.04下安装MongoDB

安装说明见官方文档
本文以MongoDB社区版(mongodb-org-5.0)为例,系统为Ubuntu20.04。

MongoDB安装

sudo apt-get install gnupg
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
sudo apt-get update
sudo apt-get install -y mongodb-org

安装过程中会自动创建/var/lib/mongodb和/var/log/mongodb目录,并使用mongodb作为用户。

启动服务

sudo systemctl start mongod
sudo systemctl enable mongod
sudo systemctl status mongod

可能无法启动,报错如下:

● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Sun 2022-02-06 10:39:14 CST; 1s ago
       Docs: https://docs.mongodb.org/manual
    Process: 29891 ExecStart=/usr/bin/mongod --config /etc/mongod.conf (code=exited, status=14)
   Main PID: 29891 (code=exited, status=14)

此时需要修改目录权限,再重新启动即可。

sudo chown -R mongodb:mongodb /var/lib/mongodb/
sudo chown -R mongodb:mongodb /var/log/mongodb/
sudo chown -R mongodb:mongodb /tmp/*.sock
 mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor prese>
     Active: active (running) since Sun 2022-02-06 10:40:11 CST; 1s ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 29998 (mongod)
     Memory: 58.7M
     CGroup: /system.slice/mongod.service
             └─29998 /usr/bin/mongod --config /etc/mongod.conf

你可能感兴趣的:(mongodb,数据库,database)