Q4M をFreeBSD8.2に導入する

Q4Mは奥一穂さんによって開発されたMySQLのストレージエンジンとして実装されてるMessage Queueです。 Message Queueの説明や使い方はnekokakさんが書いてる下記記事が参考になります。

Perl Hackers Hub 第10回 ジョブキューで後回し大作戦―TheSchwartz,Qudo,Q4M(3)

このQ4MはMySQLのストレージエンジンとして実装されてるので、インストールするにはMySQLに組み込む必要があります。 しかしどのバージョンでも良い訳ではなく、MySQL 5.1系だけが対応となってる。 利用しているMySQLが5.1であれば既存のMySQLに組み込んでもいいのだが何か問題が起きた場合のことを考えるとそれとは別にQ4M専用のMySQL5.1をインストールして使う方法もアリかと思います。 今回はQ4M専用のMySQL5.1をFreeBSD8.2にインストールしてみる。 FreeBSDには便利なパッケージシステムportsがありますが依存関係で複数のMySQLを入れるとまずいため今回はソースから入れます。

ここからGenericLinuxのソースコードをダウンロードする {http://dev.mysql.com/downloads/mysql/5.1.html#downloads}(http://dev.mysql.com/downloads/mysql/5.1.html#downloads)

◆Q4Mインストール

$ mkdir /usr/local/q4m
$ tar zxf mysql-5.1.60.tar.gz
$ cd mysql-5.1.60

/usr/local/q4m 以下に導入innodb pluginなどは入れませんあくまでQ4M専用です

./configure     --prefix=/usr/local/q4m     --with-mysqld-ldflags="-static"     --with-client-ldflags="-static"     --enable-assembler     --enable-thread-safe-client     --with-charset=utf8     --with-zlib-dir=bundled     --with-big-tables     --with-mysqld-user=nobody     --with-pic     --with-extra-charsets=all     --with-readline     --without-debug     --enable-shared     --with-fast-mutexes     --with-comment="Q4M"     --with-server-suffix="-q4m"     --with-unix-socket-path="/tmp/mysql_q4m.sock"     --with-tcp-port=13306     --with-plugins=none     --without-plugin-daemon_example     --without-plugin-ftexample     --without-plugin-archive     --without-plugin-blackhole     --without-plugin-example     --without-plugin-federated     --without-plugin-innobase     --without-plugin-innodb_plugin     --without-docs     --without-man
$ make
$ make install

database用ディレクトリとconfファイルの作成

$ mkdir /usr/local/q4m/data
$ vi /usr/local/q4m/data/my.cnf

/usr/local/q4m/data/my.cnf

# Example MySQL config file for small systems.
#
# This is for a system with little memory (<= 64M) where MySQL is only used
# from time to time and it's important that the mysqld daemon
# doesn't use much resources.
#
# MySQL programs look for option files in a set of
# locations which depend on the deployment platform.
# You can copy this option file to one of those
# locations. For information about these locations, see:
# http://dev.mysql.com/doc/mysql/en/option-files.html
#
# In this file, you can use all long options that a program supports.
# If you want to know which options a program supports, run the program
# with the "--help" option.

# The following options will be passed to all MySQL clients
[client]
#password       = your_password
port            = 13306
socket          = /tmp/mysql_q4m.sock

# Here follows entries for some specific programs

# The MySQL server
[mysqld]
port            = 13306
socket          = /tmp/mysql_q4m.sock
skip-locking
key_buffer_size = 16K
max_allowed_packet = 1M
table_open_cache = 4
sort_buffer_size = 64K
read_buffer_size = 256K
read_rnd_buffer_size = 256K
net_buffer_length = 2K
thread_stack = 128K

# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (using the "enable-named-pipe" option) will render mysqld useless!
#
#skip-networking
server-id       = 1

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates

[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M

[mysqlhotcopy]
interactive-timeout

データベースの初期化

$ /usr/local/q4m/bin/mysql_install_db --defaults-file=/usr/local/q4m/data/my.cnf
$ chmod 755 /usr/local/q4m/data

Q4M本体pluginのインストール

$ wget http://q4m.kazuhooku.com/dist/q4m-0.9.5.tar.gz
$ tarr zxvf q4m-0.9.5.tar.gz
$ cd q4m-0.9.5
$ CPPFLAGS="-I/usr/local/q4m/include/mysql" CFLAGS="-L/usr/local/q4m/lib/mysql" ./configure     --with-mysql=src/mysql-5.1.60     --prefix=/usr/local/q4m
$ make
$ mkdir -p /usr/local/q4m/lib/mysql/plugin
$ cp src/.libs/libqueue_engine.so /usr/local/q4m/lib/mysql/plugin/
$ cat support-files/install.sql | /usr/local/q4m/bin/mysql -S /tmp/mysql_q4m.sock

Q4Mが組み込まれたか確認

$ cd /usr/local/q4m
$ bin/mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.1.60-q4m Q4M

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> show plugins;
+------------+--------+----------------+--------------------+---------+
| Name       | Status | Type           | Library            | License |
+------------+--------+----------------+--------------------+---------+
| binlog     | ACTIVE | STORAGE ENGINE | NULL               | GPL     |
| CSV        | ACTIVE | STORAGE ENGINE | NULL               | GPL     |
| MEMORY     | ACTIVE | STORAGE ENGINE | NULL               | GPL     |
| MyISAM     | ACTIVE | STORAGE ENGINE | NULL               | GPL     |
| MRG_MYISAM | ACTIVE | STORAGE ENGINE | NULL               | GPL     |
| QUEUE      | ACTIVE | STORAGE ENGINE | libqueue_engine.so | GPL     |
+------------+--------+----------------+--------------------+---------+
6 rows in set (0.00 sec)

以下が見えていれば成功です

| QUEUE      | ACTIVE | STORAGE ENGINE | libqueue_engine.so | GPL     |

Q4Mを使ってみる

mysql> use test;
mysql> CREATE TABLE queue_test (
    -> message TEXT NOT NULL
    -> )ENGINE = QUEUE;
Query OK, 0 rows affected (0.01 sec)

mysql> INSERT INTO queue_test (message) VALUES ('test1'), ('test2'), ('test3');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0


mysql> SELECT * FROM queue_test;
+---------+
| message |
+---------+
| test1   |
| test2   |
| test3   |
+---------+
3 rows in set (0.00 sec)


mysql> SELECT queue_wait('queue_test');
+--------------------------+
| queue_wait('queue_test') |
+--------------------------+
|                        1 |
+--------------------------+
1 row in set (0.00 sec)

mysql> SELECT * FROM queue_test;
+---------+
| message |
+---------+
| test1   |
+---------+
1 row in set (0.00 sec)


mysql> SELECT queue_end();
+-------------+
| queue_end() |
+-------------+
|           1 |
+-------------+
1 row in set (0.00 sec)



mysql> SELECT queue_abort();
+---------------+
| queue_abort() |
+---------------+
|             1 |
+---------------+
1 row in set (0.00 sec)

FreeBSDrc起動スクリプト

/usr/local/etc/rc.d/q4m として作成 自動起動するにはrc.confに q4m_enable="yes" が必要です

#!/bin/sh
#
#
# Add the following line to /etc/rc.conf to enable mysql:
# q4m_enable (bool):  Set to "NO" by default.
#                       Set it to "YES" to enable MySQL.
# q4m_limits (bool):  Set to "NO" by default.
#                       Set it to yes to run `limits -e -U mysql`
#                       just before mysql starts.
# q4m_dbdir (str):    Default to "/var/db/mysql"
#                       Base database directory.
# q4m_args (str):     Custom additional arguments to be passed
#                       to mysqld_safe (default empty).
#

. /etc/rc.subr

name="q4m"
rcvar=`set_rcvar`

load_rc_config $name

: ${q4m_enable="NO"}
: ${q4m_limits="NO"}
: ${q4m_dbdir="/usr/local/q4m/data"}
: ${q4m_args=""}

mysql_user="tooru"
mysql_limits_args="-e -U ${mysql_user}"
pidfile="${q4m_dbdir}/`/bin/hostname`.pid"
command="/usr/local/q4m/bin/mysqld_safe"
command_args="--defaults-extra-file=${q4m_dbdir}/my.cnf --user=${mysql_user} --datadir=${q4m_dbdir} --pid-file=${pidfile} ${q4m_args} > /dev/null 2>&1 &"
procname="/usr/local/q4m/bin/mysqld"
start_precmd="${name}_prestart"
mysql_install_db="/usr/local/q4m/bin/mysql_install_db"
mysql_install_db_args="--ldata=${q4m_dbdir}"

q4m_create_auth_tables()
{
        eval $mysql_install_db $mysql_install_db_args >/dev/null
        [ $? -eq 0 ] && chown -R ${mysql_user}:${mysql_user} ${q4m_dbdir}
}

q4m_prestart()
{
        if [ ! -d "${q4m_dbdir}/mysql/." ]; then
                q4m_create_auth_tables || return 1
        fi
        if checkyesno q4m_limits; then
                eval `/usr/bin/limits ${mysql_limits_args}` 2>/dev/null
        else
                return 0
        fi
}

run_rc_command "$1"

参考

http://blog.nomadscafe.jp/2011/12/q4m---mysql-casual-advent-calendar-2011.html

http://gihyo.jp/dev/serial/01/perl-hackers-hub/001003

http://blog.hirokikana.com/?p=211

created:

Back to top