【LWC】MySQL(MariaDB)連携

記事内に広告が含まれています。

CentOS7に構築された「spigot」で、プラグインである「LWC」を「MariaDB」と連携させる手順です

MariaDBのインストール・設定

MariaDBインストール

まずは「MariaDB」をインストールします。

(CentOS7からは、データベースがMySQLの互換DBであるMariaDBに変更されています。)

# yum install mariadb mariadb-server

DB起動

「yum」でのインストールが完了しましたら、「mariadb」を起動します。

# systemctl start mariadb

自動起動設定

「MariaDB」の自動起動設定も忘れずに行いましょう。

# systemctl enable mariadb
ln -s '/usr/lib/systemd/system/mariadb.service' '/etc/systemd/system/multi-user.target.wants/mariadb.service'

MariaDB初期設定

MariaDBは「mysql_secure_installation」を実行することで、データベースのrootユーザパスワードを設定できたり、anonymousユーザを削除したりといった、最低限行っておきたい初期設定を行うことができます。

# mysql_secure_installation

設定項目

「mysql_secure_installation」で設定する項目です。

設定項目 設定内容
Enter current password for root (enter for none): MariaDBのrootユーザパスワード入力
インストール直後は何も設定されていないので「Enter」キーを押下する
Set root password? [Y/n] rootユーザのパスワードを設定するかどうかの確認
「y」を入力するかそのまま「Enter」キーを押下しパスワードを設定する
Remove anonymous users? [Y/n] 初期設定で作成されている「anonymous」ユーザを削除するかの確認
「y」を入力するかそのまま「Enter」キーを押下し削除する
Disallow root login remotely? [Y/n] MariaDBの「root」ユーザのリモートログインを禁止するかの確認
「y」を入力するかそのまま「Enter」キーを押下し禁止する
Remove test database and access to it? [Y/n] 初期設定で作成されている「test」データベースを削除するかの確認
「y」を入力するかそのまま「Enter」キーを押下し削除する
Reload privilege tables now? [Y/n] 今設定したユーザ設定をすぐに反映させるかの確認
「y」を入力するかそのまま「Enter」キーを押下し反映させる

mysql_secure_installation実行ログ

# mysql_secure_installation
/bin/mysql_secure_installation: 行 379: find_mysql_client: コマンドが見つかりま せん

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

DB作成

LWCで使用するデータベースを作成します。

MariaDBへ接続

データベースを作成するために、MariaDBのrootユーザでMariaDBに接続します。

# mysql -u root -p
Enter password:   <---DBの初期設定で決めたrootユーザのパスワードを入力

LWC用DB作成

LWCで使用するためのデータベースを作成します。

DB作成書式
create database [データベース名];

今回は「spilwc」という名前のデータベースを作成します。

※データベースの名前に特に決まりはないので、お好き名前のデータベースを作成してください。

DB作成例
MariaDB [(none)]> create database spilwc ;
Query OK, 1 row affected (0.00 sec)

無事作成されると、「Query OK, 1 row affected」というメッセージが表示されます。

LWC用DB管理ユーザ作成

データベースの作成が完了しましたら、次にLWC用のデータベース管理ユーザを作成します。

ユーザ作成書式
grant all privileges on [データベース].* to “[管理ユーザ名]"@"localhost" identified by “[パスワード]";

今回は「spilwc」というデータベースの管理ユーザを「lwcadmin」という名前で、パスワードを「lwcpass」として作成しました。

管理ユーザ作成例
MariaDB [(none)]> grant all privileges on spilwc.* to "lwcadmin"@"localhost" identified by "lwcpass";
Query OK, 0 rows affected (0.00 sec)

ユーザ情報の反映

「flush privileges」コマンドで、作成したユーザの情報を反映させます。

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

作業が終わりましたら、「quit」コマンドを実行してデータベースへから切断します。

MariaDB [(none)]> quit

以上で、データベース側の設定は完了です。

LWC設定

LWCとデータベースの連携は、「core.yml」ファイルで設定します。
「core.yml」ファイルは、LWCをインストールした「plugins」フォルダ内にある「LWC」フォルダの中にあります。

ここからの作業は、core.ymlファイルの所有者ユーザ(spigotの実行ユーザ)で行います。

core.yml設定変更

「core.yml」内の「database」セクションにMySQL(MySQL)との連携を設定する箇所がありますので、そこを編集していきます。

$ cp -p core.yml core.yml_org
$ vi core.yml

変更部分

変更の必要がある部分は下記のとおりです。

設定項目 初期値 変更後の設定
adapter sqlite mysqlへ変更
database lwc LWC用に作成したDB名に変更
sername ‘’ LWC用DBの管理ユーザ名
password ‘’ LWC用DB管理ユーザパスワード
prefix lwc_ 変更したい場合のみ
設定変更後
database:
    adapter: mysql
    path: plugins/LWC/lwc.db
    host: localhost
    database: spilwc
    username: 'lwcadmin'
    password: 'lwcpass'
    prefix: lwc_
    ping_interval: 300

※今回の手順の場合「path」「host」「prefix」「ping_interval」については、設定変更の必要はありません。

設定反映

「core.yml」の編集が終わりましたら、設定を反映させるために「spigot」を再起動します。

動作確認

「spigot」の再起動が完了しましたら、動作確認を行なっていきます。

ログの確認

「spigot」の起動ログを確認すると、「Connecting to MySQL」といったメッセージが表示されて、データベースに接続に行っていることが確認できます。

[08:42:23 INFO]: [LWC] Connecting to MySQL

正常に接続できた場合は、データベースへの接続関連のメッセージはこれぐらいなのですが、接続に失敗した際はその旨のメッセージが表示されます。

データベースへの接続失敗メッセージ

[08:45:09 INFO]: [LWC] Loading LWC v4.5.0-SNAPSHOT
[08:45:09 INFO]: [WorldGuard] Loading WorldGuard v6.1
[08:45:09 INFO]: [LWC] Enabling LWC v4.5.0-SNAPSHOT
[08:45:09 INFO]: [LWC] Connecting to MySQL
[08:45:13 INFO]: [LWC] Failed to connect to MySQL: 0 - Could not create connection to database server. Attempted reconnect 3 times. Giving up.
[08:45:13 INFO]: [LWC] Connection failure cause: Access denied for user 'lwcadmin'@'localhost' (using password: YES)
[08:45:13 INFO]: [LWC] Disabling LWC v4.5.0-SNAPSHOT
[08:45:13 INFO]: [LWC] Flushing protection updates (0)

「Failed to connect to MySQL」といったようなメッセージが表示されて、データベースへの接続が出来なかったことが分かります。

この場合は「core.yml」で設定した、下記の項目に間違いが無いか確認してみてください。

  • データベース名
  • データベース管理ユーザ名
  • データベース管理ユーザのパスワード

ゲーム内での確認

Minecraftのクライアントからサーバにログインして、「LWC」のコマンドを実行して正常に動作するか確認してください。

DB確認

次に、DBを確認してみます。

LWCのデータベース管理ユーザで「MariaDB」に接続し、データベースの中身を確認してみます。

※何も保護していない場合は当然ながらデータが無いので、チェストなどに保護設定を掛けてから確認してください。

MariaDBへの接続

まずは「MariaDB」に接続します。

DB接続書式
mysql -u [LWCデータベース管理ユーザ] -p
MariaDBへの接続例

LWC用のデータベースの管理ユーザで「MariaDB」に接続しています。

$ mysql -u lwcadmin -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.47-MariaDB MariaDB Server

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement

テーブルデータ確認

作成したDBにデータが記録されているか確認ですが、今回は「lwc_protections」というテーブルの中身を確認してみます。

ちなみに「lwc_protections」には、ブロックの保護情報に関するデータが記録されています。

データ確認書式
select * from [LWC用データベース名].lwc_protections ;
データ確認例

下の実行例は、LWC用のDBとして作成した「spilwc」の「lwc_protecitons」というテーブルの中身を表示させています。

MariaDB [(none)]> select * from spilwc.lwc_protections ;
+----+--------------------------------------+------+------+------+------+--------------------------+---------+-------+----------+-------------------------+---------------+--------+
| id | owner                                | type | x    | y    | z    | data                     | blockId | world | password | date                    | last_accessed | rights |
+----+--------------------------------------+------+------+------+------+--------------------------+---------+-------+----------+-------------------------+---------------+--------+
|  1 | ad6bb107-d45a-43fb-b0fa-64d91ef48ef3 |    2 |  -16 |   64 |  169 | {"flags":[],"rights":[]} |      61 | world |          | 2016-03-08 04:31:42.115 |    1457379106 | NULL   |
|  2 | ad6bb107-d45a-43fb-b0fa-64d91ef48ef3 |    2 |  -16 |   64 |  171 | {"rights":[],"flags":[]} |      54 | world |          | 2016-05-05 23:22:28.165 |    1462458157 | NULL   |
+----+--------------------------------------+------+------+------+------+--------------------------+---------+-------+----------+-------------------------+---------------+--------+
2 rows in set (0.00 sec)

正常に「LWC」と「MariaDB」の連携されると、上記のようにデータベースにデータが書き込まれていきます。

これで作業は完了となります。

おつかれさまでした。

データベース変更時の注意点

「LWC」のデータベースを初期設定の「SQLite」から「MariaDB(MySQL)」へ変更を行う場合は、「SQLite」から「MariaDB(MySQL)」へ自動的にデータが移行されますが、その逆の場合は自動的にデータの移行は行われずに、それまでの保護情報は消えてしまいますので、データベースの変更する場合は注意してください。

  • SQLiteからMariaDB(MySQL)へDB変更 自動的にデータ移行される
  • MariaDB(MySQL)からSQLiteへDB変更 データ移行されない

どうしてもMariaDB(MySQL)から、SQLiteにDBを変更したい場合は手動でデータの移行を行う必要があります。

SQLiteからMariaDB(MySQL)へ変更

実際にデータベースを変更して確認してみました。

SQLiteのデータ確認

事前にいくつかのブロックに保護設定を掛けた状態で、「SQLite」で作成されたデータベースの「lwc_protections」テーブルのスキーマ情報と、その中のデータを見てみました。

lwc_protectionsテーブルスキーマ確認
$ sqlite3 ./lwc.db
sqlite> .schema lwc_protections
CREATE TABLE lwc_protections ( id INTEGER PRIMARY KEY , owner VARCHAR(255) , type INTEGER , x INTEGER , y INTEGER , z INTEGER , flags INTEGER , data TEXT , blockId INTEGER , world VARCHAR(255) , password VARCHAR(255) , date VARCHAR(255) , last_accessed INTEGER  );
CREATE INDEX protections_main ON lwc_protections (x, y, z, world);
CREATE INDEX protections_utility ON lwc_protections (owner);
CREATE INDEX protections_type ON lwc_protections (type);

lwc_protectionsテーブルデータ確認

sqlite> select * from lwc_protections;
1|ad6bb107-d45a-43fb-b0fa-64d91ef48ef3|2|-16|64|171||{"rights":[],"flags":[]}|54|world||2016-05-09 04:42:13.162|1462736826
2|ad6bb107-d45a-43fb-b0fa-64d91ef48ef3|0|-14|65|175||{"rights":[],"flags":[{"id":3}]}|196|world||2016-05-09 04:43:54.998|1462736821
3|ad6bb107-d45a-43fb-b0fa-64d91ef48ef3|0|-15|65|175||{"rights":[],"flags":[]}|196|world||2016-05-09 04:44:12.454|1462736822
4|ad6bb107-d45a-43fb-b0fa-64d91ef48ef3|1|-16|64|169|||61|world|a94a8fe5ccb19ba61c4c0873d391e987982fbbd3|2016-05-09 04:44:44.36|1462736684

4ブロック分の保護情報についてのデータが格納されていることが確認できました。

データベースをSQLiteからMariaDB(MySQL)変更

ここで「LWC」のデータベースを「MariaDB(MySQL)」へ変更し、MariaDBのデータベースを確認してみます。

MariaDB(MySQL)のデータ確認

データベース変更後に「MariaDB」で作成されたデータベースを確認してみたところ、SQLiteからデータが引き継がれている事が確認できました。

MariaDB [spilwc]> select * from lwc_protections;
+----+--------------------------------------+------+------+------+------+----------------------------------+---------+-------+------------------------------------------+-------------------------+---------------+--------+
| id | owner                                | type | x    | y    | z    | data                             | blockId | world | password                                 | date                    | last_accessed | rights |
+----+--------------------------------------+------+------+------+------+----------------------------------+---------+-------+------------------------------------------+-------------------------+---------------+--------+
|  1 | ad6bb107-d45a-43fb-b0fa-64d91ef48ef3 |    2 |  -16 |   64 |  171 | {"flags":[],"rights":[]}         |      54 | world |                                          | 2016-05-09 04:42:13.162 |    1462799311 | NULL   |
|  2 | ad6bb107-d45a-43fb-b0fa-64d91ef48ef3 |    0 |  -14 |   65 |  175 | {"flags":[{"id":3}],"rights":[]} |     196 | world |                                          | 2016-05-09 04:43:54.998 |    1462736821 | NULL   |
|  3 | ad6bb107-d45a-43fb-b0fa-64d91ef48ef3 |    0 |  -15 |   65 |  175 | {"flags":[],"rights":[]}         |     196 | world |                                          | 2016-05-09 04:44:12.454 |    1462736822 | NULL   |
|  4 | ad6bb107-d45a-43fb-b0fa-64d91ef48ef3 |    1 |  -16 |   64 |  169 | {"flags":[],"rights":[]}         |      61 | world | a94a8fe5ccb19ba61c4c0873d391e987982fbbd3 | 2016-05-09 04:44:44.36  |    1462736684 | NULL   |
+----+--------------------------------------+------+------+------+------+----------------------------------+---------+-------+------------------------------------------+-------------------------+---------------+--------+
4 rows in set (0.00 sec)

MariaDB(MySQL)からSQLiteへ変更

次にMariaDB(MySQL)からSQLiteへデータベースを変更して、データが引き継がれるかどうかの確認を行ってみます。

MariaDB(MySQL)からSQLiteへデータベースを変更するにあたり、違いが分かるように保護されていたブロックを一個だけ破壊しておきました。
「id」が1であったブロックを破壊したので、MariaDBのデータベース上からデータがなくなっていることが確認できます。

MariaDB [spilwc]> select * from lwc_protections;
+----+--------------------------------------+------+------+------+------+----------------------------------+---------+-------+------------------------------------------+-------------------------+---------------+--------+
| id | owner                                | type | x    | y    | z    | data                             | blockId | world | password                                 | date                    | last_accessed | rights |
+----+--------------------------------------+------+------+------+------+----------------------------------+---------+-------+------------------------------------------+-------------------------+---------------+--------+
|  2 | ad6bb107-d45a-43fb-b0fa-64d91ef48ef3 |    0 |  -14 |   65 |  175 | {"flags":[{"id":3}],"rights":[]} |     196 | world |                                          | 2016-05-09 04:43:54.998 |    1462736821 | NULL   |
|  3 | ad6bb107-d45a-43fb-b0fa-64d91ef48ef3 |    0 |  -15 |   65 |  175 | {"flags":[],"rights":[]}         |     196 | world |                                          | 2016-05-09 04:44:12.454 |    1462736822 | NULL   |
|  4 | ad6bb107-d45a-43fb-b0fa-64d91ef48ef3 |    1 |  -16 |   64 |  169 | {"flags":[],"rights":[]}         |      61 | world | a94a8fe5ccb19ba61c4c0873d391e987982fbbd3 | 2016-05-09 04:44:44.36  |    1462736684 | NULL   |
+----+--------------------------------------+------+------+------+------+----------------------------------+---------+-------+------------------------------------------+-------------------------+---------------+--------+
3 rows in set (0.00 sec)

データベースをMariaDB(MySQL)からSQLite変更

ここで「LWC」のデータベースを「SQLite」へ変更データベースを確認してみます。

$ sqlite3 ./lwc.db
SQLite version 3.7.17 2013-05-20 00:56:22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"

テーブルのスキーマ情報は表示されました。

sqlite> .schema lwc_protections
CREATE TABLE lwc_protections ( id INTEGER PRIMARY KEY , owner VARCHAR(255) , type INTEGER , x INTEGER , y INTEGER , z INTEGER , flags INTEGER , data TEXT , blockId INTEGER , world VARCHAR(255) , password VARCHAR(255) , date VARCHAR(255) , last_accessed INTEGER  );
CREATE INDEX protections_main ON lwc_protections (x, y, z, world);
CREATE INDEX protections_utility ON lwc_protections (owner);
CREATE INDEX protections_type ON lwc_protections (type);

次に、テーブルのデータを表示されてみましたが、何も表示されず「MariaDB」からはデータが移行されていないことが確認できました。

※ゲーム内でも保護設定が消えていました。

sqlite> select * from lwc_protections;

手作業でデータを移行すれば引き継ぎできるみたいだけど、それはまた今度時間があればためしてみます…

コメント

  1. 初心者 より:

    初めまして
    現在SQLの導入を行っていますが、
    自動起動設定の所にて
    ln -s ‘/usr/lib/systemd/system/mariadb.service’ ‘/etc/systemd/system/multi-user.target.wants/mariadb.service’
    を実行した所
    ln: シンボリックリンク `/etc/systemd/system/multi-user.target.wants/mariadb.service’ の作成に失敗しました: ファイルが存在します
    と表示されますがそのまま進めてても大丈夫でしょうか?
    systemctl enable mariadbは実行できました。

    • tamohiko より:

      初心者さん

      はじめまして!
      ご質問ありがとうございます。

      > ln: シンボリックリンク `/etc/systemd/system/multi-user.target.wants/mariadb.service’ の作成に失敗しました: ファイルが存在します
      と表示されますがそのまま進めてても大丈夫でしょうか?

      ファイルが存在するということなので、すでに自動起動の設定が行われているんだと思います。
      MariaDBの自動起動設定状況の確認は下記のコマンドで確認できます。

      # systemctl is-enabled mariadb
      enabled

      コマンドの実行結果が上記のように「enabled」と表示されれば、自動起動の設定がされているので問題はありません。

      以上、よろしくお願いします。

タイトルとURLをコピーしました