转自:http://blog.sina.com.cn/s/blog_8c2525390101h0dv.html

mysql 创建一个用户 hail,密码 hail,指定一个数据库 haildb 给 hail

mysql -u root -p
password
use mysql;
insert into user(host,user,password) values('localhost','hail',password('hail'));
flush privileges;
create database haildb;
grant all privileges on haildb.* to hail@localhost identified by 'hail';
flush privileges;

如果想指定部分权限给用户

grant select,update on haildb.* to hail@localhost identified by 'hail';
flush privileges;

删除用户

delete from user where user='hail' and host='localhost';
flush privileges;

删除用户数据库

drop database haildb;

修改指定用户密码

update user set password=password('new_password') where user='hail' and host='localhost';
flush privileges;


1.远程登录mysql
mysql -h ip -u root -p 密码
2.创建用户
格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by “密码”;
例1:增加一个test1用户,密码为123456,可以在任何主机上登录,并对所有数据库有查询,增加,修改和删除的功能。需要在mysql的root用户下进行
mysql>grant select,insert,update,delete on *.* to test1@”%” identified by “123456″;
mysql>flush privileges;
例2:增加一个test2用户,密码为123456,只能在192.168.2.12上登录,并对数据库student有查询,增加,修改和删除的功能。需要在mysql的root用户下进行
mysql>grant select,insert,update,delete on student.* to test2@192.168.2.12 identified by “123456″;
mysql>flush privileges;
例3:授权用户test3拥有数据库student的所有权限
mysql>grant all privileges on student.* to test3@localhost identified by ’123456′;
mysql>flush privileges;
3.修改用户密码
mysql>update mysql.user set password=password(’123456′) where User=’test1′ and Host=’localhost’;
mysql>flush privileges;
4.删除用户
mysql>delete from user where user=’test2′ and host=’localhost’;
mysql>flush privileges;
5.删除数据库和删除表
mysql>drop database 数据库名;
mysql>drop table 表名;
6.删除账户及权限
drop user 用户名@’%’
drop user 用户名@localhost
**************************************************************************************
grant 详细解析如下:
**************************************************************************************
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 来统一管理。
Category: Post
You can follow any responses to this entry via RSS.
Comments are currently closed, but you can trackback from your own site.
=========================================================================
1.创建用户并授权
grant语句的语法:
grant privileges (columns) on what to user identified by “password” with grant option
要使用该句型,需确定字段有:
privileges 权限指定符权限允许的操作
alter 修改表和索引
create 创建数据库和表
delete 删除表中已有的记录
drop 抛弃(删除)数据库和表
index 创建或抛弃索引
insert 向表中插入新行
reference 未用
select 检索表中的记录
update 修改现存表记录
file 读或写服务器上的文件
process 查看服务器中执行的线程信息或杀死线程
reload 重载授权表或清空日志、主机缓存或表缓存。
shutdown 关闭服务器
all 所有;
all privileges同义词
usage 特殊的“无权限”权限
以上权限分三组:
第一组:适用于数据库、表和列如:alter create delete drop index insert select update
第二组:数管理权限 它们允许用户影响服务器的操作 需严格地授权 如:file process reload shut*
第三组:权限特殊 all意味着“所有权限” uasge意味着无权限,即创建用户,但不授予权限
columns
权限运用的列(可选)并且你只能设置列特定的权限。如果命令有多于一个列,应该用逗号分开它们。
what
权限运用的级别。权限可以是全局,定数据库或特定表.
user
权限授予的用户,由一个用户名和主机名组成,许两个同名用户从不同地方连接.缺省:mysql用户password
赋予用户的口令(可选),如果你对用户没有指定identified by子句,该用户口令不变.
用identified by时,口令字符串用改用口令的字面含义,grant将为你编码口令.
注:set password使用password()函数
with grant option
用户可以授予权限通过grant语句授权给其它用户(可选)
实例讲解:
grant all on db_book.* to huaying@koowo.com identified by “yeelion”   只能在本地连接
grant all on db_book.* to huaying@vpn.koowo.com identified by “yeeliong”  允许从此域连接
grant all on db_book.* to huaying@% identified by “yeelion”   允许从任何主机连接 注:”%”字符起通配符作用,与like模式匹配的含义相同。
grant all on db_book.* to huaying@%.koowo.com identified by “yeelion”;  允许huaying从koowo.com域的任何主机连接
grant all on db_book.* to huaying@192.168.1.189 identified by “yeelion”
grant all on db_book.* to huaying@192.168.1.% identified by “yeelion”
grant all on db_book.* to huaying@192.168.1.0/17 identified by “yeelion”
允许从单IP 段IP或一子网IP登陆
注:有时 用户@IP 需用引号 如”huaying@192.168.1.0/17″
grant all on *.* to huaying@localhost identified by “yeelion” with grant option
添加超级用户huaying 可在本地登陆做任何操作.
grant reload on *.* to huaying@localhost identified by “yeelion” 只赋予reload权限
grant all on db_book to huaying@koowo.com indetified by “yeelion” 所有权限
grant select on db_book to huaying@% indetified by “yeelion” 只读权限
grant select,insert,delete,update on db_book to huaying@koowo.com indetified by “yeelion”
只有select,insert,delete,update的权限
grant select on db_book.storybook to huaying@localhost indetified by “yeelion” 只对表
grant update (name) on db_book.storybook to huaying@localhost 只对表的name列 密码不变
grant update (id,name,author) on db_book.storybook to huaying@localhost 只对表的多列
grant all on book.* to “”@koowo.com 允许koowo.com域中的所有用户使用库book
grant all on book.* to huaying@%.koowo.com indetified by “yeelion” with grant option
允许huaying对库book所有表的管理员授权.
2.撤权并删除用户
revoke的语法类似于grant语句
to用from取代,没有indetifed by和with grant option子句. 如下:
revoke privileges (columns) on what from user
user:必须匹配原来grant语句的你想撤权的用户的user部分。
privileges:不需匹配,可以用grant语句授权,然后用revoke语句只撤销部分权限。
revoke语句只删权限不删用户,撤销了所有权限后user表中用户记录保留,用户仍然可以连接服务器.
要完全删除一个用户必须用一条delete语句明确从user表中删除用户记录:
delete from user where user=”huaying”
flush privileges; 重载授权表
注:使用grant和revoke语句时,表自动重载,而你直接修改授权表时不是.
实例:
1.创建数据库
CREATE DATABASE  `fypay` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
2.为创建的数据库增加用户fypay
grant create,select,insert,update,delete,drop,alter on fypay.* to fypay@”%” identified by “testfpay”;
3.删除fypay用户
delete from user where user=”fypay”
drop user fypay@localhost
4.刷新数据库
flush privileges;

转:mysql 创建一个用户,指定一个数据库的更多相关文章

  1. mysql创建新用户并分配数据库权限

    下面展示了如何在Linux中创建和设置一个MySQL用户. 首先以root身份登录到MySQL服务器中. $ mysql -u root -p 当验证提示出现的时候,输入MySQL的root帐号的密码 ...

  2. mysql创建本地用户及赋予数据库权限的方法示例

    大家在安装 mysql 时通常会生成一个超级用户 root,很多人之后就一直沿用这一个用户,虽然这会很方便,但超级用户权限太大,在所有地方使用它通常是一个安全隐患. 这一点跟操作系统的用户管理也是类似 ...

  3. Mysql创建新用户后无法登录,提示 Access denied for user 'username'@'localhost' (using password: YES)

    MySQL创建新用户后无法登录,提示 Access denied for user 'username'@'localhost' (using password: YES) ,多半是因为存在匿名用户, ...

  4. win7问题解决,凭据管理器和无法访问,不允许一个用户使用一个以上用户名与服务器或共享资源进行多重连接。

    WIN7凭据管理器,如果你用一个帐号远程登录以后在电脑中会记住这个信息,假如你想用另外的帐号,那么就到控制面板-凭据管理器里中进行修改或者删除. 如果你登录以后提示,“无法访问.不允许一个用户使用一个 ...

  5. mysql 创建新用户、数据库、授权

    创建用户 1.登录mysql mysql -uroot -p 2.创建本地用户(2.3选其一) #use mysql;             //选择mysql数据库 #create user 'w ...

  6. Mysql 创建普通用户、数据库、表、插入记录,用户赋权

    C:\phpStudy\MySQL\bin>mysql -uroot -proot -h127.0.0.1 //创建用户 mysql> insert into mysql.user (ho ...

  7. Mysql创建新用户方法

    1. CREATE USER 语法: CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 例子: CREATE USER 'dog'@'lo ...

  8. MySQL创建新用户、增加账户的2种方法及使用实例

    可以用两种方式创建MySQL账户:1.使用GRANT语句2.直接操作MySQL授权表最好的方法是使用GRANT语句,因为这样更精确,错误少.创建超级用户: mysql> GRANT ALL PR ...

  9. MySQL创建远程用户并授权

    今天需要在本地测试系统功能,因为本地没有数据库,就需要在程序里面连接远程数据库: 先用ssh登录远程服务器,用root连上数据库看看情况: mysql> select Host,User,Pas ...

  10. MySQL创建新用户以及ERROR 1396 (HY000)问题解决

     登 录mysql mysql -u root -p 创建允许本地 IP访问localhost的Mysql数据库时出错 create user 'lijing'@'localhost' identif ...

随机推荐

  1. python_way day12 sqlalchemy,原生mysql命令

    python_way day12  sqlalchemy,mysql原生命令 1.sqlalchemy 2.mysql 原生命令 一,sqlalchemy SQLAlchemy本身无法操作数据库,其必 ...

  2. Xcode error: conflicting types for 'XXXX'

    问题描述:在main方法中调用了一个写在main方法后面的方法,比如: void main(){ A(); } void A(){} Xcode编译后就报错:conflicting types for ...

  3. 搭建Mono for Android开发环境(用离线版)

    上面为我现在的离线版的安装包内容(不知道为什么上传的图名字显示不出来,郁闷),这些文件可以到http://pan.baidu.com/s/1ntM8U4T这里去下载: 安装步骤如下: 1)jdk-6u ...

  4. hibernate.properties与hibernate.cfg.xml 区别

    Hibernate的数据库连接信息是从配置文件中加载的. Hibernate的配置文件有两种形式:一种是XML格式的文件,一种是properties属性文件. 一)hibernate.cfg.xml ...

  5. sqlplus入门基础语句

    关于Oracle 首先Oracle一个数据库由若干个表空间组成,每个表空间由若干个数据文件(或设备)组成,每个数据文件由若干个盘区组成,每个盘区由若干个block组成.这是Oracle的物理结构. 逻 ...

  6. oracle中between

    oracle中between and包含边界值,也就是所谓的闭区间. 如 between 1 and 100,则表示包含1和100及以内的一切数值. 如以下语句: 1 2 3 4 5 6 7 8 9 ...

  7. 转:C/C++基本数据类型所占字节数

    参考:http://blog.csdn.net/vast_sea/article/details/8076934 关于这个基本的问题,很早以前就很清楚了,C标准中并没有具体给出规定那个基本类型应该是多 ...

  8. mysql报关于用户密码1045(28000),几种处理方法 (zhuan)

    http://blog.itpub.net/29371470/viewspace-1409075/ http://blog.csdn.net/rosten/article/details/250658 ...

  9. 常用的 文件 MIME类型

    估计很多朋友对不同后缀的文件对应的MIME类型不熟悉(实际上这么多我也记不住), 所以将平常常见的一些文件后缀对应的MIME类型写了一个对照表,现在奉献给大家: .asx,video/x-ms-asf ...

  10. C# Socket编程(3)编码和解码

    在网络通信中,很多情况下:比如说QQ聊天,通讯双方直接传递的都是字符信息.但是字符信息并不能够直接通过网络传输,这些字符集必须先转换成一个字节序列后才能够在网络中传输,于是这里就产生了编码和解码的概念 ...