知っての通り、MariaDBはMySQLの創設者が、MySQLをforkして作られた派生系のDBですね。
MySQLがOracle傘下に入ってしまったのでその辺でごにょごにょすることがあったんでしょうか。事情については調べるといろいろ出てくると思うので調べてみると楽しいかも知れません。
それはさておき、実際にインストールしてSQLを叩いたりしてみますね。
MariaDBインストール編
最初開発マシンのMacに入れようとしてこちらのエントリを見てやってみました。
http://qiita.com/kozmats/items/ac32f1ac1d676a09e3b2](http://qiita.com/kozmats/items/ac32f1ac1d676a09e3b2)
が、mysqlと競合するからmysqlをunlinkせよと。。
開発中のプロジェクトがMySQLで動いているものも多いのでちょっとサクッとMariaDBに変更するわけにはいかないっす。
1 2 3 4 5 6 7 8 9 10 11 12 |
$ brew install mariadb Error: Cannot install mariadb because conflicting formulae are installed. mysql: because mariadb, mysql, and percona install the same binaries. Please `brew unlink mysql` before continuing. Unlinking removes a formula's symlinks from /usr/local. You can link the formula again after the install finishes. You can --force this install, but the build may fail or cause obscure side-effects in the resulting software. |
しょうがないのでVagrantでCentos6.5でやりますわ。
主にこちらを参考にさせていただきました。
http://love-zawa.hatenablog.com/entry/2014/07/24/CentOS_6_5%E3%81%ABMariaDB_10_0_12%E3%82%92%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB(yum%E3%82%92%E4%BD%BF%E7%94%A8)
yumリポジトリを追加。
1 2 3 4 5 6 7 8 9 10 |
$ rpm --import https://yum.mariadb.org/RPM-GPG-KEY-MariaDB $ sudo vi /etc/yum.repos.d/mariadb.repo [mariadb] name = MariaDB baseurl = http://yum.mariadb.org/10.0.12/centos6-x86 gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB gpgcheck=1 enabled=1 |
yumでインストールします。
怒られました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
$ sudo yum -y install MariaDB-client MariaDB-server --> Finished Dependency Resolution Error: Package: 2:postfix-2.6.6-6.el6_5.x86_64 (base) Requires: libmysqlclient.so.16()(64bit) Removing: mysql-libs-5.1.71-1.el6.x86_64 (@anaconda-CentOS-201311272149.x86_64/6.5) libmysqlclient.so.16()(64bit) Obsoleted By: MariaDB-compat-10.0.12-1.el6.i686 (mariadb) Not found Updated By: mysql-libs-5.1.73-3.el6_5.x86_64 (base) libmysqlclient.so.16()(64bit) Error: Package: 2:postfix-2.6.6-6.el6_5.x86_64 (base) Requires: libmysqlclient.so.16(libmysqlclient_16)(64bit) Removing: mysql-libs-5.1.71-1.el6.x86_64 (@anaconda-CentOS-201311272149.x86_64/6.5) libmysqlclient.so.16(libmysqlclient_16)(64bit) Obsoleted By: MariaDB-compat-10.0.12-1.el6.i686 (mariadb) Not found Updated By: mysql-libs-5.1.73-3.el6_5.x86_64 (base) libmysqlclient.so.16(libmysqlclient_16)(64bit) You could try using --skip-broken to work around the problem You could try running: rpm -Va --nofiles --nodigest |
mysql-libsをremoveするといけるみたい。うん、うまくいった。
1 2 |
$ yum remove mysql-libs |
初期化処理を実行。これは多分やらなくても良いかも。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
$ mysql_secure_installation /usr/bin/mysql_secure_installation: line 379: find_mysql_client: コマンドが見つかりません NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here. Enter current password for root (enter for none): OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation. Set root password? [Y/n] n ... skipping. By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? [Y/n] n ... skipping. Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y/n] n ... skipping. By default, MariaDB comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? [Y/n] n ... skipping. Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] n ... skipping. Cleaning up... All done! If you've completed all of the above steps, your MariaDB installation should now be secure. Thanks for using MariaDB! |
MariaDBサービスが追加されるわけではなく、mysqlサービスをそのまま使うみたい。
1 2 3 |
$ sudo chkconfig mysql on $ sudo service mysql start |
DBに接続するのも、mysqlコマンドのままでいける。普通にインストールしたらmysqlはしれっとリプレースされるのか。
1 2 3 4 5 6 7 8 9 10 11 |
$ mysql -u root Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 4 Server version: 10.0.12-MariaDB MariaDB Server Copyright (c) 2000, 2014, Oracle, SkySQL Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> |
基本的にはmysql互換らしいから大丈夫っちゃ大丈夫だとは思うけど。
http://www.e-agency.co.jp/column/20130208.html
Dynamic Columnsを試して見る
こちらが本題。Dynamic Columnsはそのまま動的カラムってことなんですが、NoSQLの仕組みを融合したようなものです。
PostgresであればJSON型というものが存在していて、1カラムに構造的なJSONデータを持つことができるので、RDBの堅牢さとドキュメント指向DBの柔軟さのおいしいとこどりができたりします。
ざっくり言っちゃうとBLOB型で同じようなことができる仕組みですね。JSONとは限らないんだけど。
具体的なSQLはこちらを参考にさせていただきました。
http://d.hatena.ne.jp/interdb/20140414/1397448302
試しようにDBとテーブルを作ります。
1 2 3 4 5 |
MariaDB [(none)]> create database sampledb; MariaDB [(none)]> use sampledb; Database changed MariaDB [sampledb]> CREATE TABLE dynamic_col_test (id int, dynamic_col blob); |
dynamic_colにkey,valueを保存して確認してみます。
普通にselectするとBLOB型なのでちょっと読みづらいですね。
1 2 |
MariaDB [sampledb]> INSERT INTO dynamic_col_test VALUES (1, COLUMN_CREATE('name', 'Alice', 'color', 'red')); |
1 2 3 4 5 6 7 8 |
MariaDB [sampledb]> select * from dynamic_col_test; +------+----------------------------------+ | id | dynamic_col | +------+----------------------------------+ | 1 | c namecolor!Alice!red | +------+----------------------------------+ 1 |
dynamic_colにもう一件データを追加してまたselect
してみます。一件追加されているのがわかります。
1 2 |
MariaDB [sampledb]> INSERT INTO dynamic_col_test VALUES (1, COLUMN_CREATE('name', 'Bob', 'color', 'blue')); |
1 2 3 4 5 6 7 8 |
MariaDB [sampledb]> select * from dynamic_col_test; +------+----------------------------------+ | id | dynamic_col | +------+----------------------------------+ | 1 | c namecolor!Alice!red | | 1 | C namecolor!Bob!blue | +------+----------------------------------+ |
もうちょっと読みやすい形式で取得します。COLUMN_GETを使用すれば普通のカラムっぽい形式で取得できます。
1 2 3 4 5 6 7 8 |
MariaDB [sampledb]> SELECT id, COLUMN_GET(dynamic_col, 'name' as char) as name FROM dynamic_col_test; +------+-------+ | id | name | +------+-------+ | 1 | Alice | | 1 | Bob | +------+-------+ |
nameだけではなく、colorも同時に取得したい場合。複数のフィールドを取ろうとすると割と構文が冗長なっちゃいますね。別の書き方があるのかな?
1 2 3 4 5 6 7 8 |
MariaDB [sampledb]> SELECT id, COLUMN_GET(dynamic_col, 'name' as char) as name, COLUMN_GET(dynamic_col, 'color' as char) as color FROM dynamic_col_test; +------+-------+-------+ | id | name | color | +------+-------+-------+ | 1 | Alice | red | | 1 | Bob | blue | +------+-------+-------+ |
dynamic_colにageフィールドを追加してみます。
1 2 |
MariaDB [sampledb]> UPDATE dynamic_col_test SET dynamic_col = COLUMN_ADD(dynamic_col, 'age', 27) WHERE id = 1; |
selectで確認してみます。
うーん。複数フィールドとれてはいるけど、これだとbobもaliceも27歳になっちゃってる。COLUMN_ADDで指定した値は全ドキュメントに反映されるのかな?
1 2 3 4 5 6 7 8 |
MariaDB [sampledb]> SELECT id, COLUMN_GET(dynamic_col, 'name' as char) as name, COLUMN_GET(dynamic_col, 'color' as char) as color, COLUMN_GET(dynamic_col, 'age' as char) as age FROM dynamic_col_test; +------+-------+-------+------+ | id | name | color | age | +------+-------+-------+------+ | 1 | Alice | red | 27 | | 1 | Bob | blue | 27 | +------+-------+-------+------+ |
dynamic_colに存在するすべてのフィールド名を取得してみます。column_listというのを使います。
1 2 3 4 5 6 7 8 |
MariaDB [sampledb]> select id, column_list(dynamic_col) from dynamic_col_test; +------+--------------------------+ | id | column_list(dynamic_col) | +------+--------------------------+ | 1 | `age`,`name`,`color` | | 1 | `age`,`name`,`color` | +------+--------------------------+ |
column_jsonを使えばjson形式で取得できますね。これは使い勝手良さそう。
1 2 3 4 5 6 7 8 |
MariaDB [sampledb]> select id, column_json(dynamic_col) from dynamic_col_test; +------+-----------------------------------------+ | id | column_json(dynamic_col) | +------+-----------------------------------------+ | 1 | {"age":27,"name":"Alice","color":"red"} | | 1 | {"age":27,"name":"Bob","color":"blue"} | +------+-----------------------------------------+ |
実際アプリケーションで使おうとするとDynamic Column内で検索したくなりますよね。
こちらを参考にやってみました。
https://mariadb.com/blog/dynamic-columns-tutorial-part-2-searching-and-updating
nameがBobのデータだけ取得してみます。
1 2 3 4 5 6 7 |
MariaDB [sampledb]> SELECT id, column_json(dynamic_col) FROM dynamic_col_test WHERE COLUMN_GET(dynamic_col, 'name' AS CHAR) = 'Bob'; +------+----------------------------------------+ | id | column_json(dynamic_col) | +------+----------------------------------------+ | 1 | {"age":27,"name":"Bob","color":"blue"} | +------+----------------------------------------+ |
なるほどなるほど。基本的なところは少し理解しました。
通常のカラムとDynamic Columnsを同時に取得すると、1対多の結合結果みたいな感じで返ってくる感覚ですね。
まとめ
実は言うと今回はRailsから使いたかったので、ActiveRecordがDynamic Columnsに対応しているのがどうかコミットログ読んだりしたけど、MariaDBに関するログは見つけられなかった。
たぶんMySQLと同じようにSQLで何かする分には同じように使えるんだろうけどそれだったらMariaDBに乗り換える意味がないので微妙。
Dynamic Columnsを使いたい部分は自前でSQL組む感じで使えるかもだけどちょっとめんどそうだからやめといた方が良さそう。

原田 敦

最新記事 by 原田 敦 (全て見る)
- Rails Engineでブログ機能追加するgemを作る - 2015年3月15日
- WEBエンジニア一人だけでサービスを作りきる方法-夫婦のための自動ごはん予定お知らせサービス「GoHaaan」制作でやったこと - 2015年3月7日
- CentOS6でMariaDBのDynamic Columnsを試してみた - 2015年2月28日