How to connect with Django and external mysqlDB - django

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
}
}

Related

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

Specify the --ssl option for django database connection

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},
}
}

i am getting error while giving my ip and localhost name in postgres admin

I am getting error while giving my IP address and localhost name in PostgreSQL admin, any help would be appreciated.
You can take a look at my PostgreSQL admin image here:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'myproject',
'USER': 'myprojectuser',
'PASSWORD': 'fakepassword',
'HOST': 'localhost',
'PORT': '',
}
}

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.

How to add a remote postgres database in my django app?

What I'am trying to do is to use 2 databases in my django app. One is to be accessed from a remote server. Django settings has something like this
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'snackvoxadmin'
},
'users': {
.....
}
}
The database user has a url like similar to this one: postgres://a78adj1he81....
You can decompose your database url and configure it like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
And the pattern for a database url is :
postgres://user:password#host:post/database
https://docs.djangoproject.com/en/1.8/ref/settings/#databases
Or you can use the package dj-database-url to directly use the database url.
E.g. from readme :
import dj_database_url
DATABASES = {'default': dj_database_url.parse('postgres://...')}
That URL presumably consists of a username, a password, and a host name/IP address. You could split them up yourself or use the dj-database-url library.