dimanche 18 mai 2014

Configurer mysql Amazon RDS pour stocker les caractères chinois - Stack Overflow


How to configure the Amazon RDS to store Chinese Characters in the right way? Currently the text all becomes '????'.


I already created new parameter group and set character set as utf8 and modify the instance. Below are the detail info:


mysql> show variables like '%char%';
+--------------------------+-------------------------------------------+
| Variable_name | Value |
+--------------------------+-------------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /rdsdbbin/mysql-5.5.27.R1/share/charsets/ |
+--------------------------+-------------------------------------------+


+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_unicode_ci |
+----------------------+-----------------+



After following @Peter Venderberghe's answer to configure RDS, I still can not let it store Chinese characters correctly.


Finally, I used mysql command line to connect to RDS (you can use mysql workbench or other client tool you like)



mysql -u USERNAME -pPASSWORD -h HOSTNAMEORIP DATABASENAME



note:


1). remember to add your current ip to security group inbound of RDS before connecting to RDS


2). you can get related information(security groups, USERNAME...etc) on RDS console besides PASSWORD. PASSWORD should be the one you created for the instance. HOSTNAMEORIP is "Endpoint"(without the port) on the console.


3). there is no space between -p and PASSWORD. it's exactly -pPASSWORD.


After connecting to mysql with the tool you are comfortable with, you need to give it some commands:


# set character set and collation for database
mysql> alter database DATABASENAME CHARACTER SET utf8 COLLATE utf8_unicode_ci;
--------
# set character set and collation for new records in a table
mysql> ALTER TABLE TABLENAME CHARACTER SET utf8 COLLATE utf8_unicode_ci;
# set character set and collation for existing records in a table
mysql> ALTER TABLE TABLENAME CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;

you may want to write a script to do this task if you have many tables. However, it's not in the scope of this answer.


After configuring as above, you should be able to rock with Chinese characters!




Make sure that you are using UTF-8 character set.


The easiest way to set the charsets permanently is to update your client my.cnf with the following:


[client]
default-character-set=utf8

Detailed information about the connection charset you can find here: http://dev.mysql.com/doc/refman/5.5/en/charset-connection.html


When using the MySQL API functions ( PHP client e.g. ) you can set the connection charset by sending the query


SET NAMES utf8

You can configure Amazon RDS to use Utf-8 character set by following this blog: http://matthew.mceachen.us/blog/howto-configure-an-amazon-rds-instance-to-use-utf-8-925.html


I hope this helps.



How to configure the Amazon RDS to store Chinese Characters in the right way? Currently the text all becomes '????'.


I already created new parameter group and set character set as utf8 and modify the instance. Below are the detail info:


mysql> show variables like '%char%';
+--------------------------+-------------------------------------------+
| Variable_name | Value |
+--------------------------+-------------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /rdsdbbin/mysql-5.5.27.R1/share/charsets/ |
+--------------------------+-------------------------------------------+


+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_unicode_ci |
+----------------------+-----------------+


After following @Peter Venderberghe's answer to configure RDS, I still can not let it store Chinese characters correctly.


Finally, I used mysql command line to connect to RDS (you can use mysql workbench or other client tool you like)



mysql -u USERNAME -pPASSWORD -h HOSTNAMEORIP DATABASENAME



note:


1). remember to add your current ip to security group inbound of RDS before connecting to RDS


2). you can get related information(security groups, USERNAME...etc) on RDS console besides PASSWORD. PASSWORD should be the one you created for the instance. HOSTNAMEORIP is "Endpoint"(without the port) on the console.


3). there is no space between -p and PASSWORD. it's exactly -pPASSWORD.


After connecting to mysql with the tool you are comfortable with, you need to give it some commands:


# set character set and collation for database
mysql> alter database DATABASENAME CHARACTER SET utf8 COLLATE utf8_unicode_ci;
--------
# set character set and collation for new records in a table
mysql> ALTER TABLE TABLENAME CHARACTER SET utf8 COLLATE utf8_unicode_ci;
# set character set and collation for existing records in a table
mysql> ALTER TABLE TABLENAME CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;

you may want to write a script to do this task if you have many tables. However, it's not in the scope of this answer.


After configuring as above, you should be able to rock with Chinese characters!



Make sure that you are using UTF-8 character set.


The easiest way to set the charsets permanently is to update your client my.cnf with the following:


[client]
default-character-set=utf8

Detailed information about the connection charset you can find here: http://dev.mysql.com/doc/refman/5.5/en/charset-connection.html


When using the MySQL API functions ( PHP client e.g. ) you can set the connection charset by sending the query


SET NAMES utf8

You can configure Amazon RDS to use Utf-8 character set by following this blog: http://matthew.mceachen.us/blog/howto-configure-an-amazon-rds-instance-to-use-utf-8-925.html


I hope this helps.


0 commentaires:

Enregistrer un commentaire