如何在Django中从sqlite切换到mysql数据库?

huangapple go评论55阅读模式
英文:

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'

]

huangapple
  • 本文由 发表于 2023年7月18日 02:41:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76707279.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定