Home » MySQL » Tomcat&mysql encoding format

Tomcat&mysql encoding format

encoding format in the commonly encountered problems in web development, first of all is that we use the server, then we connect to the database. Here is the web server Tomcat and the database mysql encoding format related to the modification of


Tomcat

by default, Tomcat uses the encoding: iso8859-1


modify the conf/server.xml file under Tomcat


finds the following code:


<Connector port= "8080"  protocol= "HTTP/1.1"  connectionTimeout= "20000"  "8443"  /> 
redirectPort=

this code specifies the port number of the Tomcat listening for the HTTP request.

You can add an attribute here: URIEncoding, which sets the property value to UTF-8 so that Tomcat (default ISO-8859-1 encoding) handles the get request UTF-8 with the encoding of.


After the

modification is completed:


<Connector port= "8080"  protocol= "HTTP/1.1"  connectionTimeout= "20000"  redirectPort= "8443"   URIEncoding= "UTF-8  />
"
Modify the MySql default encoding

ySQL default encoding is Latin1, does not support Chinese, to support the need to change the database's default encoding to GBK or utf8.
at noon
1, need to root the identity of the user can view the database log encoding (using the root user login command: >mysql -u root - P, after the two input root user password), command database encoding for:


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 code is Latin1 and needs to be modified as GBK or utf8;

Among them, character_set_client is client-side encoding; character_set_connection is the encoding used to establish connections; the encoding of the character_set_database database;

The encoding of the character_set_results result set;

Character_set_server database server encoding;

As long as the four encoding methods are guaranteed, there will be no garbled problems.

Another command to see the database encoding:

Show variables like 'collation%';


2, Linux system, modify the MySQL database default encoding steps:

Stop MySQL running,

/etc/init.d/mysql start (stop) is the start and stop server

The MySQL master configuration file is my.cnf, and the general directory is /etc/mysql

Var/lib/mysql/ places the database table folder, where the MySQL is equivalent to the date folder MySQL under Windows

When we need to modify the default encoding of the MySQL database, we need to edit the my.cnf file for encoding and modify the configuration file my.cnf at Linux, where the file location defaults to the /etc/my.cnf file

MySQL

to find the client configuration [client], add
below
The default-character-set=utf8 default character set is utf8

After finding [mysqld], add

The default-character-set=utf8 default character set is utf8

Init_connect= 'SET, NAMES, utf8' (when setting the connection to the MySQL database uses utf8 encoding to run the MySQL database for utf8)


When

is modified, restart mysql, query the database code again to find the change of encoding mode:


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 standard MySQL versions, and for /etc/my.cnf files, you need to copy from mysql/support-files folder CP my-large.cnf to /etc/my.cnf


3, windows system, you can delete the MySQL database, and reinstall, in the installation process, you can directly use the Mysql Server Instance Config Wizard settings


4, when the MySQL database server has been deleted data is not suitable for heavy equipment, can individual specified database encoding.MySQL specifies the encoding method is very flexible and diversified, you can specify the table level encoding, row level encoding, can even specify the field level encoding
.
The following example gives you two ways to specify code when you create a database:

()) CREATE, DATABASE, ms_db, CHARACTER, SET, utf8, COLLATE, utf8_general_ci,;

()) create, database, if, not, exists, netctoss, default, character, set, utf8,;

5, if you use the external access, can determine the request in the connection encoding format such as jdbc:mysql://localhost:3306/mysql? UseUnicode=true&characterEncoding=utf-8 (Note: do not appear any spaces, otherwise an error)

6, execute script: specify the encoding format set names GBK (note, not UTF-8) to modify

Before execution:

After execution:

From the executive order before and after that set names GBK can only modify the character_set_client, character_set_connection, character_set_results encoding, and this modification is the window level, only for the window, open another window modification is invalid. Can also be found in the underlying database encoding has not changed, insert the data or to the utf8 encoding.




copyright notice: This article is original article for blogger. Without permission from blogger,.

must not be reprinted

Latest