Nextcloud

【techblog】Nextcloudの管理用CLI「occ」詳細: データベース管理に関するコマンド

はじめに

NextcloudにはCLI環境としてoccというものがあり、さまざまな設定、管理を行うことができます。 ここではoccで使える様々なコマンド群のうち、データベース管理に関するコマンド群についてまとめてみます。

※occ の使い方については、 【techblog】Nextcloudの管理用CLI「occ」コマンドについての記事を参照ください。
※occ の共通オプションについては、【techblog】Nextcloudの管理用CLI「occ」詳細: Nextcloud自体に対するコマンド群、共通オプション設定の記事を参照ください。

環境について

ここで記載するoccを実行するNextcloudのバージョンは、2019年3月24日現在で最新の15.0.5を利用します。
WebサーバはNginxを利用した環境で実施しております。ApacheをWebサーバとして利用している場合は、sudo -u のユーザIDをapacheに読み替えてください。

データベース管理に関するコマンド群

データベース管理に関するコマンドは以下のようなものがあります。

  • db:add-missing-indices
  • db:convert-filecache-bigint
  • db:convert-mysql-charset
  • db:convert-type

これらのコマンドを実施する前には、Nextcloudをメンテナンスモードにするとともに、念のためデータベースのバックアップも取得をしておきましょう。

db:convert-type

現在動いているNextcloudのデータベースを別データベースにコンバートします。SQLiteで動いているデータベースのコンバートもできます。データベースのコンバートに合わせてNextcloudの定義ファイル(config.php)もコンバート先の設定に変更されます。

使い方

db:convert-type [オプション] <種類> <ユーザ名> <ホスト> <データベース>

種類

コンバート先となるデータベースの種類を指定します。データベースの種類としては以下のものを選択できます。

  • mysql: MySQL / MariaDB
  • pgsql: PostgreSQL
  • oci: Oracle

ユーザ名

コンバート先となるデータベースのユーザ名を指定します。

ホスト

コンバート先となるデータベースのホスト(ホスト名/IPアドレス)を指定します。

データベース

コンバート先となるデータベースのデータベース名を指定します。

オプション

  • --port=PORT
    コンバート先となるデータベースのポート番号を指定します。標準的なポート番号を使う場合は省略できます。
  • --password=PASSWORD
    コンバート先となるデータベースに接続するためのパスワードを指定します。このオプションを省略した場合はコマンド実行時にパスワード入力を求められます。
  • --clear-schema
    コンバート先となるデータベースにある全てのテーブルを削除します。
  • --all-apps
    インストール済みのアプリだけではなく全てのアプリのスキーマを作成します。
  • --chunk-size=CHUNK-SIZE
    一つのクエリで処理するデータベース行の最大数を指定します。これより大きいテーブルはこのサイズのまとまりで処理されます。コンバート中にプロセスがメモリ不足になった場合はこれを下げてください。これを指定しない場合の初期値は1000です。

使用例

①SQLiteで動いているNextcloudデータベースをMySQL(MariaDB)に変換する。

$ sudo -u nginx php /var/www/html/nextcloud/occ db:convert-type mysql nextcloud localhost nextcloud
What is the database password? [パスワード]
Creating schema in new database
oc_accounts
    0 [>---------------------------]oc_activity
 13/13 [============================] 100%oc_activity_mq
    0 [>---------------------------]oc_addressbookchanges
    0 [>---------------------------]oc_addressbooks
 1/1 [============================] 100%oc_appconfig
 106/106 [============================] 100%oc_authtoken
 1/1 [============================] 100%oc_bruteforce_attempts
    0 [>---------------------------]oc_calendar_invitations
    0 [>---------------------------]oc_calendar_resources
    0 [>---------------------------]oc_calendar_rooms
    0 [>---------------------------]oc_calendarchanges
    0 [>---------------------------]oc_calendarobjects
    0 [>---------------------------]oc_calendarobjects_props
    0 [>---------------------------]oc_calendars
 1/1 [============================] 100%oc_calendarsubscriptions
    0 [>---------------------------]oc_cards
    0 [>---------------------------]oc_cards_properties
    0 [>---------------------------]oc_comments
    0 [>---------------------------]oc_comments_read_markers
    0 [>---------------------------]oc_credentials
    0 [>---------------------------]oc_dav_shares
    0 [>---------------------------]oc_directlink
    0 [>---------------------------]oc_federated_reshares
    0 [>---------------------------]oc_file_locks
    0 [>---------------------------]oc_filecache
 137/137 [============================] 100%oc_files_trash
    0 [>---------------------------]oc_flow_checks
    0 [>---------------------------]oc_flow_operations
    0 [>---------------------------]oc_group_admin
    0 [>---------------------------]oc_group_user
 1/1 [============================] 100%oc_groups
 1/1 [============================] 100%oc_jobs
 22/22 [============================] 100%oc_migrations
Skipping migrations table because it was already filled by running the migrations
oc_mimetypes
 18/18 [============================] 100%oc_mounts
 1/1 [============================] 100%oc_notifications
    0 [>---------------------------]oc_notifications_pushtokens
    0 [>---------------------------]oc_oauth2_access_tokens
    0 [>---------------------------]oc_oauth2_clients
    0 [>---------------------------]oc_preferences
 3/3 [============================] 100%oc_properties
    0 [>---------------------------]oc_schedulingobjects
    0 [>---------------------------]oc_share
    0 [>---------------------------]oc_share_external
    0 [>---------------------------]oc_storages
 2/2 [============================] 100%oc_systemtag
    0 [>---------------------------]oc_systemtag_group
    0 [>---------------------------]oc_systemtag_object_mapping
    0 [>---------------------------]oc_trusted_servers
    0 [>---------------------------]oc_twofactor_backupcodes
    0 [>---------------------------]oc_twofactor_providers
 1/1 [============================] 100%oc_users
 1/1 [============================] 100%oc_vcategory
    0 [>---------------------------]oc_vcategory_to_object
    0 [>---------------------------]oc_whats_new
    0 [>---------------------------]

db:add-missing-indices

パフォーマンスを向上させるなどの理由で既存のデータベーステーブルに新しいインデックスを追加することがあります。このコマンドでは、不足しているインデックスをチェックしインデックスを追加します。

使い方

db:add-missing-indices

使用例

①不足しているインデックスをチェックしインデックスを追加する(出力例は不足しているインデックスが何もない場合です)。

$ sudo -u nginx php /var/www/html/nextcloud/occ db:add-missing-indices
Check indices of the share table.
Done.

db:convert-filecache-bigint

NextcloudのファイルキャッシュのカラムをBigInt型に変換します。登録されているレコード数が多い場合には時間がかかりますので、コマンドを実行する前にWebサーバを停止させるかNextcloudをメンテナンスモードにすることがおすすめです。Nextcloudの管理画面(「概要」メニューの「セキュリティ&セットアップ警告」のメッセージ)で以下のような警告が表示された場合に実行します。
※最新のNextcloudの初期インストール状態でも表示されます。

  • Some columns in the database are missing a conversion to big int. Due to the fact that changing column types on big tables could take some time they were not changed automatically. By running ‘occ db:convert-filecache-bigint’ those pending changes could be applied manually. This operation needs to be made while the instance is offline. For further details read the documentation page about this.
    • filecache.mtime
    • filecache.storage_mtime

使い方

db:convert-filecache-bigint

使用例

①NextcloudのファイルキャッシュのカラムをBigInt型に変換する

$ sudo -u nginx php /var/www/html/nextcloud/occ db:convert-filecache-bigint
Following columns will be updated:

* filecache.mtime
* filecache.storage_mtime

This can take up to hours, depending on the number of files in your instance!
Continue with the conversion (y/n)? [n] y

db:convert-mysql-charset

MySQL/MariaDBのキャラクターセットでutf8mb4を使用するようにコンバートを行います。

使い方

db:convert-mysql-charset

使用例

①MySQL/MariaDBのキャラクターセットでutf8mb4を使用するようにコンバートを行う(出力例はすべてコンバート済みで何も行わない場合です)。

$ sudo -u nginx php /var/www/html/nextcloud/occ db:convert-mysql-charset   
All tables already have the correct collation -> nothing to do