英文:
How to switch from sqlite to mysql database in django?
问题
我在Cpanel上部署了我的Django项目,现在想要更改默认数据库:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'your_database_name',
'USER': 'your_database_user',
'PASSWORD': 'your_database_password',
'HOST': 'localhost',
'PORT': '3306',
}
}
但是当我尝试迁移时出现以下错误:
TypeError: sequence item 1: expected a bytes-like object, str found
在出现这个错误后,数据库创建了两个表,分别是django_content_type和django_migrations。但是当我使用Django的默认数据库时,一切正常迁移。
英文:
I deployed my Django project on Cpanel, now when I want to change the default database:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
to the MySQL database (the ENGINE of the database is 'ENGINE': 'mysql.connector.django',
because i'm not be able to install mysqlclient), it didn't migrate and gave me this error:
TypeError: sequence item 1: expected a bytes-like object, str found
After this error, the database created 2 tables named django_content_type and django_migrations.
but when I'm using the default database of Django it works and migrates everything
答案1
得分: 0
我通过以下步骤解决了我的问题:
1_ 如果您想在数据库中使用MySQL连接器,请安装mysql-connector-python
DATABASES = {
'default': {
'ENGINE': 'mysql.connector.django',
}
}
2_ 在setting.py的INSTALLED_APPS中添加mysql.connector.django
INSTALLED_APPS = [
'mysql.connector.django'
]
英文:
I solved my problem by going through these steps:
1_ install the mysql-connector-python if you want to use mysql connector in your database
DATABASES = {
'default': {
'ENGINE': 'mysql.connector.django',
}
}
2_ Put mysql.connector.django in your INSTALLED_APPS in setting.py
INSTALLED_APPS = [
'mysql.connector.django'
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论