Django - MongoDB:(无法连接到localhost:27017)连接被拒绝 [英] Django - MongoDB: (could not connect to localhost:27017) Connection refused

查看:2249
本文介绍了Django - MongoDB:(无法连接到localhost:27017)连接被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到麻烦与Django合作的问题,我的设置是Mac OS X 10.7。我按照这里提供的教程: http://django-mongodb.org/topics/setup.html 。我已经尝试了virtualenv和没有(这是我第一次安装django,所以我不应该有任何冲突)。



在settings.py

  DATABASES = {
'default':{
'ENGINE':'django_mongodb_engine',
'NAME' :'my_database'
}
}

在firstapp.models.py

从django.db导入模型
从djangotoolbox.fields导入ListField

class Post( model.Model):
title = models.CharField()
text = models.TextField()
tags = ListField()
comments = ListField()

在我的shell(python manage.py shell)中,我尝试运行:


$ b $来自mydjango.firstapp.models的b

  Post 
post = Post.objects.create();

但是我不断得到以下内容: DatabaseError:无法连接到localhost:27017: Errno 61]拒绝连接完整追踪



如果我将settings.py切换到以下内容:

  DATABASES = {
'default':{
'ENGINE':'django_mongodb_engine',
'NAME':'my_database',
'USER':'',
'PASSWORD':'',
' HOST':'localhost',
'PORT':'27017',
'SUPPORTS_TRANSACTIONS':False,
},
}

我在shell中收到此错误:不正确配置:端口必须是int的实例

解决方案

您是否分别设置了MongoDB?你如何链接似乎没有超过MongoDB配置。它假定数据库已经在运行。在任何情况下,MongoDB似乎都会被关闭,或者至少在别的地方听。



最后一个错误(... int int)只是因为你指定了'27017'(一个字符串),而不是配置字典中的 27017 。但即使如此,它应该等同于第一个简单的配置。


I am having issues getting mongodb working with Django, my setup is Mac OS X 10.7. I followed the tutorial available here: http://django-mongodb.org/topics/setup.html . I have tried with both virtualenv and without (this is my first time installing django so I shouldn't have any conflicts).

In settings.py

DATABASES = {
   'default' : {
      'ENGINE' : 'django_mongodb_engine',
      'NAME' : 'my_database'
   }
}

In firstapp.models.py

from django.db import models
from djangotoolbox.fields import ListField

class Post(models.Model):
    title = models.CharField()
    text = models.TextField()
    tags = ListField()
    comments = ListField()

In my shell (python manage.py shell), I try running:

from mydjango.firstapp.models import Post
post = Post.objects.create();

But I keep getting the following: DatabaseError: could not connect to localhost:27017: [Errno 61] Connection refused (full traceback)

If I switch settings.py to the following:

DATABASES = {  
    'default': {  
        'ENGINE': 'django_mongodb_engine',  
        'NAME': 'my_database',  
        'USER': '',  
        'PASSWORD': '',  
        'HOST': 'localhost',  
        'PORT': '27017',  
        'SUPPORTS_TRANSACTIONS': False,  
    },  
} 

I get this error in the shell: ImproperlyConfigured: port must be an instance of int

解决方案

Did you setup MongoDB separately? The howto you link doesn't seem to go over the MongoDB configuration. It assumes the database is already running. In any case MongoDB seems down, or is at least listening somewhere else.

The last error ("...instance of int") is just because you specified '27017' (a string) instead of 27017 in the configuration dictionary. But even then it should be equivalent to the first, simpler configuration.

这篇关于Django - MongoDB:(无法连接到localhost:27017)连接被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆