来源:www.cncfan.com | 2006-3-20 | (有2970人读过)
资料库维护
接下来,我们以简单的通讯录资料库作为例子,来介绍如何用 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 的管理将会更加方便。
|