python mysql connect_Python connect MySQL | 學步園

yum -y install rpm-build gcc-c++ mysql mysql-devel readline-devel openssl-devel gmp-devel ncurses-devel gdbm-devel expat-devel libGL-devel libX11-devel tcl-devel tk-devel tix-devel sqlite-devel db4-devel python python-devel

[root@lamop ~]# tar -zxvf setuptools-0.6c11.tar.gz

[root@lamop ~]# cd setuptools-0.6c11

[root@lamop setuptools-0.6c11]# python setup.py build

[root@lamop setuptools-0.6c11]# python setup.py install

[root@lamop ~]# tar -zxvf MySQL-python-1.2.3.tar.gz

[root@lamop ~]# cd MySQL-python-1.2.3

[root@lamop MySQL-python-1.2.3]# cat site.cfg |grep -v ^#

[options]

embedded = False

threadsafe = True

static = False

mysql_config = /usr/lib64/mysql/mysql_config

registry_key = SOFTWARE\MySQL AB\MySQL Server 5.0

[root@lamop MySQL-python-1.2.3]# python setup.py build

[root@lamop MySQL-python-1.2.3]# python setup.py install

[root@lamop ~]# python

Python 2.6.6 (r266:84292, Jul 10 2013, 22:48:45)

[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import MySQLdb

>>>

[root@lamop ~]# cat conn_mysql.py

#/usr/bin/env python

import MySQLdb

conn=MySQLdb.connect(host='localhost',port=30306,user='root',passwd='root',unix_socket='/data/mysql/mysql.sock',db='test')

cur=conn.cursor()

cur.execute('select @@version')

data=cur.fetchone()

print data

conn.close()

[root@lamop ~]# python conn_mysql.py

('5.5.25-log',)

[root@lamop ~]# cat show_var.py

#/usr/bin/env python

import MySQLdb

conn=MySQLdb.connect(host='localhost',port=30306,user='root',passwd='idontcare',unix_socket='/data/mysql/mysql.sock',db='mysql')

cur=conn.cursor()

runit=cur.execute("show variables like 'datadir'")

print "%s: %s" %(cur.fetchone())

#print cur.fetchone()

conn.close()

[root@lamop ~]#

[root@lamop ~]# python show_var.py

datadir: /data/mysql/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值