Skip to content

Latest commit

 

History

History
287 lines (208 loc) · 7.72 KB

README.hant.md

File metadata and controls

287 lines (208 loc) · 7.72 KB

lib_mysqludf_redis

提供一組 UDF 指令於 Mysql/MariaDB 中存取 Redis。

English | 繁體中文 | 日本語

目錄

簡介

Alt text

回目錄

系統需求

  • 架構: Linux 64-bit(x64)
  • 編譯器: GCC 4.1.2+
  • MariaDB 5.5+
  • Redis 1.2+
  • 相依套件:
    • MariaDB development library 5.5+
    • hiredis 0.13.3+
    • cJSON 1.6+

回目錄

編譯與安裝外掛元件

安裝相依套件

CentOS

# 安裝工具
$ yum install -y make wget gcc git

# 安裝 mariadb development tool
$ yum install -y mariadb-devel

Debain

# 安裝工具
$ apt-get install -y make wget gcc git

# 安裝 mariadb development tool
$ apt-get install -y libmariadb-dev

FreeBSD

# 安裝工具
$ pkg install -y gmake wget gcc git-lite

要編譯外掛元件最簡單的方式就是直接執行 makemake install。或於FreeBSD上使用 gmakegmake install

$ make

# 安裝外掛程式庫到目的資料夾
$ make install

# 安裝 UDF 到 Mysql/MariaDB 伺服器
$ make installdb

附記:如果使用的 Mysql/MariaDB 是早期版本,或是使用手動編譯方式安裝,預設的 include 路徑可能無法使用;請於編譯時使用 make INCLUDE_PATH=`mysql_config --variable=pkgincludedir` 指定 INCLUDE_PATH 變數進行編譯。

編譯參數

  • install

    安裝外掛程式庫到指定的 Mysql 外掛資料夾。

  • installdb

    安裝/註冊 UDFs 到 Mysql/MariaDB 伺服器。

  • uninstalldb

    卸載/註銷 UDFs。

  • clean

    清除編譯檔案。

  • distclean

    如同 clean 指令,且同時還清理相依套件資源。

編譯變數

下面是編譯時的變數,可以被使用在 make

  • HIREDIS_MODULE_VER

    要提供給元件編譯的 hiredis 版本。如果該值為空或未指定,其預設值為 0.13.3

  • CJSON_MODULE_VER

    要提供給元件編譯的 cJSON 版本。如果該值為空或未指定,其預設值為 1.6.0

  • INCLUDE_PATH

    指定要被參考的 MariaDB/Mysql C 標頭檔。如果該值為空或未指定,其預設值指定為 Mysql pkgincludedir 變數。這個值可以經由下列命令取得:

    $ echo `mysql_config --variable=pkgincludedir`/server
  • PLUGIN_PATH

    指定 MariaDB/Mysql 的外掛元件檔案路徑。這個值可以在 MariaDB/Mysql 中,經由 SHOW VARIABLES LIKE '%plugin_dir%'; 指令取得。如果該值為空或未指定,其預設值指定為 Mysql plugindir 變數。這個值可以經由下列命令取得:

    $ mysql_config --plugindir

範例:

# 指定 MariaDB/Mysql 的外掛元件檔案路徑為 /opt/mysql/plugin
$ make PLUGIN_PATH=/opt/mysql/plugin
$ make install

回目錄

安裝與卸載 UDF

使用 make 安裝 UDF:

$ make installdb

或於 Mysql/MariaDB 中,手動執行下列 sql 陳述式:

mysql>  CREATE FUNCTION `redis` RETURNS STRING SONAME 'lib_mysqludf_redis.so';

要卸載/註銷 UDF,可以使用 make uninstalldb;或於 Mysql/MariaDB 中,執行下列 sql 陳述式:

mysql>  DROP FUNCTION IF EXISTS `redis`;

回目錄

使用方式

redis($connection_string, $command, [$args...])

呼叫 Redis 命令,藉由指定 $connection_string, $command 以及命令參數。

  • $connection_string - 表示要連線的 Redis 主機,使用 DSN 連線字串表示,其內容必須是下列形式之一:
    • redis://:<password>@<host>:<port>/<database>/
    • redis://:<password>@<host>/<database>/
    • redis://@<host>:<port>/<database>/
    • redis://@<host>/<database>/
  • $command, $args... - Redis 命令與其參數。請詳見 Redis 官網 https://redis.io/commands

函式回傳 JSON 字串指示操作成功或失敗。

若成功則輸出:

{
   "out": "OK"
}

若失敗則輸出:

{
   "err": "Connection refused"
}

下列範例說明函式的使用方式,並且與 redis-cli 命令工具作對照。

/*
  下面的陳述式如同:

    $ redis-cli -h 127.0.0.1 -n 8 PING
    PONG
*/
mysql>  SELECT `redis`('redis://@127.0.0.1/8/', 'PING')\G
*************************** 1. row ***************************
`redis`('redis://@127.0.0.1/8/', 'PING'): {
        "out":  "PONG"
}
1 row in set (0.00 sec)



/*
  下面的陳述式如同:

    $ redis-cli -h 127.0.0.1 -a foobared -n 8 PING
    PONG
*/
mysql>  SELECT `redis`('redis://:[email protected]/8/', 'PING')\G
*************************** 1. row ***************************
`redis`('redis://:[email protected]/8/', 'PING'): {
        "out":  "PONG"
}
1 row in set (0.00 sec)



/*
    $ redis-cli -h 127.0.0.1 -p 80 -n 8 PING
    Could not connect to Redis at 127.0.0.1:80: Connection refused
*/
mysql>  SELECT `redis`('redis://@127.0.0.1:80/8/', 'PING')\G
*************************** 1. row ***************************
`redis`('redis://@127.0.0.1:80/8/', 'PING'): {
        "err":  "Connection refused"
}
1 row in set (0.00 sec)



/*
    $ redis-cli -h 127.0.0.1 -n 8 HMSET myhash field1 Hello field2 World
    OK
*/
mysql>  SELECT `redis`('redis://@127.0.0.1/8/', 'HMSET', 'myhash', 'field1', 'Hello', 'field2', 'World')\G
*************************** 1. row ***************************
`redis`('redis://@127.0.0.1/8/', 'HMSET', 'myhash', 'field1', 'Hello', 'field2', 'World'): {
        "out":  "OK"
}
1 row in set (0.00 sec)



/*
    $ redis-cli -h 127.0.0.1 -n 8 HGET myhash field1
    "Hello"
*/
mysql>  SELECT `redis`('redis://@127.0.0.1/8/', 'HGET', 'myhash', 'field1')\G
*************************** 1. row ***************************
`redis`('redis://@127.0.0.1/8/', 'HGET', 'myhash', 'field1'): {
        "out":  "Hello"
}
1 row in set (0.00 sec)



-- redis-cli -h 127.0.0.1 -n 0 SET foo bar
mysql>  SELECT `redis`('redis://@127.0.0.1/0/', 'SET', 'foo', 'bar')

-- redis-cli -h 127.0.0.1 -n 0 SCAN 0 MATCH prefix*
mysql>  SELECT `redis`('redis://@127.0.0.1/0/', 'SCAN', '0', 'MATCH', 'prefix*')

回目錄

待完成事項

  • 實作 Redis 連線驗證機制。(2017-12-30)
  • 補充 redis DSN 字串建構函式。

回目錄

授權條款

請參閱 LICENSE

回目錄

相關連結

回目錄