码迷,mamicode.com
首页 > 数据库 > 详细

Linux命令:MySQL系列之十--MySQL用户和权限管理、mysql管理员密码重置

时间:2017-05-11 22:14:14      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:grant flush privileges revoke


MySQL用户账号包括:用户名@主机名

    用户名:16个字符以内

    主机:主机有以下几种表现方式

      主机名: mysql,www.magedu.com

      IP地址: 172.16.90.111

      网络地址:172.16.0.0/255.255.0.0

      通配符:%,_   %:任意字符  _:任意一个

         172.16.%.%     %.magedu.com 

权限级别:全局级别、库级别、表级别、列级别、存储过程和存储函数级别

   全局级别:SELECT * FROM db\G; 查询全局库级别的权限

**************************************************************************

CREATE USER username@host [IDENTIFIED BY ‘password‘] 创建用户

DROP USER ‘username‘@‘host‘; 刪除用戶

RENAME USER old_name TO new_name; 重命名用戶

SHOW GRANTS FOR ‘username‘@‘host‘; 查看用户权限列表

FLUSH PRIVILEGES ; 刷新权限列表

GRANT PRIVILEGES ON [object_type] db.* TO ‘username‘@‘host‘; 给用户增加权限

REVOKE SELECT ON db.* FROM ‘username‘@‘host‘; 取消用户的SELECT权限

**************************************************************************


**************************************************************************

MySQL数据库ROOT用户密码忘记解决方案步骤:


  第一:先关闭mysqld进程,并修改配置文件/etc/my.cnf

[root@lamp ~]# service mysqld stop  #先停止mysqld进程

Shutting down MySQL..                             [  OK  ]  

[root@lamp ~]# vim /etc/init.d/mysqld #修改mysqld的启动脚本,修改内容如下红色框内,保存退出

技术分享

[root@lamp ~]# service mysqld start  #启动mysqld进程

Starting MySQL..                                [  OK  ] 

[root@lamp ~]# mysql  #此时登录mysql即可不需要用户名和密码

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 1

Server version: 5.5.28-log Source distribution

Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. 

mysql> USE mysql

Database changed

mysql> SELECT User,Host,Password FROM user; #查询user表的三个字段,是需要密码登录的

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

| User | Host      | Password                                  |

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

| root | localhost | *A198E6EEE923DA319BBF86C99624479A198E6EEE9 |

| root | lamp      | *A198E6EEE9823DA319BBF86C99624479A198E6EEE9 |

| root | 127.0.0.1 | *A198E6EEE9DA319BBF86C99624479A198E6EEE9 |

| root | ::1       | *A198E6EEE93DA319BBF86C99624479A198E6EEE9 |

| test | localhost | *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29 |

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

5 rows in set (0.00 sec)

mysql> UPDATE user SET Password=PASSWORD(‘redhat‘) WHERE User=‘root‘; #此时由于跳过

了grant权限列表,所以只能通过修改user表的Password字段的值来修改用户密码。

Query OK, 0 rows affected (0.00 sec)

Rows matched: 4  Changed: 0  Warnings: 0

mysql> SELECT User,Host,Password FROM user; #查询user表的三个字段,是需要密码登录的

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

| User | Host      | Password                                  |

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

| root | localhost | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |

| root | lamp      | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |

| root | 127.0.0.1 | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |

| root | ::1       | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |

| test | localhost | *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29 |

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

5 rows in set (0.00 sec)

mysql>\q

[root@lamp ~]# service mysqld stop  #停止mysqld进程

Shutting down MySQL.                              [  OK  ]

[root@lamp ~]# vim /etc/init.d/mysqld #修改启动脚本,把之前修改的内容去掉后保存退出

技术分享

[root@lamp ~]# service mysqld start #启动mysqld进程

Starting MySQL..                                 [  OK  ]

[root@lamp ~]# mysql #此时直接登录mysql提示输入用户及密码

ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)

[root@lamp ~]# mysql -uroot -p  #指定通过root账号登录 -p指定需要输入密码登录

Enter password:      输入正确的密码后登录mysql 

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 2

Server version: 5.5.28-log Source distribution


Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.


Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.


Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.


mysql>            #至此mysql数据库密码遗忘的问题便解决了。 


**************************************************************************



下表为权限对应的作用范围:

技术分享


创建用户 CREATE USER:CREATE USER username@host [IDENTIFIED BY ‘password‘]创建用户,并通过IDENTIFIED BY ‘password‘,设定密码。

Usage:

mysql> CREATE USER test@localhost IDENTIFIED BY ‘test‘; #创建一个用户test本地数据库账号,

密码为test。

Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES ; #刷新权限列表

Query OK, 0 rows affected (0.00 sec)

mysql> SHOW GRANTS FOR test@localhost\G; #查看数据库账户test@localhost的权限列表

*************************** 1. row ***************************

Grants for test@localhost: GRANT USAGE ON *.* TO ‘test‘@‘localhost‘ IDENTIFIED BY PASSWORD ‘*94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29‘

1 row in set (0.00 sec)

重新打开另一个客户端,登录以test账号登录mysql

[root@lamp ~]# mysql -utest -p  #登录mysql数据库以test用户,输入用户密码

Enter password: 

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 17

Server version: 5.5.28-log Source distribution

Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql> SHOW DATABASES;  #登录成功,查看该账号下的数据库

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

| Database           |

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

| information_schema |

| test               |

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

2 rows in set (0.00 sec)


权限设定GRANT:GRANT PRIVILEGES ON [object_type] db.* TO ‘username‘@‘host‘; #指定权限PRIVILEGES  ON指定对象名称db.*  object_type指定对象类型 TO username@host 指定用户。

object_type对象类型有:TABLE(表) FUNCTION(函数) PROCEDURE(程序、库)

Usage:GRANT EXECUTE ON FUNCTION db.abc TO ‘username‘@‘host‘; #授权给username@host用户,对

db数据库的abc函数有执行权限。

 GRANT UPDATE(Age) ON db.testtb TO ‘username‘@‘host‘; #授权给username@host用户对db数据库的,testtb表的Age字段具有UPDATE权限。



本文出自 “学linux历程” 博客,请务必保留此出处http://woyaoxuelinux.blog.51cto.com/5663865/1924645

Linux命令:MySQL系列之十--MySQL用户和权限管理、mysql管理员密码重置

标签:grant flush privileges revoke

原文地址:http://woyaoxuelinux.blog.51cto.com/5663865/1924645

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!