快捷搜索:   服务器  安全  linux 安全  MYSQL  dedecms

Linux操作系统下MySQL数据库的使用方法(2)

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 的管理将会更加方便。

顶(1)
踩(0)

您可能还会对下面的文章感兴趣:

最新评论