Django-redis configuration to use socket rather than TCP - django

I'm attempting to use django-redis using Unix sockets rather than a TCP connection:
This is the settings.py configuration:
CACHES = {
'default': {
'BACKEND': 'redis_cache.cache.RedisCache',
'LOCATION': 'unix:/tmp/redis.sock:1',
'OPTIONS': {
'PASSWORD': '',
'PICKLE_VERSION': -1, # default
'PARSER_CLASS': 'redis.connection.HiredisParser',
'CLIENT_CLASS': 'redis_cache.client.DefaultClient',
},
},
}
and this is an extract of the redis config file at /etc/redis/6379.conf:
# Specify the path for the unix socket that will be used to listen for
# incoming connections. There is no default, so Redis will not listen
# on a unix socket when not specified.
#
unixsocket /tmp/redis.sock
unixsocketperm 755
Still I receive a ConnectionInterrumped exception, which stands for an error during connection. Any ideas about what this configuration's issue is?
P.S. My Django version is 1.5.1, django-redis is 3.3 and hiredis is 0.0.1.

EDIT: Apparently I read the cache provider wrong, the below answer is the solution for django-redis-cache, not django-redis. I'll let the answer remain though, since changing cache provider and using this config seems to have solved the problem.
You should not need the unix: prefix, and the backend setting looks strange;
'default': {
'BACKEND': 'redis_cache.RedisCache',
'LOCATION': '/tmp/redis.sock',
'OPTIONS': { ...

Related

ElastiCache(redis) cluster mode enabled with django

I configured ElastiCache redis with cluster mode enabled.
I want to connect ElastiCache with local Django.
So I configured bastion host.
I connected ElastiCache(non-cluster mode) with local Django. I tried cache.set(), cache.get(). It's OK.
I installed 'Django-redis-cache' and 'settings.py' is like that.
CACHES = {
'default': {
'BACKEND': 'redis_cache.RedisCache',
'LOCATION': 'localhost:6379',
}
}
But I have problem when I connect ElastiCache(cluster mode) with django.
I tried tunneling with ElastiCache configuration endpoint.
When I use the same 'settings.py', error message is like that.
'SELECT is not allowed in cluster mode'
So, I changed 'settings.py'.
CACHES = {
'default': {
'BACKEND': 'redis_cache.RedisCache',
'LOCATION': 'localhost:6379',
'OPTIONS': {
'DB': 0
},
}
}
And then, error message is like that.
'MOVED 4205 xx.xx.xx.xxx:6379'
What I have to do?
There are no example which connect ElastiCache(cluster mode) with Django.

redis.exceptions.ConnectionError: Error -3 connecting to redis:6379. Temporary failure in name resolution

redis.service is active and I can connect to redis-cli but in Django I got the issue when Celery try to get access to redis:6379 (as I understand).
CELERY_RESULT_BACKEND = os.environ.get('REDISCLOUD_URL', 'redis://localhost:6379/0')
CACHEOPS_REDIS = os.environ.get('CACHEOPS_REDIS', 'redis://localhost:6379/9')
CACHES = {
'default': {
'BACKEND': 'redis_lock.django_cache.RedisCache',
'LOCATION': 'redis://127.0.0.1:6379/',
'OPTIONS': {
'CLIENT_CLASS': 'django_redis.client.DefaultClient',
'DB': 3,
}
}
}
It happened after I reinstalled Ubuntu. Before was ok.

GCP, Django problem connection refused when trying to connect to the server

My django project runns normally on localhost and on heroku also, but when I deployed it to google cloud platform I am getting this error:
could not connect to server: Connection refused
Is the server running locally and accepting connections on Unix domain socket "/cloudsql/<connection_name>/.s.PGSQL.5432"?
The connection to the database in settings.py looks like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'database_name',
'USER': 'postgres',
'PASSWORD': "password",
# production
'HOST': '/cloudsql/connection_name',
'PORT': '5432',
}
Additionally, my app.yaml looks like
runtime: python37
handlers:
- url: /static
static_dir: static/
- url: /.*
script: auto
env_variables:
DJANGO_SETTINGS_MODULE: fes_app.settings
requirements.txt looks like this plus
sqlparse==0.4.2
toml==0.10.2
uritemplate==4.1.1
urllib3==1.25.11
whitenoise==5.2.0
twilio==6.9.0
I have tried using the binary version of psycopg, also gave a role client sql to the service account in the cloud.
**NOTE : ** I am using an app engine standard environment
Likely need more information to help you out, but did you follow the tutorial below in the official Google Docs? That's usually how I get started and then I make modifications from there.
I would compare how Google is deploying a Django app to your own settings and see what's missing. For example, your requirements.txt file does not look complete (unless you only pasted part of it) so I would start there.
https://cloud.google.com/python/django/appengine

Value of Memcache location on PythonAnyWhere

What LOCATION should I point Memcached to after deployment on pythonanywhere server? For local I am using this setting and things are working fine.
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
}
}
I need to change 'LOCATION' to replace localhost. Any guidance?
You can location set to a path.
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': 'unix:~/memcached.sock', }}
However, I don't think pythonanywhere lets you use memcache, since you can't use 'sudo apt-get' on a pythonanywhere console, and using memcache requires you to install it. (sudo apt-get install memcached)

How to use Django with Sql Server

I normally use Postgres for my database needs with Django but I recently started at a company which use MSSQL on a Windows environment. Long story short I had to rewrite the database properties in settings.py. Unfortunately, I have NO idea how to connect to a SQL Server using Pyodbc and they're running Python 3.x so I can't use Django-Pyodbc. While trying to run it I'm getting a:
"Data source name not found and no default driver specified (0) (SQLDriverConnect)')"
Here is my current db config as it stands. I'm probably doing something wrong but it is very difficult to find resources since most Django+Sql Server results either use FreeTDS or Django-Pyodbc (neither are options).
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': 'db_name_on_server',
'USER': 'my_acct',
'PASSWORD': 'nope',
'HOST': 'x.x.x.x',
'PORT': '1433',
'OPTIONS': { # Options are not edited
'driver': 'SQL Server', # What it displays as on odbc admin
'dsn': 'System DSN', # What it displays as on odbc admin
'use_legacy_datetime': False
}
Old question, but it might help someone. These are the settings I use in Windows to connect to a SQL Server. As #flipperpa said, I also use django-pyodbc-azure
pyodbc==3.0.10
django-pyodbc-azure==1.10.0.1
However, the pyodbc that pip will download doesn't work for me. As suggested in this answer, go this site http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyodbc and download either depending on the version of Python 3.5 you have installed:
pyodbc‑3.0.10‑cp35‑none‑win32.whl if you have a 32-bit Python 3.5 install
pyodbc‑3.0.10‑cp35‑none‑win_amd64.whl if you have a 64-bit Python 3.5 install
Then use these settings to connect:
'default': {
'ENGINE': 'sql_server.pyodbc',
'HOST': "localhost",
'USER': "--user name--",
'PASSWORD': "--password--",
'NAME': "--database name--",
'PORT': 1433,
'OPTIONS': {
'driver' : 'SQL Server Native Client 11.0',
'MARS_Connection' : True,
'driver_supports_utf8' : True,
},
}
For anyone that is using linux, MS has now released official odbc drivers for SQL server and is officially (if more than a little tacitly) supporting the django-pyodbc-azure project. I highly recommend using it and also the native linux SQL Server ODBC driver over FreeTDS.
I've had my best luck with the following stack, and we're a Python 3 shop exclusively:
FreeTDS 0.95
pyodbc 3.0.10
django-pyodbc-azure 1.8.3 (assuming Django 1.8+)
Assuming you have odbc.ini, odbcinst.ini, and freetds.conf all squared away, here's an example of settings that work for me. Some of these depend on whether or not you're using SQL Server 2008+:
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'HOST': 'yourserver.com',
'PORT': '1433',
'NAME': 'your_db',
'USER': 'your_user',
'PASSWORD': 'your_pw',
'AUTOCOMMIT': True,
'OPTIONS': {
'driver': 'FreeTDS',
'unicode_results': True,
'host_is_server': True,
'extra_params': 'tds_version=7.2;',
},
},
}
You'll also need to include 'use_legacy_datetime': True, if you're running SQL Server 2005 or less, otherwise, it will use the new SQL Server 2008+ date fields. It will also automatically set to true if you're using an outdated driver. Good luck!
Official backend fork from Microsoft: Microsoft MSSQL Django
They also have this reference Azure SQL Database Django sample.
Set 'ENGINE': 'mssql' to use it. Example:
DATABASES = {
"default": {
"ENGINE": "mssql",
"NAME": "mydb",
"USER": "user#myserver",
"PASSWORD": "password",
"HOST": "myserver.database.windows.net",
"PORT": "",
"OPTIONS": {
"driver": "ODBC Driver 17 for SQL Server",
},
},
}
Meanwhile there is django-mssql-backend for MSSQL support and this how my settings look like (Django 3.0, django-mssql-backend 2.8.1)
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'NAME': os.getenv('DB_NAME'),
'USER': os.getenv('DB_USER'),
'PASSWORD': os.getenv('DB_PASSWORD'),
'HOST': os.getenv('DB_HOST'),
'PORT': os.getenv('DB_PORT'),
'TEST': {
'NAME': os.getenv('DB_NAME'),
},
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
'extra_params': 'MARS_Connection=Yes'
},
}
}