MySql changes the database encoding to UTF8 to avoid garble problems

  • 2020-05-19 06:03:25
  • OfStack

It is important to specify the code when mysql is creating a database. Many developers use the default code. Encoding the database can largely avoid the garbled code problems caused by the dump export.

Web data 1 is encoded as UTF8, while the database defaults to latin. We can reduce the setting of database creation by changing the default database encoding mode to UTF8, and we can also avoid the messy code problem caused by carelessness to the greatest extent.

The standard we follow is that the encoding of databases, tables, fields, and pages or text should be unified
We can view the current code of the database by the command: mysql > SHOW VARIABLES LIKE 'character%';
We found that many of them correspond to latin1, and our goal is to replace latin1 with UTF8 the next time we use this command.

Stage 1:
mysql sets the encoding command
 
SET character_set_client = utf8; 
SET character_set_connection = utf8; 
SET character_set_database = utf8; 
SET character_set_results = utf8; 
SET character_set_server = utf8; 

Then mysql > SHOW VARIABLES LIKE 'character %'; You can see it's all utf8.
But that's just an illusion

This will only work in the current state and will not work when the database service is restarted.
So if you want to get rid of the mess you have to modify the my.ini file,
Start with my.ini (no addition under the label, some modification)
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
default-character-set=utf8
default-character-set =utf8 should be added to the above three section. In normal times, we may only add mysqld1.
Then restart mysql and execute
mysql > SHOW VARIABLES LIKE 'character%';
Make sure all Value entries are utf8.
But here comes the nasty thing again,
|character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 note that this configuration | character_set_server | latin1 can not be set to UTF8 interaction will still be messy.

Stage 2: find the following
X:\%path%\MySQL\MySQL Server 5.0\bin\MySQLInstanceConfig.exe
Restart the Settings and set the default encoding to utf8. This will give you the desired effect.
mysql > 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 | C:\Program Files\MySQL\MySQL Server 5.0\share\charsets\ |
+--------------------------+---------------------------------------------------------+
8 rows in set
Other notes:

1. When adding utf8 to the table, the Collation of the table field can be added or not, and the default is utf8_general_ci.
 
CREATE TABLE `tablename4` ( 
`id` int(11) NOT NULL AUTO_INCREMENT, 
`varchar1` varchar(255) DEFAULT NULL, 
`varbinary1` varbinary(255) DEFAULT NULL, 
PRIMARY KEY (`id`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 

2. When saving xxx. php/jsp, select the code utf8
header('conten-type:text/html;charset=utf-8');
Execute 1 before performing the CRUD operation
mysql_query("set names utf8");

-------------------------
Connection database setup encoding
jdbc:mysql:// address :3306/ database name? characterEncoding = utf8
-------------------------
The common code in java is UTF-8; GBK; GB2312; ISO - 8859-1.
Corresponding to the code utf8 in mysql database; gbk; gb2312; latin1

Related articles: