1. 程式人生 > 其它 >linux下安裝mongodb,總結常見的報錯問題

linux下安裝mongodb,總結常見的報錯問題

現在window下面安裝了一遍,覺得很簡單。然後就到linux下面裝一遍,結果各種報錯,還是值得寫個部落格理一理。

首先到其官網上下載最新穩定版,解壓到目錄,如/usr/local/mongodb

然後切換到mongodb下,建立data資料夾和logs檔案;

安裝步驟:

  • 進入/usr/local目錄下
  • cd /usr/local
  • 建立mongodb資料夾,作為安裝目標資料夾
    mkdir mongodb
  • 解壓縮檔案,並且移動到mongodb資料夾下
    tar -zxvf mongodb-linux-x86_64-2.6.7.tgz
  • 移動解壓縮後的資料夾下的所有檔案到mongodb資料夾下
    cd mongodb-linux-x86_64-2.6.7
    mv * /usr/local/mongodb
  • 建立data資料夾用於存放資料,建立logs檔案用於存放檔案
    cd /usr/local/mongodb
    mkdir data
    touch logs

如果許可權不夠自行設定一下檔案許可權

啟動MongoDB服務

cd bin
./mongod -dbpath=/usr/local/mongodb/data -logpath=/usr/local/mongodb/logs

這個時候如果出現 ./mongod: /lib/ld-linux.so.2: bad ELF interpreter: No such file or directory 錯誤!

解決辦法

1、在64系統裡執行32位程式如果出現/lib/ld-linux.so.2:
bad ELF interpreter: No such file or directory,安裝下glic即可

yum install glibc.i686



2、error while loading shared libraries: libz.so.1:
cannot open shared object file: No such file or directory

yum install zlib.i686

然後繼續啟動MongoDB服務

cd bin
./mongod -dbpath=/usr/local/mongodb/data -logpath=/usr/local/mongodb/logs

這個時候如果出現 errorwhileloadingsharedlibraries:libstdc++.so.6:cannotopensharedobjectfile:Nosuchfileordirectory

錯誤!

解決辦法:執行命令:yumwhatprovideslibstdc++.so.6

yumwhatprovideslibstdc++.so.6 
[root@iZ2ze7dyjfik9i0bgl5o1cZ mongodb]# yum whatprovides libstdc++.so.6
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
libstdc++-4.4.7-18.el6.i686 : GNU Standard C++ Library
Repo : base
Matched from:
Other : libstdc++.so.6



libstdc++-4.4.7-18.el6.i686 : GNU Standard C++ Library
Repo : installed
Matched from:
Other : Provides-match: libstdc++.so.6

[root@iZ2ze7dyjfik9i0bgl5o1cZ mongodb]# yum install libstdc++-4.4.7-3.el6.i686

這個時候如果出現 如下錯誤:

Error:Multilib version problems found. This often means that the root

cause is something else and multilib version checking is just

pointing out that there is a problem. Eg.:

1. You have an upgrade for libstdc++ which is missing some

dependency that another package requires. Yum is trying to

solve this by installing an older version of libstdc++ of the

different architecture. If you exclude the bad architecture

yum will tell you what the root cause is (which package

requires what). You can try redoing the upgrade with

--exclude libstdc++.otherarch ... this should give you an error

message showing the root cause of the problem.

2. You have multiple architectures of libstdc++ installed, but

yum can only see an upgrade for one of those arcitectures.

If you don't want/need both architectures anymore then you

can remove the one with the missing update and everything

will work.

3. You have duplicate versions of libstdc++ installed already.

You can use "yum check" to get yum show these errors.

...you can also use --setopt=protected_multilib=false to remove

this checking, however this is almost never the correct thing to

do as something else is very likely to go wrong (often causing

much more problems).

Protected multilib versions: libstdc++-4.4.7-18.el6.i686 != libstdc++-4.4.7-4.el6.x86_64

是因為 多個庫共存衝突

解決辦法:

[root@iZ2ze7dyjfik9i0bgl5o1cZ mongodb]# yum install libstdc++-4.4.7-3.el6.i686 --setopt=protected_multilib=false

再次啟動MongoDB服務

cd bin
./mongod -dbpath=/usr/local/mongodb/data -logpath=/usr/local/mongodb/logs

完成!

後臺服務啟動

./mongod -dbpath=/usr/local/mongodb/data -logpath=/usr/local/mongodb/logs --fork

後臺許可權啟動

./mongod -dbpath=/usr/local/mongodb/data -logpath=/usr/local/mongodb/logs --fork --auth

現在mongodb就能啟動成功了。如果已經啟動,則可以先終止,等配置完在重新啟動。

注意,上述我們啟動MongoDB都是手動使用mongod來啟動,這樣關閉計算機後,下次再進來它又沒啟動了,所以還得手動啟動,因此,為避免這種繁瑣的工作,可以把mongod放到服務自啟動項中,這樣計算機一開啟mongod服務也就啟動了。 
編輯/etc/rc.local,加入下述程式碼然後再儲存即可。 
1.#add mongonDB service

2.rm -rf /data/mongodb_data/* 
&& /usr/local/mongodb/bin/mongod  --dbpath=/data/mongodb_data/ --logpath=/data/mongodb_log/mongodb.log --logappend&  


我們重啟計算機再看MongoDB是否啟動,重啟後可以直接使用 mongo命令登入,最終發現是可以成功的。

另外,我們使用mongo命令登入MongoDB還要轉到mongo命令所在目錄再執行./mongo,這樣是不是有些麻煩?因此,我們可以簡化這點,將該命令檔案copy到/usr/bin下,這樣就可以在任何目錄下使用mongo命令了。
netstat -anp

找到mongodb的pid 如3303

kill -9 3303
即可結束程序