MySQL "InnoDB: Unable to lock ./ibdata1" — A Second Server Is Running

Only one mysqld can own the data files. 'Unable to lock ibdata1' means another instance (often a stray container or unclean shutdown remnant) holds them. Find it, don't delete anything.

What you'll see

Root causes

A second mysqld process (often an old container) is alive

docker ps -a | grep mysql shows the older container still running or restarting. ps aux | grep mysqld on the host finds stray processes.

Unclean shutdown left stale locks (rare)

If no process holds the files (lsof /var/lib/mysql shows nothing), an unclean stop left lock remnants — mysqld normally clears these; force-clearing needs a clean start attempt first.

Fix it

  1. Identify what holds the data directory
    docker ps -a --filter name=mysql; sudo lsof +D /var/lib/mysql 2>/dev/null | head
  2. Stop the other instance cleanly
    docker stop <old-container>   # or: sudo systemctl stop mysql  — clean stop lets the next instance acquire locks
  3. Start yours and watch the log for recovery
    docker logs -f <container>   # expect 'InnoDB: ... started' after redo replay; loop-restarts = look for disk errors
  4. Two MySQLs on one host? Separate them properly
    # distinct data dirs AND ports; compose networks so only one claims 3306

Field note

Never delete ibdata1 to 'fix' the lock — that's your data. Every legit fix is about process ownership. Same-named compose projects are a common source: 'docker compose up' in two checkouts creates two containers over one volume.

Common questions

Can I just remove the lock file?

There is no separate lock file to delete — mysqld locks the data files themselves (flock). If lsof shows no holder, it's a stale-file situation that a clean start usually resolves; deleting ibdata1 destroys your data.

Why do two MySQL containers start on the same volume?

Usually stale compose projects or an 'always restart' policy surviving a docker kill. docker ps -a for all names + volumes, and pin project names to avoid collisions.

Ship it right the first time

Our most-documented failures, packaged as ready-to-ship starter kits: Docker, Kubernetes, and Terraform.

Browse the template store →

One-time. Yours to modify. Instant download from the NinjaOps template store.