Deploy Django on AWS Elastic Beanstalk cannot connect to sql server - python

I'm trying to deploy django on to AWS EB. I have a RDS linked to the instance and database is migrated without any problem.
When I log onto the server using ssh and try to create a superuser, I run into following errors:
django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' (111)")
I used the same setup for several other django site, didn't have the same problem.
The only difference this time is in the view I'm using native sql, I wonder if the connection.cursor() is causing this problem.
In settings I have:
if 'RDS_HOSTNAME' in os.environ:
print "live site server connection: ", os.environ['RDS_HOSTNAME']
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': os.environ['RDS_DB_NAME'],
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
},
'USER': os.environ['RDS_USERNAME'],
'PASSWORD': os.environ['RDS_PASSWORD'],
'HOST': os.environ['RDS_HOSTNAME'],
'PORT': os.environ['RDS_PORT'],
}
}
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'new_dash',
'USER': 'root',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
/opt/python/run/venv/bin/python manage.py createsuperuser
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 63, in execute
return super(Command, self).execute(*args, **options)
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 342, in execute
self.check()
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 374, in check
include_deployment_checks=include_deployment_checks,
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/core/management/base.py", line 361, in _run_checks
return checks.run_checks(**kwargs)
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/core/checks/model_checks.py", line 30, in check_all_models
errors.extend(model.check(**kwargs))
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/models/base.py", line 1266, in check
errors.extend(cls._check_fields(**kwargs))
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/models/base.py", line 1337, in _check_fields
errors.extend(field.check(**kwargs))
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 893, in check
errors = super(AutoField, self).check(**kwargs)
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 208, in check
errors.extend(self._check_backend_specific_checks(**kwargs))
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 311, in _check_backend_specific_checks
return connections[db].validation.check_field(self, **kwargs)
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/backends/mysql/validation.py", line 41, in check_field
field_type = field.db_type(connection)
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 629, in db_type
return connection.data_types[self.get_internal_type()] % data
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/__init__.py", line 33, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 183, in data_types
if self.features.supports_microsecond_precision:
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/backends/mysql/features.py", line 54, in supports_microsecond_precision
return self.connection.mysql_version >= (5, 6, 4) and Database.version_info >= (1, 2, 5)
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 367, in mysql_version
with self.temporary_connection() as cursor:
File "/usr/lib64/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 564, in temporary_connection
cursor = self.cursor()
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 231, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 204, in _cursor
self.ensure_connection()
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
self.connect()
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
self.connect()
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/backends/base/base.py", line 171, in connect
self.connection = self.get_new_connection(conn_params)
File "/opt/python/run/venv/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 263, in get_new_connection
conn = Database.connect(**conn_params)
File "/opt/python/run/venv/local/lib64/python2.7/site-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/opt/python/run/venv/local/lib64/python2.7/site-packages/MySQLdb/connections.py", line 193, in __init__
super(Connection, self).__init__(*args, **kwargs2)
django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' (111)")

Related

Django Cannot Connect to Remote MySQL Server

I am running a Django and attempting to connect to a MySQL server at my university.
First of all:
In order to connect the database, I normally SSH into a remote server (with authentication) of theirs, and then run MySQL and enter my MySQL authentications.
This works without an issue, in Terminal as well as DataGrip.
Now, using PyCharm, I have installed mysqlclient in the intepreter and changed my
settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': '[DB_NAME]',
'USER': '[DB_USERNAME]',
'PASSWORD': '[DB_PASSWORD]',
'HOST': '[REMOTE SERVER ADDRESS HOSTING MYSQL]',
'PORT': '3306',
}
}
Also using PyCharm, I have even opened up a data source in the Data Sources Tool Window and connected through SSH tunnel and MySQL authentication to the database. This is successful, and it is possible to make queries.
However, I try to run:
manage.py check
and I receive:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site- packages/django/db/backends/base/base.py", line 213, in ensure_connection
self.connect()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/base/base.py", line 189, in connect
self.connection = self.get_new_connection(conn_params)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 274, in get_new_connection
conn = Database.connect(**conn_params)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/MySQLdb/__init__.py", line 86, in Connect
return Connection(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/MySQLdb/connections.py", line 204, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on '[REMOTE SERVER ADDRESS HOSTING MYSQL]' (60)")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Applications/PyCharm.app/Contents/helpers/pycharm/django_manage.py", line 43, in <module>
run_module(manage_file, None, '__main__', True)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/runpy.py", line 205, in run_module
return _run_module_code(code, init_globals, run_name, mod_spec)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/Users/skillapop/Desktop/streamcity_project/manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/commands/check.py", line 68, in handle
fail_level=getattr(checks, options['fail_level']),
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/base.py", line 359, in check
include_deployment_checks=include_deployment_checks,
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/base.py", line 346, in _run_checks
return checks.run_checks(**kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/checks/model_checks.py", line 30, in check_all_models
errors.extend(model.check(**kwargs))
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/base.py", line 1283, in check
errors.extend(cls._check_fields(**kwargs))
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/base.py", line 1358, in _check_fields
errors.extend(field.check(**kwargs))
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 913, in check
errors = super(AutoField, self).check(**kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 219, in check
errors.extend(self._check_backend_specific_checks(**kwargs))
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 322, in _check_backend_specific_checks
return connections[db].validation.check_field(self, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/mysql/validation.py", line 49, in check_field
field_type = field.db_type(self.connection)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 644, in db_type
return connection.data_types[self.get_internal_type()] % data
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 174, in data_types
if self.features.supports_microsecond_precision:
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/mysql/features.py", line 53, in supports_microsecond_precision
return self.connection.mysql_version >= (5, 6, 4) and Database.version_info >= (1, 2, 5)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 385, in mysql_version
with self.temporary_connection() as cursor:
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/contextlib.py", line 81, in __enter__
return next(self.gen)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/base/base.py", line 591, in temporary_connection
cursor = self.cursor()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/base/base.py", line 254, in cursor
return self._cursor()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/base/base.py", line 229, in _cursor
self.ensure_connection()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
self.connect()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
self.connect()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/base/base.py", line 189, in connect
self.connection = self.get_new_connection(conn_params)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 274, in get_new_connection
conn = Database.connect(**conn_params)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/MySQLdb/__init__.py", line 86, in Connect
return Connection(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/MySQLdb/connections.py", line 204, in __init__
super(Connection, self).__init__(*args, **kwargs2)
django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on '[REMOTE SERVER ADDRESS HOSTING MYSQL]' (60)")
It is also important to note that I HAVE connected a Django project to this exact same server and database before in a test run. Now, that test project and the new project does NOT connect.
Any help would be appreciated!

Create a Django Database

This code works on other people's local computer - we aren't running it in production yet. But mine isn't working. A coworker indicated that I need to create a database. Prior to using mysql, I was using sqlite, which didn't require this.
When I run python manage.py runserver this is what I get:
XX-MacBook-Pro:xx xx$ python manage.py runserver
Performing system checks...
Unhandled exception in thread started by <function wrapper at 0x104ebb668>
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/core/management/commands/runserver.py", line 116, in inner_run
self.check(display_num_errors=True)
File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "/Library/Python/2.7/site-packages/django/core/checks/registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "/Library/Python/2.7/site-packages/django/core/checks/model_checks.py", line 28, in check_all_models
errors.extend(model.check(**kwargs))
File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 1170, in check
errors.extend(cls._check_fields(**kwargs))
File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 1247, in _check_fields
errors.extend(field.check(**kwargs))
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py", line 925, in check
errors = super(AutoField, self).check(**kwargs)
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py", line 208, in check
errors.extend(self._check_backend_specific_checks(**kwargs))
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py", line 317, in _check_backend_specific_checks
return connections[db].validation.check_field(self, **kwargs)
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/validation.py", line 18, in check_field
field_type = field.db_type(connection)
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py", line 625, in db_type
return connection.data_types[self.get_internal_type()] % data
File "/Library/Python/2.7/site-packages/django/db/__init__.py", line 36, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/Library/Python/2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 184, in data_types
if self.features.supports_microsecond_precision:
File "/Library/Python/2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/features.py", line 53, in supports_microsecond_precision
return self.connection.mysql_version >= (5, 6, 4) and Database.version_info >= (1, 2, 5)
File "/Library/Python/2.7/site-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 359, in mysql_version
with self.temporary_connection():
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "/Library/Python/2.7/site-packages/django/db/backends/base/base.py", line 564, in temporary_connection
cursor = self.cursor()
File "/Library/Python/2.7/site-packages/django/db/backends/base/base.py", line 233, in cursor
cursor = self.make_cursor(self._cursor())
File "/Library/Python/2.7/site-packages/django/db/backends/base/base.py", line 204, in _cursor
self.ensure_connection()
File "/Library/Python/2.7/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
self.connect()
File "/Library/Python/2.7/site-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Library/Python/2.7/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
self.connect()
File "/Library/Python/2.7/site-packages/django/db/backends/base/base.py", line 171, in connect
self.connection = self.get_new_connection(conn_params)
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 264, in get_new_connection
conn = Database.connect(**conn_params)
File "/Library/Python/2.7/site-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/Library/Python/2.7/site-packages/MySQLdb/connections.py", line 193, in __init__
super(Connection, self).__init__(*args, **kwargs2)
django.db.utils.OperationalError: (1049, "Unknown database 'testdb'")
My settings file has this:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
...
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'testdb',
'USER': ‘xx’,
'PASSWORD': ‘xx’,
'HOST': 'localhost'
}
}
I've tested the user, password and know that they work. My MySQL is fully functional.
If I remove the DATABASES 'NAME' I get:
django.db.utils.OperationalError: (1046, 'No database selected')
Presumably you have tested the username and password by going into the mysql shell. So you can just do the same thing again, and from there do CREATE DATABASE testdb.

Not able to run 'python manage.py migrate'

I am new to programming and learning the web development. I have installed:
-Django 1.9
-postgresql 9.5
-psycopg2(using whl file) db api for the postgresql on Windows 7 os.
not a virtual environment.
I have set up the database connection in my settings.py file for my project as following.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'Test',
'User': 'postgres',
'PASSWORD': 'admin',
'HOST': 'localhost',
'PORT': '5432',
}
}
but when I am running migrate I am running in to following problem. I want to connect the postgres to my django
See the error below:
Traceback (most recent call last):
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\backends\base\base.py", line 199, in ensure_connection
self.connect()
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\backends\base\base.py", line 171, in connect
self.connection = self.get_new_connection(conn_params)
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\backends\postgresql\base.py", line 175, in get_new_connection
connection = Database.connect(**conn_params)
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\psycopg2\__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)psycopg2.OperationalError
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\core\management\__init__.py", line 353, in execute_from_command_line
utility.execute()
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\core\management\__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\core\management\base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\core\management\base.py", line 399, in execute
output = self.handle(*args, **options)
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\core\management\commands\migrate.py", line 89, in handle
executor = MigrationExecutor(connection, self.migration_progress_callback)
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\migrations\executor.py", line 20, in __init__
self.loader = MigrationLoader(self.connection)
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\migrations\loader.py", line 49, in __init__
self.build_graph()
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\migrations\loader.py", line 176, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\migrations\recorder.py", line 65, in applied_migrations
self.ensure_schema()
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\migrations\recorder.py", line 52, in ensure_schema
if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\backends\base\base.py", line 231, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\backends\base\base.py", line 204, in _cursor
self.ensure_connection()
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\backends\base\base.py", line 199, in ensure_connection
self.connect()
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\utils.py", line 95, in __exit__six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\utils\six.py", line 685, in reraise
raise value.with_traceback(tb)
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\backends\base\base.py", line 199, in ensure_connection
self.connect()
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\backends\base\base.py", line 171, in connect
self.connection = self.get_new_connection(conn_params)
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\postgresql\base.py", line 175, in get_new_connection
connection = Database.connect(**conn_params)
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\psycopg2\__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)
django.db.utils.OperationalError
I have checked with python script the psycopg2 is able to connect the database in postgresql but when I am running migrate from django project, its not working and getting above error.
Your database config:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'Test',
'User': 'postgres',
'PASSWORD': 'admin',
'HOST': 'localhost',
'PORT': '5432',
}
}
Should be:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'Test',
'USER': 'postgres',
'PASSWORD': 'admin',
'HOST': 'localhost',
'PORT': '5432',
}
}
Notice that User should be USER

Django mysql database credentials read_default_file giving error

I am providing mySql credentials from a different conf file to django project.
Earlier it was working fine, but now it stopped working after I reinstalled my ubuntu.
settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'read_default_file': './connection.cnf',
}
}
}
connection.cnf:
[client]
database=test
user=root
password=rajiv#123
HOST=localhost
PORT=3306
default-charset=utf8
Also, the location of the conf file is as follows:
rajiv#ubuntu:~/djangoProjects/test$ ls -l
total 9
drwxrwxrwx 1 root root 4096 Dec 28 11:04 test
drwxrwxrwx 1 root root 4096 Dec 27 14:00 testApp
-rwxrwxrwx 1 root root 103 Dec 3 00:37 connection.cnf
-rwxrwxrwx 1 root root 252 Oct 15 11:36 manage.py
The error given is as follows:
rajiv#ubuntu:~/djangoProjects/test$ python manage.py runserver
Performing system checks...
Warning: World-writable config file '/home/rajiv/djangoProjects/test/connection.cnf' is ignored
Unhandled exception in thread started by <function wrapper at 0x7fa2c1d28cf8>
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 116, in inner_run
self.check(display_num_errors=True)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 426, in check
include_deployment_checks=include_deployment_checks,
File "/usr/local/lib/python2.7/dist-packages/django/core/checks/registry.py", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "/usr/local/lib/python2.7/dist-packages/django/core/checks/model_checks.py", line 28, in check_all_models
errors.extend(model.check(**kwargs))
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 1170, in check
errors.extend(cls._check_fields(**kwargs))
File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 1247, in _check_fields
errors.extend(field.check(**kwargs))
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 925, in check
errors = super(AutoField, self).check(**kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 208, in check
errors.extend(self._check_backend_specific_checks(**kwargs))
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 317, in _check_backend_specific_checks
return connections[db].validation.check_field(self, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/validation.py", line 18, in check_field
field_type = field.db_type(connection)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 625, in db_type
return connection.data_types[self.get_internal_type()] % data
File "/usr/local/lib/python2.7/dist-packages/django/db/__init__.py", line 36, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 184, in data_types
if self.features.supports_microsecond_precision:
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/features.py", line 53, in supports_microsecond_precision
return self.connection.mysql_version >= (5, 6, 4) and Database.version_info >= (1, 2, 5)
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 33, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 359, in mysql_version
with self.temporary_connection():
File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py", line 564, in temporary_connection
cursor = self.cursor()
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py", line 231, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py", line 204, in _cursor
self.ensure_connection()
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py", line 199, in ensure_connection
self.connect()
File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py", line 199, in ensure_connection
self.connect()
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/base.py", line 171, in connect
self.connection = self.get_new_connection(conn_params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 264, in get_new_connection
conn = Database.connect(**conn_params)
File "/usr/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 206, in __init__
super(Connection, self).__init__(*args, **kwargs2)
django.db.utils.OperationalError: (1045, "Access denied for user 'rajiv'#'localhost' (using password: NO)")
Not able to figure out why is it giving error.
If anything is wrong, can anyone suggest some other methods of doing it.
Well if you still want's to use options read_default_file
Then Solution is
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'read_default_file': os.path.join(BASE_DIR, 'connection.cnf'),
}
}
}
connection.cnf
[client]
database = test
user = root
password = rajiv#123
default-character-set = utf8
https://code.djangoproject.com/ticket/24653
Try setting the permission of the config file to 644
chmod 644 connection.cnf
The problem is that because the permissions are too open, the file is ignored, and therefore you are not logged in into mysql.
I faced the same problem and it was pointing to the wrong path.
Try printing the path to the terminal
print(os.path.join(BASE_DIR, ''))
And then correct the path to point to the file
Adding double quotes to the password string worked for me.

django 1.8.3 python manage.py migrate Linux [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/core/management/base.py", line 393, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/core/management/base.py", line 443, in execute
self.check()
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/core/management/base.py", line 481, in check
include_deployment_checks=include_deployment_checks,
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/core/checks/registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/core/checks/model_checks.py", line 28, in check_all_models
errors.extend(model.check(**kwargs))
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/db/models/base.py", line 1181, in check
errors.extend(cls._check_fields(**kwargs))
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/db/models/base.py", line 1258, in _check_fields
errors.extend(field.check(**kwargs))
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/db/models/fields/__init__.py", line 934, in check
errors = super(AutoField, self).check(**kwargs)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/db/models/fields/__init__.py", line 207, in check
errors.extend(self._check_backend_specific_checks(**kwargs))
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/db/models/fields/__init__.py", line 306, in _check_backend_specific_checks
return connection.validation.check_field(self, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/db/backends/mysql/validation.py", line 18, in check_field
field_type = field.db_type(connection)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/db/models/fields/__init__.py", line 614, in db_type
return connection.data_types[self.get_internal_type()] % data
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/db/__init__.py", line 36, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/utils/functional.py", line 60, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/db/backends/mysql/base.py", line 196, in data_types
if self.features.supports_microsecond_precision:
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/utils/functional.py", line 60, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/db/backends/mysql/features.py", line 52, in supports_microsecond_precision
return self.connection.mysql_version >= (5, 6, 4) and Database.version_info >= (1, 2, 5)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/utils/functional.py", line 60, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/db/backends/mysql/base.py", line 371, in mysql_version
with self.temporary_connection():
File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/db/backends/base/base.py", line 462, in temporary_connection
cursor = self.cursor()
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/db/backends/base/base.py", line 162, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/db/backends/base/base.py", line 135, in _cursor
self.ensure_connection()
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/db/backends/base/base.py", line 130, in ensure_connection
self.connect()
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/db/utils.py", line 97, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/db/backends/base/base.py", line 130, in ensure_connection
self.connect()
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/db/backends/base/base.py", line 119, in connect
self.connection = self.get_new_connection(conn_params)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/db/backends/mysql/base.py", line 276, in get_new_connection
conn = Database.connect(**conn_params)
File "/usr/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 187, in __init__
super(Connection, self).__init__(*args, **kwargs2)
django.db.utils.OperationalError: (1049, "Unknown database '/home/workspace/django/mysite/db.mysql'")
and my database setting in settings.py is
'ENGINE': 'django.db.backends.mysql',
'NAME':os.path.join(BASE_DIR, 'jangoo'), #<--idk what to type here
'USER':'root', #I created a database
'PASSWORD':'1234', # named jangoo in mysql
'HOST':'127.0.0.1'
name in db settings most be name of db
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test',
'USER': 'root',
'PASSWORD': '123567',
'HOST': 'localhost', # Or an IP Address that your DB is hosted on
'PORT': '3306',
'OPTIONS': {"init_command": "SET storage_engine=MyISAM"},
}
}

Categories

Resources