使用utf8mb4连接到mysql 5.7.9数据库的问题

时间:2015-12-05 17:25:26

标签: python mysql database character-encoding

我有一个我在年初写的python-flask播客应用程序。数据库配置为使用utf8mb4字符集(完整utf-8)(并具有默认排序规则utf8mb4_bin)

我在mac Yosemite上使用python-mysql-1.2.5 mysql 5.7.9,但我的python应用程序在尝试连接数据库时挂起。通过自制软件安装。安装到/usr/local/Cellar/mysql/5.7.9 /

经过大量调试后,我可以看到挂机正在进行中 /Users/xxx/repos/mypodweb/env/lib/python2.7/site-packages/MySQLdb/connections.py

在函数中:

import types, _mysql
...
def set_character_set(self, charset):
        """Set the connection character set to charset. The character
        set can only be changed in MySQL-4.1 and newer. If you try
        to change the character set from the current value in an
        older version, NotSupportedError will be raised."""
        if charset == "utf8mb4":
            py_charset = "utf8"
        else:
            py_charset = charset
        if self.character_set_name() != charset:
            try:
                super(Connection, self).set_character_set(charset) #<<<-- here
            except AttributeError:
                if self._server_version < (4, 1):
                    raise NotSupportedError("server is too old to set charset")
                self.query('SET NAMES %s' % charset)
                self.store_result()
        self.string_decoder.charset = py_charset
        self.unicode_literal.charset = py_charset

所以这引用了_mysql.set_character_set(charset),它只挂起而没有输出......

所以看起来_mysql.so是本机库绑定?我猜是这样......

有一点我注意到有一个不同版本的mysql库 - mysql 5.7.9只有/usr/local/Cellar/mysql/5.7.9/lib/libmysqlclient.20.dylib 但_mysql.so引用了libmysqlclient.18.dylib

该库似乎可以工作,因为我可以在挂起后看到mysql中的睡眠连接。所以有一个连接,然后库尝试在其上设置字符集。 (第2行)。

mysql> SHOW FULL PROCESSLIST;
+----+------+-----------------+--------------+---------+------+----------+-----------------------+
| Id | User | Host            | db           | Command | Time | State    | Info                  |
+----+------+-----------------+--------------+---------+------+----------+-----------------------+
|  3 | root | localhost       | NULL         | Query   |    0 | starting | SHOW FULL PROCESSLIST |
|  5 | root | localhost:52486 | mypodweb_dev | Sleep   |    4 |          | NULL                  |
+----+------+-----------------+--------------+---------+------+----------+-----------------------+
2 rows in set (0.00 sec)

因此,当我最初尝试迁移我的数据库时,我正在使用alembic来管理迁移,从而发生错误。我也尝试使用默认的alembic env.py文件来使用QueuePool(而不是NullPool) - 唯一的区别是我在mysql中获得了2个与mypodweb_dev的睡眠连接。

mysql> SHOW FULL PROCESSLIST;
+----+------+-----------------+--------------+---------+------+----------+-----------------------+
| Id | User | Host            | db           | Command | Time | State    | Info                  |
+----+------+-----------------+--------------+---------+------+----------+-----------------------+
|  4 | root | localhost:53359 | mypodweb_dev | Sleep   |  961 |          | NULL                  |
|  5 | root | localhost:53856 | mypodweb_dev | Sleep   |   19 |          | NULL                  |
|  6 | root | localhost       | NULL         | Query   |    0 | starting | SHOW FULL PROCESSLIST |
+----+------+-----------------+--------------+---------+------+----------+-----------------------+

当我谷歌解决这个问题时,我找不到任何东西。寻找一个修复或解决方法,只会让我去,因为它只是我的本地开发环境,我被困在mo的起点。

1 个答案:

答案 0 :(得分:0)

python-mysql-1.2.5仅支持最高5.5的MySQL