0%

MySQL错误及解决方案

MySQL错误及解决方案

MySQL 错误及解决方案

mysql:1071 - Specified key was too long; max key length is 1000 bytes

问题描述:
    在给 Mysql 表添加`unique`索引时弹出此提示
解决方案:
    1. 修改索引字段长度
    2. 修改DB engine 至 innodb

mysql: 1093 - You can’t specify target table ‘pre_the_public’ for update in FROM clause, Time: 0.005000s

问题描述:
    delete a from pre_the_public a where (a.type,a.name) in (
        select type,name from pre_the_public group by type,name having count(*) > 1) 
    and id not in (
        select min(id) from pre_the_public group by type,name having count(*) > 1
    )

    不能先select出同一表中的某些值再update这个表(在同一语句中)

解决方案 - 您不能在内部查询中引用该表(但是,你可以在外部使用子查询解决):
    delete from pre_the_public where (type,name) in (
        select a.type,a.name from (select * from pre_the_public) AS a group by a.type,a.name having count(*) > 1) 
    and id not in (
        select min(b.id) from (select * from pre_the_public) AS b group by b.type,b.name having count(*) > 1
    );

    这显然会导致必要的字段被隐式地复制到临时表中,因此是允许的。
摘自:
http://www.cnblogs.com/chy1000/archive/2010/03/02/1676282.html

2021-01-13 SQLSTATE[HY000]: General error: 1615 Prepared statement needs to be re-prepared

今天业务监控告警提示:Prepared statement needs to be re-prepared,错误码:1615。同一台 MySQL 的多台服务器均有告警提示,过了一段时候后就没有了(无法复现)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# MySQL 版本
select version();
5.5.3-m3-log

show GLOBAL status like 'open%table%';
Open_table_definitions 416
Open_tables 614
Opened_table_definitions 41215
Opened_tables 65059

# 查看服务器默认配置值
# 所有线程打开的表的数量
SHOW VARIABLES LIKE '%table_open_cache%';
table_open_cache 614
# 可以存储在定义缓存中的表定义的数量
SHOW VARIABLES LIKE '%table_definition_cache%';
table_definition_cache 400

# 2021-01-15
show GLOBAL status like 'open%table%';
Open_table_definitions 400
Open_tables 614
Opened_table_definitions 41832
Opened_tables 66734

# 网上解决方案(未实际操作验证)
set global table_open_cache=16384;
set global table_definition_cache=16384;
参考

错误:Waiting for table metadata lock

  • 长事务运行,阻塞DDL,继而阻塞所有同表的后续操作

    可通过 show processlist 命令查看正在进行的操作(包括读,此时alter table语句无法获取到 metadata 独占锁,会进行等待。

  • 未提交事务阻塞DDL,继而引发同表的后续操作

    1
    select * from information_schema.innodb_trx;

    在事务没有完成之前,TableA上的锁不会释放,alter table同样获取不到metadata的独占锁。通过以上命令查询出未提交事务的 THREAD_ID,然后 kill 掉,让其回滚。

  • 通过show processlist看不到TableA上有任何操作,在information_schema.innodb_trx中也没有任何进行中的事务。这很可能是因为在一个显式的事务中,对TableA进行了一个失败的操作(比如查询了一个不存在的字段),这时事务没有开始,但是失败语句获取到的锁依然有效,没有释放。从performance_schema.events_statements_current表中可以查到失败的语句。

    1
    select * from performance_schema.events_statements_current;
参考