View and modify the mysql encoding method

Source: Internet
Author: User

Check that the default mysql encoding method 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 twice), and the database encoding command is:> show variables like 'character % '; + metrics + | Variable_name | Value | + metrics + | character_set_client | latin1 | character_set_connection | latin1 | character_set_database | latin1 | character_set_files Ystem | binary | metrics | latin1 | character_set_server | latin1 | character_set_system | utf8 | character_sets_dir |/usr/share/mysql/charsets/| + metrics + from the preceding information we can see that the database code is latin1, you need to change it to gbk or utf8. character_set_client is the client encoding method, character_set_connection is the encoding used to establish a connection, character_set_database database encoding, character_set_results result set encoding, characte R_set_server Database Server encoding. As long as the above four encoding methods are the same, there will be no garbled issues. Another command for viewing database encoding:> show variables like 'collation % '; 2. in linux, to modify the default encoding of the MySQL database, stop MySQL running/etc/init. d/mysql start (stop) is the master configuration file for MySQL to start and stop the server. cnf. The common directory is/etc/mysql var/lib/mysql/, where the database table folder is placed, here, mysql is equivalent to the date folder of mysql in windows. When we need to modify the default encoding of 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. find the client in the cnf file and configure [client]. Add default-character-set = utf8. The default character set is utf8. Add default-cha in [mysqld ]. Racter-set = utf8 the default character SET is utf8 init_connect = 'set NAMES utf8' (set utf8 encoding when connecting to the mysql database to run mysql database utf8, restart mysql and query the database encoding again to find the encoding method changed:> show variables like 'character % '; + -------------------------- + bytes + | Variable_name | Value | + -------------------------- + bytes + | character_set_client | utf8 | character_set_connection | utf8 | character_set_d Atabase | utf8 | bytes | binary | bytes | utf8 | character_set_server | utf8 | character_set_system | utf8 | character_sets_dir |/usr/share/mysql/charsets/| + bytes + ---------------------------- + this method is also valid for standard mysql versions, for/etc/my. cnf file, 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 installation, you can directly use Mysql Server Instance Config Wizard settings 4. When the MySQL database Server has data that is not suitable for deletion and reinstallation, you can specify the encoding method of the database individually. 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 two ways to specify 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 external access, 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 see any space, otherwise error) 6. Run the Script: Specify the encoding format set names gbk (note, not a UTF-8) before you can modify the execution: after execution: we can see from the execution before and after the command that set names gbk can only modify the encoding methods of character_set_client, character_set_connection, and character_set_results. This modification is window-level and 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.