Modify the MySQL configuration file To make the character set settings of the database and the server operating system consistent.
VI/etc/My. CNF settings (if this file is not found, create one)
[Mysqld]
Datadir =/var/lib/MySQL
Socket =/var/lib/MySQL. Sock
Default-character-set = utf8 (the key sentence is added so that the database is stored in utf8 by default)
After modification, restart the database.
Run the mysql-u root-p command again to enter the database system, and run the show variables like 'character _ SET _ % 'command to view the following content:
+ -------------------------- + ----------------------------------------------------------------------- +
| Variable_name | value |
+ -------------------------- + ----------------------------------------------------------------------- +
| Character_set_client | Latin1 |
| Character_set_connection | Latin1 |
| Character_set_database | Utf8 |
| Character_set_filesystem | binary |
| Character_set_results | Latin1 |
| Character_set_server | Utf8 |
| Character_set_system | Utf8 |
| Character_sets_dir |/usr/local/mysql-5.1.20-beta-linux-i686-glibc23/share/MySQL/charsets/|
+ -------------------------- + ----------------------------------------------------------------------- +
Utf8 has been used for key projects, but this is not enough, Make sure that the client uses the utf8 character set for operations. .
Use the following command to log on: MySQL -- default-character-set = utf8-u root-P
Run the show variables like 'character _ SET _ % 'command 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/local/mysql-5.1.20-beta-linux-i686-glibc23/share/MySQL/charsets/|
+ -------------------------- + ----------------------------------------------------------------------- +
In this way, the commands sent by the client are all in the utf8 format. For example, if you create a database and a table, utf8 encoding is used by default, and you do not need to specify it again.
It should be noted that if a database has been created before the encoding is modified, You need to delete the original database and recreate the database.