巨人の肩の上に登る

先人の積み重ねた発見に基づいて、なにかを発見しようとすることを指す。

MavericksでPythonからMariaDBを使ってみる

f:id:mayo_yamasaki:20140401165933p:plain

Mac OS X Mavericksで、MariaDBを使ってみた。

MariaDBとは

MariaDBは、MySQLから派生したOSSなリレーショナルデータベースです。
細かい話は下記の記事を参照されたい。

MariaDB - Wikipedia

MySQLとMariaDBの違い一覧 - 技術メモ置き場

MariaDBのinstall

今回は、homebrewを使ってinstallした。
公式で説明されている、Mac OS X へのinstall方法を参考にした。
Building MariaDB on Mac OS X using Homebrew - MariaDB Knowledge Base

brew installすると下記のようなErrorが出てしまったので、MySQLをunlinkしてから再実行すると解決した。

$ 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.
$ brew unlink mysql
Unlinking /usr/local/Cellar/mysql/5.6.16... 105 links removed
$ brew install mariadb

mariadbを使ってみる。

$ mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.5.9-log Source distribution

Copyright (c) 2000, 2014, Oracle, Monty Program Ab and others.

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

MySQL [(none)]>


PythonからMariaDBを使う

MariaDBのドライバは、基本的にMySQLと同じ物を使えるようです。今回は、MySQL-pythonを利用します。

pipでinstallしようとしたところ、下記のようなErrodで止まってしまいました。どうやら、Mavericksにアップデートしたことが原因のようです。

$ sudo pip install MySQL-pyhton
...
clang: error: unknown argument: '-mno-fused-madd' [-Wunused-command-line-argument-hard-error-in-future]

clang: note: this will be a hard error (cannot be downgraded to a warning) in the future

error: command 'cc' failed with exit status 1
...

Stackoverflowに似たような質問があり、その回答を試してみたところ上手くいきました。

$ export CFLAGS=-Qunused-arguments
$ export CPPFLAGS=-Qunused-arguments
$ sudo -E pip install MySQL-python

さっそく試してみる。

import MySQLdb
connection = MySQLdb.connect(host="HOST_NAME",db="DB_NAME",user="USER_NAME", passwd="PASSWARD")
cursor = connection.cursor()
cursor.execute("select * from TABLE_NAME")
result = cursor.fetchall()
print result
cursor.close()
connection.close()

簡単ですが、以上です。


関連記事