I got thid error:
(2013, 'Lost connection to MySQL server during query')
I have read that I can increase database timeout.
What do I do?
Settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'c9',
'USER': ddd,
'PASSWORD': '',
'HOST': ddd,
'PORT': '3306',
'ATOMIC_REQUESTS': True
},
'OPTIONS': {
'timeout': 99999999,
'net_read_timeout': 9999999
}
Database object declaration is not correct, OPTIONS should be declared inside default object
#settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'c9',
'USER': ddd,
'PASSWORD': '',
'HOST': ddd,
'PORT': '3306',
'ATOMIC_REQUESTS': True,
'OPTIONS':{
'timeout': 99999999,
'net_read_timeout': 9999999
},
}
}
Related
Django's settings.py file has a DATABASES dictionary that stores configuration information for any number of database backends:
# settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'test': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'testing',
'USER': 'bert',
'PASSWORD': '***',
'HOST': 'remotemysql.com',
'PORT': '3306',
},
'dev': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'development',
'USER': 'ernie',
'PASSWORD': '***',
'HOST': 'remotemysql.com',
'PORT': '3306',
},
...
}
I would expect the Django authors to have included a method to easily switch among these configuration options, such as a separate variable somewhere
USE_THIS_DB = 'test';
in order to easily switch between testing, development, production, etc. databases.
I can't find this option. The only information I can find about switching databases is to manually rename the different configuration options in DATABASES so that the one I want to use is called default, which seems unnecessarily clunky, error-prone, and non-portable.
Is there no way to more elegantly switch Django among different databases at startup?
Why not just DIY in Python ?
# settings.py
DATABASES = {
'test': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'testing',
'USER': 'bert',
'PASSWORD': '***',
'HOST': 'remotemysql.com',
'PORT': '3306',
},
'dev': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'development',
'USER': 'ernie',
'PASSWORD': '***',
'HOST': 'remotemysql.com',
'PORT': '3306',
},
'sqlite': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
}
USE_THIS_DB = 'test'
DATABASES['default'] = DATABASES[USE_THIS_DB]
My database and project work great using this database setting:
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'myprojectdb',
'USER': 'myprojectuser',
'PASSWORD': 'my_secret_password',
'HOST': 'localhost',
'PORT': '5432',
'ATOMIC_REQUESTS': True
},
}
But I want to add a 'readonly' database entry for my readonly db user, like this, so that I can run django-sql-explorer:
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'myprojectdb',
'USER': 'myprojectuser',
'PASSWORD': 'my_secret_password',
'HOST': 'localhost',
'PORT': '5432',
'ATOMIC_REQUESTS': True
},
'readonly': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'myprojectdb',
'USER': 'myprojectreadonly',
'PASSWORD': 'your_eyes_only_secret',
'HOST': 'localhost',
'PORT': '5432',
'ATOMIC_REQUESTS': True
}
}
And now django throws a couple different errors. If I try to do anything with migrations, I get:
django.db.utils.ProgrammingError: permission denied for relation django_migrations
If I try to runserver, I get:
"AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODEL
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'users.User' that has not been installed
It seems like when I add the second database setting, django is attempting to use that for migrations, running the server, etc, even though it's not 'default'
If I comment out the second entry, everything works great again.
Any recommendations on how to correct this?
Edit: If I change the settings to use the same username and password, everything works great, so it's not just an issue of django being confused on names. IE:
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'myprojectdb',
'USER': 'myprojectuser',
'PASSWORD': 'my_secret_password',
'HOST': 'localhost',
'PORT': '5432',
'ATOMIC_REQUESTS': True
},
'readonly': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'myprojectdb',
'USER': 'myprojectuser',
'PASSWORD': 'my_secret_password',
'HOST': 'localhost',
'PORT': '5432',
'ATOMIC_REQUESTS': True
}
}
The two configurations shares the same database names 'NAME': 'myprojectdb'
So when you try to migrates dbname seems to conflicts so the applications is having an indecision on which to use The previous or the later
I'm currently using a single PostgreSQL database, with standard settings.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': password,
'HOST': 'localhost',
'PORT': '',
}
}
My question is, can I keep using the default postgres setup, and just perform CREATE EXTENSION postgis in the shell to get access to postgis features? Or do I need to add a postgis database separately, like below:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': password,
'HOST': 'localhost',
'PORT': '',
}
'geodata': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'geodjango',
'USER': 'geo',
},
}
You can keep using the default postgres setup, just changing the engine to: django.contrib.gis.db.backends.postgis
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': password,
'HOST': 'localhost',
'PORT': '',
}
}
I'm testing my code on Windows 10. I have a Django application that needs to call a stored procedure on a remote SQL Server database. Here's the DATABASES snippet from settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db1',
'HOST': 'mycompany.com',
'PORT': '3306',
'USER': 'user',
'PASSWORD': 'pw',
},
'ss': {
'ENGINE': 'django_pyodbc',
'NAME': 'db2',
'HOST': 'myserver\SQLEXPRESS',
'USER': 'myuser',
'PASSWORD': 'mypw',
'PORT': '1433',
# 'DRIVER': 'SQL Server',
'OPTIONS': {
'driver_supports_utf8': True,
'host_is_server': True, # must be True for remote db
'autocommit': True,
'unicode_results': True,
'extra_params': 'tds_version=8.0',
},
},
}
Here's a code snippet from my view:
cursor = connections['ss'].cursor()
cursor.execute("{call dbo.mysproc(?)}", (id))
When I execute the cursor.execute statement I get this error:
django.db.utils.DatabaseError: ('The SQL contains 1 parameter markers,
but 36 parameters were supplied', 'HY000')
My parameter, id, is a GUID.
Thoughts?
Here's the fix, simply changed the parentheses surrounding the parameter to square brackets:
cursor.execute("{call dbo.mysproc(?)}", [id])
I found this by trial and error.
I am new to django.
I need to use mysql database. I also need to specify the host, username, password..etc
I tried to change my .settings file but it is not working
DATABASES = {
'default': {
'ENGINE': 'mysql',
'NAME': 'database',
'USER': 'root',
'PASSWORD': 'passwd',
'HOST': 'localhost',
'PORT': '',
}
}
Its django.db.backends.mysql not just mysql
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'database',
'USER': 'root',
'PASSWORD': 'passwd',
'HOST': 'localhost',
'PORT': '',
}
}
What you need to use is the backend module connection
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'yourdatabase',
'USER': 'yourusername',
'PASSWORD': 'yourpassword',
'HOST': 'localhost',
'PORT': '',
}