How to view and modify character encoding in MySQL, and view character encoding in mysql
The default encoding of MySQL is Latin1, which does not support Chinese characters. You need to change the default encoding of the database to gbk or utf8 at noon.
1. You need to log in as the root user to view the database encoding method (the command for logging in as the root user is:> mysql-u root-p, enter the password of the root user two times later). The encoding command for viewing the database is:
>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/ |+--------------------------+----------------------------+
From the above information, we can see that the database is encoded as latin1 and needs to be modified to gbk or utf8;
Character_set_client is the client encoding method;
Character_set_connection indicates the encoding used to establish a connection;
Character_set_database database encoding;
Character_set_results result set encoding;
Character_set_server database server code;
As long as the above four encoding methods are the same, there will be no garbled problem.
Another command for viewing database encoding:
> Show variables like 'collation % ';
2. in linux, follow these steps to modify the default encoding of the MySQL database:
Ü stop MySQL running
/Etc/init. d/mysql start (stop) is used to start and stop the server.
Ü the MySQL main configuration file is my. cnf, and the General Directory is/etc/mysql
Var/lib/mysql/stores the database table folder. Here, mysql is equivalent to the date folder of mysql in windows.
Ü when we need to modify the default encoding of the MySQL database, we need to edit my. cnf file for encoding modification. in linux, modify the mysql configuration file my. cnf, default file location/etc/my. cnf File
Find the client configuration [client] and add it below
Default-character-set = utf8 default character set: utf8
Add in [mysqld]
Default-character-set = utf8 default character set: utf8
Init_connect = 'set NAMES utf8' (utf8 encoding is used to SET the connection to the mysql database to run utf8)
After modification, restart mysql and re-query the database encoding to find the encoding method changed:
>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/share/mysql/charsets/ |+--------------------------+----------------------------+
This method is also valid for the standard mysql version, for the/etc/my. cnf file, you need to copy from the mysql/support-files Folder cp my-large.cnf to/etc/my. cnf
3,In windows, you can delete the MySQL database and reinstall it. During the installation process, you can directly use Mysql Server Instance Config Wizard for settings.
4,When the MySQL database server has data that is not suitable for deletion and reinstallation, You can individually specify the encoding method of the database. The encoding method specified by MySQL is flexible and diverse. You can specify the table-level encoding, row-level encoding, or even field-level encoding.
The following example shows how to specify the encoding when creating a database:
1) create database ms_db character set utf8 COLLATE utf8_general_ci;
2) create database if not exists netctoss default character set utf8;
5,If you are using an external access method, you can determine the Request Encoding format in the connection, for example: jdbc: mysql: // localhost: 3306/mysql? UseUnicode = true & characterEncoding = UTF-8 (Note: do not contain any spaces; otherwise, an error occurs)
6. Execute the script:Specify the encoding format set names gbk (note, not the UTF-8) can be modified
Before execution:
After execution:
It can be seen from the execution before and after the command that set names gbk can only modify character_set_client, character_set_connection, character_set_results encoding method, and this modification is at the window level, only valid for this window, the modification in another window is invalid. We can also find that the encoding method at the underlying layer of the database has not changed, and the data is still maintained by UTF-8 encoding after being inserted.
The above is all the implementation methods for viewing and modifying character encoding in MySQL provided by xiaobian. I hope you can provide more support ~