今日导入数据到阿里云上的mysql 5.5,遇到乱码问题,参考了网上不少相关帖子,顺便记录下来,供参考:
# mysql --version
mysql Ver 14.14 Distrib 5.5.40, for linux2.6 (x86_64) using readline 5.1
1)先查看编码信息:
mysql> show variables like ‘character%‘
-> ;
+--------------------------+----------------------------------+
| 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 | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)
2. 修改配置文件中的参数,修改mysql的/ect/my.cnf文件中的字符集键值
在[mysqld]字段里加入
character
-
set
-server=utf8,如下:
[mysqld]
port
= 3306
socket
= /var/lib/mysql/mysql.sock
character
-
set
-server=utf8
【网上很多说,在mysql压缩目录下,有一堆的参数配置文件,比如:my-innodb-he
avy-4G.ini,复制一下这个文件,自己改下名字,比如叫:my.ini
或暂停mysql服务,在Mysql安装目录下找到my.ini文件,这个我真没找到(:)
mysql> SHOW VARIABLES LIKE ‘character%‘;
+--------------------------+----------------------------------+
| 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/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)
3. 导入.sql文件数据 【在确保导入前后,mysql的编码设置相同的前提下,导入数据】
mysql –u root -p
source /home/wwwftp/comm.sql 【直接导入,不需要 use xxxx】
show databases; 【检查数据库导入成功】
参考
阿里云服务器实战(二): Linux MySql5.6数据库乱码问题
http://blog.csdn.net/johnny901114/article/details/28463005