我收到一个错误尝试连接到远程mysql数据库
Windows 7客户端通过python 2.7 MySQLdb 1.2.5 sqlalchemy 1.0.9.这是最近将服务器的默认字符集更改为utf8mb4的结果.服务器正在运行MySQL 5.5.50.
我这样连接:
DB_ENGINE = sqlalchemy.create_engine("mysql+mysqldb://{user}:{pass}@{host}:{port}/{database}?charset=utf8mb4".format(**DB_SETTINGS))
Session = sqlalchemy.orm.sessionmaker(bind=DB_ENGINE)
错误是:
File "C:\Applications\Python27\lib\site-packages\sqlalchemy\engine\default.py", line 385, in connect
return self.dbapi.connect(*cargs, **cparams)
File "C:\Applications\Python27\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "C:\Applications\Python27\lib\site-packages\MySQLdb\connections.py", line 221, in __init__
self.set_character_set(charset)
File "C:\Applications\Python27\lib\site-packages\MySQLdb\connections.py", line 312, in set_character_set
super(Connection, self).set_character_set(charset)
sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (2019, "Can't initialize character set utf8mb4 (path: C:\\mysql\\\\share\\charsets\\)")
服务器的my.cnf包含以下内容:
init_connect = 'SET collation_connection = utf8mb4_unicode_ci'
init_connect = 'SET NAMES utf8mb4'
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
skip-character-set-client-handshake
我从Ubuntu客户端连接到数据库没有问题,所以我怀疑问题出在Windows客户端而不是服务器的配置上.
MySQL文档建议错误消息可能是由于客户端编译时没有多字节字符集支持:
http://dev.mysql.com/doc/refman/5.7/en/cannot-initialize-character-set.html
但是,由于这是Windows,我只是下载客户端而无法控制其编译标志.
我尝试过以各种方式安装MySQLdb:
>从dev.mysql.com下载并安装MySQL Connector / Python .msi
>从pypi下载并安装MySQLdb 1.2.5 .exe
>从Windows命令提示符运行“pip install mysql-python”
这些都导致MySQLdb库似乎无法处理utf8mb4字符集.
任何帮助将非常感激!
解决方法:
请考虑以下清单:
>你检查了MySQL configuration file (/etc/my.cnf)吗?它应该是:
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
您可以通过以下方式验证它们:
mysql> SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';
+--------------------------+--------------------+
| Variable_name | Value |
+--------------------------+--------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | utf8mb4 |
| character_set_system | utf8 |
| collation_connection | utf8mb4_unicode_ci |
| collation_database | utf8mb4_unicode_ci |
| collation_server | utf8mb4_unicode_ci |
+--------------------------+--------------------+
10 rows in set (0.00 sec)
– 感谢Mathias’s blog post
> Enfore强制在Python和MySQL之间使用UTF-8:
# Connect to mysql.
dbc = MySQLdb.connect(host='###', user='###', passwd='###', db='###', use_unicode=True)
# Create a cursor.
cursor = dbc.cursor()
# Enforce UTF-8 for the connection.
cursor.execute('SET NAMES utf8mb4')
cursor.execute("SET CHARACTER SET utf8mb4")
cursor.execute("SET character_set_connection=utf8mb4")
# Do database stuff.
# Commit data.
dbc.commit()
# Close cursor and connection.
cursor.close()
dbc.close()
>感谢Tomasz Nguyen’s answer on *
> Official tip from MySQL关于无法初始化字符集:
此错误可能具有以下任何原因:
>字符集是多字节字符集,您不支持客户端中的字符集.在这种情况下,您需要通过使用-DDEFAULT_CHARSET = charset_name或-DWITH_EXTRA_CHARSETS = charset_name选项运行CMake来重新编译客户端.见2.9.4节,“MySQL Source-Configuration Options”.
>所有标准MySQL二进制文件都使用-DWITH_EXTRA_CHARSETS = complex进行编译,它支持所有多字节字符集.见2.9.4节,“MySQL Source-Configuration Options”.
>字符集是一个未编译到mysqld中的简单字符集,字符集定义文件不在客户端期望找到它们的位置.
在这种情况下,您需要使用以下方法之一来解决问题:
>重新编译客户端并支持字符集.见2.9.4节,“MySQL Source-Configuration Options”.
>向客户端指定字符集定义文件所在的目录.对于许多客户端,您可以使用–character-sets-dir选项执行此操作.
>将字符定义文件复制到客户端期望的路径.