Skip to content

Instantly share code, notes, and snippets.

@ryugoo
Created May 22, 2012 14:19
Show Gist options
  • Save ryugoo/2769361 to your computer and use it in GitHub Desktop.
Save ryugoo/2769361 to your computer and use it in GitHub Desktop.
私的 Linux ミドルウェア環境構築メモ

私的 Linux ミドルウェア環境構築メモ

よく使う Linux ミドルウェアの環境構築メモ。対象 Linux は CentOS, Scientific Linux, Oracle Linux の 6 系。
既に gcc などの build-essentials はインストールしているものとして考える。インストールしていなければ、

yum groupinstall "Development Tools"

あたりで突っ込んでおこう。あとは wget とか w3m あたりは入れておこう。
ちなみに想定しているアーキテクチャは x86_64 なので要注意。

MySQL 関係

MySQL 関係を整える。

MySQL 5.5.x にする

デフォルトのリポジトリからダウンロードできる MySQL のバージョンは 5.1 系で安定はしているけど、
InnoDB 関係がパワーアップしていることを考えると 5.5 系をインストールしたいので rpm をダウンロードして入れる。

ダウンロードからインストールまで

作業は全て /opt 以下で行う。作業は root 権限を持つユーザで行う。ここでは root で行う。

普通にダウンロードしようとすると Oracle のサイトからチマチマとダウンロードする必要があって面倒くさい。
なので JAIST のパブリックリポジトリから引っ張ってくる。

cd /opt
wget http://ftp.jaist.ac.jp/pub/mysql/Downloads/MySQL-5.5/MySQL-client-5.5.25-1.el6.x86_64.rpm
wget http://ftp.jaist.ac.jp/pub/mysql/Downloads/MySQL-5.5/MySQL-devel-5.5.25-1.el6.x86_64.rpm
wget http://ftp.jaist.ac.jp/pub/mysql/Downloads/MySQL-5.5/MySQL-embedded-5.5.25-1.el6.x86_64.rpm
wget http://ftp.jaist.ac.jp/pub/mysql/Downloads/MySQL-5.5/MySQL-server-5.5.25-1.el6.x86_64.rpm
wget http://ftp.jaist.ac.jp/pub/mysql/Downloads/MySQL-5.5/MySQL-shared-5.5.25-1.el6.x86_64.rpm
wget http://ftp.jaist.ac.jp/pub/mysql/Downloads/MySQL-5.5/MySQL-test-5.5.25-1.el6.x86_64.rpm
wget http://ftp.jaist.ac.jp/pub/mysql/Downloads/MySQL-5.5/MySQL-shared-compat-5.5.25-1.el6.x86_64.rpm
yum install -y MySQL-*.rpm

これで MySQL 5.5 系のインストールは完了。

動作確認と my.cnf の設定

mysql --version
>>> mysql  Ver 14.14 Distrib 5.5.25, for Linux (x86_64) using readline 5.1
/etc/init.d/mysql start
>>> Starting MySQL... SUCCESS!
mysql_secure_installation
>>> Enter current password for root (enter for none): [PRESS ENTER KEY]
>>> Set root password? [Y/n] Y
>>> New password: [Typing Your Password]
>>> Re-enter new password: [Re-Typing Your Password]
>>> Remove anonymous users? [Y/n] Y
>>> Disallow root login remotely? [Y/n] Y
>>> Remove test database and access to it? [Y/n] Y
>>> Reload privilege tables now? [Y/n] Y
mysql -uroot -p
>>> … Server version: 5.5.25 MySQL Community Server (GPL) …
>>> mysql > exit
    Bye

ここまでで MySQL 5.5.x のインストールと動作確認ができているので、次に日本語環境で使うための my.cnf を最低限書く。

vi /etc/my.cnf

以下の内容を記述。

[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
character-set-server=utf8
innodb_file_per_table

文字化けさせないための最小限なので、 InnoDB や MyISAM のチューニングは別途行うこと。
:wq で保存をしたら設定反映と確認。あとで Python と連携するための DB を1つ作っておく。

/etc/init.d/mysql restart
>>> Shutting down MySQL. SUCCESS!
>>> Starting MySQL.. SUCCESS!
mysql -uroot -p
>>> mysql > SHOW GLOBAL VARIABLES LIKE 'char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
>>> CREATE DATABASE `test_db` CHARACTER SET `utf8`;
    Query OK, 1 row affected (0.00 sec)
>>> mysql > exit
    Bye

filesystem 以外全部 UTF-8 (・∀・)
システム立ち上げ時に MySQL も自動で起動するようにしておきたかったら以下のコマンドも実行しておく。

chkconfig mysql on
chkconfig mysql --list
>>> mysql          	0:off    1:off    2:on    3:on    4:on    5:on    6:off

Python 関係

Python 関係を整える。

Python 2.7.x にする

デフォルトでインストールされている Python は 2.6.6 なので、これを 2.7 系にする。
これを書いている時点の最新版は 2.7.3 なので、それをデフォルトの Python にする。

ダウンロードからインストールまで

作業は全て /opt 以下で行う。作業は root 権限を持つユーザで行う。ここでは root で行う。

cd /opt
wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz
tar xzvf Python-2.7.3.tgz
cd Python-2.7.3
./configure --enable-shared --with-threads ---enable-unicode=ucs4
make
make install
cp libpython2.7.so libpython2.7.so.1.0 /usr/lib
/sbin/ldconfig

これで Python 2.7.3 のインストールは完了。

動作確認と easy_install & pip 環境構築

python -V
>>> Python 2.7.3
wget http://peak.telecommunity.com/dist/ez_setup.py
python ez_setup.py
easy_install --help
>>> ...
easy_install pip
pip --help
>>> …

ここまで動けば Python 2.7.3 の環境構築は終わり。本当は virtualenv 使って切り分けるべきなんだろうけどね。面倒くさい。

MySQL と連携させる

MySQL-Python モジュールをインストールして MySQL と連携させる。

ダウンロードからインストールまで

pip install MySQL-Python
    … Successfully installed MySQL-Python
    Cleaning up...

動作確認

python
>>> import MySQLdb as db
>>> DSN = {}
>>> DSN["host"] = "localhost"
>>> DSN["user"] = "root"
>>> DSN["passwd"] = 設定した MySQL の root パスワード
>>> DSN["use_unicode"] = True 
>>> DSN["charset"] = "utf8"
>>> DSN["db"] = "test_db"
>>> con = db.connect(**DSN)
>>> cur = con.cursor()
>>> create_table = """CREATE TABLE `test_table` (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `name` text, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;"""
>>> cur.execute(create_table)
    0L
>>> insert_stmt = """INSERT INTO `test_table` VALUES(0, %s);"""
>>> cur.execute(insert_stmt, (u"日本語の文字列も大丈夫"))
    1L
>>> con.commit()
>>> select = """SELECT * FROM `test_table`;"""
>>> cur.execute(select)
    1L
>>> result = cur.fetchall()
>>> [vals for vals in result][0]
    (1L, u'\u65e5\u672c\u8a9e\u306e\u6587\u5b57\u5217\u3082\u5927\u4e08\u592b')
>>> cur.close()
>>> con.close()
>>> exit()

ここまで動けば Python 2.7.3 と MySQL 5.5.x の連携は終わり。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment