使用pythonDjango和Mysql。由于Binlog的原因,数据不会被插入到MySQL中

2024-09-30 22:27:31 发布

您现在位置:Python中文网/ 问答频道 /正文

当我使用django设计一个网站和delopy时,我发现如果我使用MySQL,Binlog是激活的(格式:语句)。在

此部署有我的设置:

  • 基于Django的网站
  • 乌兹基
  • 宁克斯
  • MySQL数据库

第一步,我需要将我的模型迁移到数据库,所以我输入如下:

python manage.py migrate

像这样追溯:

^{pr2}$

在互联网上搜索之后,我知道解决这个问题的一个方法是更改Binlog格式,这对我来说非常合适。在

但如果我不想更改Binlog配置,我还是想知道如何修复它。我认为Django可以支持Binlog的这种格式。在


Tags: djangopy模型数据库manage网站部署格式
3条回答

经过昨天的研究,我找到了一个解决这个问题的方法。在

  • First solution: Change the Binlog format.
    As Hariprakash said, we can change the Binlog format and fix this problem. In most of cases, it is the best way to fix it.
    You can change the setting in two ways:
    1. Hariprakash ways, but it only influences the settings in session. When you shut down the session, the seetings will be lost.
    2. You can revise Mysql settings file my.cnf
      In my.cnf, you need to add log_bin=mysql-bin and set the bin_format=ROW or binformat=MIXED which based on your choice. And then, you need to restart Mysql service. That will be effective forever.

  • Second solution: Change the default storage engine for Mysql
    Before you revise default storage engine, you need to check how many kinds of your supporting engine.
    show engines;
    And you can change the default storage engine.
    • In settings file my.cnf, you should insert default-storage-engine=INNODB after [mysqld], and remember you must insert server-id=1, this number is used for recognize your host. Do not make other host have same number.
    • Also you can use the Mysql version for 5.5.4 or older, which uses MyISAM as default storage engine according to Django document.

虽然有两种方法可以解决这个问题,但我认为没有最好的解决办法。如果有人有一个更好的答案,不修改my.cnf,请告诉我。在

我遇到了类似的问题,并通过将隔离级别更改为“可重复读取”来解决它。你真的只需要它来应用迁移,然后你就可以(?)把它取下来。在设置.py我将数据库设置更改为:

DATABASES = {
    'default': {                                                                                         
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'db_name',                                                                                 
        'USER': 'db_user',
        'PASSWORD': 'db_password',
        'HOST': 'db_host',
        'PORT': 'db_port',
        'CONN_MAX_AGE': db_conn_max_age,
        'OPTIONS': {                                                                           
            'isolation_level': "repeatable read",                                                        
        },                                                                                     
    }                                                                                            
}

然后运行python manage migrate,然后删除OPTIONS项。注意these risks。在

是否可以尝试将Binlog格式设置为

要设置格式,请使用这个

mysql> SET GLOBAL binlog_format = 'ROW';

之后使用下面的查询进行确认

^{pr2}$

相关问题 更多 >