无法在AppEngine Python上的CloudSQL中使用utf8mb4字符集 [英] Unable to use utf8mb4 character set with CloudSQL on AppEngine Python

查看:80
本文介绍了无法在AppEngine Python上的CloudSQL中使用utf8mb4字符集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了一个CloudSQL实例,该实例试图与AppEngine上的Django应用一起使用.我已经确认通过CloudSQL控制台将服务器设置为对数据库使用utf8mb4字符集:

I have set up a CloudSQL instance that I am attempting to use with my Django app on AppEngine. I've confirmed that the server is set to use utf8mb4 character set via the CloudSQL console for my database:

utf8mb4 utf8mb4_unicode_ci

如果直接与mysql cli连接,则可以成功插入和读取表情符号.但是,如果我通过Django管理员插入相同的表情符号字符,则会将其插入为????".

If I connect directly with the mysql cli, I can successfully insert and read emojis. However, if I insert the same emoji characters through the Django admin it's just inserted as "? ? ? ?".

我试图确保MySQLdb-python客户端将utf8mb4用于:

I attempted to ensure the MySQLdb-python client is using utf8mb4 with:

'ENGINE': 'django.db.backends.mysql',
...
'OPTIONS': {
    'charset': "utf8mb4",
}

但这会导致我在AppEngine上收到以下错误:

But this causes me to receive the following error on AppEngine:

(2019, "Can't initialize character set utf8mb4 (path: /usr/local/mysql/share/charsets/)")

我的app.yaml使用的是最新的" MySQLdb库:

My app.yaml is using the "latest" MySQLdb library:

libraries:
- name: MySQLdb
  version: "latest"

推荐答案

我刚刚与Google聊天,并为我们的实例工作了一切!

I just chatted with google and got everything working for our instance!

让utf8mb4在Django中工作的标准方法是在settings.py中将其指定为DATABASES ['default'] ['OPTIONS'],如下所示:

The standard way to get utf8mb4 working in Django is to specify it as DATABASES['default']['OPTIONS'] in settings.py, like this:

'OPTIONS': {'charset': 'utf8mb4'},

这会导致在MySQLdb 1.2.4b4/1.2.4/1.2.5上的App Engine中导致OperationalError;显然这意味着google正在针对其进行编译的MySQL C客户端缺少utf8mb4字符集.

This causes an OperationalError in App Engine, on MySQLdb 1.2.4b4 / 1.2.4 / 1.2.5; which apparently means that the MySQL C client google is compiling against is missing the utf8mb4 character set.

删除此OPTIONS设置.

Remove this OPTIONS setting.

解决方法是手动调用SET NAMES;编辑lib/django/db/backends/mysql/base.py,并将conn.query("SET NAMES utf8mb4")行添加到DatabaseWrapper.get_new_connection中,因此如下所示:

The workaround is to manually call SET NAMES; edit lib/django/db/backends/mysql/base.py and add a conn.query("SET NAMES utf8mb4") line into DatabaseWrapper.get_new_connection, so it looks like this:

def get_new_connection(self, conn_params):
    conn = Database.connect(**conn_params)
    conn.encoders[SafeText] = conn.encoders[six.text_type]
    conn.encoders[SafeBytes] = conn.encoders[bytes]
    conn.query("SET NAMES utf8mb4")
    return conn

确保在后端也启用了utf8mb4. App Engine Django教程中的迁移命令会生成为utf8配置的Cloud SQL实例.我需要运行以下命令来在两个表上启用utf8mb4:

Make sure that you also have utf8mb4 enabled on the backend. The migration commands in the App Engine Django tutorial result in a Cloud SQL instance configured for utf8. I needed to run these commands to enable utf8mb4 on the two tables:

ALTER TABLE polls_question CONVERT TO CHARACTER SET utf8mb4;
ALTER TABLE polls_choice CONVERT TO CHARACTER SET utf8mb4;

这篇关于无法在AppEngine Python上的CloudSQL中使用utf8mb4字符集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆