Skip to content
インメモリ検索エンジン

MygramDBサブミリ秒の全文検索

MySQL / MariaDB binlogレプリケーションで同期するインメモリ検索エンジン

MITライセンス単一バイナリMySQL & MariaDBv1.7
検索レイテンシ"quantum physics"
MySQL FULLTEXT2,566ms
MygramDBインメモリ0.08ms
27,600× 高速 · 110万件 p50

クイックスタート

MySQLでGTIDモードとレプリケーションユーザーを設定:

用語補足

GTID はMySQLの変更に付く一意な番号、binlog はMySQLの変更履歴ログです。MygramDBはbinlogを読み、GTIDで「どこまで読んだか」を管理します。詳しくは用語集を参照してください。

sql
SET GLOBAL enforce_gtid_consistency = ON;
SET GLOBAL gtid_mode = OFF_PERMISSIVE;
SET GLOBAL gtid_mode = ON_PERMISSIVE;
SET GLOBAL gtid_mode = ON;
SET GLOBAL binlog_format = ROW;
SET GLOBAL binlog_row_image = FULL;
CREATE USER 'repl_user'@'%' IDENTIFIED BY 'your_password';
-- SELECT: 全文検索対象テーブルの初期スナップショット取得用
GRANT REPLICATION SLAVE, REPLICATION CLIENT, SELECT ON mydb.* TO 'repl_user'@'%';

MySQL設定は永続化してください

SET GLOBAL は稼働中のMySQLに反映されますが、再起動後に戻る場合があります。本番では my.cnf などの永続設定にも gtid_mode=ONbinlog_format=ROWbinlog_row_image=FULL を入れてください。

bash
docker run -d --name mygramdb \
  -p 11016:11016 -p 8080:8080 \
  -e MYSQL_HOST=your-mysql-host \
  -e MYSQL_USER=repl_user \
  -e MYSQL_PASSWORD=your_password \
  -e MYSQL_DATABASE=mydb \
  -e TABLE_NAME=articles \
  -e TABLE_PRIMARY_KEY=id \
  -e TABLE_TEXT_COLUMN=content \
  -e TABLE_NGRAM_SIZE=2 \
  -e REPLICATION_SERVER_ID=12345 \
  -e NETWORK_ALLOW_CIDRS=0.0.0.0/0 \
  ghcr.io/libraz/mygram-db:latest

WARNING

NETWORK_ALLOW_CIDRS=0.0.0.0/0 はすべてのIPからの接続を許可します。本番環境では特定の範囲に制限してください(例: 10.0.0.0/8,172.16.0.0/12)。

初回ロードについて

MygramDBは起動しただけでは既存データを自動では読み込みません。最初に SYNC articles を実行して、MySQLの既存行から検索インデックスを作成します。

bash
docker exec mygramdb mygram-cli -p 11016 SYNC articles
docker exec mygramdb mygram-cli -p 11016 SYNC STATUS
echo "SEARCH articles hello world" | nc localhost 11016

クライアントライブラリ

詳しい使い方は 公式クライアント を参照してください。

Node.js v1.7 compatible

bash
npm install mygramdb-client
bash
yarn add mygramdb-client
bash
bun add mygramdb-client
javascript
import { createMygramClient } from 'mygramdb-client'

const client = createMygramClient({ host: 'localhost', port: 11016 })
await client.connect()

const results = await client.search('app_db.articles', 'hello world', {
  limit: 10
})

mygramdb-client on GitHub

Go v1.7 compatible

bash
go get github.com/libraz/go-mygram-client
go
import mygram "github.com/libraz/go-mygram-client"

client := mygram.NewClient("localhost:11016")
defer client.Close()
client.Connect()

resp, _ := client.Search("app_db.articles", "hello world", mygram.SearchOptions{
    Limit: 10,
})

go-mygram-client on GitHub

Python v1.7 compatible

bash
pip install mygramdb-client
python
from mygramdb_client import MygramClient, ClientConfig, SearchOptions

async with MygramClient(ClientConfig(host='localhost', port=11016)) as client:
    results = await client.search(
        'app_db.articles',
        'hello world',
        SearchOptions(limit=10),
    )

python-mygramdb-client on GitHub

関連プロジェクト

mysql-event-streamGitHub

MygramDBから抽出した軽量なMySQL / MariaDB CDCエンジン。binlogレプリケーションイベントをパースし、アプリケーション向けのストリーミングAPIを提供します。MySQL 8.4/9.x・MariaDB 10.11+/11.xに対応し、Node.js (N-API) とPython (ctypes) バインディングを備えます。

CDCbinlogNode.jsPython