最近開始學習mongo 在window的安裝已經掌握,操作不難,工作中使用還是在linux的使用比較多。但是參考一些文檔安裝完畢後。發現在啟動mongo時候出現了標題的錯誤
最終通過重新對路徑的設定解決了此問題
解決辦法:輸入以下命令
[root@localhost mongodb]# ./bin/mongod -dbpath=/data/db
然後介面會出現:
[root@localhost mongodb]# ./bin/mongod -dbpath=/data/dbTue Dec 11 14:45:53 Tue Dec 11 14:45:53 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.Tue Dec 11 14:45:53 Tue Dec 11 14:45:53 [initandlisten] MongoDB starting : pid=3813 port=27017 dbpath=/data/db 32-bit host=localhost.localdomainTue Dec 11 14:45:53 [initandlisten] Tue Dec 11 14:45:53 [initandlisten] ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of dataTue Dec 11 14:45:53 [initandlisten] ** see http://blog.mongodb.org/post/137788967/32-bit-limitationsTue Dec 11 14:45:53 [initandlisten] ** with --journal, the limit is lowerTue Dec 11 14:45:53 [initandlisten] Tue Dec 11 14:45:53 [initandlisten] db version v2.2.2, pdfile version 4.5Tue Dec 11 14:45:53 [initandlisten] git version: d1b43b61a5308c4ad0679d34b262c5af9d664267Tue Dec 11 14:45:53 [initandlisten] build info: Linux domU-12-31-39-01-70-B4 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_49Tue Dec 11 14:45:53 [initandlisten] options: { dbpath: "/data/db" }Tue Dec 11 14:45:53 [initandlisten] exception in initAndListen: 10296 ********************************************************************* ERROR: dbpath (/data/db) does not exist. Create this directory or give existing directory in --dbpath. See http://dochub.mongodb.org/core/startingandstoppingmongo*********************************************************************, terminatingTue Dec 11 14:45:53 dbexit: Tue Dec 11 14:45:53 [initandlisten] shutdown: going to close listening sockets...Tue Dec 11 14:45:53 [initandlisten] shutdown: going to flush diaglog...Tue Dec 11 14:45:53 [initandlisten] shutdown: going to close sockets...Tue Dec 11 14:45:53 [initandlisten] shutdown: waiting for fs preallocator...Tue Dec 11 14:45:53 [initandlisten] shutdown: closing all files...Tue Dec 11 14:45:53 [initandlisten] closeAllFiles() finishedTue Dec 11 14:45:53 dbexit: really exiting now[root@localhost mongodb]# ./bin/mongod -dbpath=/usr/local/mongodb/data/db/Tue Dec 11 14:46:53 Tue Dec 11 14:46:53 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.Tue Dec 11 14:46:53 Tue Dec 11 14:46:53 [initandlisten] MongoDB starting : pid=3819 port=27017 dbpath=/usr/local/mongodb/data/db/ 32-bit host=localhost.localdomainTue Dec 11 14:46:53 [initandlisten] Tue Dec 11 14:46:53 [initandlisten] ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of dataTue Dec 11 14:46:53 [initandlisten] ** see http://blog.mongodb.org/post/137788967/32-bit-limitationsTue Dec 11 14:46:53 [initandlisten] ** with --journal, the limit is lowerTue Dec 11 14:46:53 [initandlisten] Tue Dec 11 14:46:53 [initandlisten] db version v2.2.2, pdfile version 4.5Tue Dec 11 14:46:53 [initandlisten] git version: d1b43b61a5308c4ad0679d34b262c5af9d664267Tue Dec 11 14:46:53 [initandlisten] build info: Linux domU-12-31-39-01-70-B4 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_49Tue Dec 11 14:46:53 [initandlisten] options: { dbpath: "/usr/local/mongodb/data/db/" }Tue Dec 11 14:46:53 [initandlisten] Unable to check for journal files due to: boost::filesystem::basic_directory_iterator constructor: No such file or directory: "/usr/local/mongodb/data/db/journal"Tue Dec 11 14:46:53 [initandlisten] waiting for connections on port 27017Tue Dec 11 14:46:53 [websvr] admin web console waiting for connections on port 28017Tue Dec 11 14:47:11 [initandlisten] connection accepted from 127.0.0.1:48783 #1 (1 connection now open)Tue Dec 11 14:49:04 [initandlisten] connection accepted from 192.168.0.137:2869 #2 (2 connections now open)Tue Dec 11 14:49:04 [FileAllocator] allocating new datafile /usr/local/mongodb/data/db/temp.ns, filling with zeroes...Tue Dec 11 14:49:04 [FileAllocator] creating directory /usr/local/mongodb/data/db/_tmpTue Dec 11 14:49:05 [FileAllocator] done allocating datafile /usr/local/mongodb/data/db/temp.ns, size: 16MB, took 0.303 secsTue Dec 11 14:49:05 [FileAllocator] allocating new datafile /usr/local/mongodb/data/db/temp.0, filling with zeroes...Tue Dec 11 14:49:05 [FileAllocator] done allocating datafile /usr/local/mongodb/data/db/temp.0, size: 16MB, took 0.328 secsTue Dec 11 14:49:05 [FileAllocator] allocating new datafile /usr/local/mongodb/data/db/temp.1, filling with zeroes...Tue Dec 11 14:49:05 [conn2] build index temp.users { _id: 1 }Tue Dec 11 14:49:05 [conn2] build index done. scanned 0 total records. 0 secsTue Dec 11 14:49:05 [conn2] insert temp.users keyUpdates:0 locks(micros) w:652433 652msTue Dec 11 14:49:05 [conn2] end connection 192.168.0.137:2869 (1 connection now open)Tue Dec 11 14:49:05 [FileAllocator] done allocating datafile /usr/local/mongodb/data/db/temp.1, size: 32MB, took 0.528 secs
表示設定成功,此時不要關閉這個介面,需要重新開啟一個視窗來啟動mongo
Last login: Tue Dec 11 14:05:30 2012 from 192.168.0.137[root@localhost ~]# lsanaconda-ks.cfg Desktop install.log install.log.syslog[root@localhost ~]# cd /usr/local[root@localhost local]# lsbin gcc-4.1.2 gcc-install leveldb-1.5.0.tar.gz lib mongodb-linux-i686-2.0.8.tgz redis-1.2.6.tar.gz redis-2.6.6.tar.gz srcetc gcc-4.1.2.tar.gz include leveldb-1.7.0 libexec mongodb-linux-i686-2.2.2.tgz redis-2.4.14 sbingames gcc-build leveldb-1.5.0 leveldb-1.7.0.tar.gz mongodb redis-1.2.6 redis-2.4.14.tar.gz share[root@localhost local]# cd mongodb[root@localhost mongodb]# lsbin db GNU-AGPL-3.0 logs README THIRD-PARTY-NOTICES[root@localhost mongodb]# cd logs[root@localhost logs]# ls[root@localhost logs]# ls[root@localhost logs]# [root@localhost logs]# cd ..[root@localhost mongodb]# ./bin/mongoMongoDB shell version: 2.2.2connecting to: testWelcome to the MongoDB shell.For interactive help, type "help".For more comprehensive documentation, seehttp://docs.mongodb.org/Questions? Try the support grouphttp://groups.google.com/group/mongodb-user>
當出現這個時候表示啟動成功,然後用java代碼測試:
/** * MongoDbTest.java * 著作權(C) 2012 * 建立:cuiran 2012-12-05 13:47:22 */package com.wpndemo.mongodb;import java.net.UnknownHostException;import org.bson.types.ObjectId;import com.mongodb.BasicDBObject;import com.mongodb.DB;import com.mongodb.DBCollection;import com.mongodb.DBCursor;import com.mongodb.DBObject;import com.mongodb.Mongo;import com.mongodb.MongoException;/** * TODO * @author cuiran * @version TODO */public class MongoDbTest { private Mongo mg = null; private DB db; private DBCollection users; /** * 初始化 * TODO */ public void init(){ try {//mg=new Mongo(); mg=new Mongo("192.168.0.138",27017);} catch (UnknownHostException e) {// TODO Auto-generated catch blocke.printStackTrace();}catch (MongoException e) {// TODO Auto-generated catch blocke.printStackTrace();}//擷取temp DB 如果預設沒有 會自動建立db=mg.getDB("temp");//擷取users Collection 如果預設沒有 會自動建立users=db.getCollection("users"); } /** * 銷毀 * TODO */ public void destory(){ if(mg!=null){ mg.close(); } mg=null; db=null; users=null; System.gc(); } /** * <b>function:</b> 查詢所有資料 * @author cuiran * @createDate 2012-12-11 09:24:11 */ public void queryAll(){ print("查詢users的所有資料"); //Db遊標 DBCursor cur=users.find(); while(cur.hasNext()){ print(cur.next()); } } /** * *<b>function:</b>添加資料 * @author cuiran * @createDate 2012-12-11 09:25:23 */ public void add(){ queryAll(); print("count:"+users.count()); DBObject user=new BasicDBObject(); user.put("name", "cuiran1"); user.put("age", 27); // users.save(user);// print(users.save(user).getN()); user.put("sex", "男"); print(users.save(user).getN()); print("count:"+users.count()); queryAll(); } /** * *<b>function:</b>刪除資料 * @author cuiran * @createDate 2012-12-11 09:38:12 */ public void remove(){ queryAll(); print("刪除id = 50c68da852c2e1adb70f6d29:" + users.remove(new BasicDBObject("_id", new ObjectId("50c68da852c2e1adb70f6d29"))).getN()); queryAll(); } public void print(Object o){ System.out.println(o); } public void start(){ init(); add(); destory();// remove();// queryAll(); } /** * TODO * @param args */public static void main(String[] args) {// TODO Auto-generated method stubMongoDbTest t=new MongoDbTest();t.start();}}
運行結果:
查詢users的所有資料count:00count:1查詢users的所有資料{ "_id" : { "$oid" : "50c6d73952c25d5c9cec8c11"} , "name" : "cuiran1" , "age" : 27 , "sex" : "男"}