本文实例,运行于 MySQL 5.0 及以上版本。

MySQL 赋予用户权限命令的简单格式可概括为:

grant 权限 on 数据库对象 to 用户


一、grant 普通数据用户,查询、插入、更新、删除 数据库中所有表数据的权利。

grant select on testdb.* to common_user@'%'

grant insert on testdb.* to common_user@'%'

grant update on testdb.* to common_user@'%'

grant delete on testdb.* to common_user@'%'


或者,用一条 MySQL 命令来替代:

grant select, insert, update, delete on testdb.* to common_user@'%'


二、grant 数据库开发人员,创建表、索引、视图、存储过程、函数。。。等权限。


grant 创建、修改、删除 MySQL 数据表结构权限。

grant create on testdb.* to developer@'192.168.0.%';

grant alter  on testdb.* to developer@'192.168.0.%';

grant drop   on testdb.* to developer@'192.168.0.%';


grant 操作 MySQL 外键权限。

grant references on testdb.* to developer@'192.168.0.%';


grant 操作 MySQL 临时表权限。

grant create temporary tables on testdb.* to developer@'192.168.0.%';


grant 操作 MySQL 索引权限。

grant index on  testdb.* to developer@'192.168.0.%';


grant 操作 MySQL 视图、查看视图源代码 权限。

grant create view on testdb.* to developer@'192.168.0.%';

grant show   view on testdb.* to developer@'192.168.0.%';


grant 操作 MySQL 存储过程、函数 权限。

grant create routine on testdb.* to developer@'192.168.0.%';  -- now, can show procedure status

grant alter  routine on testdb.* to developer@'192.168.0.%';  -- now, you can drop a procedure

grant execute        on testdb.* to developer@'192.168.0.%';

三、grant 普通 DBA 管理某个 MySQL 数据库的权限。

grant all privileges on testdb to dba@'localhost'


其中,关键字 “privileges” 可以省略。


四、grant 高级 DBA 管理 MySQL 中所有数据库的权限。

grant all on *.* to dba@'localhost'


五、MySQL grant 权限,分别可以作用在多个层次上。


1. grant 作用在整个 MySQL 服务器上:

grant select on *.* to dba@localhost; -- dba 可以查询 MySQL 中所有数据库中的表。

grant all    on *.* to dba@localhost; -- dba 可以管理 MySQL 中的所有数据库


2. grant 作用在单个数据库上:

grant select on testdb.* to dba@localhost; -- dba 可以查询 testdb 中的表。


3. grant 作用在单个数据表上:

grant select, insert, update, delete on testdb.orders to dba@localhost;


4. grant 作用在表中的列上:

grant select(id, se, rank) on testdb.apache_log to dba@localhost;


5. grant 作用在存储过程、函数上:

grant execute on procedure testdb.pr_add to 'dba'@'localhost'

grant execute on function  testdb.fn_add to 'dba'@'localhost'


六、查看 MySQL 用户权限


查看当前用户(自己)权限:

show grants;


查看其他 MySQL 用户权限:

show grants for dba@localhost;


七、撤销已经赋予给 MySQL 用户权限的权限。


revoke 跟 grant 的语法差不多,只需要把关键字 “to” 换成 “from” 即可:

grant  all on *.* to   dba@localhost;

revoke all on *.* from dba@localhost;


八、MySQL grant、revoke 用户权限注意事项


1. grant, revoke 用户权限后,该用户只有重新连接 MySQL 数据库,权限才能生效。


2. 如果想让授权的用户,也可以将这些权限 grant 给其他用户,需要选项 “grant option“

grant select on testdb.* to dba@localhost with grant option;


这个特性一般用不到。实际中,数据库权限最好由 DBA 来统一管理。


#查看grant添加的用户:select user,host from mysql.user; 

#删除用户:

mysql> drop user "tongor"@localhost;


更改管理员密码


在一切正常后,要做的第一件事情是更改管理员的密码。你可以运行mysqladmin

格式: mysqladmin -u 用户名 -p 旧密码 password 新密码

SET PASSWORD FOR ‘root’@'localhost’ = PASSWORD( ‘*********’ )

set password for root=password(“456″);

update user set password=password(‘456′);

清除密码

#mysqladmin -uroot -p456 password”"

# mysqladmin -u root password newpassword


此命令把root用户的口令变成newpassword。当然你可以把口令换成其它,因为这个很容易破解。


如果忘记密码,可以使用mysqld_safe –skip-grant-tables &启动mysql后,不用密码进入。

>use mysql

>update user set password=password(“new_pass”) where user=”root”;

>flush privileges;


进入MySQL

你需要提供一个MySQL用户和此用户的口令。如果数据库运行在其它机器上,而不是你所在的这台机器上你需要指定主机名。

命令:

mysql -h <主机名> -u <用户名> -p <数据库名>

Enter password: ********

********代表你的口令;当mysql显示Enter password:提示时输入它。

例如,在此机器上,你可以敲入:

# mysql -u root -p mysql

Enter password:


操作MySQL

  在之前要指出的是:一条操作即是一条SQL语句,注意随后要跟上一个分号,以标志此条语句的结束。而且一条SQL

语句不必全在一个单独行给出,可以写入多行,最后以分号结束此语句的输入。

1> 显示数据库列表 show databases;

2> 显示库中的数据表

use mysql; #打开库

show tables;

3> 显示数据库的结构

describe 表名;

desc user;

4> 创建数据库

命令:CREATE DATABASE <数据库名> 例如,建立一个名为 test 的数据库

mysql> CREATE DATABASE test;

5> 删除数据库

命令: DROP DATABASE <数据库名>

例如,删除名为 test 的数据库

mysql> DROP DATABASE test

drop databases if exits

6> 连接数据库

命令: USE <数据库名>

例如,如果test数据库存在,尝试存取它:

mysql> USE test

屏幕提示:

Database changed

7> 建表

命令:CREATE TABLE <表名> ( <字段名1> <类型1> [,..<字段名n> <类型n>]);

例如,建立一个名为table_1的表,此表记录班上的人员及平均成绩,那么用字段 id 代表编号,为数字类型,且编号唯一,

不能为空, 缺省值为 0 ; 用字段 name 代表人名,为字符型,不为空;用字段 degree 代表成绩,为数字型,可为空。

编号id 为此表的关键字。建表如下:

Sql代码  收藏代码

CREATE TABLE table_1 (  

    id INT(4) DEFAULT '0' NOT NULL,  

    name CHAR(20) NOT NULL,  

    degree DOUBLE(16,2) ,  

    PRIMARY KEY(id)  

);  

 

8> 删除表

命令:DROP TABLE <表名>

例如,删除表名为 table_1 的表

mysql> DROP TABLE table_1;

9>插入数据

命令:INSERT INTO <表名> [( <字段名1>[,..<字段名n > ])]VALUES ( 值1 )[, ( 值n )]

例如,往表 test 中插入二条记录, 这二条记录表示:编号为1的名为joan 的成绩为96.45, 编号为2 的名为jeanny 的成绩为82.99.

mysql> INSERT INTO test VALUES(1,’joan’,96.45),(2,’jeanny’,82.99);

10> 查询表中的数据

命令: SELECT <字段1,字段2,...> FROM < 表名 > WHERE < 表达式 >

例如,查看表 test 中所有数据

mysql> SELECT * FROM test;

屏幕显示:

+—-+————-+———-+

| id | name | degree |

+—-+————-+———-+

| 1 | joan | 96.45 |

| 2 | jeanny | 82.99 |

+—-+————-+———-+ 

11> 删除表中数据

命令: DELETE FROM < 表名 > WHERE < 表达式 >

例如,删除表 test 中编号为1 的记录

mysql> DELETE FROM test WHERE id=1;

12> 修改数据库

在mysql的表中增加字段:

alter table dbname add column userid int(11) not null primary key auto_increment;

这样,就在表dbname中添加了一个字段userid,类型为int(11)。


字段类型

   1.INT[(M)]    正常大小整数类型

   2.DOUBLE[(M,D)] [ZEROFILL]    正常大小(双精密)浮点数字类型

   3.DATE   日期类型。支持的范围是1000-01-01到9999-12-31。MySQL以YYYY-MM-DD格式来显示DATE值,但是允许你使用字符串或数字把值赋给DATE列

   4.CHAR(M)    定长字符串类型,当存储时,总是是用空格填满右边到指定的长度

   5.BLOB TEXT    BLOB或TEXT类型,最大长度为65535(2^16-1)个字符。

   6.VARCHAR 变长字符串类型


mysql数据库的授权 添加用户

grant select on 数据库.* to “用户名”@“登录主机” identified by “密码”;

添加用户

1: 添加用户test_user 密码123 让他可以在任何主机上登录,并对所有数据库有查询插入修改删除的权限

mysql>grant select,insert,update,delete on *.* to ‘test_user’@'%’ identified by ‘123′;

2:添加一个用户test_user2

mysql>grant select,insert,update,delete on test.* to ‘test_user’@'localhost’ identified

3:创建一个本地的完全超级用户,admin 口令 123

grant all privileges on *.* to admin@localhost identitied by ‘124′ with grant option;

mysql>grant select,insert,delete,create,drop on *.* (或test.*/user.*/..) to 用户名@localhost identified by 密码;

 4:新建一个用户帐号以便可以访问数据库,需要进行如下操作:

   mysql> grant usage

   -> ON test.*

   -> TO testuser@localhost;

   Query OK, 0 rows affected (0.15 sec)

   此后就创建了一个新用户叫:testuser,这个用户只能从localhost连接到数据库并可以连接到test 数据库。下一步,我们必须指定testuser这个用户可以执行哪些操作:

   mysql> GRANT select, insert, delete,update

   -> ON test.*

   -> TO testuser@localhost;

   Query OK, 0 rows affected (0.00 sec)

  此操作使testuser能够在每一个test数据库中的表执行SELECT,INSERT和DELETE以及UPDATE查询操作。现在我们结束操作并退出MySQL客户程序:

删除用户

revoke all on *.* from ‘test_user’@localhost;

mysql>delete from user where User=’test_uer’;

mysql>flush privileges;

删除匿名用户

mysql>delete from user where host=’localhost’ and user=”;

mysql>flush privileges;//刷新内存授权表

优化

mysql>optimize table 表1,表2—


导入数据库表

load data infile ‘/tmp/teacher’ into tale teacher

mysqlimport school /tmp/teacher.txt

将数据库school中的全部表备份到school.sql

mysqldump –opt school>school.sql

仅备份数据库school中的一部分表teacher和student

mysqldump –opt shcool teacher student>school_teacher_student.sql

备份多个数据库

mysqldump –databases school test>school_test.sql

还原数据库:

use database

source school.sql

清空数据表:

truncate table xxx;