简体   繁体   中英

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

I attempted to connect to a mysql db using mysql connector python. 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?

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.

I just tried adding the charset argument to my connection like so:

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

If your characters actually use the superset utf8mb4 then this may cause problems, but if not, then this should work!

Use charset="utf8" while creating the connection object .

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

It works for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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