O/S : CentOS 7.x
python 3.6 버전에서 mysql을 사용하려면 MySQLdb를 사용해야 한다.
기본적으로 yum으로 설치한 python3.6 버전은 지원되지 않으므로 별도 컴파일을 통해서 지원할수 있게 진행한다
1. 기본 python3.6으로 접속후 import MySQLdb를 실행하면 에러가 발생된다
1 2 3 4 5 6 7 8 |
[root@localhost ~]# 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
1 2 3 4 5 |
[root@xinet ~]# wget https://pypi.python.org/packages/6f/86/bad31f1c1bb0cc99e88ca2adb7cb5c71f7a6540c1bb001480513de76a931/mysqlclient-1.3.12.tar.gz#md5=dbf1716e2c01966afec0198d75ce7e69 [root@localhost ~]# tar xvfz mysqlclient-1.3.12.tar.gz [root@localhost ~]# cd mysqlclient-1.3.12 |
3. 빌드 진행 후 install 진행하기 앞서 site.cfg에서 mysql 경로를 지정해준다
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 |
[root@localhost mysqlclient-1.3.12]# vi site.cfg mysql_config = /usr/local/mysql/bin/mysql_config [root@xinet 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 [root@xinet mysqlclient-1.3.12]# echo $? 0 [root@xinet mysqlclient-1.3.12]# /usr/bin/python3.6 setup.py install |
이제 python3.6 들어가서 import를 진행해보자 / 에러가 발생되지 않는다.
1 2 3 4 5 |
[root@xinet 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 구문으로 이제 정상적으로 데이터베이스를 읽어들이는지 확인해보자
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
[root@xinet 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 버전을 확인할수 있다.
1 2 |
[root@xinet root]# python3.6 mysql.py database version : 10.1.31-MariaDB |