python mysql 연동

 

설치 하기 전에 python 버전을 확인.

[root@test install]# python -V
Python 2.4.3

MySQL-python 을 사용하려고 하는데, MySQL-python 은 2.4-2.7버전을 지원.
http://sourceforge.net/projects/mysql-python/ 의 아래부분 참고.
* Python versions 2.4-2.7; Python 3 support coming soon.

필요한 라이브러리를 설치.

[root@test install]# yum install gcc mysql-devel python-devel

SetupTools 설치.
SetupTools는 Python으로 만든 프로그램을 자동으로 다운로드하고 설치하는 것을 지원하는 python distutils 의 확장 유틸리티 이다.

[root@test install]# wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz#md5=7df2a529a074f613b509fb44feefe74e
[root@test install]# tar xzvf setuptools-0.6c11.tar.gz
[root@test install]# cd setuptools-0.6c11
[root@test install]# python setup.py install

 
MySQL-python 설치

[root@test install]# wget http://downloads.sourceforge.net/mysql-python/MySQL-python-1.2.3.tar.gz
[root@test install]# tar zxvf MySQL-python-1.2.3.tar.gz
[root@test install]# whereis mysql_config
mysql_config: /usr/bin/mysql_config /usr/share/man/man1/mysql_config.1.gz
[root@test install]# cd MySQL-python-1.2.3
[root@test install]# vi site.cfg
# The path to mysql_config.
# Only use this if mysql_config is not on your PATH, or you have some weird
# setup that requires it.
mysql_config = /usr/bin/mysql_config

[root@test install]# python setup.py build
[root@test install]# python setup.py install

설치 끝!!
 
mysql 접속이 정상적으로 잘 되는지 테스트 프로그램을 작성.

[root@test install]# vi test_mysql.py
import MySQLdb

db=MySQLdb.connect(host="localhost",user="userid",passwd="passwd",db="test")
db.query("""select * from test""")
r=db.store_result()

print(r.fetch_row())
print(r.fetch_row())
print(r.fetch_row())

테스트 프로그램(test_mysql.py) 구동

[root@test install]# python test_mysql.py
((1L, '20121024'),)
((2L, '20121023'),)
((3L, '20121024'),) 

GoooooD!!
 

This entry was posted in Python/Ruby/Perl and tagged , , . Bookmark the permalink.

One Response to python mysql 연동

댓글 남기기