MySQL添加用户、删除用户与授权

来源:互联网 发布:wow60数据库 编辑:程序博客网 时间:2024/05/17 04:27

mysql 5.7 增加用户并授权


1、创建用户,名字为userone,密码为123456
  create user userone identified by '123456';
2、创建名字为dbone的数据库
  create database dbone;
3、授予dbone数据库的所有权限给userone用户:
  grant all on dbone.* to userone;
PS:如果第3步授权失败,可以退出root账户,并重新登录;

mysql -uroot -proot

MySQL5.7 mysql.user表没有password字段改 authentication_string;

一. 创建用户:

命令:CREATE USER 'username'@'host' IDENTIFIED BY 'password';

例子: CREATE USER 'dog'@'localhost' IDENTIFIED BY '123456';

        CREATE USER 'dog2'@'localhost' IDENTIFIED BY '';

PS:username - 你将创建的用户名,

host - 指定该用户在哪个主机上可以登陆,此处的"localhost",是指该用户只能在本地登录,不能在另外一台机器上远程登录,如果想远程登录的话,将"localhost"改为"%",表示在任何一台电脑上都可以登录;也可以指定某台机器可以远程登录;

password - 该用户的登陆密码,密码可以为空,如果为空则该用户可以不需要密码登陆服务器。

二.授权:

命令:GRANT privileges ON databasename.tablename TO 'username'@'host'

PS: privileges - 用户的操作权限,如SELECT , INSERT , UPDATE 等(详细列表见该文最后面).如果要授予所有权限则使用ALL.;

databasename - 数据库名,

tablename-表名,如果要授予该用户对所有数据库和表的相应操作权限则可用*表示, 如*.*.

例子: GRANT SELECT, INSERT ON mq.* TO 'dog'@'localhost';

三.创建用户同时授权

mysql> grant all privileges on mq.* to test@localhost identified by '1234';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

PS:必须执行flush privileges; 

否则登录时提示:ERROR 1045 (28000): Access denied for user 'user'@'localhost' (using password: YES ) 

 

四.设置与更改用户密码

命令:SET PASSWORD FOR 'username'@'host' = PASSWORD('newpassword');

例子: SET PASSWORD FOR 'dog2'@'localhost' = PASSWORD("dog");

五.撤销用户权限

      命令: REVOKE privilege ON databasename.tablename FROM 'username'@'host';

     说明: privilege, databasename, tablename - 同授权部分.

      例子: REVOKE SELECT ON mq.* FROM 'dog2'@'localhost';

PS: 假如你在给用户'dog'@'localhost''授权的时候是这样的(或类似的):GRANT SELECT ON test.user TO 'dog'@'localhost', 则在使用REVOKE SELECT ON *.* FROM 'dog'@'localhost';命令并不能撤销该用户对test数据库中user表的SELECT 操作.相反,如果授权使用的是GRANT SELECT ON *.* TO 'dog'@'localhost';则REVOKE SELECT ON test.user FROM 'dog'@'localhost';命令也不能撤销该用户对test数据库中user表的Select 权限.

      具体信息可以用命令SHOW GRANTS FOR 'dog'@'localhost'; 查看.

六.删除用户

      命令: DROP USER 'username'@'host';

七.查看用户的授权

mysql> show grants for dog@localhost;
+---------------------------------------------+
| Grants for dog@localhost |
+---------------------------------------------+
| GRANT USAGE ON *.* TO 'dog'@'localhost' |
| GRANT INSERT ON `mq`.* TO 'dog'@'localhost' |
+---------------------------------------------+
2 rows in set (0.00 sec)

PS:GRANT USAGE:mysql usage权限就是空权限,默认create user的权限,只能连库,啥也不能干

=============================================================================================================

MySql中添加用户,新建数据库,用户授权,删除用户,修改密码(注意每行后边都跟个;表示一个命令语句结束):

1.新建用户

  1.1 登录MYSQL:

  @>mysql -u root -p

  @>密码

  1.2 创建用户:

  mysql> insert into mysql.user(Host,User,Password) values("localhost","test",password("1234"));

  这样就创建了一个名为:test 密码为:1234 的用户。

  注意:此处的"localhost",是指该用户只能在本地登录,不能在另外一台机器上远程登录。如果想远程登录的话,将"localhost"改为"%",表示在任何一台电脑上都可以登录。也可以指定某台机器可以远程登录。

  1.3 然后登录一下:

  mysql>exit;

  @>mysql -u test -p

  @>输入密码

  mysql>登录成功

 

2.为用户授权

  授权格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by "密码"; 

  2.1 登录MYSQL(有ROOT权限),这里以ROOT身份登录:

  @>mysql -u root -p

  @>密码

  2.2 首先为用户创建一个数据库(testDB):

  mysql>create database testDB;

  2.3 授权test用户拥有testDB数据库的所有权限(某个数据库的所有权限):

   mysql>grant all privileges on testDB.* to test@localhost identified by '1234';

   mysql>flush privileges;//刷新系统权限表

  格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by "密码"; 

  2.4 如果想指定部分权限给一用户,可以这样来写:

  mysql>grant select,update on testDB.* to test@localhost identified by '1234';

  mysql>flush privileges; //刷新系统权限表

  2.5 授权test用户拥有所有数据库的某些权限:   

  mysql>grant select,delete,update,create,drop on *.* to test@"%" identified by "1234";

     //test用户对所有数据库都有select,delete,update,create,drop 权限。

  //@"%" 表示对所有非本地主机授权,不包括localhost。(localhost地址设为127.0.0.1,如果设为真实的本地地址,不知道是否可以,没有验证。)

 //对localhost授权:加上一句grant all privileges on testDB.* to test@localhost identified by '1234';即可。

 

3. 删除用户

   @>mysql -u root -p

  @>密码

   mysql>Delete FROM user Where User='test' and Host='localhost';

   mysql>flush privileges;

   mysql>drop database testDB; //删除用户的数据库

删除账户及权限:>drop user 用户名@'%';

        >drop user 用户名@ localhost; 

 

4. 修改指定用户密码

    @>mysql -u root -p

    @>密码

    mysql>update mysql.user set password=password('新密码') where User="test" and Host="localhost";

    mysql>flush privileges;

 

5. 列出所有数据库

  mysql>show database;

 

6. 切换数据库

  mysql>use '数据库名';

 

7. 列出所有表

  mysql>show tables;

 

8. 显示数据表结构

  mysql>describe 表名;

 

9. 删除数据库和数据表

  mysql>drop database 数据库名;

  mysql>drop table 数据表名;

 

[原文转自:http://www.cnblogs.com/fly1988happy/archive/2011/12/15/2288554.html]


0 0
原创粉丝点击