?
一、什麼是?MySQL
MySQL?(發音為?"My?Ess?Que?Ell")是?Tcx?公司(http://www.tcx.se)開發的一個多人使用、多執行緒的?SQL?資料庫?Server。MySQL?主要的目標在快速、穩定和容易使用。
MySQL?可在此?http://www.mysql.net/?取得。
二、MySQL?的安裝
本文所使用的?MySQL?版本為?mysql-3.22.27.tar.gz(原始碼檔),作業環境為?RedHat6.0?+CLE0.8?。
MySQL?預設情況下會安裝至?/usr/local?目錄下,不過為了日後移除方便,建議將?mysql?獨立安裝在?/usr/local/mysql?目錄。底下為安裝?MySQL?的步驟:
取得?mysql-3.22.27.tar.gz?後,?於?/usr/local?目錄下解開:?
#?cd?/usr/local
#?tar?zxvf?mysql-3.22.27.tar.gz
#?cd?mysql-3.22.27
設定?configure?安裝選項,選擇安裝目錄?(prefix)以及支援中文?Big5?碼(with-charset=big5):?
#?./configure?--prefix=/usr/local/mysql?\
#--with-charset=big5
開始編譯并安裝:?
#?make
#?make?install
#?scripts/mysql_install_db
最後一個步驟是用來產生?MySQL?grant?tables(會建立一個?mysql?資料庫和一些?tables,用來管理使用?MySQL?的授權資訊,也就是使用者有哪些使用資料庫的權限)。
三、啟動、停止?MySQL
要啟動?MySQL?的方法:(以本文將?MySQL?安裝在?/usr/local/mysql?為例)
#?/usr/local/mysql/share/mysql.server?start
注意在第一次執行前,須將?mysql.server?設成可執行(chmod?744?mysql.server),另外可將這行指令加在?/etc/rc.d/rc.local?檔中,讓?MySQL?在開機時自動啟動。
要停止?MySQL?的方法:
#?/usr/local/mysql/bin/mysqladmin?shutdown
如果你為?MySQL?Administrator?root?帳號(非作業系統的?root)設了密碼,要停止?MySQL?則必須像下列這樣做,MySQL?會詢問你?root?的密碼後才會執行?shutdown?的工作:
#?/usr/local/mysql/bin/mysqladmin?-u?root?-p?shutdown
四、管理與使用?MySQL?簡介
在你開始前?
MySQL?提供了許多工具?(Client?Tools)來與?MySQL?資料庫?Server?連線,其中最主要的為?mysql?交談式連線工具與?mysqladmin?公用程式,大部份時候使用者會用?mysql?來和資料庫?Server?交談。底下就以?mysql?連線工具來介紹如何維護與使用?MySQL。(以本文安裝為例,mysql?工具位於?/usr/local/mysql/bin/mysql)。
mysql?的使用語法如下:
mysql?[-u?username]?[-h?host]?[-p[password]]?[dbname]
MySQL?資料庫有自己一套使用者帳號與權限管控方法,所以這邊所指定的?username?與??password?是?MySQL?的使用者與密碼,而不是作業系統的使用者與密碼(當然任何使用者都能執行?mysql?,然後以?MySQL?的任何帳號進入使用)?。
在你第一次安裝好?MySQL?時,MySQL?的管理帳號為?root,沒有設定密碼?(非作業系統的?root)。所以在開始前,請先照下列步驟為??root?設好密碼:
使用?mysql?與?MySQL?資料庫?Server?連線:?
#?/usr/local/mysql/bin/mysql?-u?root?mysql
Reading?table?information?for?completion?of?table?and?column?names
You?can?turn?off?this?feature?to?get?a?quicker?startup?with?-A
Welcome?to?the?MySQL?monitor.??Commands?end?with?;?or?\g.?
Your?MySQL?connection?id?is?201?to?server?version:?3.22.27?
Type?'help'?for?help.?
mysql>
在下了?mysql?-u?root??mysql?指令,指定以?root?帳號并開啟?mysql?系統資料庫,連線至?MySQL?後,會看到一些提示訊息與?mysql?工具的提示符號,以後大部份的工作皆在此提示符號下完成。
更改?MySQL系統管理者?root?密碼:?
mysql>?update?user?set?password=password('新密碼')?where?user='root';?
Query?OK,?0?rows?affected?(0.00?sec)
Rows?matched:?2??Changed:?0??Warnings:?0
mysql>?FLUSH?PRIVILEGES;
Query?OK,?0?rows?affected?(0.00?sec)
mysql>?quit
Bye
注意每個指令後要加上一個分號?";"?才會讓?mysql?開始執行。而第二道指令會讓已載入記憶體的?mysql?系統資料庫更新,最後離開?mysql?工具程式。
在更新?root?密碼後,日後要與?MySQL?連線的方法為:
mysql??-u?root?-p新密碼
或者是這樣,讓?mysql?詢問?root?的密碼:
mysql??-u?root?-p
資料庫維護?
接下來,我們以簡單的通訊錄資料庫作為例子,來介紹如何用?mysql?工具程式來做資料庫的維護(新增、授權、資料表維護等)。
首先,以?MySQL?root?帳號連線後建立一?addbook?資料庫:?
#?/usr/local/mysql/bin/mysql?-u?root?-p
Enter?password:
Welcome?to?the?MySQL?monitor.??Commands?end?with?;?or?\g.?
Your?MySQL?connection?id?is?207?to?server?version:?3.22.27?
Type?'help'?for?help.?
mysql>?create?databae?addbook;
Query?OK,?1?row?affected?(0.00?sec)
指定使用?addbook?資料庫,并建立一個?friends?資料表:?
mysql>?use?addbook;
Database?changed
mysql>?create??table?friends?(
->?name?Char(15),
->?telphone?VarChar(20),
->?icq?Char(10),
->?address?VarChar(30)
->?);
Query?OK,?0?rows?affected?(0.00?sec)
新增幾筆資料,并查詢看看:?
mysql>?insert?into?friends?values(
->?"maa",??"29016710",?"46243046",?"臺北縣新莊市"
->?);
Query?OK,?1?row?affected?(0.00?sec)
mysql>?insert?into?friends?(name,?icq,?telphone,?address?)?Values?(
->?"cxlin",?"39425893",?"7654321",?"臺北縣"
->?);
Query?OK,?1?row?affected?(0.01?sec)
mysql>?select?*?from?friends;
+-------+----------+----------+--------------+
|?name??|?telphone?|?icq??????|?address??????|
+-------+----------+----------+--------------+
|?maa???|?29016710?|?46243046?|?臺北縣新莊市?|
|?cxlin?|?7654321??|?39425893?|?臺北縣???????|
+-------+----------+----------+--------------+
2?rows?in?set?(0.00?sec)
第二個?insert?指令指定了資料欄位的插入順序,用法較第一個為彈性,而第一個指令必須依資料表建立結構時的順序插入資料。
更新、刪除資料表記錄:?
mysql>?update?friends?set?address?=??"桃園縣"?where?name??=?"cxlin";
Query?OK,?1?row?affected?(0.00?sec)
Rows?matched:?1??Changed:?1??Warnings:?0
mysql>?select?*?from?friends?where?name?=?"cxlin";
+-------+----------+----------+---------+
|?name??|?telphone?|?icq??????|?address?|
+-------+----------+----------+---------+
|?cxlin?|?7654321??|?39425893?|?桃園縣??|
+-------+----------+----------+---------+
1?row?in?set?(0.00?sec)
mysql>?delete?from?friends?where?name?=?"maa";
Query?OK,?1?row?affected?(0.01?sec)
mysql>?select?*?from??friends;
+-------+----------+----------+---------+
|?name??|?telphone?|?icq??????|?address?|
+-------+----------+----------+---------+
|?cxlin?|?7654321??|?39425893?|?桃園縣??|
+-------+----------+----------+---------+
1?row?in?set?(0.00?sec)
最後,建好資料庫與資料表後,把?addbook?資料庫中所有資料表的使用權限(select、insert、update、delete)授權給?maa@localhost(再次提醒,此處的?maa?為?MySQL?的使用者帳號,而非作業系統的?maa?帳號):?
mysql>?grant?select,?insert,?update,?delete
????->?on?addbook.*
????->?to?maa@localhost?identified?by?'1234567';
Query?OK,?0?rows?affected?(0.00?sec)
之後,可用?maa?的身份進入?MySQL?存取?addbook?資料庫:
#?/usr/local/mysql/bin/mysql?-u?maa?-p?addbook
Enter?password:
Reading?table?information?for?completion?of?table?and?column?names
You?can?turn?off?this?feature?to?get?a?quicker?startup?with?-A
Welcome?to?the?MySQL?monitor.?Commands?end?with?;?or?\g.
Your?MySQL?connection?id?is?211?to?server?version:?3.22.27
Type?'help'?for?help.
mysql>?status
--------------
./mysql??Ver?9.36?Distrib?3.22.27,?for?pc-linux-gnu?(i686)
Connection?id:??????????26
Current?database:???????addbook
Current?user:???????????maa@localhost
Server?version??????????3.22.27
Protocol?version????????10
Connection??????????????Localhost?via?UNIX?socket
UNIX?socket?????????????/tmp/mysql.sock
Uptime:?????????????????2?hours?29?min?33?sec
Threads:?11??Questions:?107??Slow?queries:?0??Opens:?11??Flush?tables:?1
?Open?7
--------------
收回資料庫使用權限的方法如下(以?MySQL?root?進入):?
mysql>?revoke?delete?on?addbook.*?from?maa@localhost;
Query?OK,?0?rows?affected?(0.00?sec)
mysql>?revoke?all?privileges?on?addbook.*?from??maa@localhost;
Query?OK,?0?rows?affected?(0.00?sec)
第二個指令用來收回全部的權限。
五、mysqladmin?公用程式的使用
mysqladmin?公用程式可用來維護?MySQL?比較一般性的工作(新增、刪除資料庫、設定使用者密碼及停止?MySQL?等等),詳細的說明可以使用?mysqladmin?--help?來查看。(以本文的安裝為例?mysqladmin??位於?/usr/local/mysql/bin/mysqladmin)。
新增資料庫?dbtest?
#?/usr/local/mysql/bin/mysqladmin?-u?root?-p?create?dbtest
Enter?password:
Database?"dbtest"?created.
刪除資料庫?
#?/usr/local/mysql/bin/mysqladmin?-u?root?-p?drop?dbtest
Enter?password:
Dropping?the?database?is?potentially?a?very?bad?thing?to?do.
Any?data?stored?in?the?database?will?be?destroyed.
Do?you?really?want?to?drop?the?'dbtest'?database?[y/N]
y
Database?"dbtest"?dropped
設定使用者密碼(將?maa?的密碼改為??7654321,mysqladmin?會先詢問?maa?的原密碼)?
#?/usr/local/mysql/bin/mysqladmin?-u?maa?-p?password?7654321
Enter?password:
#
停止?MySQL?服務?
#?./mysqladmin?-u?root?-p?shutdown
Enter?password:
注意,shutdown?MySQL?後,必須由作業系統的?root?帳號執行下列指令才能啟動?MySQL:
?/usr/local/mysql/share/mysql/mysql.server?start?
六、結語:
MySQL?資料庫的確是值得推廣的一個產品,它的穩定性已經穩得大家的贊同,只要你曾經學習過?SQL?Language(結構化查詢語言),相信要摸熟?MySQL?的使用只消一兩個小時的時間。如果搭配?PHP?(Personal??HomePage?Program)和?Apache?Web?Server,更可很輕松建構一個與資料庫結合的動態?Web?Site。如果再配合?phpMyAdmin?這個?Web?化的?MySQL?管理工具,建立?MySQL?的資料庫和??MySQL?的管理將會更加方便。
參考資料與資源:
http://www.tcx.se/?
MySQL?3.23?Reference?Manual?
The?Apache?Software?Foundation?
PHP3?PHP?Hypertext?Preprocessor?
phpMyAdmin?
posted on 2006-09-08 15:47
水煮三國 閱讀(309)
評論(0) 編輯 收藏 所屬分類:
Sybase