遷移 NextCloud SQLite 資料庫到 MariaDB 並開啓 4-byte 支援

最開始起 NextCloud 的時候,淺羽圖方便用了 SQLite 做資料庫。隨着使用時間和帳號的增加,SQLite 的效率漸漸難看,於是決定遷移到 MariaDB。

參考官方手冊的相關章節,NextCloud 可以自動完成資料庫的遷移。只需要在容器中設定好資料庫,然後運行:

$ sudo docker-compose exec --user www-data nextcloud php occ db:convert-type --all-apps --clear-schema mysql <username> <hostname> <database> --password="$DB_PASSWORD"

就可以了。然而實際情況總是比預想的要難,運行之後,出現了錯誤:

[Doctrine\DBAL\Exception\DriverException] 
An exception occurred while executing 'INSERT INTO `oc_comments` (`id`, `ch 
ildren_count`, `actor_type`, `actor_id`, `message`, `verb`, `creation_times 
tamp`, `latest_child_timestamp`, `object_type`, `object_id`, `parent_id`, ` 
topmost_parent_id`) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params 
["8", "0", "users", "asaba", "\ud83d\ude05Emoji \u597d\u4e86", "comment", 
"2018-06-15 04:36:09", null, "chat", "2", "0", "0"]: 
SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect string value: '\xF 
0\x9F\x98\x85Em...' for column 'message' at row 1 

[Doctrine\DBAL\Driver\PDOException] 
SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect string value: '\xF 
0\x9F\x98\x85Em...' for column 'message' at row 1 

[PDOException] 
SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect string value: '\xF 
0\x9F\x98\x85Em...' for column 'message' at row 1

仔細看,認爲應該是字符編碼的問題。官方文檔中有提到:

In order to use Emojis (textbased smilies) on your Nextcloud server with a MySQL database, the installation needs to be tweaked a bit.

因此,需要先開啓 4-byte 支援再去轉換資料庫。知道了原因,解決起來就很簡單了。首先,讓 MariaDB 的實例帶上參數啓動以支援 4-byte:

--character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

然後設定 NextCloud 使用 MySQL 4-byte:

$ sudo docker-compose exec --user www-data nextcloud php occ config:system:set mysql.utf8mb4 --type boolean --value="true"

最後執行轉換,就可以正常遷移到 MariaDB 了。轉換之後會自動退出維護模式並且改好資料庫設定,data/owncloud.db 也可以刪除掉了。

Comments

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

To respond on your own website, enter the URL of your response which should contain a link to this post’s permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post’s URL again. (Find out more about Webmentions.)