最快速最简单方法:
yum install MySQL-python
[root@node2 python_lib]# yum install MySQL-python
Loaded plugins: fastestmirror, refresh-packagekit, security
Determining fastest mirrors
* base: mirrors.aliyun.com
* extras: mirror.bit.edu.cn
* updates: mirrors.aliyun.com
base | 3.7 kB 00:00
extras | 3.4 kB 00:00
updates | 3.4 kB 00:00
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package MySQL-python.x86_64 0:1.2.3-0.3.c1.1.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=======================================================================================================================================================================
Package Arch Version Repository Size
=======================================================================================================================================================================
Installing:
MySQL-python x86_64 1.2.3-0.3.c1.1.el6 base 86 k
Transaction Summary
=======================================================================================================================================================================
Install 1 Package(s)
Total download size: 86 k
Installed size: 246 k
Is this ok [y/N]: y
Downloading Packages:
MySQL-python-1.2.3-0.3.c1.1.el6.x86_64.rpm | 86 kB 00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : MySQL-python-1.2.3-0.3.c1.1.el6.x86_64 1/1
Verifying : MySQL-python-1.2.3-0.3.c1.1.el6.x86_64 1/1
Installed:
MySQL-python.x86_64 0:1.2.3-0.3.c1.1.el6
Complete!
[root@node2 python_lib]# python
Python 2.6.6 (r266:84292, Nov 22 2013, 12:16:22)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>> conn=MySQLdb.connect(host="172.16.73.50",user="root",passwd="root",db="sc_centaurus",charset="utf8")
>>> cursor = conn.cursor()
>>> cursor.execute("select * from tb_sys_component")
3L
>>> data = cursor.fetchall()
>>> for row in data:
... print(row)
...
(37L, 46L, 22000L, u'poseidon', 0L, None)
(38L, 47L, 23000L, u'centaurus', 0L, None)
(39L, 48L, 21001L, u'adapter01', 0L, None)
>>> cursor.close()
>>> conn.close()
>>>