使用 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
在更新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
更新、删除资料表记录: 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
新增资料库 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 服务 # /usr/local/mysql/bin/mysqladmin -u root -p shutdown Enter password: 注意,shutdown mysql後,必须由作业系统的root帐号执行下列指令才能启动mysql: # /usr/local/mysql/share/mysql/mysql.server start