Tuesday, 24 February 2015

MongoDB Correct way to start MongoDB - Error Cannot connect to mongodb errno:61 Connection refused [Solved]

Recently, while involved in full stack Django development and developing an application using Mongodb as database backend, I encountered following error after installing Mongodb on my mac. Ok first, I installed Mongodb by using brew install command:

brew update
brew install mongodb

After installing Mongodb, I tried to access the mongo shell by using mongo command, but I got "Cannot connect to mongodb errno:61 Connection refused error" . Here is the full trace:

alis-air:~ aliraza$ mongo
MongoDB shell version: 2.6.6
connecting to: test
2015-02-24T17:51:36.067+0500 warning: Failed to connect to, reason: errno:61 Connection refused
2015-02-24T17:51:36.069+0500 Error: couldn't connect to server (, connection attempt failed at src/mongo/shell/mongo.js:146
exception: connect failed

I searched for correct ways to start Mongodb on Mac OS. I tried several solutions but the one which worked for me is as follows: In the shell write sudo mongod to start it.

$ mkdir -p /data/db
$ sudo mongod

This started my Mongodb. Next I opened another shell instance (new shell window) and wrote mongo command and it worked without Error Cannot connect to mongodb errno:61 Connection refused! MongoDB starting Problem Solved.

$ mongo
MongoDB shell version: 2.6.6
connecting to: test
Server has startup warnings: 
2015-02-24T18:20:51.384+0500 [initandlisten] 
2015-02-24T18:20:51.384+0500 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000

Friday, 30 January 2015

Django Setting DEBUG = False causes 500 Error - Django 500 error debug false - ALLOWED_HOSTS Problem [SOLVED]

Python Django alirazabhayani Full stack development
Django Debug False 500 error Solved
Being involved in Full Stack Django/Python Web Application Development for 3 years now, I recently deployed my Django application on a test server and encountered Server Error (500) on Django when template debug is set to False. That is in Django settings.py, when I changed DEBUG = FALSE from DEBUG = TRUE, it caused 500 error.

I had upgraded my application from Django 1.4 to Django 1.7. I came to know that 500 error on DEBUG = False error is encountered by everyone using django 1.5 or greater as soon as they change their settings.py to DEBUG = False. Well my problem got solved after reading several blogs and digging deeper in the Django Docs for DEBUG = False ALLOWED_HOSTS settings

What I did was, in settings.py, changed ALLOWED_HOSTS value in the following way:
# Above mentioned IP is not my actual IP. Enter your actual server IP or domain name here. 

If you have a domain name of your server, you may write:
ALLOWED_HOSTS = ['www.example.com']
You can also use a '*' wildcard to allow all hosts. But this is not recommended in the production environment.
You can find a full detail regarding the ALLOWED_HOSTS settings here hidden deep inside django document  

Saturday, 10 January 2015

Django Syncdb Create superuser ValueError: unknown locale: UTF-8 [Solved]

While setting up a new remote server for a new Django Application I am currently working on, I encountered following error as entered yes to create superuser after I entered syncdb command for the first time.
ValueError: unknown locale: UTF-8
Here is the full trace:

(django1.5)[root@DXDS2102 testdjango]# python manage.py syncdb
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table django_site

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
ValueError: unknown locale: UTF-8
As I had centOS server for my Django Application, I entered following commands which actually solved this problem:
export LC_CTYPE=en_US.UTF-8
export LC_ALL=en_US.UTF-8
After running the above command I again did syncdb but Django didn't asked me about creating superuser. So for manually creating a superuser, I entered following command:
python manage.py createsuperuser
The django superuser was created without the error of "ValueError: unknown locale: UTF-8".

Tuesday, 6 January 2015

django.core.exceptions.improperlyconfigured error loading mysqldb module no module named mysqldb - Python Django MySQL Error

In order to deal massive (Big) data in one of my applications. I am testing MySQL with MariaDB and TokuDB. Previously I had used PostgreSQL for my applications but the Reporting Engine I am designing requires some unique features to update data faster and retrieve it in an efficient manner. TokuDB being based on Fractal Indexing makes the insertion faster for Bigger Data and keeps the database compressed resulting in efficient use of storage data. While running MySQL MariaDB with Django in my virtual environment, I encountered following error: 

django.core.exceptions.improperlyconfigured error loading mysqldb module no module named mysqldb
Full Trace is:
(django1.5)alis-air:DjangoMariaDb aliraza$ python manage.py syncdb
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
  File "/Users/aliraza/Projects/VirtualEnv/django1.5/lib/python2.7/site-packages/django/core/management/__init__.py", line 453, in execute_from_command_line
  File "/Users/aliraza/Projects/VirtualEnv/django1.5/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
  File "/Users/aliraza/Projects/VirtualEnv/django1.5/lib/python2.7/site-packages/django/core/management/__init__.py", line 272, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "/Users/aliraza/Projects/VirtualEnv/django1.5/lib/python2.7/site-packages/django/core/management/__init__.py", line 77, in load_command_class
    module = import_module('%s.management.commands.%s' % (app_name, name))
  File "/Users/aliraza/Projects/VirtualEnv/django1.5/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
  File "/Users/aliraza/Projects/VirtualEnv/django1.5/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 8, in <module>
    from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal
  File "/Users/aliraza/Projects/VirtualEnv/django1.5/lib/python2.7/site-packages/django/core/management/sql.py", line 9, in <module>
    from django.db import models
  File "/Users/aliraza/Projects/VirtualEnv/django1.5/lib/python2.7/site-packages/django/db/__init__.py", line 40, in <module>
    backend = load_backend(connection.settings_dict['ENGINE'])
  File "/Users/aliraza/Projects/VirtualEnv/django1.5/lib/python2.7/site-packages/django/db/__init__.py", line 34, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/Users/aliraza/Projects/VirtualEnv/django1.5/lib/python2.7/site-packages/django/db/utils.py", line 93, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/Users/aliraza/Projects/VirtualEnv/django1.5/lib/python2.7/site-packages/django/db/utils.py", line 27, in load_backend
    return import_module('.base', backend_name)
  File "/Users/aliraza/Projects/VirtualEnv/django1.5/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
  File "/Users/aliraza/Projects/VirtualEnv/django1.5/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 17, in <module>
    raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

The solution to this error which worked for me was simply installing mysql-python module in my virtual environment. Use following command:
pip install mysql-python
and if you are not using virtualenv, then use:
sudo pip install mysql-python

Wednesday, 24 December 2014

Psycopg2 - MacOS X - library not loaded libssl.1.0.0.dylib reason image not found - Issue Resolved

I encountered following error on my MacOS X after installing PostgreSQL and Psycopg2 for my Python and Django based application.

ImportError: dlopen(/Users/aliraza/Projects/VirtualEnv/django1.5/lib/python2.7/site-packages/psycopg2/_psycopg.so, 2): Library not loaded: libssl.1.0.0.dylib
  Referenced from: /Users/aliraza/Projects/VirtualEnv/django1.5/lib/python2.7/site-packages/psycopg2/_psycopg.so
  Reason: image not found

Check if you have openssl installed by writing openssl on the commandline:
$ openssl

 If it starts, this means its installed in your system and you may skip STEP-2 and go directly to STEP-3.

If openssl isn't installed, install it by:
$ brew install openssl

Check the path of openssl lib on your machine. On my machine it was:
$ /usr/local/opt/openssl/lib/

You have to simply include this path from STEP-3 by running following command:
i.e in my case, the command with the STEP-3 path is:
$ sudo export DYLD_LIBRARY_PATH=/usr/local/opt/openssl/lib/

You may also have to create a symbolic link to PostgreSQL's lib folder. For that, first check your path of PostgreSQL's lib folder and then write following commands accordingly:

$ sudo ln -s /Library/PostgreSQL/9.4/lib/libssl.1.0.0.dylib /usr/lib/
$ sudo ln -s /Library/PostgreSQL/9.4/lib/libcrypto.1.0.0.dylib /usr/lib/

Now run python/ Django shell and import psycopg2. I had no errors after following this recipe.

Tuesday, 25 March 2014

PostgreSQL - pg_dump to copy PostgreSQL Database From One Remote Server to Another

While involved in Python + Django based projects and using postgreSQL as a database backend, there are occasions where I need to switch between servers and at times need to copy databases as well. For transferring the django application data + schema from one remote server to another, I use following ways as per convenience, available options, server restrictions etc.

1) Copying Django application data + schema (PostgreSQL) from one remote server by dumping the data in a file and then transferring that file to another remote server and restoring it. In this approach, you have got three options:

  • Dump both schema as well as Data to the file:
pg_dump -C -h host -U username db_name > /any_directory/dump_schema_and_data_file 
  • Dump only schema from PostgreSQL Database to a file:
pg_dump -Cs -h host -U username db_name > /any_directory/dump_schema_file
  • Dump only data from PostgreSQL Database to file:
pg_dump -a -h host -U username db_name > /any_directory/dump_data_file

After copying the above file to target server, you can easily restore database from file by using following command:
psql -h host -U username db_name < data_schema_file

2) Second approach I use to copy PostgreSQL database from one remote server to another is by directly using following command i.e without copying the data to intermediate file:

pg_dump -C -h localhost -U localuser db_name | psql remotehost -U remoteuser db_name

Thursday, 4 July 2013

Django - Install Psycopg2 on Mac OS X - pg_config path issue AND lipo: can't open input file - Issues Resolved

In one of my recent Django based projects, I happened to use PostgreSQL as Database Backend. In order to connect the db and for other operations with PostgreSQL, python's psycopg2 module is pretty popular and readily used. Psycopg2 got easily installed on the Linux (Ubuntu) machine but I faced some issues when I tried to install it on my Mac OS X 10.6.7. After following several tips and reading several blog posts, I finally managed to find the actual working solution which resolved the issues of installing Psycopg2 on Mac OS X. I also noticed that many people were trying to find solution of the same issues which I faced. So I am writing their solutions in a single blog post in a descriptive and step-by-step manner. There are basically two most common issues which I also faced while installing psycopg2 on Mac OS X:


When I tried to install psycopg2 on Mac OS X and entered following command on my terminal:

>pip install psycopg2

I got an error that pg_config file's path is not specified. Following is the full trace:

Error: pg_config executable not found.

Please add the directory containing pg_config to the PATH

or specify the full executable path with the option:

    python setup.py build_ext --pg-config /path/to/pg_config build ...

or with the pg_config option in 'setup.cfg'.

Command python setup.py egg_info failed with error code 1 in /tmp/pip-build/psycopg2


Provided that some version of PostgreSQL is already installed, you will have to explicitly add the PATH of pg_config file in your PostgreSQL bin folder. In my case, the installation PATH for PostgreSQL is:


so to define the PATH of pg_config file, I entered following command in my terminal:
> PATH=$PATH:/opt/local/lib/postgresql91/bin/

After running this command, the pg_config path error while installing psycopg2 was resolved


While installing psycopg2 on my Mac OS X, the pip install psycopg2 command returned following error:

lipo: can't open input file: /var/tmp//ccTy5xAu.out (No such file or directory)

Full trace of the error is as follows:

psycopg/psycopgmodule.c:951: fatal error: error writing to -: Broken pipe

compilation terminated.

lipo: can't open input file: /var/tmp//ccTy5xAu.out (No such file or directory)

error: command 'gcc-4.2' failed with exit status 1


After a googling a bit, and trying several recipes, the solution that worked for me was the following command which overrides the ARCHFLAGS architecture settings:

sudo env ARCHFLAGS="-arch i386 -arch x86_64" pip install psycopg2

This command finally installed psycopg2 on my Mac OS X without any issues.