keepcalculator

06 Numbers and Powers

MySQL Error Troubleshooting: Common Fixes

Card No. 06·48099 · Updated · Standard formula, shown below

MySQL errors can stop your application or website in its tracks, but most common ones have straightforward fixes. This article walks you through the most frequent MySQL error messages, explains what causes them, and gives you step-by-step solutions to get your database running again.

Why MySQL Errors Happen

MySQL is a relational database management system used by millions of websites and applications. Errors can arise from many sources: incorrect SQL syntax, permission issues, connection problems, resource limits, or data type mismatches. Understanding the underlying cause is the first step to a fix. Many errors are misdiagnosed because the error message is cryptic or the real problem is different from what it appears to be.

Common Connection Errors

Connection errors are among the most frequent MySQL issues. They usually mean your client cannot reach the MySQL server.

  • ERROR 2002 (HY000): Can’t connect to local MySQL server through socket – This often means the MySQL server is not running, or the socket file path is wrong. Check with systemctl status mysql (Linux) or look for the socket path in your config file (my.cnf or my.ini).
  • ERROR 2003 (HY000): Can’t connect to MySQL server on ‘host’ (111) – The server is not listening on the expected IP/port, or a firewall is blocking it. Verify MySQL is bound to the right address (e.g., bind-address = 0.0.0.0) and that port 3306 is open.
  • ERROR 1045 (28000): Access denied for user ‘user’@’host’ – Wrong username or password, or the user is not allowed to connect from that host. Use GRANT to allow the host, or reset the password with ALTER USER.

For connection timeouts, check connect_timeout and wait_timeout variables. If you’re using a connection pool, stale connections can cause intermittent failures.

SQL Syntax Errors

Syntax errors are the most common type. The error message usually shows the exact line and character position, but the real mistake is often earlier.

  • ERROR 1064 (42000): You have an error in your SQL syntax – Check for missing commas, unclosed parentheses, or using reserved words as column names. For example, SELECT * FROM table WHERE select = 1 fails because select is a reserved word. Use backticks: WHERE `select` = 1.
  • Missing semicolons in multi-statement queries – Ensure each statement ends with a semicolon when using the command line client.
  • Incorrect string quoting – Use single quotes for strings, not double quotes (unless ANSI_QUOTES is enabled).

To debug, break your query into smaller parts, or use EXPLAIN to see how MySQL interprets it.

Data Type and Constraint Errors

These errors occur when you try to insert or update data that doesn’t fit the column definition.

  • ERROR 1366 (HY000): Incorrect integer value: ‘abc’ for column ‘id’ – The value is not a valid number. Check your input and the column type.
  • ERROR 1264 (HY000): Out of range value for column ‘age’ – The number is too large for the column’s data type (e.g., TINYINT max 127). Use a larger type like INT or BIGINT.
  • ERROR 1406 (22001): Data too long for column ‘name’ – The string exceeds the column’s length. Either trim the input or increase the column size with ALTER TABLE ... MODIFY COLUMN.
  • ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails – You’re trying to insert a value that doesn’t exist in the parent table, or you’re deleting a row that is referenced. Check the referenced table’s data.

For NULL values, ensure the column allows NULL or provide a default. Use SHOW CREATE TABLE to see the exact constraints.

Lock and Deadlock Errors

When multiple transactions try to modify the same data, locks can cause conflicts.

  • ERROR 1205 (HY000): Lock wait timeout exceeded – A transaction is holding a lock for too long. Increase innodb_lock_wait_timeout (default 50 seconds), or find and kill the blocking transaction with SHOW PROCESSLIST.
  • ERROR 1213 (40001): Deadlock found – Two transactions wait on each other’s locks. MySQL automatically rolls back one transaction. To avoid, keep transactions short, access tables in the same order, and use consistent indexes.

Monitor locks with SHOW ENGINE INNODB STATUS to see the latest deadlock details.

Resource and Configuration Errors

These errors happen when MySQL runs out of resources or hits a limit.

  • ERROR 1040 (HY000): Too many connections – The connection limit is reached. Increase max_connections (e.g., SET GLOBAL max_connections = 500), but be careful with memory. Also, check for connection leaks in your app.
  • ERROR 1114 (HY000): The table is full – The disk is full or the table reached its max size (for MyISAM). Free up disk space or switch to InnoDB.
  • ERROR 1030 (HY000): Got error 28 from storage engine – Usually means disk full. Check df -h.

Slow queries are not errors but can cause timeouts. Use EXPLAIN to analyze and add indexes.

Frequently Asked Questions

How do I reset a forgotten MySQL root password?

Stop the MySQL service, start it with --skip-grant-tables, connect as root without a password, then run FLUSH PRIVILEGES; and ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';. Restart MySQL normally. On some systems, you need to use mysqld_safe --skip-grant-tables.

Why does MySQL say “Unknown database”?

This error means the database name you specified does not exist. Check for typos, and ensure the database was created with CREATE DATABASE. If you’re using a hosted service, the database name might be prefixed with your username.

How can I fix “Table doesn’t exist” even though I can see it?

This often happens due to case sensitivity. On Linux, table names are case-sensitive, so Users and users are different. Check the exact case with SHOW TABLES. Also, ensure you’re connected to the correct database.

What causes “Duplicate entry” errors and how do I solve them?

This occurs when you try to insert a value that already exists in a unique or primary key column. Check the existing data with SELECT, and either use INSERT IGNORE, ON DUPLICATE KEY UPDATE, or remove the duplicate.

How do I fix “Unknown column” in a query?

This means the column name doesn’t exist in the table. Verify the column name with DESCRIBE table_name. Common causes are typos, using an alias incorrectly, or referencing a column from a subquery that isn’t accessible.

Prevention and Best Practices

Many MySQL errors can be avoided with good habits. Always back up your database before making schema changes. Use version control for your SQL scripts. Test queries on a staging environment first. Monitor your MySQL logs (error.log) regularly. Set up alerts for disk space and connection usage. Use EXPLAIN to optimize slow queries before they cause timeouts. Keep your MySQL version up to date to get bug fixes and security patches.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →