创建数据库
mysqladmin -h '192.168.2.233' -u root -p create namc;
查看新创建的数据库编码
mysql> SHOW VARIABLES LIKE 'character%'
-> ;
+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+
编码设置
set character_set_client=utf8;
set character_set_connection=utf8;
set character_set_database=utf8;
set character_set_filesystem=binary;
set character_set_results=utf8;
set character_set_server=utf8;
以上是临时设置编码,如果数据库重新启动,编码会还原。
永久设置mysql的配置文件中的编码。
vi /etc/my.cnf
在[client]字段里加入default-character-set=utf8,如下:
[client]
port = 3306
socket = /var/lib/mysql/mysql.sock
default-character-set=utf8
在[mysqld]字段里加入character-set-server=utf8,如下:
[mysqld]
port = 3306
socket = /var/lib/mysql/mysql.sock
character-set-server=utf8
或者
default-character-set=utf8
在[mysql]字段里加入default-character-set=utf8,如下:
[mysql]
no-auto-rehash
default-character-set=utf8
修改完成后,service mysql restart重启mysql服务就生效。注意:[mysqld]字段与[mysql]字段是有区别的。
使用SHOW VARIABLES LIKE ‘character%’;查看,发现数据库编码全已改成utf8。
+--------------------------+----------------------------+
| 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/ |
如果上面的都修改了还乱码,那剩下问题就一定在connection连接层上。解决方法是在发送查询前执行一下下面这句(直接写在SQL文件的最前面):
SET NAMES ‘utf8′;
它相当于下面的三句指令:
SET character_set_client = utf8;
SET character_set_results = utf8;
SET character_set_connection = utf8;
修改ssh显示中文格式
vim /etc/sysconfig/i18n
LANG="en_US.UTF-8"
SYSFONT="latarcyrheb-sun16"
将以上内容修改为以下内容
LANG="zh_CN.UTF-8"
SYSFONT="zh_CN.UTF-8:zh_CN:zh"