Garbled MySql in Redhat5

Source: Internet
Author: User

I installed redhat on the Virtual Machine and installed the mysql database. I accessed the database locally using java and directly accessed it using jdbc. Later, I found that there was a garbled problem when the database Stored Chinese characters, and then I found information on the Internet to solve the problem. Now the problem has been solved. I will summarize several garbled issues. Change the default character set encoding of the database: Change the mysql configuration file in/etc/my. cnf. This file may be available after mysql is installed. If not, go back to the root directory and query the cnf file: Java code find/-iname '*. cnf '-print copy small. cnf, my-medium.cnf, my-huge.cnf, one of the my-innodb-heavy-4G.cnf to/etc, renamed my. cnf: the Java code cp/usr/share/mysql/my-medium.cnf/etc/my. the cnf copy path is modified based on the query results. Modify my. cnfJava code vi/etc/my. cnf adds the Java code default-character-set = gbk under [client] And the Java code default-character-set = gbk under [mysqld], and then restarts mysql. Log on to mysql to check whether the query takes effect: Java code mysql> show variables like 'collation _ % '; mysql> show variables like 'character _ set _ % '; note if you are setting UTF-8 please set utf8 instead of UTF-8, otherwise your mysql cannot be started upon restart. Modify the character set of the database and then modify the character set of the database: Java code mysql> use mydb mysql> alter database mydb character set gbk; we recommend that you specify the character set when creating a database. The Java code is mysql> create database mydb character set gbk. In addition to using commands to modify the character set of data, you can also modify the configuration file/var/lib/mysql/mydb/db. optJava code default-character-set = latin1 default-collation = latin1_swedish_ci for Java code default-character-set = gbk default-collation = gbk_general_ci note when restarting MySQL table creation: in addition to modifying the character set of mysql and its database, note that when creating a table, you may use the exported S QL statements. These statements may contain the character set for TABLE creation. You must modify or delete these statements: SQL code CREATE TABLE 'newtable' ('id' int (11) NOT NULL, 'name' varchar (50) character set latin1 COLLATE latin1_swedish_ci null default null, 'age' int (100) null default null, primary key ('id ')) ENGINE = MyISAM default character set = gbk COLLATE = gbk_chinese_ci CHECKSUM = 0 ROW_FORMAT = DYNAMIC DELAY_KEY_WRITE = 0; connection: if the database is correctly configured, so when you use a management tool such as Navicat for MySQL, there will be no problem, There will inevitably be problems in the sequence. In addition to database considerations, you must specify the character set to connect to the database. Java code jdbc: mysql: // 192.168.154.128: 3306/t2? UseUnicode = true & characterEncoding = gbk program: However, even here it may still be garbled, but it is not so serious, so the handler is converted in the code. Java code String newStr = new String (s. getBytes ("ISO-8859-1"), "GBK"); someone may say that garbled code is garbled, not serious? In fact, Garbled text can be divided into two situations: storage or other aspects of chaos, and storage is correct but the display is problematic. For example, if you set it correctly, there is no problem with the management tool, but there is a problem with the program reading. If the Garbled text you see does not know what the display is, it is serious. But if you see ???? In my opinion, the lack of conversion is not a big problem. Of course, this is my personal opinion. Please do not give it a point. Author: cuisuqiang

Related Article

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.