# install

## 環境

安裝環境：ubuntu 18.04

windows沒裝過

## 安裝流程

### 1. 在ubuntu新增public key

先直接使用官網語法取得key

```
wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -
```

如果有gnupg相關錯誤：

```
 sudo apt-get install gnupg
```

如果出現錯誤訊息no valid openpgp data found，可以嘗試將語法拆開來輸入

先輸入

```
wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc
```

再輸入

```
sudo apt-key add server-4.2.asc
```

\[暴力解]如果還是不行，直接手動產生一個.asc檔，再將url中的key貼上儲存

### 2. 創建list file

要在/etc/apt/sources.list.d/建立檔案，內容包含server到件網址，語法：

```
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list
```

若執行時遇到certificate is not trusted驗證問題，可嘗試將上述檔案中的連結的HTTPS的S拿掉試試看，

變成：

```
echo "deb [ arch=amd64 ] http://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list
```

### 3. 重新載入上述設定

```
sudo apt-get install -y mongodb-org
```

### 4. 開始安裝

預設安裝目前最新版本

```
sudo apt-get install -y mongodb-org
```

### 5. 檢查安裝結果

重啓、啓動、停止、查看mongoDB的狀態

```
sudo systemctl status mongodb
sudo systemctl stop mongodb
sudo systemctl start mongodb
sudo systemctl restart mongodb
```

或者

```
sudo service mongod start
sudo service mongod status
sudo service mongod stop
sudo service mongod restart
```

確認開啟後(running)

打開新的vmd輸入，看看能否正常連線：

```
mongo
```

或者

```
mongo --eval 'db.runCommand({ connectionStatus: 1 })'
```

若回傳資訊也包含1則表示成功

參考文章：\
<https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/>\
<https://www.howtoing.com/how-to-install-mongodb-on-ubuntu-18-04>\
<https://www.twblogs.net/a/5c8e694cbd9eee35fc1528a2>\
<https://medium.com/feveral%E7%9A%84%E7%A8%8B%E5%BC%8F%E7%AD%86%E8%A8%98/%E5%9C%A8-ubuntu-%E4%B8%8A%E5%AE%89%E8%A3%9D-mongodb-df3ef7f4279f>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://stb11816.gitbook.io/python_note/database/mongodb/install.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
