Connecting from python to MySQL on CentOS 6.4

Install the required modules.

$ sudo yum -y install python-setuptools
$ sudo yum -y install python-devel
$ sudo easy_install pip
$ sudo pip install MySQL-python

I will test

mysql_test.py


import MySQLdb

connection = MySQLdb.connect(db="python_test", user="root")
cursor = connection.cursor(cursorclass=MySQLdb.cursors.DictCursor)
cursor.execute("select * from users")
result = cursor.fetchall()

print(result)

cursor.close()
connection.close()

result:

$ python mysql_test.py
({'id': 1L, 'name': 'user1'}, {'id': 2L, 'name': 'user2'})

Recommended Posts

Connecting from python to MySQL on CentOS 6.4
Switch from python2.7 to python3.6 (centos7)
Steps to install MySQL 8.0 on CentOS 8.1
Update Python on Mac from 2 to 3
Touch MySQL from Python 3
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Use MySQL from Python
Install Python3.4 on CentOS 6.6
Use MySQL from Python
Introducing Python 2.7 to CentOS 6.6
Install Python 2.7.3 on CentOS 5.4
Connect python to mysql
Problems connecting to MySQL from Docker environment (Debian)
Connect to MySQL with Python on Raspberry Pi
Post from Python to Slack
Cheating from PHP to Python
Use MySQL from Anaconda (python)
Preferences to generate animated GIFs from Python on Mac
Yum command to access MySQL with Python 3 on Linux
Install Python 3.8 on CentOS 7 (SCL)
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
From python to running instance on google cloud platform
Update python on Mac to 3.7-> 3.8
Connect to sqlite from python
Install Python 3.8 on CentOS 8 (AppStream)
Notes on installing Python on CentOS
How to install Python2.7 python3.5 with pyenv (on RHEL5 CentOS5) (2016 Nov)
Notes on importing data from MySQL or CSV with Python
Connect to centos6 on virtualbox with ssh connection from Mac
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
Call Matlab from Python to optimize
Steps to deploy EMLauncher on CentOS 8
Python 3.6 on Windows ... and to Xamarin.
Create folders from '01' to '12' with python
Introduction to Python Hands On Part 1
Notes on using MeCab from Python
Post from python to facebook timeline
Steps to install VirtualBox on CentOS
How to install PyPy on CentOS
Connect to utf8mb4 database from python
Set up Python environment on CentOS
How to install TensorFlow on CentOS 7
Python (from first time to execution)
Post images from Python to Tumblr
Create a python environment on centos
How to use Mysql in python
Notes on accessing dashDB from python
Install Python on CentOS using Pyenv
How to access wikipedia from python
Python to switch from another language
How to install Maven on CentOS
Steps to install python3 on mac
Build a python3 environment on CentOS7
Install Python on CentOS using pyenv
Call C / C ++ from Python on Mac
Did not change from Python 2 to 3
How to rebuild python environment from pyenv on Mac environment (El Capitan)
Things to note when running Python on EC2 from AWS Lambda
Simple code to call a python program from Javascript on EC2
[Python + heroku] From the state without Python to displaying something on heroku (Part 1)