DELAEMON BLOG

Live as if you were to die tomorrow. Learn as if you were to live forever.

MySQL innodb-memcached on OS X

OS XMySQLinnodb-memcached を試す。

環境

OS X 10.10.5

libmemcached.so image not found

1番楽そうなbrew install で試す
mysqlをインストールして、innnodb_memcached_config.sqlを流し込む、プラグインを有効にする

% brew install mysql
% mysql -u root <  /usr/local/Cellar/mysql/5.7.9/share/mysql/innodb_memcached_config.sql
% mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.9 Homebrew

Copyright (c) 2000, 2015, 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> SELECT * FROM innodb_memcache.containers\G
*************************** 1. row ***************************
                  name: aaa
             db_schema: test
              db_table: demo_test
           key_columns: c1
         value_columns: c2
                 flags: c3
            cas_column: c4
    expire_time_column: c5
unique_idx_name_on_key: PRIMARY
1 row in set (0.00 sec)

mysql> SELECT * FROM test.demo_test;
+----+--------------+------+------+------+
| c1 | c2           | c3   | c4   | c5   |
+----+--------------+------+------+------+
| AA | HELLO, HELLO |    8 |    0 |    0 |
+----+--------------+------+------+------+
1 row in set (0.00 sec)

mysql> INSTALL PLUGIN daemon_memcached soname "libmemcached.so";
ERROR 1126 (HY000): Can't open shared library '/usr/local/Cellar/mysql/5.7.9/lib/plugin/libmemcached.so' (errno: 2 dlopen(/usr/local/Cellar/mysql/5.7.9/lib/plugin/libmemcached.so, 2): image not found)

エラー。libmemcached.soが存在しない。lsコマンドでディレクトリをみる限り確かになかった。他をfindしても無い

API version for DAEMON plugin is too different

MySQL :: Download MySQL Community Server
上記のURLのソースコードをダウンロードすればlibmemcached.soは含まれてるけど、バージョンが5.7.10。
brew install mysql でインストールされるmysqlは 5.7.9。
なのでlibmemcached.soを/usr/local/Cellar/mysql/5.7.9/lib/plugin/ディレクトリにコピーしてもエラー。

mysql>  INSTALL PLUGIN daemon_memcached soname "libmemcached.so";
ERROR 1126 (HY000): Can't open shared library 'libmemcached.so' (errno: 0 API version for DAEMON plugin is too different)
解決方法

MySQL :: Download MySQL Community Server
dmgでインストールしたmysqlならlibmemcached.soが含まれる。
あとは公式ドキュメント通りの手順でOK

dmgダウンロード後、クリックしてインストール開始。終了ポップアップにrootパスワードが表示されるのでメモっておく

/usr/local/mysql/bin/mysql_secure_installation
/usr/local/mysql/bin/mysqld_safe
/usr/local/mysql/bin/mysql -u root -p <  /usr/local/mysql/share/innodb_memcached_config.sql
/usr/local/mysql/bin/mysql -u root -p
mysql>  INSTALL PLUGIN daemon_memcached soname "libmemcached.so";
Query OK, 0 rows affected (0.01 sec)

telnetでset, get試す

telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to localhost
set key1 0 0 5
abcde
STORED
get key1
VALUE key1 0 5
abcde
END
get key2
END

phoenix アプリのフロントエンド(html/css/javascript)更新をsystemdのunitファイルに追加した

今のsystemd unitファイルはこんな感じ。フロントエンド更新のためにExecStartPreを追加した。

[root@host ~]# cat /usr/lib/systemd/system/phoenix.service
[Unit]
Description = hello phoenix

[Service]
EnvironmentFile=/etc/environments/phoenix
WorkingDirectory=/home/dela/www/hello/
ExecStartPre=/usr/local/src/elixir-1.1.1/bin/elixir /usr/local/src/elixir-1.1.1/bin/mix clean
ExecStartPre=/usr/local/src/elixir-1.1.1/bin/elixir /usr/local/src/elixir-1.1.1/bin/mix deps.get --only prod 
ExecStartPre=node node_modules/brunch/bin/brunch build 
ExecStartPre=/usr/local/src/elixir-1.1.1/bin/elixir /usr/local/src/elixir-1.1.1/bin/mix phoenix.digest
ExecStartPre=/usr/local/src/elixir-1.1.1/bin/elixir /usr/local/src/elixir-1.1.1/bin/mix compile.protocols
ExecStart=/usr/local/src/elixir-1.1.1/bin/elixir -pa _build/prod/consolidated -S /usr/local/src/elixir-1.1.1/bin/mix phoenix.server
Restart = always
Type = simple
User = dela

[Install]
WantedBy = multi-user.target

以下、追加したコマンド

mix clean

Delete generated application files.
前回のコンパイル結果を削除
Mix.Tasks.Clean

mix deps.get --only prod 

Get all out of date dependencies, i.e. dependencies that are not available or have an invalid lock. --only - only fetch dependencies for given environment.
プロダクトモード用の依存パッケージの解決
Mix.Tasks.Deps.Get

brunch build

builds a project for distribution. By default it enables minification.
フロントエンドのコンパイル
brunchって知らなかった。phoenixのアプリのデフォルトのfrontend build tool。glupとかに変更もできる。
Brunch - ultra-fast HTML5 build tool

mix phoenix.digest

Digests and compress static files.
コンパイル結果をphoenixアプリが参照してるディレクトリへ配置
Mix.Tasks.Phoenix.Digest – Phoenix v1.0.3

Fedora22のログインシェルを楽しげに表示する

ログインシェルをこうする。
f:id:delaemon:20151118002832p:plain

楽しげなパッケージをインストール

[root@host ~]# yum install cowsay fortune-mod figlet
Yum command has been deprecated, redirecting to '/usr/bin/dnf install cowsay fortune-mod figlet'.

Fedora22からyumの代わりにdnfというパッケージ管理が採用された。
yumはpython2でしか動かない、Fedora23でpython3が入る予定。

dnf install cowsay fortune-mod figlet

.bashrcに以下を記載

exec fortune | cowsay
figlet -f slant Welcome to the `hostname`

sshなどでログインすると牛が毎回しゃべる。

phoenix app using systemd on fedora22(GMO Conoha)

.serviceファイルを作る

[root@host ~]# touch /usr/lib/systemd/system/phoenix.service
[root@host ~]# vim /usr/lib/systemd/system/phoenix.service
[root@host ~]# cat /usr/lib/systemd/system/phoenix.service
[Unit]
Description = hello phoenix

[Service]
EnvironmentFile=/etc/environments/phoenix
WorkingDirectory=/home/dela/www/hello/
ExecStart = /usr/local/src/elixir-1.1.1/bin/elixir -pa _build/prod/consolidated -S /usr/local/src/elixir-1.1.1/bin/mix phoenix.server
Restart = always
Type = simple
User = dela

[Install]
WantedBy = multi-user.target

環境変数もファイルへ記述

[root@host ~]# mkdir /etc/environments/
[root@host ~]# vim /etc/environments/phoenix
[root@host ~]# cat /etc/environments/phoenix
MIX_ENV=prod
PORT=4001

serviceが認識されているか確認

[root@host ~]# systemctl list-unit-files --type=service | grep phoenix
phoenix.service                             disabled

自動起動に登録

[root@host ~]# sudo systemctl enable phoenix
Created symlink from /etc/systemd/system/multi-user.target.wants/phoenix.service to /usr/lib/systemd/system/phoenix.service.
[root@host ~]# systemctl list-unit-files --type=service | grep phoenix
phoenix.service                             enabled

デーモン起動

[root@host ~]# sudo systemctl start phoenix

build nginx proxy for elixir phoenix app on fedora22(GMO Conoha)

phoenixアプリをポート80で動かせたけど、
別のサービスも同じサーバー上で動かすのを想定してnginxのproxyを立てた。
fedora使っていて思うのはパッケージが新しいバージョンを追いかけてるのと、
ssdのせいかもしれないけどインストール終わるがすごく速い。快適。

現在のstableバージョンがyumで入る。

[root@host ~]# yum install nginx
#ささー
[root@host ~]# nginx -v
nginx version: nginx/1.8.0

nginx.serviceを自ら用意しないといけないかなと思ったけど自動で作られてた。

[root@host ~]# cat /lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=mixed
PrivateTmp=true

[Install]
WantedBy=multi-user.target

nginx.confにproxy_pass追記

[root@host ~]# cat /etc/nginx/nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    # 省略
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
	    proxy_pass http://localhost:4001; #ここだけ追記
        }
    # 省略
    }
}

phoenixアプリの config/prod.exs の該当部分のみ

config :hello, Hello.Endpoint,
  http: [port: {:system, "PORT"}],
  url: [host: "localhost", port: 4001],
  cache_static_manifest: "priv/static/manifest.json"

nginxをsystemdで起動

[root@host ~]# systemctl start nginx

これでhttp://takeshionodera.net/phoenix/にアクセスするとport 4001にプロキシされてブラウザで表示されるようになった。

Firewalldのポート開放 & phoenix app 起動 @Fedora22(Conoha VPS)

phoenixのサンプル画面がブラウザから確認できなかった。
サーバー上でwgetすると取れるし、ローカルPCからpingも通る。
tcpdump port 4000 みてると、たしかにブラウザからのアクセスは届いてはいた。
ファイアーウォールまわりだろうとはすぐ絞れたけど
iptables使ってない、firewalldになったとかでハマった。
systemctl restart firewalld とするべきを firewall-cmd --reloadしていた。
/usr/lib/firewalld/services/http.xmlをコピーして
/etc/firewalld/services/http.xmlを書き換えるとか試したけど、コマンドでやるのが確実。

以下でポート開放された。

現状の確認

[root@host ~]# firewall-cmd --list-all
FedoraServer (default, active)
  interfaces: eth0
  sources:
  services: cockpit dhcpv6-client ssh
  ports:
  masquerade: no
  forward-ports:
  icmp-blocks:
  rich rules:

サービスを追加

[root@host ~]# firewall-cmd --permanent --add-service=http
success
[root@host ~]# firewall-cmd --permanent --add-service=https
success

ポートを開ける

[root@host ~]# firewall-cmd --permanent --add-port=80/tcp
success
[root@host ~]# firewall-cmd --permanent --add-port=4000/tcp
success

firewalldを再起動

[root@host ~]# systemctl restart firewalld
[root@host ~]# firewall-cmd --list-all
FedoraServer (default, active)
  interfaces: eth0
  sources:
  services: cockpit dhcpv6-client http https ssh
  ports: 4000/tcp 80/tcp
  masquerade: no
  forward-ports:
  icmp-blocks:
  rich rules:

phoenixのサンプルアプリを作成

[user@host app] mix phoenix.new app

アプリを port 4000 で起動

[user@host app]# mix phoenix.server

ビルドは以下に作られる

[user@host app]# tree -L 1 _build/
_build/
├── dev
└── prod

アプリのホスト名・ポートなどの設定変更は以下のファイルを書き換える。モード別に配置されている

tree -L 1 config/
config/
├── config.exs
├── dev.exs
├── prod.exs
├── prod.secret.exs
└── test.exs

production mode ( port 80 )で起動

[user@host app]# MIX_ENV=prod mix compile.protocols
[user@host app]# sudo MIX_ENV=prod PORT=80 /usr/local/src/elixir-1.1.1/bin/elixir -pa _build/prod/consolidated -S /usr/local/src/elixir-1.1.1/bin/mix phoenix.server