「MySQL」- cannot be converted from type x to type y @20210224

在MySQL主从复制过程中遇到一个错误,提示int类型的列无法转换为bigint。问题源于二进制日志还未同步到从库时对从库字段进行了类型修改,且未设置slave_type_conversions。解决方法包括设置slave_type_conversions为ALL_NON_LOSSY或避免在同步期间修改类型。此外,参考了使用REPAIR TABLE命令修复类似问题的文章。
部署运行你感兴趣的模型镜像

问题描述

在 MySQL 主从复制时,产生如下错误:

Column 0 of table 'database_name.table_name' cannot be converted from type 'int' to type 'bigint(20)'

根据提示是int类型无法转换为bigint类型,但是原因是什么?

系统环境

属性参数
MySQLMaster-Master Replication
SELECT @@version;5.7.20-log
SELECT @@binlog_format;ROW
SELECT @@slave_type_conversions; 

问题原因

首先,数据库的SQL语句是执行成功后,才写入二进制日志中的。

# 在我们的场景中

这也是推测出来:当时主库的二进制日志还没有完全同步到从库,我们对从库的字段进行了修改,从int修改为bigint类型。并且数据库没有设置slave_type_conversions属性,因此主从之间不允许类型转换。二进制日志为ROW格式,而二进制日志中的字段为int类型,但是目标中为bigint类型。

另外我们开启了log_slave_updates属性,但是从库的binlog中并没有找到来自主库的记录。

解决办法

int转换为bigint是无损的,所以可以进行slave_type_conversions=ALL_NON_LOSSY设置。

或者不要在二进制日志同步期间进行这些操作。其实还是要懂数据库里的一些细节上的东西。

然而,事情向来不会这么简单。在「ERROR 1677: Column of table cannot be converted from type varchar(0) to type varchar(20)」一文中,作者通过执行REPAIR TABLE修复了该问题。

参考文献

5.2.4.2. Setting The Binary Log Format
16.1.6.3 Replication Slave Options and Variables/slave_type_conversions
Row-based replication: attribute promotion and demotion.
Type conversion modes (slave_type_conversions variable).
MySQL Row-Based Binary Logs
Is MySQL Replication Affected by a High-Latency Interconnect?
MySQL binary log - write operation
4.1.10.2 Replication of Columns Having Different Data Types
MySQL Row-Based Binary Logs
ERROR 1677: Column of table cannot be converted from type varchar(0) to type varchar(20)

您可能感兴趣的与本文相关的镜像

GPT-oss:20b

GPT-oss:20b

图文对话
Gpt-oss

GPT OSS 是OpenAI 推出的重量级开放模型,面向强推理、智能体任务以及多样化开发场景

Beginning configuration step: Writing configuration file Saving my.ini configuration file... Saved my.ini configuration file. Ended configuration step: Writing configuration file Beginning configuration step: Updating Windows Firewall rules Adding a Windows Firewall rule for MySQL57 on port 3306. Attempting to add a Windows Firewall rule with command: netsh.exe advfirewall firewall add rule name="Port 3306" protocol=TCP localport=3306 dir=in action=allow 纭畾銆? Successfully added the Windows Firewall rule. Ended configuration step: Updating Windows Firewall rules Beginning configuration step: Adjusting Windows service Attempting to grant the required filesystem permissions to the 'NT AUTHORITY\NetworkService' account. Granted permissions to the data directory. Adding new service New service added Ended configuration step: Adjusting Windows service Beginning configuration step: Initializing database (may take a long time) Deleting the data directory from a previous (failed) configuration... Attempting to run MySQL Server with --initialize-insecure option... Starting process for MySQL Server 5.7.44... Starting process with command: F:\mysql\m\bin\mysqld.exe --defaults-file="F:\mysql\y\my.ini" --console --initialize-insecure=on --lower-case-table-names=1... TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). InnoDB: New log files created, LSN=45790 InnoDB: Creating foreign key constraint system tables. mysqld: File '.\姹熷鐪?slow.log' not found (Errcode: 2 - No such file or directory) Could not use 姹熷鐪?slow.log for logging (error 2 - No such file or directory). Turning logging off for the server process. To turn it on again: fix the cause, then either restart the query logging by using "SET GLOBAL SLOW_QUERY_LOG=ON" or restart the MySQL server. No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 42751b4d-8dec-11f0-b5f1-025034608cb2. Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher. A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher. CA certificate ca.pem is self signed. root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option. Process for mysqld, with ID 19428, was run successfully and exited with code 0. Successfully started process for MySQL Server 5.7.44. MySQL Server 5.7.44 intialized the database successfully. Ended configuration step: Initializing database (may take a long time) Beginning configuration step: Updating permissions for the data folder and related server files Attempting to update the permissions for the data folder and related server files... Inherited permissions have been converted to explicit permissions. Full control permissions granted to: NETWORK SERVICE. Full control permissions granted to: Administrators. Full control permissions granted to: CREATOR OWNER. Full control permissions granted to: SYSTEM. Access to the data directory is removed for the users group. Permissions for the data folder and related server files are updated correctly. Ended configuration step: Updating permissions for the data folder and related server files Beginning configuration step: Starting the server Attempting to start service MySQL57.................... 只有在任务处于完成状态(RanToCompletion、Faulted 或 Canceled)时才能释放它。 Ended configuration step: Starting the server . Found existing data directory, no need to initialize the database. Found existing data directory, no need to initialize the database.帮我解决
最新发布
09-11
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值