Mysql是一种常用的关系型数据库,也是Web开发中常用的数据库之一。但是,在中文字符的存储和操作中,经常出现乱码问题,这对于我们的数据处理非常不利。为了解决这个问题,我们需要采取一些特定的措施。
1. 查看当前系统所用的字符集
我们需要打开Mysql的命令行界面,输入以下命令:
show variables like '%char%';
这时,我们可以看到系统当前所用的各种字符集,包括字符集名称、字符集编码等,如下所示:
+--------------------------+-------------------------------------------------------+
| Variable_name | Value |
+--------------------------+-------------------------------------------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | utf8mb4 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql-8.0.23-linux-glibc2.17-x86_64/charsets/ |
+--------------------------+-------------------------------------------------------+
2. 修改数据库字符集
我们需要打开Mysql的命令行界面,进入需要修改字符集的数据库中,输入以下命令:
ALTER DATABASE database_name CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
其中,database_name是需要修改字符集的数据库名称,utf8mb4是字符集名称,utf8mb4_general_ci是字符集的排序规则,根据实际情况来选择。
3. 修改数据表字符集
我们需要打开Mysql的命令行界面,进入需要修改字符集的数据表中,输入以下命令:
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
其中,table_name是需要修改字符集的数据表名称,utf8mb4是字符集名称,utf8mb4_general_ci是字符集的排序规则,根据实际情况来选择。
示例1:数据库和数据表都采用utf8mb4
假设我们要创建一个数据库和一个数据表,用来存储中文数据。我们可以通过以下命令来创建:
CREATE DATABASE test DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
USE test;
CREATE TABLE user (id INT(11) AUTO_INCREMENT, name VARCHAR(16), PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
执行完成后,我们可以通过以下命令来查看字符集和排序规则:
show variables like '%char%';
SHOW FULL COLUMNS FROM user;
结果应该如下所示:
+--------------------------+-------------------------------------------------------+
| Variable_name | Value |
+--------------------------+-------------------------------------------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | utf8mb4 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql-8.0.23-linux-glibc2.17-x86_64/charsets/ |
+--------------------------+-------------------------------------------------------+
+-------+--------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
+-------+--------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
| id | int | NULL | NO | PRI | NULL | auto_increment | select,insert,update,references | |
| name | varchar(16) | utf8mb4_general_ci | YES | | NULL | | select,insert,update,references | |
+-------+--------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
示例2:数据库和数据表采用utf8mb4和gbk
假设我们已经有了一个默认采用gbk编码的数据库test_gbk,其中包含一个数据表user_gbk。现在我们想要创建一个新的字符集为utf8mb4的数据库test_utf8,其中包含一个数据表user_utf8,我们可以通过以下命令来创建:
CREATE DATABASE test_utf8 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
USE test_utf8;
CREATE TABLE user_utf8 (id INT(11) AUTO_INCREMENT, name VARCHAR(16), PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
USE test_gbk;
ALTER DATABASE test_gbk CHARACTER SET gbk COLLATE gbk_chinese_ci;
ALTER TABLE user_gbk CONVERT TO CHARACTER SET gbk COLLATE gbk_chinese_ci;
执行完成后,我们可以通过以下命令来查看字符集和排序规则:
show variables like '%char%';
SHOW FULL COLUMNS FROM user_utf8;
SHOW FULL COLUMNS FROM test_gbk.user_gbk;
结果应该如下所示:
+--------------------------+-------------------------------------------------------+
| Variable_name | Value |
+--------------------------+-------------------------------------------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | utf8mb4 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql-8.0.23-linux-glibc2.17-x86_64/charsets/ |
+--------------------------+-------------------------------------------------------+
+-------+-------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
+-------+-------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
| id | int | NULL | NO | PRI | NULL | auto_increment | select,insert,update,references | |
| name | varchar(16) | utf8mb4_general_ci | YES | | NULL | | select,insert,update,references | |
+-------+-------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
+-------+-------------+------------------+------+-----+---------+----------------+---------------------------------+---------+
| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
+-------+-------------+------------------+------+-----+---------+----------------+---------------------------------+---------+
| id | int | NULL | NO | PRI | NULL | auto_increment | select,insert,update,references | |
| name | varchar(16) | gbk_chinese_ci | YES | | NULL | | select,insert,update,references | |
+-------+-------------+------------------+------+-----+---------+----------------+---------------------------------+---------+
通过以上的步骤,我们可以已经成功地解决了Mysql中文乱码的问题。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Mysql彻底解决中文乱码问题的方案(Illegal mix of collations for operation) - Python技术站