mysql数据校验过程中的字符集问题处理_MySQL
场景:
主库DB:utf8字符集
备库DB:gbk字符集
需求:
校验主备数据是否一致,并且修复
校验过程:
设置主库连接为utf8,设置备库连接为gbk,分别进行查询,将返回的的结果集按记录逐字段比较。
显示结果:
原本相同的汉字字符,数据校验认为不一致。
原因分析:
对于主库而已,由于建立连接的字符集为UTF8,则返回的汉字字符编码为UTF8格式;对于备库而言则是GBK格式,而程序中通过字符串比较函数strcasecmp进行比较,显然不同的字符集编码,相同的字符有不同的二进制,因此结果肯定不会相等。
进一步分析:
那么对于这种情况,建立连接应该采用哪种字符集呢?GBK or UTF8。其实选择任何一种字符集都是OK的,只要是访问主库和备库的字符集保持一致即可,唯一的区别在于,若选择的字符集与客户端的字符集不一致,可能导致无法正常显示字符,即字符显示为乱码。
我们以客户端的字符集为例,详细说说三种情况:【这里的客户端可以认为是SecureCRT】
备注:绿色框代表DB字符集,黄色框代表连接字符集,橙色框代表客户端
第一种情况:
就是上述的情况,主库返回字符的GBK编码,备库返回字符的UTF8编码,因此进行字段比对,则会出现误差。
第二种情况:
访问主库的连接不变,备库连接由UTF8变为GBK,因此进行返回时,数据库会将DB的字符集转为GBK返回给客户端,那么对于客户端而已,相同字符都是通过GBK编码表示,因此二进制相等,校验结果正确。
第三种情况:
访问主库和备库的连接都是UTF8,因此对于主库而已,返回给客户端的字符编码由GBK转为UTF8,此时主库和备库都是UTF8编码,校验结果正确。但由于客户端实质是GBK编码方式显示,因此返回的汉字字符都是乱码,但不影响校验结果的正确性。
修复:
既然选择与主备库任一一个相同的字符集去访问,都不会影响校验结果的正确性,那么影响修复呢?由于UTF8的编码范围比GBK编码范围要大,因此若采用GBK连接访问UTF8编码DB,有可能出现部分字符GBK不能表示的情况。
我们拿第二种情况说明,此时主库为GBK,备库为UTF8,使用GBK访问UTF8。假设存在UTF8转为GBK过程中部分字符丢失,这时候主备库肯定是不一致的,因为存在部分字符GBK无法表示。 假设修复语句如下:
Update t set c1=master_value where c1=slave_value and id=?
其中t表示表名,id是主键表示某一行,master_value为主库c1列的值,slave_value为备库c1列的值。此时,slave_value由于UTF8转为GBK已经丢失,因此语句执行最终影响0行记录,无法修复。
结论:
客户端访问两个不同字符集库进行数据校验时,连接采用表示范围更大的字符集。比如我们常用的字符集表示范围如下:
Latin 附:mysql客户端与服务器通信时字符集编码转换流程 相关参数:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Building an SQL database involves 10 steps: selecting DBMS; installing DBMS; creating a database; creating a table; inserting data; retrieving data; updating data; deleting data; managing users; backing up the database.
