Chinese garbled characters in OpenStack Databases
The SQL connection of openstack must be configured with UTF-8 to avoid Chinese garbled characters.
SQL _connection = mysql: // nova: xxx @ qa-mysql1: 3306/nova? Charset = 'utf-8'
However, if UTF-8 is not set before, the database cannot be cleared at will. to update a field in the openstack database, you cannot directly connect to the database using UTF-8. Otherwise, Chinese characters are inserted into the database, however, garbled characters are displayed in openstack. Even worse, openstack fails to insert full-width Chinese characters.
So how to update the database in this case?
The answer is simple, that is, using the same database connection method as openstack. openstack uses sqlalchemy. I write a simple database connection as follows:
#! /Usr/bin/python
# Coding = UTF-8
From sqlalchemy import create_engine
From sqlalchemy import MetaData
From sqlalchemy. SQL import select
From sqlalchemy. SQL import update
Engine = create_engine ('mysql: // nova: password @ qa-mysql1: 3306/nova ', convert_unicode = True)
Metadata = MetaData ()
Meta = metadata
Meta. reflect (bind = engine)
Conn = engine. connect ()
Secgroup = meta. tables ['security _ groups ']
U = update (secgroup). where (secgroup. c. id = 60). values (name = 'Chinese ')
Result=conn.exe cute (u)
Note that convert_unicode = True must be added when create_engine is used. If this parameter is not added, the following decoding error is returned.
Query = query % db. literal (args)
File "/usr/share/pyshared/MySQLdb/connections. py", line 264, in literal
Return self. escape (o, self. encoders)
File "/usr/share/pyshared/MySQLdb/connections. py", line 202, in unicode_literal
Return db. literal (u. encode (unicode_literal.charset ))
UnicodeEncodeError: 'Latin-1 'codec can't encode characters in position 0-1: ordinal not in range (256)
Install and deploy Openstack on Ubuntu 12.10
Ubuntu 12.04 OpenStack Swift single-node deployment Manual
OpenStack cloud computing quick start tutorial
Deploying OpenStack for enterprises: what should be done and what should not be done
CentOS 6.5 x64bit quick OpenStack Installation
This article permanently updates the link address: