一、安裝Connector/Python:
python
# wget http://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-python-1.0.11.zip # unzip mysql-connector-python-1.0.11.zip # cd mysql-connector-python-1.0.11 # python setup.py install
二、檢查是否安裝成功mysql
>>> from distutils.sysconfig import get_python_lib >>> print get_python_lib() /usr/local/lib/python2.7/site-packages
三、安裝MySQL-python-1.2.5sql
下載地址https://pypi.python.org/pypi/MySQL-python/1.2.5
python2.7
安裝同步驟1測試
四、安裝3時的問題fetch
若是使用python setup.py 安裝,提示mysql_config找不到 EnvironmentError: mysql_config not found 須要安裝下面開發包,有的須要自行下載 #apt-get install libmysqlclient-dev #apt-get install python-devel # apt-get install libmysqld-dev 安裝完成之後須要修改MySQL-python-1.2.3文件夾下面的setup_posix.py中的第26行, mysql_config.path = "mysql_config"改成mysql_config的路徑 whereis mysql_config查找路徑 mysql_config.path = "/usr/bin/mysql_config" 再使用 python setup.py build python setup.py install 就能夠
五、測試使用ui
>>> import mysql.connector >>> conn = mysql.connector.connect(user='root', password='123456', database='test', use_unicode=True) >>> cursor = conn.cursor() >>> cursor.execute('create table user (id varchar(20) primary key, name varchar(20))') >>> cursor.execute('insert into user (id, name) values (%s, %s)', ['1', 'Michael']) >>> cursor.rowcount 1 >>> conn.commit() >>> cursor.close() True >>> cursor = conn.cursor() >>> cursor.execute('select * from user where id = %s', '1') >>> values = cursor.fetchall() >>> values [(u'1', u'Michael')] >>> cursor.close() True >>> conn.close()