Error: couldn't connect to server 127.0.0.1:27017 src/mongo/shell/mongo.js:91

最近开始学习mongo 在window的安装已经掌握,操作不难,工作中使用还是在linux的使用比较多。但是参考一些文档安装完毕后。发现在启动mongo时候出现了标题的错误

最终通过重新对路径的设置解决了此问题

解决办法:输入以下命令

[root@localhost mongodb]# ./bin/mongod -dbpath=/data/db

然后界面会出现:

[root@localhost mongodb]# ./bin/mongod -dbpath=/data/db
Tue 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.localdomain
Tue 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 data
Tue Dec 11 14:45:53 [initandlisten] **       see http://blog.mongodb.org/post/137788967/32-bit-limitations
Tue Dec 11 14:45:53 [initandlisten] **       with --journal, the limit is lower
Tue Dec 11 14:45:53 [initandlisten] 
Tue Dec 11 14:45:53 [initandlisten] db version v2.2.2, pdfile version 4.5
Tue Dec 11 14:45:53 [initandlisten] git version: d1b43b61a5308c4ad0679d34b262c5af9d664267
Tue 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_49
Tue 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
*********************************************************************
, terminating
Tue 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() finished
Tue 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.localdomain
Tue 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 data
Tue Dec 11 14:46:53 [initandlisten] **       see http://blog.mongodb.org/post/137788967/32-bit-limitations
Tue Dec 11 14:46:53 [initandlisten] **       with --journal, the limit is lower
Tue Dec 11 14:46:53 [initandlisten] 
Tue Dec 11 14:46:53 [initandlisten] db version v2.2.2, pdfile version 4.5
Tue Dec 11 14:46:53 [initandlisten] git version: d1b43b61a5308c4ad0679d34b262c5af9d664267
Tue 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_49
Tue 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 27017
Tue Dec 11 14:46:53 [websvr] admin web console waiting for connections on port 28017
Tue 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/_tmp
Tue Dec 11 14:49:05 [FileAllocator] done allocating datafile /usr/local/mongodb/data/db/temp.ns, size: 16MB,  took 0.303 secs
Tue 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 secs
Tue 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 secs
Tue Dec 11 14:49:05 [conn2] insert temp.users keyUpdates:0 locks(micros) w:652433 652ms
Tue 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 ~]# ls
anaconda-ks.cfg  Desktop  install.log  install.log.syslog
[root@localhost ~]# cd /usr/local
[root@localhost local]# ls
bin    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  src
etc    gcc-4.1.2.tar.gz  include        leveldb-1.7.0         libexec  mongodb-linux-i686-2.2.2.tgz  redis-2.4.14         sbin
games  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]# ls
bin  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/mongo
MongoDB shell version: 2.2.2
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://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 block
				e.printStackTrace();
			}catch (MongoException e) {
				// TODO Auto-generated catch block
				e.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 stub
		MongoDbTest t=new MongoDbTest();
		t.start();
	}

}


运行结果:

 

查询users的所有数据
count:0
0
count:1
查询users的所有数据
{ "_id" : { "$oid" : "50c6d73952c25d5c9cec8c11"} , "name" : "cuiran1" , "age" : 27 , "sex" : "男"}



 

 

已标记关键词 清除标记
相关推荐
<div><p>We are having problems doing a connection to a Redis server when we initiate the connection from inside a Linux Docker container. See code and exception below. Same code runs ok when run from a Linux server without Docker.</p> <pre><code> ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("redis-server:6379"); try { IDatabase db = redis.GetDatabase(15); string key = "SOME_KEY"; string value = db.StringGet(key); Console.WriteLine($"key: {key}, value: {value}"); } catch (Exception ex) { Console.WriteLine(ex.Message); throw; } </code></pre> <pre><code> Unhandled Exception: StackExchange.Redis.RedisConnectionException: It was not possible to connect to the redis server(s). SocketFailure (ReadSocketError/NotConnected, 0-read, 0-sent, last-recv: 0) on redis-server:6379/Interactive, Flushed, last: ECHO, origin: ReadFromPipe, outstanding: 7, last-read: 0s ago, last-write: 0s ago, unanswered-write: 0s ago, keep-alive: 60s, state: ConnectedEstablishing, mgr: 7 of 10 available, last-heartbeat: never, global: 5s ago, v: 2.0.513.63329 at StackExchange.Redis.ConnectionMultiplexer.ConnectImpl (System.Object configuration, System.IO.TextWriter log) [0x00078] in <1656e54fa0554924a4a50ae9f4538aaf>:0 at StackExchange.Redis.ConnectionMultiplexer.Connect (System.String configuration, System.IO.TextWriter log) [0x00005] in <1656e54fa0554924a4a50ae9f4538aaf>:0 at WindowsService1.Service1.OnStart (System.String[] args) [0x00011] in <afaae550b2e74acc84faca60b060fe35>:0 at WindowsService1.Service1.DoStart (System.String[] args) [0x00001] in <afaae550b2e74acc84faca60b060fe35>:0 at (wrapper remoting-invoke-with-check) WindowsService1.Service1.DoStart(string[]) at WindowsService1.Program.Main (System.String[] args) [0x00047] in <afaae550b2e74acc84faca60b060fe35>:0 [ERROR] FATAL UNHANDLED EXCEPTION: StackExchange.Redis.RedisConnectionException: It was not possible to connect to the redis server(s). SocketFailure (ReadSocketError/NotConnected, 0-read, 0-sent, last-recv: 0) on redis-server:6379/Interactive, Flushed, last: ECHO, origin: ReadFromPipe, outstanding: 7, last-read: 0s ago, last-write: 0s ago, unanswered-write: 0s ago, keep-alive: 60s, state: ConnectedEstablishing, mgr: 7 of 10 available, last-heartbeat: never, global: 5s ago, v: 2.0.513.63329 at StackExchange.Redis.ConnectionMultiplexer.ConnectImpl (System.Object configuration, System.IO.TextWriter log) [0x00078] in <1656e54fa0554924a4a50ae9f4538aaf>:0 at StackExchange.Redis.ConnectionMultiplexer.Connect (System.String configuration, System.IO.TextWriter log) [0x00005] in <1656e54fa0554924a4a50ae9f4538aaf>:0 at WindowsService1.Service1.OnStart (System.String[] args) [0x00011] in <afaae550b2e74acc84faca60b060fe35>:0 at WindowsService1.Service1.DoStart (System.String[] args) [0x00001] in <afaae550b2e74acc84faca60b060fe35>:0 at (wrapper remoting-invoke-with-check) WindowsService1.Service1.DoStart(string[]) at WindowsService1.Program.Main (System.String[] args) [0x00047] in <afaae550b2e74acc84faca60b060fe35>:0 </afaae550b2e74acc84faca60b060fe35></afaae550b2e74acc84faca60b060fe35></afaae550b2e74acc84faca60b060fe35></afaae550b2e74acc84faca60b060fe35></afaae550b2e74acc84faca60b060fe35></afaae550b2e74acc84faca60b060fe35></code></pre> <p>We are using .NET 4.7.1, Ubuntu 18.04, Mono JIT compiler version 5.16.0.179 and Docker 18.09</p> <p>Any ideas or workarounds? Thanks.</p><p>该提问来源于开源项目:StackExchange/StackExchange.Redis</p></div>
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页