排除 MySQL 技術疑難問題: Difference between revisions

Jump to navigation Jump to search
 
(11 intermediate revisions by the same user not shown)
Line 59: Line 59:
* 檢查 mysql 數據文件夾所在的磁碟空間是否足夠。例如,在 {{Linux}} 上輸入 {{kbd | key=df -h}}
* 檢查 mysql 數據文件夾所在的磁碟空間是否足夠。例如,在 {{Linux}} 上輸入 {{kbd | key=df -h}}
* 更多資訊請見 [http://stackoverflow.com/questions/18719748/error-1006-hy000-cant-create-database-errno-13-mysql-5-6-12 ERROR 1006 (HY000) 無法創建資料庫 (errno: 13) MySQL 5.6.12 - Stack Overflow]。
* 更多資訊請見 [http://stackoverflow.com/questions/18719748/error-1006-hy000-cant-create-database-errno-13-mysql-5-6-12 ERROR 1006 (HY000) 無法創建資料庫 (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>
解決方式
* 手動刪除資料庫目錄中殘留的非 MySQL 檔案(例如 .sql 檔案),再重新執行 DROP DATABASE;或直接手動刪除整個資料夾。
根本原因
* MySQL 的 DROP DATABASE 只會移除它所管理的檔案。資料目錄中若存在其他外來檔案(例如 .sql 備份檔),將導致 rmdir 無法移除資料夾,進而觸發 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 282: Line 291:
相關問題:"錯誤!: SQLSTATE[HY000]: 一般錯誤:1290 MySQL 伺服器正在使用 --secure-file-priv 選項運行,所以它不能執行這個聲明"<ref>[https://stackoverflow.com/questions/32737478/how-should-i-tackle-secure-file-priv-in-mysql 資料庫 - 如何處理 MySQL 中的 --secure-file-priv? - Stack Overflow]</ref>
相關問題:"錯誤!: SQLSTATE[HY000]: 一般錯誤:1290 MySQL 伺服器正在使用 --secure-file-priv 選項運行,所以它不能執行這個聲明"<ref>[https://stackoverflow.com/questions/32737478/how-should-i-tackle-secure-file-priv-in-mysql 資料庫 - 如何處理 MySQL 中的 --secure-file-priv? - Stack Overflow]</ref>


== SQL 查詢錯誤 ==
== SQL 查詢語法錯誤 ==
=== 捕獲異常:SQLSTATE[HY093]:無效的參數編號:參數未定義 ===
=== 捕獲異常:SQLSTATE[HY093]:無效的參數編號:參數未定義 ===
解決方案
解決方案


* 問號的數量與查詢值的數量不一致 <ref>[https://stackoverflow.com/questions/10966251/sqlstatehy093-invalid-parameter-number-parameter-was-not-defined php Invalid parameter number: parameter was not defined - Stack Overflow]</ref>
* 問號的數量與查詢值的數量不一致 <ref>[https://stackoverflow.com/questions/10966251/sqlstatehy093-invalid-parameter-number-parameter-was-not-defined php Invalid parameter number: parameter was not defined - Stack Overflow]</ref>
Line 311: Line 319:
$ file plain_text.sql
$ file plain_text.sql
plain_text.sql:UTF-8 Unicode 文本,帶有非常長的行
plain_text.sql:UTF-8 Unicode 文本,帶有非常長的行
</pre>
=== 錯誤 1055: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ===
訊息
<pre>
查詢發生錯誤 (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>
遇到錯誤的查詢
<pre>
-- 建立範例資料表
CREATE TABLE posts (
    id INT,
    post_id VARCHAR(10),
    author VARCHAR(50),
    content TEXT,
    likes INT,
    post_time DATETIME
);
-- 插入範例資料
INSERT INTO posts VALUES
(1, 'A123', 'Alice', '第一篇文章', 10, '2024-01-01'),
(2, 'A123', 'Bob', '推推', 5, '2024-01-02'),
(3, 'A123', 'Bob', '讚讚', 3, '2024-01-03');
-- 這會報錯
SELECT
    id,          -- 問題: 沒有在 GROUP BY 中
    post_id,
    author,
    GROUP_CONCAT(content SEPARATOR "\n") AS content,    -- 問題: 沒有在 GROUP BY 中
    likes        -- 問題: 沒有在 GROUP BY 中
FROM posts
GROUP BY post_id, author;
</pre>
解決方案:修正後的查詢 (1) SELECT 中的每個欄位要麼放在 GROUP BY 中、或 (2) 使用彙總函數 (如 MAX, MIN, SUM, COUNT, GROUP_CONCAT)
<pre>
-- 方法一:取消 `sql_mode=only_full_group_by`:
有兩種方法可以取消 `sql_mode=only_full_group_by`:
1. 暫時性修改 (僅對當前會話有效)
```sql
SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
```
2. 永久性修改 (修改 MySQL 設定檔)
- 找到 MySQL 設定檔 (通常是 `my.cnf` 或 `my.ini`)
- 在 `[mysqld]` 區段加入或修改:
```ini
[mysqld]
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
```
注意事項:
1. 修改設定檔後需要重啟 MySQL 服務才會生效
2. 不建議取消這個設定,因為:
  - 可能導致查詢結果不可預測
  - 違反 SQL 標準
  - 可能在未來版本中產生相容性問題
建議的做法是修正 SQL 查詢而不是更改 SQL mode。
-- 方法二: 把所有欄位加入 GROUP BY
SELECT
    id,
    post_id,
    author,
    content,
    likes
FROM posts
GROUP BY post_id, author, id, content, likes;
-- 方法三: 對非 GROUP BY 的欄位使用彙總函數
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 355: Line 446:
* [https://www.eversql.com/sql-syntax-check-validator/ SQL 語法檢查在線上,SQL 驗證器,即時 SQL 編譯器在線上 - EverSQL] {{exclaim}} 不支持 [https://www.php.net/manual/en/pdo.prepare.php PHP: PDO::prepare],其中查詢語法包含問號。
* [https://www.eversql.com/sql-syntax-check-validator/ SQL 語法檢查在線上,SQL 驗證器,即時 SQL 編譯器在線上 - EverSQL] {{exclaim}} 不支持 [https://www.php.net/manual/en/pdo.prepare.php PHP: PDO::prepare],其中查詢語法包含問號。


== 連接和網絡錯誤 ==
== 連接和網路錯誤 ==
=== MySQL 伺服器已斷開 (has gone away) ===
=== MySQL 伺服器已斷開 (has gone away) ===
步驟
步驟
Line 441: Line 532:
INSERT IGNORE INTO `target`.`table` SELECT * FROM `source`.`table` LIMIT 0, 10000;
INSERT IGNORE INTO `target`.`table` SELECT * FROM `source`.`table` LIMIT 0, 10000;
</pre>
</pre>
=== 從 MySQL 資料轉移至 MSSQL ===
情況: 需要將 MySQL 資料庫中的資料同步或遷移至 Microsoft SQL Server。
解決方案:
* (手動方式) 匯出 MySQL 資料為 MSSQL 相容格式: {{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>
* (自動方式) 使用 [https://learn.microsoft.com/en-us/sql/ssma/sql-server-migration-assistant?view=sql-server-ver17 Microsoft SQL Server Migration Assistant]、''$'' [https://www.webyog.com/product/sqlyog SQLyog] 工具 (適用於 {{Win}})。設定特定查詢來控制要從 MySQL 轉移至 Microsoft SQL Server 的資料。
常見挑戰:
* 大型資料集的連線逾時問題: 參考 2013 錯誤的故障排除方法,例如延長逾時參數或實施批次處理。
* 資料類型不相容: 在遷移前驗證兩系統間的類型對應,以防止資料損壞或遺失。


== 資源和環境錯誤 ==
== 資源和環境錯誤 ==
Line 628: Line 730:
<references/>
<references/>


{{Template:Troubleshooting}}
{{Template: Data factory flow in Mandarin}}


[[Category:MySQL]]
[[Category: MySQL]]
[[Category:Database]]
[[Category: Database]]
[[Category:Data Science]]
[[Category: Data Science]]
[[Category: Revised with LLMs]]

Navigation menu