python3.6 import MySQLdb ( python 3.6 mysql import )

O/S : CentOS 7.x

python 3.6 버전에서 mysql을 사용하려면 MySQLdb를 사용해야 한다.
기본적으로 yum으로 설치한 python3.6 버전은 지원되지 않으므로 별도 컴파일을 통해서 지원할수 있게 진행한다 

1. 기본 python3.6으로 접속후 import MySQLdb를 실행하면 에러가 발생된다

[[email protected] ~]# python3.6
Python 3.6.4 (default, Dec 19 2017, 14:48:12) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'MySQLdb'

2. 그럼 소스파일을 다운로드 후 압축 해제
다운로드 주소 : https://pypi.python.org/pypi/mysqlclient

[[email protected] ~]# wget https://pypi.python.org/packages/6f/86/bad31f1c1bb0cc99e88ca2adb7cb5c71f7a6540c1bb001480513de76a931/mysqlclient-1.3.12.tar.gz#md5=dbf1716e2c01966afec0198d75ce7e69

[[email protected] ~]# tar xvfz mysqlclient-1.3.12.tar.gz 

[[email protected] ~]# cd mysqlclient-1.3.12

3. 빌드 진행 후 install 진행하기 앞서 site.cfg에서 mysql 경로를 지정해준다

[[email protected] mysqlclient-1.3.12]# vi site.cfg 

mysql_config = /usr/local/mysql/bin/mysql_config



[[email protected] mysqlclient-1.3.12]# /usr/bin/python3.6 setup.py build
running build
running build_py
creating build
creating build/lib.linux-x86_64-3.6
copying _mysql_exceptions.py -> build/lib.linux-x86_64-3.6
creating build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/__init__.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/compat.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/connections.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/converters.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/cursors.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/release.py -> build/lib.linux-x86_64-3.6/MySQLdb
copying MySQLdb/times.py -> build/lib.linux-x86_64-3.6/MySQLdb
creating build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/CR.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/ER.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
copying MySQLdb/constants/REFRESH.py -> build/lib.linux-x86_64-3.6/MySQLdb/constants
running build_ext
building '_mysql' extension
creating build/temp.linux-x86_64-3.6
gcc -pthread -Wno-unused-result -Wsign-compare -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -Dversion_info=(1,3,12,'final',0) -D__version__=1.3.12 -I/usr/local/mysql/include/mysql -I/usr/local/mysql/include/mysql/.. -I/usr/include/python3.6m -c _mysql.c -o build/temp.linux-x86_64-3.6/_mysql.o
gcc -pthread -shared -Wl,-z,relro build/temp.linux-x86_64-3.6/_mysql.o -L/usr/local/mysql/lib -L/usr/lib64 -lmysqlclient -lpthread -lz -lm -ldl -lssl -lcrypto -lpython3.6m -o build/lib.linux-x86_64-3.6/_mysql.cpython-36m-x86_64-linux-gnu.so


[[email protected] mysqlclient-1.3.12]# echo $?
0

[[email protected] mysqlclient-1.3.12]# /usr/bin/python3.6 setup.py install

 이제 python3.6 들어가서 import를 진행해보자 / 에러가 발생되지 않는다.

[[email protected] mysqlclient-1.3.12]# /usr/bin/python3.6
Python 3.6.4 (default, Dec 19 2017, 14:48:12) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb

MYSQL 버전을 알아보는 python 구문으로 이제 정상적으로 데이터베이스를 읽어들이는지 확인해보자

[[email protected] root]# vi mysql.py 


#!/usr/bin/python3.6
import MySQLdb
import time

### db info
db = MySQLdb.connect(host="localhost", # your host, usually localhost
 user="sqlmonitor", # your username
 passwd="sqlmonitor", # your password
 db="sqlmonitor") # name of the data base

# you execute all the queries you need
cursor = db.cursor()

cursor.execute("SELECT VERSION()")

data = cursor.fetchone()

print("database version : %s " %data)

db.close()

 python3.6 으로 해당 파일을 실행하면 MYSQL 버전을 확인할수 있다.

[[email protected] root]# python3.6 mysql.py 
database version : 10.1.31-MariaDB 

코멘트 쓰기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다.

다음의 HTML 태그와 속성을 사용할 수 있습니다:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



배송정보
배송조회를 하시려면 송장번호를 클릭하세요
배송조회
상품명
주문번호
택배사
송장번호