Arjun learns to read error messages
Error 1045, 1064, 1062, 1452 — what every common MySQL error means and how to fix it
Arjun joined Priya's team. His first week he made the classic beginner mistakes. The app started showing errors. Priya sat with him and showed him exactly how to read MySQL errors and fix them.
Every MySQL error has a number and a message. Once you learn to read them, debugging becomes fast.
THE MOST COMMON MYSQL ERRORS AND HOW TO FIX THEM
ERROR 1045: Access denied for user
ERROR 1045 (28000): Access denied for user 'appuser'@'localhost' (using password: YES)This means: wrong password, user does not exist, or user does not have permission.-- Check if user exists:
SELECT user, host FROM mysql.user WHERE user = 'appuser';-- Reset password:
ALTER USER 'appuser'@'localhost' IDENTIFIED BY 'new_password';-- Grant permissions:
GRANT ALL PRIVILEGES ON myapp_db.* TO 'appuser'@'localhost';
FLUSH PRIVILEGES;-- Check what permissions a user has:
SHOW GRANTS FOR 'appuser'@'localhost';ERROR 1064: Syntax error
ERROR 1064 (42000): You have an error in your SQL syntax near 'FORM users' at line 1MySQL tells you WHERE the error is. Look just before that position.
Common causes:
- Misspelled keyword (FORM instead of FROM)
- Missing comma between column names
- Missing quote around a string value
- Missing semicolon at end-- Debug tip: run simpler parts of the query first:
SELECT * FROM users; -- does the table exist?
SELECT id FROM users; -- does the column exist?
SELECT id FROM users WHERE id = 1; -- does WHERE work?ERROR 1062: Duplicate entry
ERROR 1062 (23000): Duplicate entry 'rahul@example.com' for key 'email'You tried to INSERT a value that already exists in a UNIQUE column.-- Find the existing row:
SELECT * FROM users WHERE email = 'rahul@example.com';-- Options:
INSERT IGNORE INTO users ... -- skip if duplicate
INSERT ... ON DUPLICATE KEY UPDATE ... -- update if duplicateERROR 1452: Cannot add or update a child row (foreign key violation)
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint failsYou tried to insert an order with user_id = 999 but no user with id 999 exists.-- Check what value you are inserting:
SELECT * FROM users WHERE id = 999; -- does this user exist?-- Temporarily disable foreign key checks (only for bulk imports):
SET FOREIGN_KEY_CHECKS = 0;
-- do your import
SET FOREIGN_KEY_CHECKS = 1;ERROR 1406: Data too long
ERROR 1406 (22001): Data too long for column 'name' at row 1The value you tried to insert is longer than the column allows.-- Check the column definition:
DESCRIBE users;-- Fix: increase the column size:
ALTER TABLE users MODIFY COLUMN name VARCHAR(200);ERROR 1054: Unknown column
ERROR 1054 (42S22): Unknown column 'usr.name' in 'field list'You used a column name or alias that does not exist or is misspelled.DESCRIBE users; -- see actual column names
SHOW TABLES; -- confirm table existsERROR 2002: Cannot connect to MySQL socket
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'MySQL is not running.sudo systemctl status mysql
sudo systemctl start mysql
sudo journalctl -u mysql -n 50 -- see why it failed to startCHECKING THE ERROR LOG
-- Find the error log location:
SHOW VARIABLES LIKE 'log_error';-- Read it:
sudo tail -50 /var/log/mysql/error.logGENERAL DEBUGGING PROCESS
Step 1: Read the full error message. Note the error number.
Step 2: Check what line/position MySQL says the error is at.
Step 3: Run a simpler version of the query to isolate the problem.
Step 4: Check DESCRIBE tablename to confirm columns and types.
Step 5: Check if referenced data exists (for foreign key errors).
Step 6: Check SHOW PROCESSLIST for lock or connection issues.SHOW PROCESSLIST; -- see all active connections and their queries
SHOW VARIABLES LIKE '%timeout%'; -- connection and query timeout settingsArjun fixed 6 errors in his first week using this process. By week 2, he was solving errors in minutes instead of hours.
Error 1045 is Access Denied — check if the user exists, has the right host, and has the correct password
Error 1064 is a syntax error — MySQL tells you the position, look just before that point for a misspelling or missing character
Error 1062 is a duplicate key — a UNIQUE constraint is violated, use INSERT IGNORE or ON DUPLICATE KEY UPDATE
Error 1452 is a foreign key violation — the referenced row does not exist, check the parent table first
SHOW PROCESSLIST shows all active connections and their current query — essential for diagnosing hangs and locks