Ubuntu下MySQL的配置,使完美支持中文

来自个人维基
2015年5月6日 (三) 16:08Hovercool讨论 | 贡献的版本

(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航搜索

安装完以后:

$:mysql -u root -p

查看字符编码初始值:

mysql> show 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 | /usr/share/mysql/charsets/ |

+--------------------------+----------------------------+

修改字符集,不然对UTF-8的支持会出问题(看这里):

$:cd /etc/mysql/
$:sudo gedit my.cnf

更改或加入:

[mysqld]下

default-character-set=utf8 
  • 如果启动不了,提示“mysqld: unknown variable 'default-character-set=utf8'”,则可换成:
character_set_server=utf8

[client]下

default-character-set=utf8

[server]下

default-character-set=utf8,init_connect='SET NAMES utf8;'

保存

重启SQL:

$:sudo /etc/init.d/mysql restart

查看字符编码初始值:

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 | /usr/share/mysql/charsets/ |

+--------------------------+----------------------------+

最后,创建DB时最好指明字符属性

$:create database database-name character set utf8