盡管用文件形式將數(shù)據(jù)保存到磁盤,已經(jīng)是一種不錯(cuò)的方式。但是,人們還是發(fā)明了更具有格式化特點(diǎn),并且寫入和讀取更快速便捷的東西——數(shù)據(jù)庫(如果閱讀港臺(tái)的資料,它們稱之為“資料庫”)。維基百科對(duì)數(shù)據(jù)庫有比較詳細(xì)的說明:
數(shù)據(jù)庫指的是以一定方式儲(chǔ)存在一起、能為多個(gè)用戶共享、具有盡可能小的冗余度、與應(yīng)用程序彼此獨(dú)立的數(shù)據(jù)集合。
到目前為止,地球上有三種類型的數(shù)據(jù):
在本教程中,我們主要介紹常用的開源的數(shù)據(jù)庫,其中 MySQL 是典型代表。
MySQL 是一個(gè)使用非常廣泛的數(shù)據(jù)庫,很多網(wǎng)站都是用它。關(guān)于這個(gè)數(shù)據(jù)庫有很多傳說。例如維基百科上這么說:
MySQL(官方發(fā)音為英語發(fā)音:/ma? ??skju???l/ "My S-Q-L",[1],但也經(jīng)常讀作英語發(fā)音:/ma? ?si?kw?l/ "My Sequel")原本是一個(gè)開放源代碼的關(guān)系數(shù)據(jù)庫管理系統(tǒng),原開發(fā)者為瑞典的 MySQL AB 公司,該公司于 2008 年被升陽微系統(tǒng)(Sun Microsystems)收購。2009 年,甲骨文公司(Oracle)收購升陽微系統(tǒng)公司,MySQL 成為 Oracle 旗下產(chǎn)品。
MySQL 在過去由于性能高、成本低、可靠性好,已經(jīng)成為最流行的開源數(shù)據(jù)庫,因此被廣泛地應(yīng)用在 Internet 上的中小型網(wǎng)站中。隨著 MySQL 的不斷成熟,它也逐漸用于更多大規(guī)模網(wǎng)站和應(yīng)用,比如維基百科、Google 和 Facebook 等網(wǎng)站。非常流行的開源軟件組合 LAMP 中的“M”指的就是 MySQL。
但被甲骨文公司收購后,Oracle 大幅調(diào)漲 MySQL 商業(yè)版的售價(jià),且甲骨文公司不再支持另一個(gè)自由軟件項(xiàng)目 OpenSolaris 的發(fā)展,因此導(dǎo)致自由軟件社區(qū)們對(duì)于 Oracle 是否還會(huì)持續(xù)支持 MySQL 社區(qū)版(MySQL 之中唯一的免費(fèi)版本)有所隱憂,因此原先一些使用 MySQL 的開源軟件逐漸轉(zhuǎn)向其它的數(shù)據(jù)庫。例如維基百科已于 2013 年正式宣布將從 MySQL 遷移到 MariaDB 數(shù)據(jù)庫。
不管怎么著,MySQL 依然是一個(gè)不錯(cuò)的數(shù)據(jù)庫選擇,足夠支持讀者完成一個(gè)相當(dāng)不小的網(wǎng)站。
你的電腦或許不會(huì)天生就有 MySQL(是不是有的操作系統(tǒng),在安裝的時(shí)候就內(nèi)置了呢?的確有,所以特別推薦 Linux 的某發(fā)行版),它本質(zhì)上也是一個(gè)程序,若有必要,須安裝。
我用 ubuntu 操作系統(tǒng)演示,因?yàn)槲蚁嘈抛x者將來在真正的工程項(xiàng)目中,多數(shù)情況下是要操作 Linux 系統(tǒng)的服務(wù)器,并且,我酷愛用 ubuntu。還有,本教程的目標(biāo)是 from beginner to master,不管是不是真的 master,總要裝得像,Linux 能夠給你撐門面。
第一步,在 shell 端運(yùn)行如下命令:
sudo apt-get install mysql-server
運(yùn)行完畢,就安裝好了這個(gè)數(shù)據(jù)庫。是不是很簡單呢?當(dāng)然,當(dāng)然,還要進(jìn)行配置。
第二步,配置 MySQL
安裝之后,運(yùn)行:
service mysqld start
啟動(dòng) mysql 數(shù)據(jù)庫。然后進(jìn)行下面的操作,對(duì)其進(jìn)行配置。
默認(rèn)的 MySQL 安裝之后根用戶是沒有密碼的,注意,這里有一個(gè)名詞“根用戶”,其用戶名是:root。運(yùn)行:
$mysql -u root
在這里之所以用 -u root 是因?yàn)槲椰F(xiàn)在是一般用戶(firehare),如果不加 -u root 的話,mysql 會(huì)以為是 firehare 在登錄。
進(jìn)入 mysql 之后,會(huì)看到>符號(hào)開頭,這就是 mysql 的命令操作界面了。
下面設(shè)置 Mysql 中的 root 用戶密碼了,否則,Mysql 服務(wù)無安全可言了。
mysql> GRANT ALL PRIVILEGES ON *.* TO root@localhost IDENTIFIED BY "123456";
用 123456 做為 root 用戶的密碼,應(yīng)該是非常愚蠢的,如果在真正的項(xiàng)目中,最好別這樣做,要用大小寫字母與數(shù)字混合的密碼,且不少于 8 位。
以后如果在登錄數(shù)據(jù)庫,就可以用剛才設(shè)置的密碼了。
安裝之后,就要運(yùn)行它,并操作這個(gè)數(shù)據(jù)庫。
$ mysql -u root -p
Enter password:
輸入數(shù)據(jù)庫的密碼,之后出現(xiàn):
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 373
Server version: 5.5.38-0ubuntu0.14.04.1 (Ubuntu)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
看到這個(gè)界面內(nèi)容,就說明你已經(jīng)進(jìn)入到數(shù)據(jù)里面了。接下來就可以對(duì)這個(gè)數(shù)據(jù)進(jìn)行操作。例如:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| carstore |
| cutvideo |
| itdiffer |
| mysql |
| performance_schema |
| test |
+--------------------+
用這個(gè)命令,就列出了當(dāng)前已經(jīng)有的數(shù)據(jù)庫。
對(duì)數(shù)據(jù)庫的操作,除了用命令之外,還可以使用一些可視化工具。比如 phpmyadmin 就是不錯(cuò)的。
更多數(shù)據(jù)庫操作的知識(shí),這里就不介紹了,讀者可以參考有關(guān)書籍。
MySQL 數(shù)據(jù)庫已經(jīng)安裝好,但是 Python 還不能操作它,還要繼續(xù)安裝 Python 操作數(shù)據(jù)庫的模塊——Python-MySQLdb
Python-MySQLdb 是一個(gè)接口程序,Python 通過它對(duì) mysql 數(shù)據(jù)實(shí)現(xiàn)各種操作。
在編程中,會(huì)遇到很多類似的接口程序,通過接口程序?qū)α硗庖粋€(gè)對(duì)象進(jìn)行操作。接口程序就好比鑰匙,如果要開鎖,人直接用手指去捅,肯定是不行的,那么必須借助工具,插入到鎖孔中,把鎖打開,之后,門開了,就可以操作門里面的東西了。那么打開鎖的工具就是接口程序。誰都知道,用對(duì)應(yīng)的鑰匙開鎖是最好的,如果用別的工具(比如錘子),或許不便利(其實(shí)還分人,也就是人開鎖的水平,如果是江洋大盜或者小毛賊什么的,擅長開鎖,用別的工具也便利了),也就是接口程序不同,編碼水平不同,都是考慮因素。
啰嗦這么多,一言蔽之,Python-MySQLdb 就是打開 MySQL 數(shù)據(jù)庫的鑰匙。
如果要源碼安裝,可以這里下載 Python-mysqldb:https://pypi.Python.org/pypi/MySQL-Python/
下載之后就可以安裝了。
ubuntu 下可以這么做:
sudo apt-get install build-essential Python-dev libmysqlclient-dev
sudo apt-get install Python-MySQLdb
也可以用 pip 來安裝:
pip install mysql-Python
安裝之后,在 python 交互模式下:
>>> import MySQLdb
如果不報(bào)錯(cuò),恭喜你,已經(jīng)安裝好了。如果報(bào)錯(cuò),恭喜你,可以借著錯(cuò)誤信息提高自己的計(jì)算機(jī)水平了,請求助于 google 大神。
要先找到老婆,才能談如何養(yǎng)育自己的孩子,同理連接數(shù)據(jù)庫之先要建立數(shù)據(jù)庫。
$ mysql -u root -p
Enter password:
進(jìn)入到數(shù)據(jù)庫操作界面:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 373
Server version: 5.5.38-0ubuntu0.14.04.1 (Ubuntu)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
輸入如下命令,建立一個(gè)數(shù)據(jù)庫:
mysql> create database qiwsirtest character set utf8;
Query OK, 1 row affected (0.00 sec)
注意上面的指令,如果僅僅輸入:create database qiwsirtest,也可以,但是,我在后面增加了 character set utf8,意思是所建立的數(shù)據(jù)庫 qiwsirtest,編碼是 utf-8 的,這樣存入漢字就不是亂碼了。
看到那一行提示:Query OK, 1 row affected (0.00 sec),就說明這個(gè)數(shù)據(jù)庫已經(jīng)建立好了,名字叫做:qiwsirtest
數(shù)據(jù)庫建立之后,就可以用 Python 通過已經(jīng)安裝的 mysqldb 來連接這個(gè)名字叫做 qiwsirtest 的庫了。
>>> import MySQLdb
>>> conn = MySQLdb.connect(host="localhost",user="root",passwd="123123",db="qiwsirtest",port=3306,charset="utf8")
逐個(gè)解釋上述命令的含義:
注:connect 中的 host、user、passwd 等可以不寫,只有在寫的時(shí)候按照 host、user、passwd、db (可以不寫)、port 順序?qū)懢涂梢裕丝谔?hào) port=3306 還是不要省略的為好,如果沒有 db 在 port 前面,直接寫 3306 會(huì)報(bào)錯(cuò).
其實(shí),關(guān)于 connect 的參數(shù)還不少,下面摘抄來自mysqldb 官方文檔的內(nèi)容,把所有的參數(shù)都列出來,還有相關(guān)說明,請看官認(rèn)真閱讀。不過,上面幾個(gè)是常用的,其它的看情況使用。
connect(parameters...)
Constructor for creating a connection to the database. Returns a Connection Object. Parameters are the same as for the MySQL C API. In addition, there are a few additional keywords that correspond to what you would pass mysql_options() before connecting. Note that some parameters must be specified as keyword arguments! The default value for each parameter is NULL or zero, as appropriate. Consult the MySQL documentation for more details. The important parameters are:
If False, text-like columns are returned as normal strings, but you can always write Unicode strings.
This must be a keyword parameter.
If not present, the default character set is used.
This must be a keyword parameter.
If not present, the session SQL mode will be unchanged.
This must be a keyword parameter.
已經(jīng)完成了數(shù)據(jù)庫的連接。
總目錄 | 上節(jié):將數(shù)據(jù)存入文件 | 下節(jié):mysql數(shù)據(jù)庫(2)
如果你認(rèn)為有必要打賞我,請通過支付寶:qiwsir@126.com,不勝感激。