Troubleshooting of MySQL errors: Difference between revisions
Tag: wikieditor |
|||
| (18 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
Troubleshooting of MySQL errors | Troubleshooting of MySQL errors | ||
{{LanguageSwitcher | content = [[Troubleshooting of MySQL errors | | {{LanguageSwitcher | content = [[Troubleshooting of MySQL errors | English]], [[排除 MySQL 技術疑難問題|漢字]]}} | ||
| Line 67: | Line 67: | ||
* Check if the disk space where mysql data folder located is enough. e.g. Input {{kbd | key=df -h}} on {{Linux}} | * Check if the disk space where mysql data folder located is enough. e.g. Input {{kbd | key=df -h}} on {{Linux}} | ||
* More on [http://stackoverflow.com/questions/18719748/error-1006-hy000-cant-create-database-errno-13-mysql-5-6-12 ERROR 1006 (HY000) Can't create database (errno: 13) MySQL 5.6.12 - Stack Overflow]. | * More on [http://stackoverflow.com/questions/18719748/error-1006-hy000-cant-create-database-errno-13-mysql-5-6-12 ERROR 1006 (HY000) Can't create database (errno: 13) MySQL 5.6.12 - Stack Overflow]. | ||
=== ERROR 1010: Error dropping database (can't rmdir) === | |||
Message: <pre>Error dropping database (can't rmdir '.\<db_name>\', errno: 17)</pre> | |||
Solution | |||
* Manually delete the leftover non-MySQL files (e.g. .sql files) inside the database directory, then retry DROP DATABASE — or simply delete the entire folder manually. | |||
Root Cause | |||
* MySQL's DROP DATABASE only removes files it manages. Foreign files left in the data directory (such as .sql dumps) prevent rmdir from removing the folder, causing errno 17 (ENOTEMPTY). | |||
=== ERROR 1017 - Can't find file: '.\DATABASE\TABLE.frm' (errno: 22 - Invalid argument) === | === ERROR 1017 - Can't find file: '.\DATABASE\TABLE.frm' (errno: 22 - Invalid argument) === | ||
| Line 137: | Line 146: | ||
/*!40101 SET character_set_client = @saved_cs_client */; | /*!40101 SET character_set_client = @saved_cs_client */; | ||
</pre> | </pre> | ||
=== Table already exists when using RENAME TABLE === | |||
Situation: When replacing a table across databases using <code>RENAME TABLE</code>, the following error occurs even after renaming the existing table as a backup: | |||
<pre lang="text"> | |||
Table '.\DATABASE_NAME\TABLE_NAME' already exists | |||
</pre> | |||
Possible cause: The source and destination tables have the same foreign key constraint name. Renaming the existing table leaves its custom constraint name unchanged, causing a naming conflict when the new table is moved into the destination database. MySQL 5.7, 8.0, and 8.4 require foreign key constraint names to be unique within a database. See [https://docs.oracle.com/cd/E17952_01/mysql-5.7-en/create-table-foreign-keys.html MySQL Foreign Key Constraints]. | |||
Solution: | |||
'''1. Confirm that the backup table name is available, and check the foreign key names on both tables.''' | |||
<pre lang="sql"> | |||
SHOW CREATE TABLE target_db.example_items; | |||
SHOW CREATE TABLE staging_db.example_items; | |||
</pre> | |||
Look for <code>FOREIGN KEY</code> in the output. For example: | |||
<pre lang="sql"> | |||
KEY `ix_example_parent_id` (`parent_id`), | |||
CONSTRAINT `fk_example_parent` | |||
FOREIGN KEY (`parent_id`) | |||
REFERENCES `example_parents` (`id`) | |||
ON DELETE SET NULL | |||
</pre> | |||
<table> | |||
<tr><th>Syntax</th><th>Meaning</th></tr> | |||
<tr><td><code>KEY ...</code></td><td>An index; '''this alone does not indicate a foreign key'''</td></tr> | |||
<tr><td><code>CONSTRAINT fk_example_parent</code></td><td>The foreign key constraint name, used when dropping the foreign key</td></tr> | |||
<tr><td><code>FOREIGN KEY (parent_id)</code></td><td>The foreign key column in this table</td></tr> | |||
<tr><td><code>REFERENCES example_parents (id)</code></td><td>The referenced parent table and column</td></tr> | |||
<tr><td><code>ON DELETE SET NULL</code></td><td>Sets the corresponding foreign key value to <code>NULL</code> when the referenced parent row is deleted</td></tr> | |||
</table> | |||
An <code>fk_</code> prefix alone does not identify a foreign key. Look for <code>FOREIGN KEY (...) REFERENCES ...</code> to confirm. | |||
Also, <code>SHOW CREATE TABLE</code> lists only foreign keys defined on that table. It does not list foreign keys in other tables that reference it. | |||
'''2. If the foreign key constraint is unnecessary, drop the conflicting constraint from the existing destination table, then swap the tables.''' | |||
<pre lang="sql"> | |||
ALTER TABLE target_db.example_items | |||
DROP FOREIGN KEY fk_example_parent; | |||
RENAME TABLE | |||
target_db.example_items TO target_db.example_items_backup, | |||
staging_db.example_items TO target_db.example_items; | |||
</pre> | |||
Notes: | |||
* Dropping the foreign key preserves the column and index, but removes referential integrity checks and the original <code>ON DELETE SET NULL</code> behavior. | |||
* If the foreign key is required, recreate the constraint with a different name. | |||
* After the swap, verify which database the foreign key references. Foreign keys in other tables follow the renamed old table; they do not automatically switch to the replacement table. See [https://docs.percona.com/percona-toolkit/pt-online-schema-change.html Percona’s Foreign Key Handling Documentation]. | |||
== Access and Authentication Errors == | == Access and Authentication Errors == | ||
=== [Warning] Using a password on the command line interface can be insecure === | |||
Problem Description: | |||
When exporting or importing MySQL databases via bash commands, you may encounter the following warning: | |||
<pre> | |||
mysql: [Warning] Using a password on the command line interface can be insecure. | |||
or | |||
mysqldump: [Warning] Using a password on the command line interface can be insecure. | |||
</pre> | |||
This typically occurs when using commands like: | |||
<pre> | |||
mysqldump -u USERNAME -p -h 127.0.0.1 --default-character-set=utf8 DATABASE > DATABASE.sql | |||
</pre> | |||
Solution: To resolve this warning and improve security, follow these steps: | |||
# Set up a login path using [https://dev.mysql.com/doc/refman/8.4/en/mysql-config-editor.html mysql_config_editor]: {{kbd | key=<nowiki>mysql_config_editor set --login-path=local --host=localhost --user=root --password</nowiki>}} | |||
# Use the login path when exporting or importing MySQL databases (1) For exporting: {{kbd | key=<nowiki>mysqldump --login-path=local --default-character-set=utf8 DATABASE > DATABASE.sql</nowiki>}} (2) For importing: {{kbd | key=<nowiki>pv DATABASE.sql.gz | gunzip | mysql --login-path=local --default_character_set utf8 --force DATABASE</nowiki>}} | |||
# To view all configured login paths {{kbd | key=<nowiki>mysql_config_editor print --all</nowiki>}} | |||
By using a login path, you avoid exposing your password in the command line, thus resolving the security warning. | |||
=== ERROR 1044 (42000): Access denied for user 'USER'@'localhost' to database 'DATABASE_NAME' === | === ERROR 1044 (42000): Access denied for user 'USER'@'localhost' to database 'DATABASE_NAME' === | ||
| Line 253: | Line 347: | ||
) final | ) final | ||
WHERE final.rank <= 10 | WHERE final.rank <= 10 | ||
</pre> | |||
=== ERROR 1055: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column === | |||
Message | |||
<pre> | |||
Query Error (1055): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.posts.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by | |||
</pre> | |||
Query with Error | |||
<pre> | |||
-- Create sample table | |||
CREATE TABLE posts ( | |||
id INT, | |||
post_id VARCHAR(10), | |||
author VARCHAR(50), | |||
content TEXT, | |||
likes INT, | |||
post_time DATETIME | |||
); | |||
-- Insert sample data | |||
INSERT INTO posts VALUES | |||
(1, 'A123', 'Alice', 'First post', 10, '2024-01-01'), | |||
(2, 'A123', 'Bob', 'Great!', 5, '2024-01-02'), | |||
(3, 'A123', 'Bob', 'Nice!', 3, '2024-01-03'); | |||
-- This will cause an error | |||
SELECT | |||
id, -- Problem: Not in GROUP BY | |||
post_id, | |||
author, | |||
GROUP_CONCAT(content SEPARATOR "\n") AS content, -- This is OK as it uses aggregate function | |||
likes -- Problem: Not in GROUP BY | |||
FROM posts | |||
GROUP BY post_id, author; | |||
</pre> | |||
Solution: Fix the query by either (1) including all columns in GROUP BY or (2) using aggregate functions (like MAX, MIN, SUM, COUNT, GROUP_CONCAT) | |||
<pre> | |||
-- Method 1: disable `sql_mode=only_full_group_by` | |||
``` | |||
There are two ways to disable `sql_mode=only_full_group_by`: | |||
1. Temporary Change (Only affects current session) | |||
```sql | |||
SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY','')); | |||
``` | |||
2. Permanent Change (Modify MySQL configuration file) | |||
- Locate MySQL configuration file (usually `my.cnf` or `my.ini`) | |||
- Add or modify in the `[mysqld]` section: | |||
```ini | |||
[mysqld] | |||
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION | |||
``` | |||
Important Notes: | |||
1. After modifying the configuration file, MySQL service needs to be restarted to take effect | |||
2. Disabling this setting is not recommended because: | |||
- May lead to unpredictable query results | |||
- Violates SQL standards | |||
- May cause compatibility issues in future versions | |||
The recommended approach is to fix the SQL query rather than changing the SQL mode. | |||
``` | |||
-- Method 2: Add all columns to GROUP BY | |||
SELECT | |||
id, | |||
post_id, | |||
author, | |||
content, | |||
likes | |||
FROM posts | |||
GROUP BY post_id, author, id, content, likes; | |||
-- Method 3: Use aggregate functions for non-GROUP BY columns | |||
SELECT | |||
MAX(id) as id, | |||
post_id, | |||
author, | |||
GROUP_CONCAT(content) as contents, | |||
SUM(likes) as total_likes | |||
FROM posts | |||
GROUP BY post_id, author; | |||
</pre> | </pre> | ||
| Line 323: | Line 503: | ||
=== Error: MySQL Incorrect datetime value: '0000-00-00 00:00:00' === | === Error: MySQL Incorrect datetime value: '0000-00-00 00:00:00' === | ||
Workaround solution< | Workaround solution<ref>[https://stackoverflow.com/questions/35565128/mysql-incorrect-datetime-value-0000-00-00-000000 MySQL Incorrect datetime value: '0000-00-00 00:00:00' - Stack Overflow]</ref>: | ||
<pre> | <pre> | ||
SET SQL_MODE='ALLOW_INVALID_DATES'; | SET SQL_MODE='ALLOW_INVALID_DATES'; | ||
</pre> | </pre> | ||
=== Optimization Solutions for Query Performance === | |||
Originally, product_id (VARCHAR, 200) was used as the primary key. When the data volume reached approximately 600,000, data processing became very slow. Adding a field id (int) as the primary key can solve the problem of slow query speed. | |||
== Connection and Network Errors == | == Connection and Network Errors == | ||
| Line 405: | Line 589: | ||
</pre> | </pre> | ||
* If still not work, reboot the server and restart the MySQL service. | * If still not work, reboot the server and restart the MySQL service. | ||
=== SQLSTATE[HY000] [2002] Can't assign requested address === | |||
Error message | |||
<pre> | |||
Caught PDOException: /path/to/script.php ERROR: Cannot connect: SQLSTATE[HY000] [2002] Can't assign requested address | |||
utf8: SQLSTATE[HY000] [2002] Can't assign requested address | |||
Caught PDOException: /path/to/script.php ERROR: Cannot connect: SQLSTATE[HY000] [2002] Operation timed out | |||
utf8: SQLSTATE[HY000] [2002] Operation timed out | |||
</pre> | |||
Walkaround Solution: | |||
* You can extend the interval between each MySQL save operation. | |||
Explanation: | |||
It typically occurs when your application is unable to establish a connection to the specified database server. | |||
=== Resolving "Host 'xx.xx.xxx.xxx' is blocked because of many connection errors" === | |||
Error Message | |||
<pre> | |||
Host 'xx.xx.xxx.xxx' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts' | |||
</pre> | |||
Understanding the Issue | |||
(1) Check the threshold value | |||
MySQL's mechanism works as follows: once the same source host (identified by IP or hostname) accumulates a number of "connection errors" reaching the value configured in `max_connect_errors`, MySQL automatically blocks that source and refuses further connections from it. | |||
<pre> | |||
SHOW VARIABLES LIKE 'max_connect_errors'; | |||
</pre> | |||
The threshold found was 100. | |||
(2) Check which hosts are currently recorded/blocked | |||
<pre> | |||
SELECT IP, HOST, SUM_CONNECT_ERRORS, COUNT_HOST_BLOCKED_ERRORS | |||
FROM performance_schema.host_cache | |||
ORDER BY SUM_CONNECT_ERRORS DESC; | |||
</pre> | |||
Confirmed that the only blocked IP is `xx.xx.xxx.xxx`, with `SUM_CONNECT_ERRORS` exactly matching the threshold of 100, and no other IPs were affected. | |||
Resolving the Issue | |||
⚠️ Note: `flush-hosts` / `FLUSH HOSTS` clears the **entire** host cache, not just a single IP. If the query results show records for other hosts as well, they will all be reset at the same time. For a shared production environment, it's recommended to confirm with other administrators before running this. | |||
(1) In a terminal or command prompt, run: | |||
<pre> | |||
mysqladmin -u root -p flush-hosts | |||
</pre> | |||
or | |||
<pre> | |||
mysql -u root -p | |||
Once inside the MySQL shell, run: FLUSH HOSTS; | |||
</pre> | |||
Note: The `FLUSH HOSTS` syntax has been deprecated since MySQL 8.0.23 and removed in 8.4<ref>[https://dev.mysql.com/doc/refman/8.4/en/performance-schema-host-cache-table.html MySQL :: MySQL 8.4 Reference Manual :: 29.12.22.3 The host_cache Table]</ref><ref>[https://oneuptime.com/blog/post/2026-03-31-mysql-fix-max-connect-errors-blocked-hosts/view How to Fix max_connect_errors and Blocked Hosts in MySQL]</ref>. If your environment is on a newer version, use the officially recommended alternative instead: | |||
<pre> | |||
TRUNCATE TABLE performance_schema.host_cache; | |||
</pre> | |||
(2) Verify after execution | |||
<pre> | |||
SELECT IP, HOST, SUM_CONNECT_ERRORS, COUNT_HOST_BLOCKED_ERRORS | |||
FROM performance_schema.host_cache | |||
ORDER BY SUM_CONNECT_ERRORS DESC; | |||
</pre> | |||
Result: no rows returned (0 rows), indicating the host cache has been cleared — the previous connection error counts and blocked status have been reset, and the IP should now be able to connect normally. | |||
=== Transfer Data from MySQL to MSSQL === | |||
Condition: Need to synchronize or transfer data from MySQL database to Microsoft SQL Server. | |||
Solution: | |||
* (Manually method) Export MySQL data in a format compatible with Microsoft SQL Server: {{kbd | key=<nowiki>mysqldump --compatible=mssql [database_name]</nowiki>}}<ref>[https://dev.mysql.com/doc/refman/8.4/en/mysqldump.html#option_mysqldump_compatible MySQL :: MySQL 8.4 Reference Manual :: 6.5.4 mysqldump — A Database Backup Program]</ref> | |||
* (Automated method) Use [https://learn.microsoft.com/en-us/sql/ssma/sql-server-migration-assistant?view=sql-server-ver17 Microsoft SQL Server Migration Assistant] or ''$'' [https://www.webyog.com/product/sqlyog SQLyog] tool (for {{Win}}). Use query commands to specify the data to be transferred from MS SQL server to MySQL server. | |||
Example mysqldump command | |||
<pre> | |||
mysqldump -h 127.0.0.1 -u root -p \ | |||
--skip-lock-tables \ | |||
--single-transaction \ | |||
--default-character-set=utf8 \ | |||
--compatible=mssql \ | |||
--skip-extended-insert \ | |||
--no-auto-rehash \ | |||
--skip-comments \ | |||
database_name > database_name.sql | |||
</pre> | |||
Related issues: | |||
* For large data volumes, connection timeout issues may occur. In such cases, refer to Error 2013 handling methods by increasing connection timeout settings or processing data in batches. | |||
* The two database systems have differences in data types, so data compatibility should be verified before transfer. | |||
== Resource and Environmental Errors == | == Resource and Environmental Errors == | ||
| Line 443: | Line 731: | ||
=== MySQL server has gone away === | === MySQL server has gone away === | ||
Error conditions: | |||
* Enable the option {{kbd | key = log_error}} in MySQL config file e.g. {{kbd | key = <nowiki>log_error="file_name_of_error_log"</nowiki>}}<ref>[https://dev.mysql.com/doc/refman/8.0/en/error-log.html MySQL :: MySQL 8.0 Reference Manual :: 5.4.2 The Error Log]</ref>. | * Enable the option {{kbd | key = log_error}} in MySQL config file e.g. {{kbd | key = <nowiki>log_error="file_name_of_error_log"</nowiki>}}<ref>[https://dev.mysql.com/doc/refman/8.0/en/error-log.html MySQL :: MySQL 8.0 Reference Manual :: 5.4.2 The Error Log]</ref>. | ||
* Example error log located at {{kbd | key = <nowiki>file_name_of_error_log</nowiki>}} are as following: | * Example error log located at {{kbd | key = <nowiki>file_name_of_error_log</nowiki>}} are as following: | ||
| Line 450: | Line 738: | ||
2019-05-23T08:52:19.989876Z 99980 [Note] Aborted connection 99980 to db: 'DB_NAME' user: 'DB_USER' host: 'localhost' (Got a packet bigger than 'max_allowed_packet' bytes) | 2019-05-23T08:52:19.989876Z 99980 [Note] Aborted connection 99980 to db: 'DB_NAME' user: 'DB_USER' host: 'localhost' (Got a packet bigger than 'max_allowed_packet' bytes) | ||
</pre> | </pre> | ||
Solution: | |||
* Increase the value of {{kbd | key = <nowiki>max_allowed_packet</nowiki>}}<ref>[https://stackoverflow.com/questions/5688403/how-to-check-and-set-max-allowed-packet-mysql-variable php - how to check and set max_allowed_packet mysql variable - Stack Overflow]</ref> if the MySQL user has the {{kbd | key = <nowiki>SUPER</nowiki>}} privilege<ref>[https://dev.mysql.com/doc/refman/8.0/en/privileges-provided.html MySQL :: MySQL 8.0 Reference Manual :: 6.2.2 Privileges Provided by MySQL]</ref>. | * Increase the value of {{kbd | key = <nowiki>max_allowed_packet</nowiki>}}<ref>[https://stackoverflow.com/questions/5688403/how-to-check-and-set-max-allowed-packet-mysql-variable php - how to check and set max_allowed_packet mysql variable - Stack Overflow]</ref> if the MySQL user has the {{kbd | key = <nowiki>SUPER</nowiki>}} privilege<ref>[https://dev.mysql.com/doc/refman/8.0/en/privileges-provided.html MySQL :: MySQL 8.0 Reference Manual :: 6.2.2 Privileges Provided by MySQL]</ref>. | ||
| Line 481: | Line 771: | ||
Validation | Validation | ||
* Query the syntax {{kbd | key=<nowiki>SHOW VARIABLES LIKE 'tmpdir';</nowiki>}} to validate the modification of mysql configuration file. | * Query the syntax {{kbd | key=<nowiki>SHOW VARIABLES LIKE 'tmpdir';</nowiki>}} to validate the modification of mysql configuration file. | ||
=== errno 11 - Resource temporarily unavailable === | |||
Message: '''Can't create a new thread (errno 11 "Resource temporarily unavailable")''' | |||
Solution: [https://duntuk.com/how-raise-ulimit-open-files-and-mysql-openfileslimit How to permanently raise ulimit 'open files' and MySQL 'open_files_limit'] | |||
=== errno 41 - Error dropping database === | === errno 41 - Error dropping database === | ||
| Line 609: | Line 904: | ||
solution: | solution: | ||
* LIMIT the rows of MySQL query | * LIMIT the rows of MySQL query | ||
== Further Reading == | |||
* If you cannot find the answer you're looking for, you can visit [https://stackoverflow.com/questions/tagged/mysql Unanswered 'mysql' Questions - Stack Overflow] | |||
== References == | == References == | ||
<references/> | <references/> | ||
{{Template: | {{Template:Data factory flow}} | ||
[[Category:MySQL]] | [[Category: MySQL]] | ||
[[Category:Database]] | [[Category: Database]] | ||
[[Category:Data Science]] | [[Category: Data Science]] | ||
[[Category: Revised with LLMs]] | |||