I am facing a problem using python, mysql.connector (8.1.0) and trying to open 2 connections on 2 different servers:
If I run :
from mysql.connector import MySQLConnection
if __name__ in '__main__':
# A
try:
c1 = MySQLConnection(
host='host1',
user='*',
password='*',
database='A'
)
c1.close()
except Exception as e:
print(e)
finally:
print('c1')
# B
try:
c2 = MySQLConnection(
host='host2',
user='*',
password='*',
database='B'
)
c2.close()
except Exception as e:
print(e)
finally:
print('c2')
I got exception : Character set 'utf8' unsupported for c2
If I run only part B, it's Ok. It's as if something was set globally after the first connection.
any idea?
EDIT: Got it ! CharacterSet.desc is a class variable set at begining.
from mysql.connector import MySQLConnection as MySQLConnection
from mysql.connector.constants import CharacterSet
if __name__ in '__main__':
desc = CharacterSet.desc.copy()
try:
c1 = MySQLConnection(
host='host1',
user='*',
password='*',
database='A'
)
c1.close()
except Exception as e:
print(e)
finally:
print('c1')
CharacterSet.desc = desc
try:
c2 = MySQLConnection(
host='host2',
user='*',
password='*',
database='B'
)
c2.close()
except Exception as e:
print(e)
finally:
print('c2')
It works now
If both connections (c1 and c2) work fine when run separately but produce the "Character set 'utf8' unsupported for c2" error when run together, it's likely due to a compatibility issue with the character set configurations between the two connections. you may try below steps:
Specify Character Set for Both Connections: Explicitly specify the character set for both connections to ensure they use the same character set. For example, you can use 'utf8mb4' for both connections
Use Separate Connection Instances: Ensure that you are not inadvertently sharing a connection instance between the two connections (c1 and c2). Each connection should have its own separate instance.