java串連MongoDB報錯解決

來源:互聯網
上載者:User

標籤:hat   div   直接   cep   pat   nec   解決   啟動   配置   

報錯內容

com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=127.0.0.1:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: connect}}]

 

這個意思大概是串連不成功,127.0.0.1這是本地的,可是linux虛擬機器的ip 192.168.xx.xx

這樣linux串連的跟java串連的不一樣,所有要把ip變一樣。

1.如果linux沒有設定ip的話,預設是127.0.0.1,但是linux的ip又不是這個,所有java串連不到

解決辦法就是設定MongoDB啟動的ip為linuxip

1.啟動的時候可以直接在後面加ip     --bind_ip=192.168.88.134

/home/mongodb/bin/mongod --fork  --bind_ip=192.168.88.134 --dbpath=/home/mongodb/data/db --logpath=/home/mongodb/data/log/mongodb.log  --logappend

2.自己建立一個mongodb.conf 檔案,把要設定的值放裡面 ,我只放了幾個主要的測試

# 記錄檔位置logpath=/usr/local/server/mongodb/logs# 以追加方式寫入日誌logappend=true# 是否以守護進程方式運行fork = true# 預設27017port = 27017# 資料庫檔案位置dbpath=/usr/local/server/mongodb/data#ip地址bind_ip = 127.0.0.1

啟動就要加設定檔了

./mongod --config /usr/local/server/mongodb/mongodb.conf

java串連MongoDB報錯解決

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.