Home >Backend Development >Python Tutorial >How to change the default database to mysql in Django (detailed process)
The content of this article is about how to change the default database to mysql in Django (detailed process). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Django uses the sqlite3 database by default. Today I studied how to replace it with the common mysql database.
Since the project uses python3, and MySQLdb does not have a version that supports python3, if you use the python3.x version, an error will be reported when pip install MySQLdb.
Later I found through Google that pymysql can be used instead of MySQLdb
1 Add the following code to the __init__.py file in the project root directory:
import pymysql pymysql.install_as_MySQLdb()
2 Use mysqlclient instead of MySQLdb , the installation method is:
pip install mysqlclient
3 Change the database configuration in the project setting.py to:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'test', 'USER': 'username', 'PASSWORD': 'passwd', 'HOST': 'localhost', 'PORT': '3306' } }
4 Finally, through the python manage.py migrate command, Django will automatically create the corresponding configuration in the database Table
Operations to perform: Apply all migrations: admin, auth, contenttypes, polls, sessions Running migrations: Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying admin.0001_initial... OK Applying admin.0002_logentry_remove_auto_add... OK Applying admin.0003_logentry_add_action_flag_choices... OK Applying contenttypes.0002_remove_content_type_name... OK Applying auth.0002_alter_permission_name_max_length... OK Applying auth.0003_alter_user_email_max_length... OK Applying auth.0004_alter_user_username_opts... OK Applying auth.0005_alter_user_last_login_null... OK Applying auth.0006_require_contenttypes_0002... OK Applying auth.0007_alter_validators_add_error_messages... OK Applying auth.0008_alter_user_username_max_length... OK Applying auth.0009_alter_user_last_name_max_length... OK Applying polls.0001_initial... OK Applying sessions.0001_initial... OK
5 When creating the admin user, I encountered the following error
python manage.py createsuperuser Superuser creation skipped due to not running in a TTY. You can run `manage.py createsuperuser` in your project to create one manually.
I checked later and found that it was because I used git to execute the command and switched to the command line that comes with Windows. This problem can be solved!
The above is the detailed content of How to change the default database to mysql in Django (detailed process). For more information, please follow other related articles on the PHP Chinese website!