目標:python
在Python中經過「import torndb」,去訪問mysql數據庫。mysql
一.安裝mysql-server:linux
1.ubuntu安裝mysql-server,
sudo apt-get install mysql-server
2.登錄mysql:
mysql -u root -p
3.mysql的一些經常使用操做:
1.show databases;
2.create database db_name;
3.use db_name;
4.show tables;
5.describe db; --顯示錶的結構
git
二.安裝mysqldbgithub
1.去https://sourceforge.net/projects/mysql-python/ 下載源碼
2.解壓:
tar xvzf MySQL-python-1.2.4b4.tar.gz
3.進入安裝目錄:
cd MySQL-python-1.2.4b4/
4.將site.cfg中的mysql_config = /usr/local/mysql/bin/mysql_config 這一行前的#去掉,而且把mysql_config的路徑設置正確(若是報mysql_config not found的錯誤,查看note1)。
5.編譯:
python setup.py build
6.安裝:
sudo python setup.py install
7.測試,在python的命令行:
import MySQLdb
note1: 報錯:mysql_config not found
1.用whereis mysql_config 查找mysql_config ,若是有,跳過2,執行3。
2.安裝mysql-dev:
sudo apt-get install libmysqlclient-dev
3.將site.cfg中的mysql_config = /usr/local/mysql/bin/mysql_config 這一行前的#去掉,而且把mysql_config的路徑設置正確。
參考:http://blog.csdn.net/lmh12506/article/details/9198661
note2: 報錯:_mysql.c:29:20: fatal error: Python.h: No such file or directory
1.須要安裝python-dev:
sudo apt-get install python-dev
參考:http://www.cyberciti.biz/faq/debian-ubuntu-linux-python-h-file-not-found-error-solution/
sql
三.安裝tornado:數據庫
1.下載源碼:https://github.com/bdarnell/torndb
2.解壓文件:
unzip torndb-master.zip
3.進入安裝目錄:
cd torndb-master/
4.編譯:
python setup.py build
5.安裝:
sudo python setup.py install
6.測試是否安裝成功,在python命令行下:
import torndb
效果:ubuntu
>>> import torndb
>>> mdb = torndb.Connection('127.0.0.1:3306', 'db_name', 'user_name', 'user_pass', max_idle_time=5)
>>> mdb
<torndb.Connection object at 0x2724ed0>
>>> mdb._ensure_connected()
>>> mdb.query("SELECT * from test")
[{'id': 1L, 'name': u'chenjf'}]
>>> tornado