How to define the settings of the django application for using the mongodb server running on the same instance as that of the django project. I tried it with 127.0.0.1, port 27017 (which I assume is the default port at which the mongodb server runs), in the settings of the django application. I then tried it with the IP address of the aws instance, but with no luck. It always gives me this error:
ConnectionError: You have not defined a default connection
My django project has the following mongo settings.
MONGO_SETTINGS = {
'DB_NAME' : 'spotmentor',
'HOST' : '127.0.0.1',
'PORT' : 27017,
'USERNAME' : '',
'PASSWORD' : ''
}
Then I used the mongoengine connect to establish the connection.
I am importing the above MONGO_SETTINGS as mongoset and
from mongoengine import connect
connect(mongoset.get('DB_NAME'), host = mongoset.get('HOST'), port = mongoset.get('PORT'), username = mongoset.get('USERNAME'), password = mongoset.get('PASSWORD'))
I changed the value of the HOST key to the aws instance public IP and still I got the same ConnectionError.
I have also defined:
DATABASES = {
'default' : {
'ENGINE': 'django.db.backends.dummy',
}
}
How can I resolve this?
mongoengine does not require any extra settings to connect to mongodb. The settings that you have provided must suffice.
I suggest you re-check your installation of mongodb.
Try sudo apt-get remove mongodb
and sudo apt-get install mongodb
This should solve your problem.
Also, you need not define dummy db backend if you are not using sql-databases.
Related
I am new to Django. I was trying to implement Redis cache system into my Django project. I am using AWS free tier to host my Django project on EC2 machine using gunicorn web server and trying to integrate AWS Redis Cache. I have added below entry in my settings.py file:
CACHE = {
'default': {
'BACKEND' : "redis_cache.cache.RedisCache",
'LOCATION' : "redis://xxx.xxx.xxxxx.cache.amazonaws.com/1",
'OPTIONS' : {
'CLIENT_CLASS' : 'redis_cache.client.DefaultClient',
},
}
}
And below is my view function:
def usertable(request):
obj = userdetails.objects.get(id=1)
name = obj.name
if cache.get(name):
cache_name = cache.get(name)
print ("From CACHE")
else:
cache_name = obj.name
cache.set(name, cache_name)
print ("*****************FROM DB********************")
context = {
'name' : cache_name,
}
This code is working for me and I can see From CACHE printed in my terminal. But the key value pair which is set if I manually connect to redis using below cli tool:
redis-cli -h xx.xx.xxxxx…cache.amazonaws.com -p 6379 -n 1
on giving keys * I do not see any key value pair is set.
I am not sure if this is the correct way to test integration of Redis cache. Kindly advice if anyone had tried Redis Cache system.
I want to connect my django application to MS-SQL server 2014 database.
I wrote this code for making connections.
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'HOST':'DESKTOP-6UNRAN0',
'PORT':'1433',
'NAME': 'MOVIE',
'COLLATION' : '',
}
}
I have installed sql_server.pyodbc
pip install django-pyodbc-azure
as mentioned in the documentation https://pypi.org/project/django-pyodbc-azure/. I am still getting error
django.db.utils.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
I no longer recommend using django-pyodbc-azure, as it is no longer maintained by the author. The active PyPI project for SQL Server in Django is currently django-mssql-backend. However, it only supports Django 2.2 and above. I would highly recommend upgrading to Django 2.2 (a long term support release), if not Django 3.0. 2.1 is no longer supported, and this will save you headaches down the road for a little bit of work now. I'm going to assume you're on Linux.
Step One: Install Microsoft's Driver for Linux (You May Also Use FreeTDS)
If you want to use Microsoft's driver, you can install it like this:
sudo curl https://packages.microsoft.com/config/rhel/7/prod.repo > /etc/yum.repos.d/mssql-release.repo
sudo yum remove unixODBC-utf16 unixODBC-utf16-devel
sudo ACCEPT_EULA=Y yum install msodbcsql17
Step Two: Create a Database and Service User in SQL Server
In SQL Server, set up a service user to your Django database. This script will create a user with the minimum permissions needed to the underlying database.
/*
This Script Creates a SQL Server Database, Login and User
With Appropriate Permissions for a Production Django Project
with migrations. Simply fill out the variables below (#db_name and #db_password)
Username will be set to database name + '_user' by default.
*/
DECLARE #db_name VARCHAR(MAX) = 'project'
DECLARE #db_password VARCHAR(MAX) = 'project_password'
DECLARE #db_user VARCHAR(MAX) = #db_name + '_user'
--
--
USE master
DECLARE #cmd VARCHAR(MAX)
-- Server scope: create SQL Server login and permissions
SET #cmd = 'CREATE LOGIN ' + #db_user + ' WITH PASSWORD = ''' + #db_password + ''''
EXEC(#cmd)
SET #cmd = 'GRANT VIEW SERVER STATE TO ' + #db_user
EXEC(#cmd)
SET #cmd = 'CREATE DATABASE [' + #db_name + ']'
EXEC(#cmd)
-- DB scope: create user for server login and permissions
SET #cmd = 'USE [' + #db_name + '];'
SET #cmd = #cmd + 'CREATE USER ' + #db_user + ' FOR LOGIN ' + #db_user + ';'
SET #cmd = #cmd + 'GRANT SELECT, INSERT, UPDATE, DELETE, ALTER, CREATE TABLE, REFERENCES, EXEC TO ' + #db_user
EXEC(#cmd)
Step Three: Configure Django
Finally, let's set up Django itself to point to SQL Server. In your Django project with your venv activated:
pip install django-mssql-backend
DATABASES = {
'default': {
'ENGINE': 'sql_server.pyodbc',
'HOST': 'dbserver.your-domain.com',
'PORT': '1433',
'NAME': 'project',
'USER': 'project_user',
'PASSWORD': 'project_password',
'OPTIONS': {
'driver': 'ODBC Driver 17 for SQL Server',
'unicode_results': True,
},
},
}
If you're using FreeTDS or another driver, change the OPTIONS line, 'driver': 'ODBC Driver 17 for SQL Server'. That should do it.
Good luck!
It is expected that you know if you want to connect to SQL Server you'll have to use/install ODBC as it is native data access API
Regarding documentation lets look into following lines
a Django Microsoft SQL Server external DB backend that uses ODBC by
employing the pyodbc library
Compatible with Micosoft ODBC Driver for SQL Server, SQL Server
Native Client, and FreeTDS ODBC drivers
OPTIONS
Dictionary. Current available keys are:
driver
String.
Server Native Client 11.0", "FreeTDS" etc). Default is "ODBC Driver 13
for SQL Server".
I am hosting my blogging app on google app engine in a standard environment. the website deploys successfully. but when I try to go to any other URL like anirudhmalik.in to anirudhmalik/blog/list/ it shows me 500 server error on the server side I am using PostgreSQL instance and the logs message only show me the error
projects/anirudhmalik-274008/logs/appengine.googleapis.com%2Frequest_log"
but I couldn't get any hint from this message and none of the post requests isworking on the website and everything is working fine in a local server or my local machine
you can check the website on anirudhmalik.in
and you can also give suggestion is google app engine is good to host your Django project and other cheap college student type hosting service thankyou
main.py
from annyportfolio.wsgi import application
app=application
app.yaml file
handlers:# This configures Google App Engine to serve the files in the app's static# directory.- - url: /static
static_dir: static-storage/
# This handler routes all requests not caught above to your main app. It is# required when static routes are defined, but can be omitted (along with# the entire handlers section) when there are no static files defined.
- url: /.*
secure: always redirect_http_response_code: 301
script: auto
env_variables:
DJANGO_SETTINGS_MODULE: annyportfolio.settings ```
I am coonecting to database by using the below code
if os.getenv('GAE_APPLICATION', None):
# Running on production App Engine, so connect to Google Cloud SQL using
# the unix socket at /cloudsql/<your-cloudsql-connection string>
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'HOST':'<instance address>',
'NAME': '<server name >',
'USER': '<server user>',
'PASSWORD': '<password of user>',
}
}
I can see in your code some issues, you need to put the name of the schema (not the server name) on the property 'name' of your default dictionary and the 'host' property need to follow the Cloud SQL connection string pattern, App Engine Standard not use the public IP to reach your PostgreSQL instance
For example:
# Running on production App Engine, so connect to Google Cloud SQL using
# the unix socket at /cloudsql/<your-cloudsql-connection string>
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'HOST':'/cloudsql/my-project:us-central1:my-sql-instance',
'NAME': 'mydjangoschema',
'USER': '<server user>',
'PASSWORD': '<password of user>',
}
To create the correct host string please follow this steps
Run this gcloud sdk command
gcloud sql instances describe [YOUR_INSTANCE_NAME]
In the output, note the value shown for [CONNECTION_NAME].
the host string must follow this pattern /cloudsql/[CONNECTION_NAME] in my example is /cloudsql/my-project:us-central1:my-sql-instance
I'm trying to connect Django to AWS Aurora server(RDS Aurora 5.6.10a) but I'm getting
django.db.utils.OperationalError: (1045, "Access denied for user 'admin'#'xx.xx.xx.xx' (using password: YES)
In Django settings.py, I've used the following settings
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': MYSQL_DATABASE,
'USER': MYSQL_USER,
'PASSWORD': MYSQL_PASSWORD,
'HOST': MYSQL_SERVER,
'PORT': '3306',
'OPTIONS': {
'ssl': {
'ca': '/Users/mac-user/workspace/project/settings/rds-combined-ca-bundle.pem',
}
}
}
}
I've also looked at similar questions like this but didn't work
I've also tried to connect using mysql-connector-python and mysql.connector.django as database engine, and ensured that I'm setting the right configuration (setting OPTIONS's ssl_ca and ssl_verify_cert) but I've also got a similar error:
mysql.connector.errors.ProgrammingError: 1045 (28000): Access denied for user 'admin'#'xx.xx.xx.xx' (using password: YES)
I've tried to connect to the database using PHP and command line and it worked fine:
mysql -h '<host>' -u '<username>' -p'<password>' --ssl-ca='<SSL_certificate_absolute_path>'
Am I missing any configuration for Django?
I don't have a direct answer to your problem, but apparently Aurora does not respond meaningfully (or correctly?) when MySQL client does not have ssl enabled (like MySQL command line and your PHP solution).
So my advice is to somehow force the MySQL client used by Django to use SSL connection, apparently it's not enabled/forced by default (the options you used might be insufficient).
I have a Django website with Postgresql backend, for which I'm utilizing pgbouncer for db connection pooling (transaction mode).
The application and the DB reside on separate servers (1 server each). I have installed pgbouncer on the application server. My question is: what should the config be in settings.py? Note that I'm using Unix sockets for connecting to pgbouncer.
My current settings.py contains:
DATABASE_URL = 'postgres://user1:pass1#xx.xxx.xxx.xxx:5432/db1'
DATABASES = {
'default': dj_database_url.config(default=DATABASE_URL)
}
Relevant sections of pgbouncer.ini are:
[databases]
db1 = host=xx.xxx.xxx.xxx port=5432 dbname=db1
listen_addr = *
listen_port = 6432
auth_type = md5
unix_socket_dir = /var/run/postgresql
pool_mode = transaction
max_client_conn = 200
default_pool_size = 300
userlist.txt contains:
"user1" "pass1"
Note: One answer is here, but doesn't work for me since the DB isn't available locally in my case. I need to set the DATABASE_URL environment variable, instead of using default = '...'.
One suggestions seems to be to treat pgbouncer as a database in settings.py. In that case, would something like the following work?
if PRODUCTION == '1':
#PRODUCTION is set to '1' if in production environment
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'pgbouncer',
'USER': 'user1',
'PASSWORD': 'pass1',
'HOST': '/var/run/postgresql',
'PORT': '6432',
}
From the docs:
pgbouncer is a PostgreSQL connection pooler. Any target application
can be connected to pgbouncer as if it were a PostgreSQL server, and
pgbouncer will create a connection to the actual server, or it will
reuse one of its existing connections.
Also,
Have your application (or the psql client) connect to pgbouncer
instead of directly to PostgreSQL server.
The configurations:
pgbouncer.ini: An example pgbouncer.ini with comments about defaults
[databases]
db1 = host=xx.xxx.xxx.xxx port=5432 dbname=db1
[pgbouncer]
listen_addr = *
listen_port = 6432
auth_type = md5
auth_file = userlist.txt
unix_socket_dir = /var/run/postgresql
pool_mode = transaction
max_client_conn = 100
default_pool_size = 20
userlist.txt:
"user1" "pass1"
to put in settings.py:
if PRODUCTION == '1':
#PRODUCTION is set to '1' if in production environment
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'db1',
'USER': 'user1',
'PASSWORD': 'pass1',
'HOST': '/var/run/postgresql',
# 'PORT': '6432',
}
Extra:
In case not using unix socket - you can set HOST : '127.0.0.1' or 'localhost' if pgbouncer is running locally, or whatever the IP of server pgbouncer will be running on.
From the docs:
If you’re using PostgreSQL, by default (empty HOST), the connection to
the database is done through UNIX domain sockets (‘local’ lines in
pg_hba.conf). If your UNIX domain socket is not in the standard
location, use the same value of unix_socket_directory from
postgresql.conf. If you want to connect through TCP sockets, set HOST
to ‘localhost’ or ‘127.0.0.1’ (‘host’ lines in pg_hba.conf). On
Windows, you should always define HOST, as UNIX domain sockets are not
available.
In case of postgreSQL For ENGINE you can use postgresql or postgresql_psycopg2 - there's difference between the both given your Django version - postgresql_psycopg2 vs posgresql.
All of your DB settings in settings.py should be identical to the settings in your pgbouncer config, except the host in settings.py will point to pgbouncer. You probably need to change 'NAME': 'pgbouncer' to 'NAME': 'db1'. Since you're using a unix socket the port shouldn't matter.