Djangoのsyncdbで_mysql_exceptions.OperationalError: (1049, "Unknown database 'hoge'")のエラーがでた場合の対処方法

Django1.2.4でプロジェクトを新たに作成して、MySQLを指定して、
初めてsyncdbするとき、次のようなエラーが出る場合があります。

_mysql_exceptions.OperationalError: (1049, "Unknown database 'hoge'")

"Unknown database 'hoge'

これはMySQLhogeというデータベースがないというエラーになります。
MySQLサーバにDjangoから接続に行って、存在しないというエラーになります。
(もしくは権限が許可されていないか)

解決方法

この解決方法について書いておきます。



MySQLhogeというデータベースがない場合は、MySQLにデータベースを
あらかじめ作成しておく必要があります。


このような感じに

mysql -u root -p -e "CREATE DATABASE hoge CHARACTER SET utf8;"

このあと、このデータベースにDjangoのsettings.pyに指定しているユーザが
アクセスできるように権限を許可しておきます。

再度syncdb

設定が終わったら、再度syncdbをやってみましょう。

% python manage.py syncdb
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table django_admin_log

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (Leave blank to use 'hogehoge'):
E-mail address: info@hogehoge.com
Password:
Password (again):
Superuser created successfully.
Installing index for auth.Permission model
Installing index for auth.Group_permissions model
Installing index for auth.User_user_permissions model
Installing index for auth.User_groups model
Installing index for auth.Message model
Installing index for admin.LogEntry model
No fixtures found.


以上で解決です。