首页 > 代码库 > Encoding of mysql for mac

Encoding of mysql for mac

Chinese characters are not displayed properly in my terminal.

Solution: set all encoding in utf8

(in mysql using utf8 not utf-8)

Step1: check your encoding of mysql

mysql> show variables like "character%";
+--------------------------+-----------------------+
| 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-5.7.16-osx10.11-x86_64/share/charsets/ |
+--------------------------+---------------------------+
8 rows in set (0.00 sec)

Maybe your terminal would display diff from mine. That‘s OK and you have to set default encoding by modify the "my.cnf" file of mysql.

Step2 : Modify default encoding of mysql

http://www.cnblogs.com/jinjidedale/p/6183451.html 

Step3: Change the database & table encoding

mysql> alter database testDB character set utf8;
Query OK, 1 row affected (0.01 sec)

mysql> alter table testTB convert to character set utf8;
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0

Check your database & table encoding now and remember to restart your mysql if necessary. 

 mysql> show create database testDB;

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

 | Database | Create Database   |

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

 | testDB   | CREATE DATABASE `testDB` /*!40100 DEFAULT CHARACTER SET utf8 */ |

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

 1 row in set (0.00 sec)

mysql> show create table testTB;
+--------+----------------+
| Table  | Create Table                                                                                              
+--------+----------------+
| testTB | CREATE TABLE `testTB` (
  `testNum` varchar(16) NOT NULL,
  `testName` varchar(32) NOT NULL,
  `testDate` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+--------+----------------+
1 row in set (0.01 sec)

Step4: change import file encoding

If you insert data by loading data infile, you have to makes sure the file has the same encdoing of utf8. The files I get from windows cannot directly use for importing. Mostly the files have Chinese character are encoding with gbk. You can open the file by Safari and click "view-encoding" to change into gbk. [cmd+A][cmd+c] copy the file into a new file opened by "text editor" and set preference of storing by encoding utf-8 in "open&store". Then the new file is totally can view in utf-8 with Chinese characters.

Finally you can import data form "test.txt" instead of insert data one by one.

mysql> load data local infile "/Users/.../Desktop/test.txt" into table testTB (testNum,testName,date);
Query OK, 4 rows affected, 1 warning (0.00 sec)
Records: 4  Deleted: 0  Skipped: 0  Warnings: 1

good luck!

 

Encoding of mysql for mac