Newbieee Newbieee - 11 months ago 73
MySQL Question

Create a Django Database - Django Newbie

I'm new to django and am trying to figure out how to create a database... seems simple enough.

This code works on other people's local computer - we arent running it in production yet. But mine isnt working. A coworker indicated that I need to actually create a database. Prior to using mysql, I was using sqlite, which didnt require this.

When I run python runserver this is what I get:

XX-MacBook-Pro:xx xx$ python 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/", line 226, in wrapper
fn(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/core/management/commands/", line 116, in inner_run
File "/Library/Python/2.7/site-packages/django/core/management/", line 426, in check
File "/Library/Python/2.7/site-packages/django/core/checks/", line 75, in run_checks
new_errors = check(app_configs=app_configs)
File "/Library/Python/2.7/site-packages/django/core/checks/", line 28, in check_all_models
File "/Library/Python/2.7/site-packages/django/db/models/", line 1170, in check
File "/Library/Python/2.7/site-packages/django/db/models/", line 1247, in _check_fields
File "/Library/Python/2.7/site-packages/django/db/models/fields/", line 925, in check
errors = super(AutoField, self).check(**kwargs)
File "/Library/Python/2.7/site-packages/django/db/models/fields/", line 208, in check
File "/Library/Python/2.7/site-packages/django/db/models/fields/", 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/", line 18, in check_field
field_type = field.db_type(connection)
File "/Library/Python/2.7/site-packages/django/db/models/fields/", line 625, in db_type
return connection.data_types[self.get_internal_type()] % data
File "/Library/Python/2.7/site-packages/django/db/", line 36, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/Library/Python/2.7/site-packages/django/utils/", line 33, in __get__
res = instance.__dict__[] = self.func(instance)
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/", line 184, in data_types
if self.features.supports_microsecond_precision:
File "/Library/Python/2.7/site-packages/django/utils/", line 33, in __get__
res = instance.__dict__[] = self.func(instance)
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/", 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/", line 33, in __get__
res = instance.__dict__[] = self.func(instance)
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/", line 359, in mysql_version
with self.temporary_connection():
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/", line 17, in __enter__
File "/Library/Python/2.7/site-packages/django/db/backends/base/", line 564, in temporary_connection
cursor = self.cursor()
File "/Library/Python/2.7/site-packages/django/db/backends/base/", line 233, in cursor
cursor = self.make_cursor(self._cursor())
File "/Library/Python/2.7/site-packages/django/db/backends/base/", line 204, in _cursor
File "/Library/Python/2.7/site-packages/django/db/backends/base/", line 199, in ensure_connection
File "/Library/Python/2.7/site-packages/django/db/", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Library/Python/2.7/site-packages/django/db/backends/base/", line 199, in ensure_connection
File "/Library/Python/2.7/site-packages/django/db/backends/base/", line 171, in connect
self.connection = self.get_new_connection(conn_params)
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/", line 264, in get_new_connection
conn = Database.connect(**conn_params)
File "/Library/Python/2.7/site-packages/MySQLdb/", line 81, in Connect
return Connection(*args, **kwargs)
File "/Library/Python/2.7/site-packages/MySQLdb/", 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__)))


'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')

Newbie question I know. I'd very much appreciate some help on this, even a tutorial that describes what to do would be terrific. I've looked into the django tutorial, but either didn't look hard enough or don't know where to find it.

Answer Source

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.