简体   繁体   English

错误 1115 (42000): 未知字符集: 'utf8mb4' 连接

[英]ERROR 1115 (42000): Unknown character set: 'utf8mb4' on connection

I attempted to connect to a mysql db using mysql connector python.我尝试使用 mysql 连接器 python 连接到 mysql 数据库。 My table was latin encoded.我的表是拉丁文编码的。 I more or less tried this from the docs:我或多或少地从文档中尝试过这个:

from mysql.connector import (connection)

cnx = connection.MySQLConnection(user='scott', password='password',
                                 host='127.0.0.1',
                                 database='employees')
cnx.close()

But I got the error:但我得到了错误:

ERROR 1115 (42000): Unknown character set: 'utf8mb4'

Why is it finding utf8mb4 if my table is Latin encoded?如果我的表是拉丁文编码的,为什么会找到utf8mb4

After searching around for a while, I found this regarding connections:https://dev.mysql.com/doc/refman/8.0/en/charset-connection.html which got me thinking.搜索了一段时间后,我发现了关于连接的这个:https ://dev.mysql.com/doc/refman/8.0/en/charset-connection.html这让我思考。

I just tried adding the charset argument to my connection like so:我只是尝试将charset参数添加到我的连接中,如下所示:

cnx = connection.MySQLConnection(user='scott',
    password='password', 
    host='127.0.0.1', 
    charset='utf8', 
    database='employees')

https://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html https://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html

If your characters actually use the superset utf8mb4 then this may cause problems, but if not, then this should work!如果您的角色实际上使用超集utf8mb4那么这可能会导致问题,但如果没有,那么这应该可行!

Use charset="utf8" while creating the connection object .创建连接对象时使用charset="utf8"

myconn = mysql.connector.connect(host="localhost", user="root", passwd="user", database="user_db", charset="utf8" )

It works for me.这个对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM