Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 214
sameagol Avatar asked Mar 14 '19 00:03

sameagol


People also ask

How do I change utf8mb4 to utf8?

To solve the problem open the exported SQL file, search and replace the utf8mb4 with utf8 , after that search and replace the utf8mb4_unicode_520_ci with utf8_general_ci . Save the file and import it into your database. After that, change the wp-config. php charset option to utf8 , and the magic starts.


2 Answers

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!

like image 125
sameagol Avatar answered Nov 03 '22 00:11

sameagol


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.

like image 26
Rahul_Patidar Avatar answered Nov 03 '22 01:11

Rahul_Patidar