After mysql is installed, Chinese insertion is unsuccessful or the problem of garbled characters after insertion

1. After installing MySQL, you should first modify the character encoding to ensure that Chinese characters will not be garbled:

check the character set:

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 | /usr/local/mysql-5.7.12-osx10.11-x86_64/share/charsets/ |
+--------- -----------------+-------------------------------- -------------------------+
8 rows in set (0.01 sec)
as above, except character_set_filesystem and character_sets_dir, other modifications need to be unified to utf8, The command is as follows:
mysql> set character_set_client=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> set character_set_connection=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> set character_set_database=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> set character_set_results=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> set character_set_server=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> set character_set_system=utf8;
Query OK, 0 rows affected (0.01 sec)
mysql> set collation_connection=utf8;
Query OK, 0 rows affected (0.01 sec)
mysql> set collation_database=utf8;
Query OK, 0 rows affected (0.01 sec)
mysql> set collation_server=utf8;
Query OK, 0 rows affected (0.01 sec)

设置database和table字符集编码为utf8:
mysql>use Young_db
mysql>alter database Young_db character set utf8;
mysql>alter table user character set utf8;


Specify the encoding set when creating:
mysql>create database Young_db character set utf8;



2. When you forget to modify and create a database or table, modify the command as follows:
View database encoding:


mysql> show create database Young_db ;
+----------+-------------------------------------- --------------------------------+
| Database | Create Database |
+---------- +------------------------------------------------- --------------------+
| Young_db | CREATE DATABASE `Young_db` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+--------- -+------------------------------------------------ ---------------------+
1 row in set (0.00 sec)


modify the encoding to utf-8:
mysql> alter database Young_db character set utf8;
Query OK, 1 row affected (0.00 sec)

mysql> show create database Young_db;
+----------+-------------------------------------------------------------------+
| Database | Create Database                                                   |
+----------+-------------------------------------------------------------------+
| Young_db | CREATE DATABASE `Young_db` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+-------------------------------------------------------------------+
1 row in set (0.00 sec)


查看数据表的编码:
mysql> use Young_db;
mysql> show create table users;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                     |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| users | CREATE TABLE `users` (
  `userId` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(45) DEFAULT NULL,
  `passwod` varchar(45) NOT NULL,
  PRIMARY KEY (`userId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)


修改表的编码:
mysql> alter table users character set utf8;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table users;
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                             |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| users | CREATE TABLE `users` (
  ​​`userId` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(45) CHARACTER SET latin1 DEFAULT NULL,
  `passwod` varchar(45) CHARACTER SET latin1 NOT NULL,
  PRIMARY KEY ( `userId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+---------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ----------------------+
1 row in set (0.00 sec)

Summary : After installing MySQL, it is recommended to unify the character set encoding first, you can follow the above 1 The operation described in (the default is Latin1 without modification), so that the modified encoding is automatically used when creating a table, and there will be no problem of Chinese garbled characters; if there is no unified character set encoding when creating a table, you can specify each time you create a database and table. Character set encoding, otherwise Chinese garbled characters will appear; if the above two are not followed, the insert operation will appear ERROR 1366 (HY000): Incorrect string value: '\xE5\xBC\x80\xE5\xA7\x8B...' for column 'name' at row 1 error, you need to modify the character set of database and table as described in 2 After encoding, it can solve the problem of unsuccessful insertion of Chinese and garbled characters after insertion.


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326942018&siteId=291194637