Ubuntu 18.04服務器安裝MongoDB

1.導入包管理系統使用的公鑰。

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

執行此命令,如果收到的是OK就是正常的。但是,如果收到指示gnupg未安裝的錯誤,則可以:

//如果反饋不是OK 則執行這步
sudo apt-get install gnupg 
//在執行
wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -

2.爲MongoDB 創建文件

echo "deb [ arch=amd64,arm64,s390x ] http://repo.mongodb.com/apt/ubuntu bionic/mongodb-enterprise/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-enterprise.list

3.安裝MongoDB Enterprise軟件包

sudo apt-get update

4.安裝MongoDB Enterprise軟件包

sudo apt-get install -y mongodb-enterprise

注意:安裝完軟件包之後,需要給文件一些權限。

官方說明:

By default, MongoDB runs using the mongodb user account. If you change the user that runs the MongoDB process, you must also modify the permission to the /var/lib/mongodb and /var/log/mongodb directories to give this user access to these directories.

To specify a different log file directory and data file directory, edit the systemLog.path and storage.dbPathsettings in the /etc/mongod.conf. Ensure that the user running MongoDB has access to these directories.

sudo -R mongodb:mongodb /var/lib/mongodb

此處很關鍵,不然會出錯 :mongodb運行狀態 Active 等於failed。

更改mongod.conf配置文件中的bindIp地址。將127.0.0.1本地地址改爲.0.0.0.0通用地址

5.檢查mongodb安裝啓動是否成功

sudo systemctl start mongod

如果出現:Failed to start mongod.service: Unit mongod.service not found.

sudo systemctl daemon-reload

在執行一次啓動(sudo systemctl start mongod)

查看狀態:

sudo systemctl status mongod

確保服務器重啓後,自啓動MongoDB:

sudo systemctl enable mongod

停止MongoDB:

sudo systemctl stop mongod

重啓MongoDB:

sudo systemctl restart mongod

6.驗證Mongodb

在服務器終端輸入:

mongo  

use admin

show dbs

出現如下:

注意:如果有人出現更改mongod.conf文件配置中的authorization:enabled之後,show不出庫,是因爲開啓了驗證模式。

需要執行db.auth('root','123456'),在show dbs。然而我沒搞明白的是設置authorization:enabled之後Navicat使用SSH連接不上,因爲不知道在那個地方驗證。所以我沒設置。

創建賬號:

db.createUser({ user: "root", pwd: "123456", roles: [ { role: "root", db: "admin" } ] })
db.auth("root","123456")
//反饋1正確。

7.連接Navicat 管理工具。

我使用的是阿里雲服務器:控制檯-->實例-->(找到所屬服務器)-->更多-->網絡和安全組-->安全組配置-->添加安全組規則

將端口防火牆打開之後,連接Navicat:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章