MySQL新建用户,授权,删除用户,修改密码

来源:互联网 发布:iphone8 允许网络连接 编辑:程序博客网 时间:2024/06/17 06:00

首先要声明一下:一般情况下,修改MySQL密码,授权,是需要有mysql里的root权限的。

注:
本操作是在WIN命令提示符下,phpMyAdmin同样适用。
    用户:phplamp  用户数据库:phplampDB

1.新建用户。

//登录MYSQL
@>mysql -u root -p
@>密码
//创建用户
mysql> insert into mysql.user(Host,User,Password) values("localhost","phplamp",password("1234"));
//刷新系统权限表
mysql>flush privileges;
这样就创建了一个名为:phplamp  密码为:1234  的用户。

然后登录一下。

mysql>exit;
@>mysql -u phplamp -p
@>输入密码
mysql>登录成功

2.为用户授权。

//登录MYSQL(有ROOT权限)。我里我以ROOT身份登录.
@>mysql -u root -p
@>密码
//首先为用户创建一个数据库(phplampDB)
mysql>create database phplampDB;
//授权phplamp用户拥有phplamp数据库的所有权限。
>grant all privileges on phplampDB.* to phplamp@localhost identified by '1234';
//刷新系统权限表
mysql>flush privileges;
mysql>其它操作

/*
如果想指定部分权限给一用户,可以这样来写:
mysql>grant select,update on phplampDB.* to phplamp@localhost identified by '1234';
//刷新系统权限表。
mysql>flush privileges;
*/

3.删除用户。
@>mysql -u root -p
@>密码
mysql>DELETE FROM user WHERE User="phplamp" and Host="localhost";
mysql>flush privileges;
//删除用户的数据库
mysql>drop database phplampDB;

4.修改指定用户密码。
@>mysql -u root -p
@>密码
mysql>update mysql.user set password=password('新密码') where User="phplamp" and Host="localhost";
mysql>flush privileges;

 

误解:

在做 dvwa 的 SQL 入侵演练时,通过如下 grant 语句后依然没有权限,以至于以为 grant 语句失效。

先新建一个用户,用户名和密码都是 gqltt

@>mysql -u root

mysql>grant all privileges on dvwa.* to gqltt@localhost identified by 'gqltt' with grant option;

mysql>flush privileges;

如下表明 grant 已经成功:

[plain] view plaincopy
  1. mysql> select * from mysql.user where user='gqltt' \G;  
  2. *************************** 1. row ***************************  
  3.                   Host: localhost  
  4.                   User: gqltt  
  5.               Password: *1A1A4491309AD204398CD4AA6FD550C1799D3403  
  6.            Select_priv: N  
  7.            Insert_priv: N  
  8.            Update_priv: N  
  9.            Delete_priv: N  
  10.            Create_priv: N  
  11.              Drop_priv: N  
  12.            Reload_priv: N  
  13.          Shutdown_priv: N  
  14.           Process_priv: N  
  15.              File_priv: N  
  16.             Grant_priv: N  
  17.        References_priv: N  
  18.             Index_priv: N  
  19.             Alter_priv: N  
  20.           Show_db_priv: N  
  21.             Super_priv: N  
  22.  Create_tmp_table_priv: N  
  23.       Lock_tables_priv: N  
  24.           Execute_priv: N  
  25.        Repl_slave_priv: N  
  26.       Repl_client_priv: N  
  27.       Create_view_priv: N  
  28.         Show_view_priv: N  
  29.    Create_routine_priv: N  
  30.     Alter_routine_priv: N  
  31.       Create_user_priv: N  
  32.             Event_priv: N  
  33.           Trigger_priv: N  
  34. Create_tablespace_priv: N  
  35.               ssl_type:  
  36.             ssl_cipher:  
  37.            x509_issuer:  
  38.           x509_subject:  
  39.          max_questions: 0  
  40.            max_updates: 0  
  41.        max_connections: 0  
  42.   max_user_connections: 0  
  43.                 plugin:  
  44.  authentication_string:  
  45. 1 row in set (0.00 sec)  

 

[plain] view plaincopy
  1. mysql> show grants for gqltt@localhost;  
  2. +-------------------------------------------------------------------------------  
  3. -------------------------------+  
  4. | Grants for gqltt@localhost  
  5.                                |  
  6. +-------------------------------------------------------------------------------  
  7. -------------------------------+  
  8. | GRANT USAGE ON *.* TO 'gqltt'@'localhost' IDENTIFIED BY PASSWORD '*1A1A4491309  
  9. AD204398CD4AA6FD550C1799D3403' |  
  10. | GRANT ALL PRIVILEGES ON `dvwa`.* TO 'gqltt'@'localhost' WITH GRANT OPTION  
  11.                                |  
  12. +-------------------------------------------------------------------------------  
  13. -------------------------------+  
  14. 2 rows in set (0.01 sec)  

 

[plain] view plaincopy
  1. mysql> select * from information_schema.schema_privileges where grantee="'gqltt'  
  2. @'localhost'";  
  3. +---------------------+---------------+--------------+-------------------------+  
  4. --------------+  
  5. | GRANTEE             | TABLE_CATALOG | TABLE_SCHEMA | PRIVILEGE_TYPE          |  
  6.  IS_GRANTABLE |  
  7. +---------------------+---------------+--------------+-------------------------+  
  8. --------------+  
  9. | 'gqltt'@'localhost' | def           | dvwa         | SELECT                  |  
  10.  YES          |  
  11. | 'gqltt'@'localhost' | def           | dvwa         | INSERT                  |  
  12.  YES          |  
  13. | 'gqltt'@'localhost' | def           | dvwa         | UPDATE                  |  
  14.  YES          |  
  15. | 'gqltt'@'localhost' | def           | dvwa         | DELETE                  |  
  16.  YES          |  
  17. | 'gqltt'@'localhost' | def           | dvwa         | CREATE                  |  
  18.  YES          |  
  19. | 'gqltt'@'localhost' | def           | dvwa         | DROP                    |  
  20.  YES          |  
  21. | 'gqltt'@'localhost' | def           | dvwa         | REFERENCES              |  
  22.  YES          |  
  23. | 'gqltt'@'localhost' | def           | dvwa         | INDEX                   |  
  24.  YES          |  
  25. | 'gqltt'@'localhost' | def           | dvwa         | ALTER                   |  
  26.  YES          |  
  27. | 'gqltt'@'localhost' | def           | dvwa         | CREATE TEMPORARY TABLES |  
  28.  YES          |  
  29. | 'gqltt'@'localhost' | def           | dvwa         | LOCK TABLES             |  
  30.  YES          |  
  31. | 'gqltt'@'localhost' | def           | dvwa         | EXECUTE                 |  
  32.  YES          |  
  33. | 'gqltt'@'localhost' | def           | dvwa         | CREATE VIEW             |  
  34.  YES          |  
  35. | 'gqltt'@'localhost' | def           | dvwa         | SHOW VIEW               |  
  36.  YES          |  
  37. | 'gqltt'@'localhost' | def           | dvwa         | CREATE ROUTINE          |  
  38.  YES          |  
  39. | 'gqltt'@'localhost' | def           | dvwa         | ALTER ROUTINE           |  
  40.  YES          |  
  41. | 'gqltt'@'localhost' | def           | dvwa         | EVENT                   |  
  42.  YES          |  
  43. | 'gqltt'@'localhost' | def           | dvwa         | TRIGGER                 |  
  44.  YES          |  
  45. +---------------------+---------------+--------------+-------------------------+  
  46. --------------+  
  47. 18 rows in set (0.00 sec)  


 

如果在 dvwa 演示程序中,用 gqltt 连接 DB ,则如下 sql 注入无法操作:

http://localhost:8081/dvwa/vulnerabilities/sqli/?id=1' union select user, password from mysql.user -- &Submit=Submit#

 

认真想想也是 gqltt 用户只有数据库 dvwa 的所有权限,当然无法查询数据库 mysql 的 user 表。

 

如果想让一个用户有像 root 一样的权限,如下操作

[plain] view plaincopy
  1. mysql> grant all privileges on *.* to gqltt@localhost identified by 'gqltt' with  
  2.  grant option;  

这样再次查询 mysql.user 时候,就有所有的权限了。

[plain] view plaincopy
  1. mysql> select * from mysql.user where user='gqltt' \G;  
  2. *************************** 1. row ***************************  
  3.                   Host: localhost  
  4.                   User: gqltt  
  5.               Password: *1A1A4491309AD204398CD4AA6FD550C1799D3403  
  6.            Select_priv: Y  
  7.            Insert_priv: Y  
  8.            Update_priv: Y  
  9.            Delete_priv: Y  
  10.            Create_priv: Y  
  11.              Drop_priv: Y  
  12.            Reload_priv: Y  
  13.          Shutdown_priv: Y  
  14.           Process_priv: Y  
  15.              File_priv: Y  
  16.             Grant_priv: Y  
  17.        References_priv: Y  
  18.             Index_priv: Y  
  19.             Alter_priv: Y  
  20.           Show_db_priv: Y  
  21.             Super_priv: Y  
  22.  Create_tmp_table_priv: Y  
  23.       Lock_tables_priv: Y  
  24.           Execute_priv: Y  
  25.        Repl_slave_priv: Y  
  26.       Repl_client_priv: Y  
  27.       Create_view_priv: Y  
  28.         Show_view_priv: Y  
  29.    Create_routine_priv: Y  
  30.     Alter_routine_priv: Y  
  31.       Create_user_priv: Y  
  32.             Event_priv: Y  
  33.           Trigger_priv: Y  
  34. Create_tablespace_priv: Y  
  35.               ssl_type:  
  36.             ssl_cipher:  
  37.            x509_issuer:  
  38.           x509_subject:  
  39.          max_questions: 0  
  40.            max_updates: 0  
  41.        max_connections: 0  
  42.   max_user_connections: 0  
  43.                 plugin:  
  44.  authentication_string:  
  45. 1 row in set (0.00 sec)  


0 0
原创粉丝点击