如何在一个实例中为多个CHARACTER SET数据库配置my.cnf

时间:2017-07-18 16:55:28

标签: mysql character-encoding collation

在一个实例中,我有两个数据库:     第一个数据库 - >的 MY_DB     第二个数据库 - >的 sample_db

mysql> show global variables like 'char%';

+--------------------------+-------------------------------------------+
| Variable_name            | Value                                     |
+--------------------------+-------------------------------------------+
| character_set_client     | latin1                                    |
| character_set_connection | latin1                                    |
| character_set_database   | latin1                                    |
| character_set_filesystem | binary                                    |
| character_set_results    | latin1                                    |
| character_set_server     | latin1                                    |
| character_set_system     | utf8                                      |
| character_sets_dir       | /rdsdbbin/mysql-5.6.27.R1/share/charsets/ |
+--------------------------+-------------------------------------------+

mysql> show global variables like 'coll%';
+----------------------+-------------------+
| Variable_name        | Value             |
+----------------------+-------------------+
| collation_connection | latin1_swedish_ci |
| collation_database   | latin1_swedish_ci |
| collation_server     | latin1_swedish_ci |
+----------------------+-------------------+

mysql> use my_db;

SHOW CREATE DATABASE my_db ;
+-------------------+-------------------------------------------------------------------------------------------+
| Database          | Create Database                                                                       
+-------------------+-------------------------------------------------------------------------------------------+
| plum_production_1 | CREATE DATABASE `my_db` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci */    |
+-------------------+-------------------------------------------------------------------------------------------+
第一个数据库;的 MY_DB

mysql> show variables like '%coll%';
+----------------------+-------------------+
| Variable_name        | Value             |
+----------------------+-------------------+
| collation_connection | utf8_general_ci   |
| collation_database   | utf8_unicode_ci   |
| collation_server     | utf8_unicode_ci   |
+----------------------+-------------------+

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.6.27.R1/share/charsets/ |
+--------------------------+-------------------------------------------+

第二个数据库:

mysql> use sample_db;

mysql> show create database sample_db;
+-----------------+----------------------------------------------------------------------------+
| Database        | Create Database                                                            
|
+-----------------+----------------------------------------------------------------------------+
| plum_production | CREATE DATABASE `plum_production` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+-----------------+----------------------------------------------------------------------------+

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

mysql> show variables like '%coll%';
+----------------------+-------------------+
| Variable_name        | Value             |
+----------------------+-------------------+
| collation_connection | utf8_general_ci   |
| collation_database   | latin1_swedish_ci |
| collation_server     | latin1_swedish_ci |
+----------------------+-------------------+

当我们需要多个排序类型的db时,如何配置my.cnf,即 我需要

my_db - character_set utf8 collat​​e utf8_unicode_ci。

Sample_db - character_set latin1 collat​​e latin1_swedish_ci。

使用上面的配置我面临一些问题,比如表格在尝试将记录插入多个表时被锁定,除了第一个insert语句表。其他查询太慢了。暂时我改了 my_db - < em> character_set latin1 collat​​e latin1_swedish_ci ,现在工作正常。 但我的要求不是这个。

my_db 表&amp; columns:字符集-Utf8,collat​​ion-utf8_unicode_ci - &gt;为了完成这项工作,我改变了

数据库: - 更改数据库my_db characterset utf8 collat​​e utf8_unicode_ci,

表: - 对于所有表 - ALTER TABLE table_names CHARACTER SET utf8 COLLATE utf8_unicode_ci;

要转换所有列: - ALTER TABLE table_names CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci; 我是以正确的方式吗?除了my.cnf之外还有什么可以改变的吗?

sample_db :Charcter set-latin1,collat​​ion-latin_swedish_ci。

我们正在使用awsrds my.cnf看起来像这样: -

[mysqld]
character_set_client: utf8
character_set_database: utf8
character_set_results: utf8
character_set_connection: utf8
character_set_server: utf8
collation_connection: utf8_unicode_ci
collation_server: utf8_unicode_ci

以及如何配置本地实例my.cnf(不在aws中)?例如:

[client]
[mysql]
[mysqld]

连接时如何设置名称utf8_mb4?是否需要在连接到该数据库时始终提及?我问了许多问题,因为我很困惑,害怕数据丢失......提前谢谢。

1 个答案:

答案 0 :(得分:0)

my.cnf主要是可以覆盖的默认值。如果你有混合物,不要担心;专注于其他设置。

<强>客户端

你有什么客户? (我所看到的只是mysql命令行工具。)可能客户端应该总是utf8mb4(mysql字符集,相当于UTF-8的外部世界)。

连接时,使用连接参数建立CHARACTER SET utf8mb4,可能是SET NAMES utf8m4;

列中的数据

每列可以有CHARACTER SETCOLLATION。如果未指定,则默认来自CREATE TABLE。如果未指定,则默认来自CREATE DATABASE。等

因此,请确保每列都是他们需要的方式。使用SHOW CREATE TABLE进行验证。

来自/来自列的客户

MySQL在客户端和服务器之间转换数据。因此,可以让客户端使用utf8mb4,但INSERTing / SELECTing使用声明为latin1的列。 (有些组合不会起作用。)

推论:如果一个数据库是latin1而另一个数据库是utf8,则没有问题。

<强>垃圾

参见&#34;最佳实践&#34;在http://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored。如果你得到胡言乱语,请看这个链接以便进一步调试/治愈。