Specify the --ssl option for django database connection - django

When I connect to my database with mysql I specify the option --ssl, or else I get the error
SSL connection is required. Please specify SSL options and retry.
I know that with MySQL you can connect with a ssl certificate where you have a copy of the certificate locally. With this approach I know how to move forward with Django, as you can specify the option as described here https://stackoverflow.com/a/12285601/2889451
However, in my case I simply pass the option --ssl, and I don't have a local copy of the certificate.
How can I enable this option in Django?
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': DB_NAME,
'USER': DB_USER,
'PASSWORD': DB_PASSWORD,
'HOST': DB_HOST,
'PORT': '',
}
}

Apparently you can solve it by passing the options as shown below:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': DB_NAME,
'USER': DB_USER,
'PASSWORD': DB_PASSWORD,
'HOST': DB_HOST,
'PORT': '',
'OPTIONS': {'ssl': True},
}
}

Related

Django not connecting to PlanetScale , SSL error

Trying to use planetscale for my db platform for a Django app that i am building. However i'm running into some errors
django.db.utils.OperationalError: (2026, "SSL connection error: no valid certificates were found, CAFile='*************', CAPath=''. One or more of the parameters passed to the function was invalid. Error 2148074333/0x8009035D")
The configuration was copied straight from planetscale
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': env('DB_NAME'),
'HOST': env('DB_HOST'),
'PORT': env('DB_PORT'),
'USER': env('DB_USER'),
'PASSWORD': env('DB_PASSWORD'),
'OPTIONS': {'ssl': {'ca': env('MYSQL_ATTR_SSL_CA')}}
}
}
Finally got it working.
the value for the OPTIONS key should be {'ssl': {'ssl-ca': env('MYSQL_ATTR_SSL_CA')}}

Solve a connection refuse error in PostgreSQL database

How do I solve this operational error? I have deployed my Django website and I have some issues with the database connection
my settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'avc',
'USER': 'postgres',
'PASSWORD': '****',
'HOST': 'localhost',
'PORT':'5432'
}
}
I am using Postgres 10
this is the error database refused the client request,
error
I assume Django needs psycopg2 to connect
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'avc',
'USER': 'postgres',
'PASSWORD': '****',
'HOST': 'localhost',
'PORT':'5432'
}
}
As well check if pg_hba.conf is accepting connections
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
please refer step by the step installation guide by edb
https://www.enterprisedb.com/postgres-tutorials/how-use-postgresql-django

Django always connects to localhost mongodb

I have followed the guide of db connection config:
https://nesdis.github.io/djongo/database-configuration/
However, it always connects to localhost one, not my setting's one.
Does anyone have any idea on this issue?
my packages versions:
Django 2.0
django-cors-headers 2.4.0
django-rest-auth 0.9.3
djangorestframework 3.9.0
djongo 1.1
mongoengine 0.16.3
pip 10.0.1
pymongo 3.7.2
urllib3 1.24.1
my settings
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'test_db',
'HOST': 'somewhere.com',
'PORT': 27017
}
}
Is seems that somewhere along the way, djongo changed the structure of the database settings. After wasting days trying to find the solution, I came across a page that had the updated structure, Try setting your DATABASE structure to this:
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'yourmongodb',
'CLIENT': {
'host': 'some-host.or.ip',
'port': 27017,
'username': 'youruser',
'password': 'yourdbpass',
'authSource': 'yourcollection',
}
},
}
Have you edited your setting in the settings.py file
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'XXXX',
'USER': 'XXX',
'PASSWORD': 'XXXX',
'HOST': 'XXX.XXX.XXX.XXX',
'PORT': '3306',
'OPTION': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
}
}
}
First: Create a SSH Tunnel to remote mysql server on localhost
ssh -N -L 3306:127.0.0.1:3306 root#192.168.0.122
192.168.0.122 >> is a remote LAN server of mine.
Second : use 127.0.0.1 as a HOST on Django connection string.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'XXXX',
'USER': 'XXX',
'PASSWORD': 'XXXX',
'HOST': '127.0.0.1',
'PORT': '3306',
'OPTION': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
}
}
OR change HOST to 127.0.0.1 in .env if using environment file.
This worked for me.

I am getting django database connection error

DATABASE_OPTIONS = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'student',
'USER': 'root',
'PASSWORD': 'pooja',
'HOST': 'localhost',
'PORT': 3306,
'init_command': 'SET storage_engine=INNODB, SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED'
},
}
Exception IS:
raise ImproperlyConfigured("settings.DATABASES is improperly configured. "
django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.
I am using mysql database I am getting above exception in django while migrating model to the database please let me know what is the error what i ma doing wrong here.
thanks you for your reply
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'student',
'USER': 'root',
'PASSWORD': 'pooja',
'HOST': 'localhost',
'PORT': 3306,
'init_command': 'SET storage_engine=INNODB, SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED'
},
}

How to connect with Django and external mysqlDB

I have 2VM. One is the "Apache+Django" server and one Mysql server.
I want to know that How connect the Django and externel mysql ?
Change below setting in your settings.py file
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydb', # remote db name
'USER': 'root', # remote db user
'PASSWORD': 'mypass', #remote db pass
'HOST': 'remote.ip.address.something',#remote ip or url
'PORT': '3306', # remote post
}
}