CentOS 7 install mysql connector for python3.6
Go to website
https://pypi.org/project/mysqlclient/#files
and download the file
mysqlclient-1.3.12.tar.gz
place in /root dir
Now from server console as root user (or su)
cd /root
gzip -d mysqlclient-1.3.12.tar.gz
tar -xvf mysqlclient-1.3.12.tar
cd /root/mysqlclient-1.3.12
yum install -y mariadb-devel
python3.6 setup.py build
python3.6 setup.py install
Now in py script
#!/usr/bin/python3.6
import MySQLdb
# Open database connection
db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )
# prepare a cursor object using cursor() method
cursor = db.cursor()
# execute SQL query using execute() method.
cursor.execute("SELECT VERSION()")
# Fetch a single row using fetchone() method.
data = cursor.fetchone()
print ("Database version : %s " % data)
# disconnect from server
db.close()
If you use this in a web browser
#!/usr/bin/python3.6
import os
import cgi
import MySQLdb
# Set content typr for web browser use
print ("Content-type: text/html\n")
# Open database connection
db = MySQLdb.connect("localhost","root","password","information_schema" )
# prepare a cursor object using cursor() method
cursor = db.cursor()
# execute SQL query using execute() method.
cursor.execute("SELECT VERSION()")
# Fetch a single row using fetchone() method.
data = cursor.fetchone()
print ("Database version : %s " % data)
# disconnect from server
db.close()
No comments:
Post a Comment