How to modify MySQL encoding in Linux affects project garbled characters: bitsCN.com
Modifying MySQL encoding in Linux affects project garbled characters
After logging on to mysql by default, you can view the system VARIABLES and their values using the show variables statement.
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/|
------------------------------------------------------
(The following is the settings under the CentOS-6.2 (different versions may be somewhat different, such as the file location. But the set content should be the same)
1. find the mysql configuration file and copy it to The etc directory. The first step is very important.
Copy/usr/share/doc/mysql-server-5.1.52/my-large.cnf to/etc/my. cnf
Command: cp/usr/share/doc/mysql-server-5.1.52/my-large.cnf/etc/my. cnf
2. open my. cnf to modify the encoding.
Add default-character-set = utf8 under [client]
Add default-character-set = utf8 under [mysqld]
Add init_connect = 'set NAMES utf8' (SET utf8 encoding when connecting to the mysql database to run the mysql database utf8)
3. restart mysql
Service mysqld restart
Enter show variables like '% character %' again ';
------------------------------------------------------
| 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/|
------------------------------------------------------
Even if the above changes are made, if the database directly creates a table and then stores the table in Chinese, the question mark is obtained. The solution is to specify the default character set utf8 when creating a database, for example:
Create database test default character set utf8;
BitsCN.com