LoginSignup
9
8

More than 5 years have passed since last update.

mariaDB10.0.21でmroongaを使ってみる

Posted at

待ってた、Mroongaのプリインストール

受託屋さんとしては本当に心待ちにしておりました。
10.0からもう入ってたらしいですね。
インストール作業でいつも何かバージョンが合わずにハゲそうになった日々。
それももう遠い昔。今後はもうこのバージョン以降しか使わない。そう決めました。

環境はRedhat系、yumが使えるやつです。
この記事はAmazonLinuxで作業したログです。

早速使ってみる

mariaDBのインストール

rpmでmariadbをインストールしましょう。
リポジトリ追加した後リポジトリのファイルを作成します。
本家の手順はこちら→ https://mariadb.com/kb/ja/yum/

sudo rpm --import https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
vi /etc/yum.repos.d/mariadb.repo

.repoファイルの中身

[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

ではインストール。

yum install openssl-libs
yum install MariaDB-devel MariaDB-client MariaDB-server
/etc/init.d/mysql start
mysql -u root

つながったかな?

では、次はMroonga編。

Mroongaプラグインを有効にする

MroongaはMariaDBをインストールしただけでは使えません。
プラグインを有効化する必要があります。

INSTALL PLUGIN Mroonga SONAME 'ha_mroonga.so';
CREATE FUNCTION last_insert_grn_id RETURNS INTEGER SONAME 'ha_mroonga.so';
CREATE FUNCTION mroonga_snippet RETURNS STRING SONAME 'ha_mroonga.so';
CREATE FUNCTION mroonga_command RETURNS STRING SONAME 'ha_mroonga.so';
CREATE FUNCTION mroonga_escape RETURNS STRING SONAME 'ha_mroonga.so';

すると、mroongaが有効になっているでしょう。

show egines;
+--------------------+---------+----------------------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                                    | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------------------+--------------+------+------------+
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables                  | NO           | NO   | NO         |
| CSV                | YES     | CSV storage engine                                                         | NO           | NO   | NO         |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                                         | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears)             | NO           | NO   | NO         |
| MyISAM             | YES     | MyISAM storage engine                                                      | NO           | NO   | NO         |
| MRG_MyISAM         | YES     | Collection of identical MyISAM tables                                      | NO           | NO   | NO         |
| Mroonga            | YES     | CJK-ready fulltext search, column store                                    | NO           | NO   | NO         |
| FEDERATED          | YES     | FederatedX pluggable storage engine                                        | YES          | NO   | YES        |
| InnoDB             | DEFAULT | Percona-XtraDB, Supports transactions, row-level locking, and foreign keys | YES          | YES  | YES        |
| Aria               | YES     | Crash-safe tables with MyISAM heritage                                     | NO           | NO   | NO         |
| ARCHIVE            | YES     | Archive storage engine                                                     | NO           | NO   | NO         |
+--------------------+---------+----------------------------------------------------------------------------+--------------+------+------------+
11 rows in set (0.00 sec)

my.cnfの設定

mroongaは、デフォルトだと3文字以上の検索しかできません。
英語圏ではそれで良いのでしょうが、じゃぱにーずな我々には致命的です。
なのでmy.cnfに1文字から検索できるように設定を書き込みましょう。

vi /etc/my.cnf.d/server.cnf

[mysqld]
innodb_ft_min_token_size=1   -- これが大事。

-- これより下はいつも設定している値。参考にどうぞ
innodb_buffer_pool_size=1024M
innodb_log_file_size=1G

server-id=100
character-set-server=utf8
collation-server=utf8_general_ci
max_connect_errors=10000
max-connections=500

設定変えたので再起動。

/etc/init.d/mysql restart

テーブルを作る

以下は、ラッパーモード(元のストレージエンジンは InnoDB)でテーブルを作成する場合で、
search_key に全文検索インデックスを張る例(トークナイザはデフォルトの “TokenBigram")

create table test (
 id int,
 `search_key` longtext COLLATE utf8_unicode_ci NOT NULL,
 PRIMARY KEY (`id`),
 FULLTEXT INDEX job_search_key (search_key) 
) ENGINE=Mroonga  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci 
  COMMENT = 'engine "InnoDB"';

insert into test values 
(1, '隣の客はよく柿食う客だ'), (2, '後ろの客はよく柿食う客だ'),
(3, '隣の客はよく西瓜食う客だ'), (4, 'すもももももももものうち');

じゃあ色々検索してみよう。

MariaDB [test]> SELECT  * FROM test
WHERE
  match( search_key ) against('+柿' in boolean mode);
+----+--------------------------------------+
| id | search_key                           |
+----+--------------------------------------+
|  1 | 隣の客はよく柿食う客だ               |
|  2 | 後ろの客はよく柿食う客だ             |
+----+--------------------------------------+
2 rows in set (0.00 sec)
MariaDB [test]> SELECT  * FROM test
WHERE
  match( search_key ) against('+柿 隣' in boolean mode);
+----+--------------------------------------+
| id | search_key                           |
+----+--------------------------------------+
|  1 | 隣の客はよく柿食う客だ               |
|  2 | 後ろの客はよく柿食う客だ             |
|  3 | 隣の客はよく西瓜食う客だ             |
+----+--------------------------------------+
3 rows in set (0.01 sec)
MariaDB [test]> SELECT   * FROM  test 
WHERE
   match( search_key ) against('+柿 +隣' in boolean mode);
+----+-----------------------------------+
| id | search_key                        |
+----+-----------------------------------+
|  1 | 隣の客はよく柿食う客だ            |
+----+-----------------------------------+
1 row in set (0.00 sec)
MariaDB [test]> SELECT   * FROM  test 
WHERE
   match( search_key ) against('+西瓜 +隣' in boolean mode);
+----+--------------------------------------+
| id | search_key                           |
+----+--------------------------------------+
|  3 | 隣の客はよく西瓜食う客だ             |
+----+--------------------------------------+
1 row in set (0.00 sec)
MariaDB [test]> SELECT   * FROM  test 
WHERE
   match( search_key ) against('+西瓜 +後ろ' in boolean mode);
Empty set (0.00 sec)
MariaDB [test]> SELECT   * FROM  test 
WHERE
   match( search_key ) against('+柿 +後ろ' in boolean mode);
+----+--------------------------------------+
| id | search_key                           |
+----+--------------------------------------+
|  2 | 後ろの客はよく柿食う客だ             |
+----+--------------------------------------+
1 row in set (0.00 sec)
MariaDB [test]> SELECT   * FROM  test 
WHERE
   match( search_key ) against('+すもも' in boolean mode);
+----+--------------------------------------+
| id | search_key                           |
+----+--------------------------------------+
|  4 | すもももももももものうち             |
+----+--------------------------------------+
1 row in set (0.00 sec)
MariaDB [test]> SELECT   * FROM  test 
WHERE
   match( search_key ) against('+もも' in booleanmode);
+----+--------------------------------------+
| id | search_key                           |
+----+--------------------------------------+
|  4 | すもももももももものうち             |
+----+--------------------------------------+
1 row in set (0.00 sec)
MariaDB [test]> SELECT   * FROM  test 
WHERE
   match( search_key ) against('+ももち' in boolean mode);
Empty set (0.00 sec)
9
8
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
8