MySQL增加/删除用户、授权、修改密码等方法和命令总结

MySQL增加/删除用户、授权、修改密码等方法和命令总结

1. MySQL添加用户
2. MySQL
修改用户密码
3.
授权和取消权限
4. MySQL
删除用户



1. MySQL添加用户
1.1
通过直接操作MySQLuser表新增用户

//
登录MySQL
[yongfu]$ mysql -uroot -p
[yongfu]$ password:

//
创建用户
mysql> use mysql;
mysql> insert into user(Host,User,Password)values('localhost','yongfu_a',password('my_password'));

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

//
退出后登录一下
mysql>exit;

[yongfu]$ mysql -uyongfu_a -p
[yongfu]$ password: 
输入密码
mysql> 
login success

//
说明:
至此创建mysql的第二个用户:yongfu_a(第一个用户是root),他的密码是my_password。需要刷新系统权限表flush privilege,该用户才能生效登录。此时该用户(yongfu_a)仅有在本机(localhost)使用密码(my_password)登录的权限,还没有其他权限,需要使用GRANT命令对该用户进行相应授权。

1.2
通过GRANT授权的方式新增用户
通过上面1.1的方式添加的用户是没有任何权限的,对用户进行授权使用GRANT命令。
//
root用户登录mysql
[yongfu]$ mysql -uroot -p

//
新增一个新用户yongfu_b
mysql> grant all privileges on *.* to yongfu_b@'192.168.1.%' identified by'my_password_2';

//
说明:
至此创建了mysql的第三个用户:yongfu_b,他的密码是my_password_2。不需要使用flushprivilege刷新系统权限表,改用户立即生效。此时该用户(yongfu_b)可以在192.168.1.0/24的网段上的任意机器使用密码my_password_2)登录。同时已经对所有库的所有表赋予了全部权限。

PSmysql>grant 权限1,权限2,…权限n on 数据库名称.表名称 to 用户名@用户地址 identified by ‘连接口令’;



权限1,权限2,…权限n代表select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file14个权限。

当权限1,权限2,…权限nall privileges或者all代替,表示赋予用户全部权限。

当数据库名称.表名称被*.*代替,表示赋予用户操作服务器上所有数据库所有表的权限。

用户地址可以是localhost,也可以是ip地址、机器名字、域名。也可以用’%'表示从任何地址连接。

连接口令可以为空,即无密码。


// 总结
从上面两种方法可以看出,使用grant方法新增用户更方便也更省事。只需要一步就可以完成新增用户和用户授权的全部操作。

2. MySQL修改用户密码
2.1
直接修改数据库修改密码
// root用户登录mysql
[yongfu]$mysql -uroot -p

// 在数据库中修改密码字段
mysql> usemysql
mysql>update user set Password = password("new_password") where User ="yongfu_a" and Host="localhost";
mysql>flush privileges;

2.2 通过GRANT方法修改密码
// 其实就是新增用户那个方法,grant本身是授权、授予的意思。
// 修改用户yongfu_b的密码为my_password_new
mysql>grant all privileges on *.* to yongfu_b@'192.168.1.%' identified by'my_password_new';

2.3 通过mysqladmin修改密码
// mysql初始化安装好后,root用户默认是没有密码的,可以通过以下命令设置密码(注意:下面命令后面不需要跟分号;否则会把分号也当成密码的一部分):
[yongfu]$mysqladmin -u root password "my_password"

// 已经设置过root密码,想要修改密码,使用如下命令:
[yongfu]$mysqladmin -u root -p"my_password" password"my_new_password"

// 修改密码,需要SUPER权限,我们尝试用mysqladmin命令修改yongfu_a的密码,得到如下错误
[yongfu]$mysqladmin -u yongfu_a -p password 'newnew_pass'

mysqladmin:Can't turn off logging; error: 'Access denied; you need the SUPER privilege forthis operation'



// 错误原因:yongfu_a只有登录权限,而没有其他任何权限,而修改密码需要SUPER权限。

// root用户登录mysql
[yongfu]$ mysql -uroot -p

// yongfu_a赋予SUPER的权限

mysql>  GRANT SUPER on *.* toyongfu_a@'localhost';

mysql> flush privileges;

mysql> exit;



// 再次使用上面的命令,正确输入原来密码,就可以将yongfu_a的密码设置为newnew_pass

[yongfu]$ mysqladmin -u yongfu_a -p password 'newnew_pass'

Enter password: 输入原密码



// 注意事项

尽量不要使用这种方式设置密码,因为shell会记录你输入的命令的历史情况,这样骇客或者别的人很容易的就可以从你的shell命令历史中知道你的mysql密码。



3. 授权和取消权限

3.1 授权

mysql使用GRANT命令对用户进行授权。前面我们已经数次接触到GRANT命令,用它添加了一个用户,也为用户修改了密码,同时为yongfu_a用户授予了SUPER权限。其实GRANT设计的目的本身只是为了授权的。

// 首先我们查看 yongfu_a 用户有哪些权限(root登录,并use mysql

mysql> select * from user where User='yongfu_a'\G;  
*************************** 1. row ***************************
                Host: localhost
                User: yongfu_a
            Password: *1603BD6A73604A872220561294D3C5916A7B1E60
          Select_priv: N
          Insert_priv: N
          Update_priv: N
          Delete_priv: N
          Create_priv: N
            Drop_priv: N
          Reload_priv: N
        Shutdown_priv: N
         Process_priv: N
            File_priv: N
           Grant_priv: N
      References_priv: N
           Index_priv: N
           Alter_priv: N
         Show_db_priv: N
           Super_priv: Y
Create_tmp_table_priv: N
     Lock_tables_priv: N
         Execute_priv: N
      Repl_slave_priv: N
     Repl_client_priv: N
     Create_view_priv: N
       Show_view_priv: N
  Create_routine_priv: N
   Alter_routine_priv: N
     Create_user_priv: N
           Event_priv: N
         Trigger_priv: N
            ssl_type: 
           ssl_cipher: 
          x509_issuer: 
         x509_subject: 
        max_questions: 0
          max_updates: 0
      max_connections: 0
max_user_connections: 0



// 从上面可以看出,yongfu_a 用户只有前面授权的SUPER权限。

// 怎么给yongfu_a用户授予增删改查的权限?使用如下命令:

mysql> GRANT SELECT,UPDATE,INSERT,DELETE on *.* toyongfu_a@'localhost';              
Query OK, 0 rows affected (0.00 sec)



// 再查看下权限情况:

mysql> select * from user where User='yongfu_a'\G;                     
*************************** 1. row ***************************
                Host: localhost
                User: yongfu_a
            Password: *1603BD6A73604A872220561294D3C5916A7B1E60
          Select_priv: Y
          Insert_priv: Y
          Update_priv: Y
          Delete_priv: Y
          Create_priv: N
            Drop_priv: N
          Reload_priv: N
        Shutdown_priv: N
         Process_priv: N
            File_priv: N
           Grant_priv: N
      References_priv: N
           Index_priv: N
           Alter_priv: N
         Show_db_priv: N
           Super_priv: Y
Create_tmp_table_priv: N
     Lock_tables_priv: N
         Execute_priv: N
      Repl_slave_priv: N
     Repl_client_priv: N
     Create_view_priv: N
       Show_view_priv: N
  Create_routine_priv: N
   Alter_routine_priv: N
     Create_user_priv: N
           Event_priv: N
         Trigger_priv: N
            ssl_type: 
           ssl_cipher: 
          x509_issuer: 
         x509_subject: 
        max_questions: 0
          max_updates: 0
      max_connections: 0
max_user_connections: 0



// 这时已经被授予了增删改查的权限了,同时发现SUPER权限还是保留的。

// 其实GRANT是不会取消权限的,取消权限使用REVOKE命令。



3.2 取消授权

// REVOKE语句的语法格式如下:

// 其中privileges是要取消的权限,user是要被取消权限的用户名,特别注意user前的关键字变为了from

REVOKE privileges (columns) on what from user ;



// 取消 yongfu_a用户的增删改查权限

mysql> REVOKE SELECT,UPDATE,INSERT,DELETE on *.* from yongfu_a@'localhost';
Query OK, 0 rows affected (0.00 sec)

mysql> select * from user where User='yongfu_a'\G;                        
*************************** 1. row ***************************
                Host: localhost
                User: yongfu_a
            Password: *1603BD6A73604A872220561294D3C5916A7B1E60
          Select_priv: N
          Insert_priv: N
          Update_priv: N
          Delete_priv: N
          Create_priv: N
            Drop_priv: N
          Reload_priv: N
        Shutdown_priv: N
         Process_priv: N
            File_priv: N
           Grant_priv: N
      References_priv: N
           Index_priv: N
           Alter_priv: N
         Show_db_priv: N
           Super_priv: Y
Create_tmp_table_priv: N
     Lock_tables_priv: N
         Execute_priv: N
      Repl_slave_priv: N
     Repl_client_priv: N
     Create_view_priv: N
       Show_view_priv: N
  Create_routine_priv: N
   Alter_routine_priv: N
     Create_user_priv: N
           Event_priv: N
         Trigger_priv: N
            ssl_type: 
           ssl_cipher: 
          x509_issuer: 
         x509_subject: 
        max_questions: 0
          max_updates: 0
      max_connections: 0
max_user_connections: 0



REVOKE语句用于取消用户的权限,而不可以删除用户。



4. 删除用户

取消了所有的权限,用户仍然可以连接到服务器。要想彻底的删除用户,必须使用DELETE语句将该用户的记录从mysql数据库中的user表中删除。

// 使用DELETE删除用户yongfu_a,代码如下:

mysql> use mysql

Database changed

mysql> delete from user where user='yongfu_a' andhost='localhost' ;

mysql>flush privileges ;

Query OK, 1 row affected (0.02 sec)

其中,delete用于删除用户,flush告诉服务器重新加载授权表。



增加:所有权限列表:

Table 13.1. Permissible Privilegesfor GRANT and REVOKE

Privilege

Meaning

ALL [PRIVILEGES]

Grant all privileges at specified access level except GRANT OPTION

ALTER

Enable use of ALTER TABLE

ALTER ROUTINE

Enable stored routines to be altered or dropped

CREATE

Enable database and table creation

CREATE ROUTINE

Enable stored routine creation

CREATE TEMPORARY TABLES

Enable use of CREATE TEMPORARY TABLE

CREATE USER

Enable use of CREATE USERDROP USERRENAME USER, and REVOKE ALL PRIVILEGES

CREATE VIEW

Enable views to be created or altered

DELETE

Enable use of DELETE

DROP

Enable databases, tables, and views to be dropped

EVENT

Enable use of events for the Event Scheduler

EXECUTE

Enable the user to execute stored routines

FILE

Enable the user to cause the server to read or write files

GRANT OPTION

Enable privileges to be granted to or removed from other accounts

INDEX

Enable indexes to be created or dropped

INSERT

Enable use of INSERT

LOCK TABLES

Enable use of LOCK TABLES on tables for which you have the SELECT privilege

PROCESS

Enable the user to see all processes with SHOW PROCESSLIST

REFERENCES

Not implemented

RELOAD

Enable use of FLUSH operations

REPLICATION CLIENT

Enable the user to ask where master or slave servers are

REPLICATION SLAVE

Enable replication slaves to read binary log events from the master

SELECT

Enable use of SELECT

SHOW DATABASES

Enable SHOW DATABASES to show all databases

SHOW VIEW

Enable use of SHOW CREATE VIEW

SHUTDOWN

Enable use of mysqladmin shutdown

SUPER

Enable use of other administrative operations such as CHANGE MASTER TOKILLPURGE BINARY LOGSSET GLOBAL, and mysqladmin debug command

TRIGGER

Enable trigger operations

UPDATE

Enable use of UPDATE

USAGE

Synonym for “no privileges”

猜你喜欢

转载自blog.csdn.net/weixin_36385007/article/details/79708904