Common SQL Errors

MySQL Error 1015: ER_CANT_LOCK – How to Fix File Locking Failures

Galaxy Team
August 5, 2025

Error 1015 indicates that MySQL cannot obtain a required file lock, usually on a table’s .frm, .ibd, or system metadata file.

Sign up for the latest in common SQL errors from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

What is MySQL error code 1015 (ER_CANT_LOCK)?

MySQL Error 1015: ER_CANT_LOCK occurs when the server cannot acquire an operating-system file lock on a table or index file. Check file permissions, open-file limits, and concurrent sessions, then retry or restart the affected operation to clear the lock.

Error Highlights

Typical Error Message

Can't lock file (errno: %d - %s)

Error Type

Storage/Locking Error

Language

MySQL

Symbol

ER_CANT_LOCK

Error Code

1015

SQL State

Explanation

Table of Contents

What is MySQL Error 1015 (ER_CANT_LOCK)?

MySQL raises Error 1015 with the message "Can't lock file" when it fails to obtain an exclusive or shared lock on a table-related file. The lock request comes from DDL, INSERT, UPDATE, or an ALTER TABLE that needs metadata consistency.

Lock acquisition can fail because the file is already locked by another MySQL thread, an external process, or the operating system due to stale descriptors.

The error stops the current statement and rolls back any partial work to preserve data integrity.

Why does fixing it matter?

Leaving file locks unresolved stalls application traffic, blocks automation tasks, and risks deadlocks that escalate into server freezes. Rapid identification and resolution restore normal throughput and prevent corrupted metadata.

What Causes This Error?

High concurrency during heavy write workloads often triggers competing locks on .ibd files inside InnoDB tablespaces.

OS limits on open files or mandatory locking options also contribute.

Permission changes on the data directory or antivirus scans that hold handles can block MySQL from locking its own files, surfacing Error 1015.

How to Fix MySQL Error 1015

First, locate the blocking session with SHOW PROCESSLIST or performance_schema metadata_lock_info.

Kill or wait for the session to finish, then retry the statement.

If no MySQL thread holds the lock, inspect the OS with lsof or fuser to find external processes. Stop the process, ensure proper permissions, and validate that the MySQL user owns the files.

Common Scenarios and Solutions

During online ALTER TABLE, Error 1015 may appear if another thread starts a concurrent ALTER.

Serialize schema changes using pt-online-schema-change or Galaxy’s endorsed query workflows.

On Windows, aggressive antivirus locks .ibd files. Add the MySQL data directory to the exclusion list to eliminate interference.

Best Practices to Avoid This Error

Apply explicit metadata locking-timeout limits (e.g., lock_wait_timeout = 60) and monitor waits.

Automate schema migrations during low-traffic windows.

Use Galaxy’s versioned query library to schedule schema changes from a single governed pipeline, reducing accidental simultaneous DDL.

Related Errors and Solutions

Error 1205 (Lock wait timeout) surfaces when a transaction waits too long rather than failing instantly. Increase innodb_lock_wait_timeout or optimize indexing.

Error 1025 (Error on rename) appears during ALTER TABLE RENAME if MySQL cannot lock both source and target tables. Resolve underlying file locks similarly to Error 1015.

.

Common Causes

Related Errors

FAQs

How do I know which process is locking the file?

Run lsof or fuser on Unix, or Process Explorer on Windows, targeting the exact .ibd/.frm file path.

Will restarting MySQL always clear Error 1015?

Restarting frees internal locks but not external ones. Ensure third-party tools release file handles before restart.

Can Galaxy prevent concurrent DDL that triggers this error?

Yes. Galaxy’s endorsed query workflow lets teams schedule schema changes in a single pipeline, avoiding overlapping ALTER TABLE commands.

Does innodb_flush_method affect file locking?

Using O_DIRECT or ALL_O_DIRECT may change how the OS handles locks, but Error 1015 typically relates to metadata files, not flushed pages.

Start Querying with the Modern SQL Editor Today!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Check out some other errors

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo