1. 程式人生 > >ubuntu18.4安裝MongoDB、啟動、配置

ubuntu18.4安裝MongoDB、啟動、配置

第1步: 匯入公鑰

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5

第2步:建立源列表檔案MongoDB

echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list

第3步:更新儲存庫

sudo apt-get update

第4步 :安裝MongoDB

sudo apt-get install -y mongodb-org

第5步:啟動MongoDB並將其新增為在啟動時啟動的服務:

systemctl start mongod
systemctl enable mongod

第一次啟動報錯:

(dev) [email protected]:~$ systemctl start mongod
Failed to start mongod.service: Unit mongod.service not found.
(dev) [email protected]:~$ sudo systemctl start mongodb
Failed to start mongodb.service: Unit mongodb.service not found.

需要配置一下:

1、建立配置檔案:

sudo nano /etc/systemd/system/mongodb.service

2.在裡面追加文字,並按ctrl+X退出

[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

3.啟動服務,檢視服務狀態

sudo systemctl start mongodb

sudo systemctl status mongodb

顯示如下資訊,表示啟動成功:

● mongodb.service - High-performance, schema-free document-oriented database
   Loaded: loaded (/etc/systemd/system/mongodb.service; disabled; vendor preset:
   Active: active (running) since Fri 2018-08-24 12:39:21 HKT; 11s ago
 Main PID: 21684 (mongod)
    Tasks: 19 (limit: 7054)
   CGroup: /system.slice/mongodb.service
           └─21684 /usr/bin/mongod --quiet --config /etc/mongod.conf

Aug 24 12:39:21 ubuntu systemd[1]: Started High-performance, schema-free documen

4.讓它永久啟動

sudo systemctl enable mongodb