mysql的受权问题

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

MySQL 赋予用户权限命令的简单格式可归纳为:sql

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


1、grant 普通数据用户,查询、插入、更新、删除 数据库中全部表数据的权利。apache

grant select on testdb.* to common_user@'%'服务器

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

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@'%'索引


2、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.%';

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

grant all privileges on testdb to dba@'localhost'


其中,关键字 “privileges” 能够省略。


4、grant 高级 DBA 管理 MySQL 中全部数据库的权限。

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


5、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'


6、查看 MySQL 用户权限


查看当前用户(本身)权限:

show grants;


查看其余 MySQL 用户权限:

show grants for dba@localhost;


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


revoke 跟 grant 的语法差很少,只须要把关键字 “to” 换成 “from” 便可:

grant  all on *.* to   dba@localhost;

revoke all on *.* from dba@localhost;


8、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;

相关文章
相关标签/搜索