LoginSignup
29
31

More than 5 years have passed since last update.

lua-nginx-module で利用できるStorage(memcache, MySQL, redis)向けのアダプタを試す

Posted at

せっかくだしnginx+lua-nginx-moduleのStorage向けアダプタ使いたい

nginx (+ lua-nginx-module) で利用できるStorage向けのアダプタについては
lua-nginx-moduleのREADME.mdに書かれている。

そのうち、リポジトリからgit cloneして、nginx.confにlua_package_pathを書いて
ライブラリまでのパスさえ指定してあげれば使えるものは以下の通り。

  • lua-resty-memcached
  • lua-resty-redis
  • lua-resty-mysql

Memcached, Redis, MySQL向け。Postgres向けのはlua-restyではなく
別途となり、ちょっと使うのめんどくさそうなので割愛。

一つ一つ使って行ってみよう。

lua-nginx-module の準備

nginx + lua-nginx-moduleのビルド、インストールについてはこちらを参照してください。
Mac OS X向けの解説になってるので、他のOSを使ってる場合は
適宜パスなり置き換えてください… (あくまで自己検証用としてやってるので…)

検証環境

検証環境は以下のとおり

説明 バージョン
OS Mac OS X 10.8.3
nginx 1.4.0
lua-nginx-module v0.8.1

lua-resty-memcached

名前の通りmemcached向けのアダプタ。

memcachedのインストール

まずmemcachedのセットアップをしよう。homebrewでサクッと。
そして立ちあげておこう。

> brew install memcahced
> memcached -vv

lua-resty-memcachedのインストール

lua-resty-memcachedを使うため、リポジトリからgit cloneしよう。

> cd /usr/local/nginx-1.4.0/lua-lib/
> git://github.com/agentzh/lua-resty-memcached.git
> cd lua-resty-memcached
> git checkout -b v0.11 v0.11  

nginx.confの設定

nginx.confを以下の形に書き換えて試しに使おう。

worker_processes  1;

error_log  logs/error.log;
pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {

    include       mime.types;
    default_type  text/plain;

    access_log  logs/access.log;

    sendfile        on;
    keepalive_timeout  65;

    gzip  on;

    lua_package_path "/usr/local/nginx-1.4.0/lua-lib/lua-resty-string/lib/?.lua;/usr/local/nginx-1.4.0/lua-lib/lua-resty-memcached/lib/?.lua;;";

    server {
        listen       8080;
        server_name  localhost;

        access_log  logs/host.access.log;

        location /get {
            content_by_lua '
                local memcached = require "resty.memcached"
                local client, err = memcached:new()
                client:connect("127.0.0.1", 11211)

                local val, flags, err = client:get("bucket")
                ngx.say("Value is: ", val)
            ';
        }

        location /set {
            content_by_lua '
                local memcached = require "resty.memcached"
                local client, err = memcached:new()
                client:connect("127.0.0.1", 11211)

                local args = ngx.req.get_uri_args()
                client:set("bucket", args.value)

                ngx.say("Saved!")
            ';
        }
    }
}

動作確認

curlでパスにアクセスしmemcachedに対し
値を保存できたり引き出せたりできるか確認する。

lua-resty-memcached

うん。大丈夫。

lua-resty-redis

Redisのインストール

redisもhomebrewでサクッとインストール。
そして起動しておく。

> brew install redis
> redis-server --loglevel verbose 

lua-resty-redisのインストール

lua-resty-redisを使うためリポジトリからgit cloneしておこう。

> cd /usr/local/nginx-1.4.0/lua-lib/
> git://github.com/agentzh/lua-resty-redis.git
> cd lua-resty-redis
> git checkout -b v0.15 v0.15 

nginx.confの設定

nginx.confを以下のように書き換える。

worker_processes 1;

error_log  logs/error.log;
pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {

    include       mime.types;
    default_type  text/plain;

    access_log  logs/access.log;

    sendfile        on;
    keepalive_timeout  65;

    gzip  on;

    lua_package_path "/usr/local/nginx-1.4.0/lua-lib/lua-resty-string/lib/?.lua;/usr/local/nginx-1.4.0/lua-lib/lua-resty-redis/lib/?.lua;;";

    server {
        listen       8080;
        server_name  localhost;

        access_log  logs/host.access.log;

        location /get {
            content_by_lua '
                local redis = require "resty.redis"
                local client = redis:new()
                client:connect("127.0.0.1", 6379)

                local args = ngx.req.get_uri_args()
                local key, field = args.key, args.field

                local result, err = client:hmget(key, field)
                ngx.say(result)
            ';
        }

        location /set {
            content_by_lua '
                local redis = require "resty.redis"
                local client = redis:new()
                client:connect("127.0.0.1", 6379)

                local args = ngx.req.get_uri_args()
                local key, field, val = args.key, args.field, args.value

                client:hmset(key, field, val)
                ngx.say("Saved!")
            ';
        }
    }
}

動作確認

curlでパスにアクセスしredisに対し
特定のハッシュフィールドに値を保存できたり引き出せたりできるか確認する。

lua-resty-redis

うん。大丈夫。

lua-resty-mysql

mysqlのインストール

mysqlもhomebrewでサクッとインストール

> brew install mysql
> mysql.server start

データベース、テーブルおよびアクセスするユーザーの作成

MySQL DBにMySQL Clientでアクセスして、

mysql -h localhost -u root -p

nginx向けのテストデータベース、テーブル及びユーザーとか作る。

> create database nginx_test;
> use nginx_test;
> create table nginx_test_table (   
      id int not null primary key auto_increment,   
      comment text not null 
    )Engine=InnoDB Default charset=utf8;
> grant all on nginx_test.* to 'nginx_test'@'localhost' identified by 'nginx_test';
> exit;

テーブルにアクセスできるか確認しとく。

> mysql -h localhost -u nginx_test --password="nginx_test" -A nginx_test; 
> show tables;

lua-resty-mysql のインストール

lua-resty-mysqlを使うためリポジトリからgit cloneしておこう。

> cd /usr/local/nginx-1.4.0/lua-lib/
> git://github.com/agentzh/lua-resty-mysql.git
> cd lua-resty-mysql
> git checkout -b v0.13 v0.13 

nginx.confの設定

nginx.confを以下のように書き換える。

worker_processes  1;

error_log  logs/error.log;
pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {

    include       mime.types;
    default_type  text/plain;

    charset utf8;

    access_log  logs/access.log;

    sendfile        on;
    keepalive_timeout  65;

    gzip  on;

    lua_package_path "/usr/local/nginx-1.4.0/lua-lib/lua-resty-string/lib/?.lua;/usr/local/nginx-1.4.0/lua-lib/lua-resty-mysql/lib/?.lua;;";

    server {
        listen       8080;
        server_name  localhost;

        access_log  logs/host.access.log;

        location /get {
            content_by_lua '
                local mysql = require "resty.mysql"
                local db, err = mysql:new()

                db:connect{
                    host = "127.0.0.1",
                    port = 3306,
                    database = "nginx_test",
                    user = "nginx_test",
                    password = "nginx_test"
                }

                local result, err, errno, sqlstate =
                    db:query("SELECT `comment` FROM `nginx_test_table` ORDER BY `id` DESC")

                for idx, records in pairs(result) do
                    for key, record in pairs(records) do
                        ngx.say(record)
                    end
                end

                db:close()
            ';
        }

        location /set {
            content_by_lua '
                local mysql = require "resty.mysql"
                local db, err = mysql:new()

                db:connect{
                    host = "127.0.0.1",
                    port = 3306,
                    database = "nginx_test",
                    user = "nginx_test",
                    password = "nginx_test"
                }

                local args = ngx.req.get_uri_args()
                local comment = ngx.quote_sql_str(args.comment)

                db:query(
                  "INSERT INTO `nginx_test_table` (`comment`) VALUES (" .. comment .. ")"
                )

                ngx.say("Saved!")

                db:close()
            ';
        }
    }
}

動作確認

curlでパスにアクセスしmysqlに対し
レコードを追加できるか確認する。

lua-resty-mysql

うん。大丈夫。

29
31
1

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
29
31