django - Connect an existing MySQL db to Django - python

I have a database that i would like to use for an app in my new Django project.
I tried adding it to the setting.py by doing the following:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'Colta_create'),
'USER': 'root',
'PASSWORD': 'toor123',
'HOST': '',
'PORT': ''
}
}
I have installed mysqlclient, but i get this error:
django.db.utils.OperationalError: (1049, "Unknown database 'colta_create'")

Related

python/django database connection issue

Database settings
DATABASES = {
'default': {
'ENGINE': os.environ.get('DB_ENGINE', "mysql"),
'NAME': os.environ.get('DB_NAME', "django_db"),
'USER': os.environ.get('DB_USER', "root"),
'PASSWORD': os.environ.get('DB_PASS', "123456798"),
'HOST': os.environ.get('DB_HOST', "localhost"),
'PORT': os.environ.get('DB_PORT'),
}
}
error
django.core.exceptions.ImproperlyConfigured: 'mysql' isn't an available database backend or couldn't be imported. Check the above exception. To use one of the built-in backends, use 'django.db.backends.XXX', where XXX is one of:
'mysql', 'oracle', 'postgresql', 'sqlite3'
Connecting with mysql to generate migration but facing issue
Its my first time and facing following above issue please guide.
Instead of :
mysql in 'ENGINE': os.environ.get('DB_ENGINE', "mysql"),
Try this:
'ENGINE': os.environ.get('DB_ENGINE', "django.db.backends.mysql"),
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_db',
'USER': 'root',
'PASSWORD': '123456798',
}
}
Try this to establish a connection.
I don't usually add HOST or Port but it still connect to database.
I only mention Host or Port if they don't have the default settings.

How to indicate to a specific version of database? [Django] [PostgreSQL]

I using django version 3.0.2.
I'd like to use postgreSQL as my instance db.
And there are two version postgreSQL in server.
After config setting.py DATABASES parameter, and runserver.
It showed error.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'dj_web_console',
'USER': 'django',
'PASSWORD': 'django',
'HOST': 'localhost',
'PORT': '',
}
}
psycopg2.OperationalError: FATAL: password authentication failed for user "django"
I'm sure that the username and password are correct.
How to config pg_path in Django as odoo:
In this case, I can use the specific version of pgsql. And run smoothly.
I solve this by indicating the port of the version I installed.
In this case, I have a version 9.6 which install in 5432 and the other is 5433.
So I just solved this by filling in 5433.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'dj_web_console',
'USER': 'django',
'PASSWORD': 'django',
'HOST': 'localhost',
'PORT': '5433',
}
}

How to connect mysql in django

Here is my connection details
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django4webo1',
'USER': 'root',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '3306',
}
}
when i start server error will come and i also need migrations table in db
super(Connection, self).__init__(*args, **kwargs2)
django.db.utils.OperationalError: (1049, "Unknown database 'django4webo1'")
You need to create first your mysql database and then configure your settings as:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '<your_db_name>',
'USER': '<your_username_in_db>'
'PASSWORD': '<your_password_to_access_db>',
'HOST': 'localhost',
'PORT': '3306'
}
}
Note: Before you run the mirations and migrate you need to create a mysql database, after that run the migrations python manage.py makemigrations and then migrate the database python manage.py migrate

Django not finding database despite it existing

I'm trying to change the default database used in Django to MySQL, I've adjusted my settings.py file like so:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'clients',
'HOST': 'localhost',
'PORT': '3307',
'USER': 'root',
'PASSWORD': '*****',
}
}
I'm still getting the django.db.utils.OperationalError: (1049, "Unknown database 'clients'"). When I try to connect to the 'mysql' database that comes default with MySQL I have no issue. I get this error when I try to use manage.py makemigrations,migrations or runserver. I have MySQL-Python installed and everything. Proof that my clients database exists:

Django on Google App Engine with Cloud SQL in dev environment

I am trying to create an application with Django on GAE and CloudSQL as the db.
I used this google developers link and this link for setting up the dev-environment. I am not able to connect to local mysql db.
Here is the DATABASE setting which I am trying to use.
if (os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine') or
os.getenv('SETTINGS_MODE') == 'prod'):
DATABASES = {
'default': {
'ENGINE': 'google.appengine.ext.django.backends.rdbms',
'INSTANCE': 'instance:appid',
'NAME': 'database_name',
}
}
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'USER': 'root',
'PASSWORD': '',
'HOST': 'localhost',
'NAME': 'database_name',
}
}
My app is working perfectly on production GAE, but when I try to start the app on dev env, I am getting this error
File "/home/sandeep/Downloads/google_appengine/google/appengine/tools/dev_appserver_import_hook.py", line 635, in __init__
raise IOError(errno.EACCES, 'file not accessible', filename)
IOError: [Errno 13] file not accessible: '/usr/local/lib/python2.7/site-packages/MySQL_python-1.2.4-py2.7-linux-x86_64.egg'
Complete stack-trace at http://pastebin.com/ZXHv0FPQ
I had installed the "python-mysql" by downloading the source and running "python setup.py install"
Edit 1
I have also tried adding the MySQLdb to the library.
- name: MySQLdb
version: "latest"
Got this error
the library "MySQLdb" is not supported
in "/home/sandeep/development/UploadImage/src/app.yaml", line 14, column 1
EDIT 2
Django syncdb is working fine with this settings and django is able to create the tables for me.But,when I try to run via "dev_appserver.py", then I got the above stacktrace.
I am able to access the cloudSQL in dev environment.
This should work as mentioned here. I don't there is anything wrong with this code snippet.
import os
if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine'):
# Running on production App Engine, so use a Google Cloud SQL database.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '/cloudsql/your-project-id:your-instance-name',
'NAME': 'django_test',
'USER': 'root',
}
}
elif os.getenv('SETTINGS_MODE') == 'prod':
# Running in development, but want to access the Google Cloud SQL instance
# in production.
DATABASES = {
'default': {
'ENGINE': 'google.appengine.ext.django.backends.rdbms',
'INSTANCE': 'your-project-id:your-instance-name',
'NAME': 'django_test',
'USER': 'root',
}
}
else:
# Running in development, so use a local MySQL database.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_test',
'USER': 'root',
'PASSWORD': 'root',
}
}
I have also been using Google App Engine with cloudsql in django and here are the settings that I have been using for deployment and local development and it works just fine !!
Settings for deployment in GAE
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '/cloudsql/instance:appid',
'NAME': 'name_of_database',
'USER': 'mysql_user',
}
}
Settings for local development with App engine sdk
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'name_of_database',
'USER': 'mysql_user',
'PASSWORD': 'pwd',
'HOST': 'ip_address_of_cloudsql_instance', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
}
}

Categories