MySQL用户管理

 

Mysql用户的作用:

       1. 登录mysql 

       2. 管理mysql

创建用户

语法:create user username@’host’ identified by 'password';

说明:
username:你将创建的用户名
host:指定该用户从哪个主机上可以登陆,如果是本地用户可用localhost,如果想让该用户可以从任意远程主机登陆,可以使用通配符%
password:该用户的登陆密码

 

mysql> create user test@'localhost' identified by 'test123';

注:mysql 8.0以后必须先创建用户再授权

查看刚创建的用户

select user,host from mysql.user;

 

修改用户密码

mysql> set password for test@'localhost'=password('hello');

mysql> flush privileges;

如果想修改当前用户密码

mysql> SET PASSWORD = PASSWORD("hello123");

删除用户

drop user test@’localhost’;

用户权限管理

查看用户权限

show grants for test@’localhost’;

注:usage 表示没有任何权限

给用户授权

GRANT privileges ON databasename.tablename TO 'username'@'host';

说明:

privileges:权限列表,all 表示所有权限,常用权限select,update,delete,create,insert

databasename.tablename:权限作用于哪个库哪个表,*.* 表示作用于所有的库和表,databasename.* 表示作用于某个库下的所有表,也可以对表的某列授权

'username'@'host':权限赋予的用户

注:mysql 8.0已经不支持授权时直接创建用户

查看权限列表

show privileges;

说明:Privilege:权限,Context:权限作用对象,Comment:说明

给test用户test库的select和insert权限

mysql> grant select,insert on test.* to test@'localhost';

查看用户权限

mysql> show grants for test@'localhost';

+--------------------------------------------------------+

| Grants for test@localhost                              |

+--------------------------------------------------------+

| GRANT USAGE ON *.* TO 'test'@'localhost'               |

| GRANT SELECT, INSERT ON `test`.* TO 'test'@'localhost' |

+--------------------------------------------------------+

查看root用户权限

mysql> show grants for root@'localhost';

+---------------------------------------------------------------------+

| Grants for root@localhost                                           |

+---------------------------------------------------------------------+

| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |

| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION        |

+---------------------------------------------------------------------+

WITH GRANT OPTION:表示该用户可以对其他用户进行授权,此权限一般只给管理员

回收用户权限

REVOKE privileges ON databasename.tablename FROM 'username'@'host';

查看用户当前权限

mysql> show grants for test@'localhost';

+----------------------------------------------------------------+

| Grants for test@localhost                                      |

+----------------------------------------------------------------+

| GRANT USAGE ON *.* TO 'test'@'localhost'                       |

| GRANT SELECT, INSERT, UPDATE ON `test`.* TO 'test'@'localhost' |

+----------------------------------------------------------------+

回收test@’localhost’用户对test库的select权限

mysql> revoke select on test.* from test@'localhost';

mysql> show grants for test@'localhost';

+--------------------------------------------------------+

| Grants for test@localhost                              |

+--------------------------------------------------------+

| GRANT USAGE ON *.* TO 'test'@'localhost'               |

| GRANT INSERT, UPDATE ON `test`.* TO 'test'@'localhost' |

+--------------------------------------------------------+

回收test@’localhost’用户对test库的所有权限

mysql> revoke all on test.* from test@'localhost';

mysql> show grants for test@'localhost';

+------------------------------------------+

| Grants for test@localhost                |

+------------------------------------------+

| GRANT USAGE ON *.* TO 'test'@'localhost' |

+------------------------------------------+

现在test用户只能登录和查看information_schema库

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

+--------------------+

注:为了数据库的安全,用户的权限应该进行严格管理

管理员忘记密码

1. 关闭mysql,/etc/init.d/mysqld stop

2. 使用mysql_safe 安全登录 并跳过授权验证和远程连接

 mysqld_safe --skip-grant-tables --skip-networking &

3. 登录mysql,修改密码

mysql> alter user root@'localhost' identified by '123';

ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

mysql>flush privileges;  #先刷新权限表

mysql> alter user root@'localhost' identified by '123';

4. 修改密码之后重启mysql,并用新密码登录

关闭mysqld_safe启动的mysql实例

mysqladmin -uroot -pxxx shutdown

或者直接kill mysql进程

正常启动mysql并登录

/etc/init.d/mysqld start

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值