【mongodb環境】在ubuntu上搭建mongodb,而且開啓遠程鏈接

【mongodb環境】在ubuntu上搭建mongodb,而且開啓遠程鏈接

標籤(空格分隔): mongodbmongodb


參考資料
mongodb 官方文檔數據庫

安裝mongodb

第一步:設置 public key

Import the public key used by the package management system.ubuntu

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6

第二步:添加包的源

Create a list file for MongoDB.¶vim

Create the /etc/apt/sources.list.d/mongodb-org-3.4.list list file using the command appropriate for your version of Ubuntu:數組

執行如下命令:添加包的源安全

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

若是是阿里雲的app

deb [ arch=amd64,arm64 ] http://mirrors.aliyun.com/mongodb/apt/ubuntu xenial/mongodb-org/3.4 multiverse

第三步:更新上面添加的包源

Reload local package database.tcp

Issue the following command to reload the local package database:工具

執行如下命令:更新ubuntu的包測試

sudo apt-get update

第四步:安裝mongodb 須要包和工具

Install the MongoDB packages.

Install the latest stable version of MongoDB.

Issue the following command:

sudo apt-get install -y mongodb-org

開始使用mongodb

Run MongoDB Community Edition

開啓mongod 服務

sudo service mongod start

中止mongod 服務

sudo service mongod stop

重啓mongod服務

sudo service mongod restart

卸載Uninstall MongoDB Community Edition

第一步:中止mongodb

sudo service mongod stop

第二步:移除關於mongodb的包

sudo apt-get purge mongodb-org*

第三步:移除關於mongodb的數據目錄

移除mongodb數據庫和日誌文件
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb

開啓遠程能夠鏈接

修改/etc/mongod.conf
vim /etc/mongod.conf
# 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: [127.0.0.1,59.110.143.210]


#processManagement:

#security:

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

找到bind_ip

修改爲下面的方式
net:
  port: 27017
  #bindIp: [127.0.0.1,59.110.143.210] # 注意這個ip 是這個主機
的ip 2017年10月30日修改,不須要這樣作了
  bindIp:  # 把這ip去掉

怎麼查看主機的ip:ifconfig

坑:記得格式不是 加空格 也不是加逗號,以數組的方式

ok

重啓mongodb

sudo service mongod restart

更改端口的話須要加防火牆

編輯防火牆方式:

vim /etc/iptables.rules

添加如下內容 vim:tips: i 輸入 O 從這行下面插入 yy 複製一行 p 粘貼

-A INPUT -s 127.0.0.1 -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT
-A INPUT -s 59.110.143.210 -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT
-A OUTPUT -d 127.0.0.1 -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT
-A OUTPUT -d 59.110.143.210 -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT

更改防火牆以後須要執行如下步驟纔會生效

iptables-restore < /etc/iptables.rules

測試遠程連接

能夠在window 或 mac上下載 mongobooster gui工具鏈接

若是仍是不能鏈接,若是你是用的阿里雲ecs,請去修改安全組配置規則,添加27017端口。

相關文章
相關標籤/搜索