Linux命令:MySQL系列之十--MySQL用户和权限管理mysql管理员密码重置
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux命令:MySQL系列之十--MySQL用户和权限管理mysql管理员密码重置相关的知识,希望对你有一定的参考价值。
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 [email protected] [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
[[email protected] ~]# service mysqld stop #先停止mysqld进程
Shutting down MySQL.. [ OK ]
[[email protected] ~]# vim /etc/init.d/mysqld #修改mysqld的启动脚本,修改内容如下红色框内,保存退出
[[email protected] ~]# service mysqld start #启动mysqld进程
Starting MySQL.. [ OK ]
[[email protected] ~]# 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
[[email protected] ~]# service mysqld stop #停止mysqld进程
Shutting down MySQL. [ OK ]
[[email protected] ~]# vim /etc/init.d/mysqld #修改启动脚本,把之前修改的内容去掉后保存退出
[[email protected] ~]# service mysqld start #启动mysqld进程
Starting MySQL.. [ OK ]
[[email protected] ~]# mysql #此时直接登录mysql提示输入用户及密码
ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)
[[email protected] ~]# 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 [email protected] [IDENTIFIED BY ‘password‘]创建用户,并通过IDENTIFIED BY ‘password‘,设定密码。
Usage:
mysql> CREATE USER [email protected] 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 [email protected]\G; #查看数据库账户[email protected]的权限列表
*************************** 1. row ***************************
Grants for [email protected]: GRANT USAGE ON *.* TO ‘test‘@‘localhost‘ IDENTIFIED BY PASSWORD ‘*94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29‘
1 row in set (0.00 sec)
重新打开另一个客户端,登录以test账号登录mysql
[[email protected] ~]# 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 [email protected] 指定用户。
object_type对象类型有:TABLE(表) FUNCTION(函数) PROCEDURE(程序、库)
Usage:GRANT EXECUTE ON FUNCTION db.abc TO ‘username‘@‘host‘; #授权给[email protected]用户,对
db数据库的abc函数有执行权限。
GRANT UPDATE(Age) ON db.testtb TO ‘username‘@‘host‘; #授权给[email protected]用户对db数据库的,testtb表的Age字段具有UPDATE权限。
本文出自 “学linux历程” 博客,请务必保留此出处http://woyaoxuelinux.blog.51cto.com/5663865/1924645
以上是关于Linux命令:MySQL系列之十--MySQL用户和权限管理mysql管理员密码重置的主要内容,如果未能解决你的问题,请参考以下文章
Linux命令:MySQL系列之十四--MySQL备份与还原(LVM逻辑卷)
Linux命令:MySQL系列之十二--MySQL备份与还原(重要章节)
Linux命令:MySQL系列之十四--MySQL备份与还原(xtrabackup工具重要章节)
Linux命令:MySQL系列之十二--MySQL备份与还原(针对单张表SELECT备份)