MySQL用户管理
Posted wangziqiang123
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL用户管理相关的知识,希望对你有一定的参考价值。
用户管理
1、新建普通用户
(1)使用CREATE USER语句创建新用户
使用格式:
CREATE USER 'user'@'host' [IDENTIFIED BY [PASSWORD] 'password'] [,'user'@'host' [IDENTIFIED BY [PASSWORD] 'password']...]
user:新建用户
host:主机名
IDENTIFIED BY 设置用户密码
示例:
MariaDB [(none)]> CREATE USER test@'localhost' IDENTIFIED BY '123456';
在mysql.user表中会插入一行记录
MariaDB [(none)]> SELECT user,host,password FROM mysql.user;
+------+-----------------------+-------------------------------------------+
| user | host | password |
+------+-----------------------+-------------------------------------------+
| root | localhost | |
| root | localhost.localdomain | |
| root | 127.0.0.1 | |
| root | ::1 | |
| | localhost | |
| | localhost.localdomain | |
| test | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+------+-----------------------+-------------------------------------------+
(2)在musql.user表中INSERT用户
MariaDB [(none)]> INSERT INTO mysql.user (user,host,password) VALUES ("testA","192.168.4.60",PASSWORD('123456'));
执行完INSERT之后,要使用如下命令生效。
MariaDB [(none)]> FLUSH PRIVILEGES;
(3)使用GRANT创建用户
使用格式:
GRANT priv_type ON database.table TO user[IDENTIFIED BY [PASSWORD] 'password'] [,user [IDENTIFIED BY [PASSWORD] 'password']...]
priv_type:授予的权限
database.table:权限的范围
user:新建用户
IDENTIFIED BY 设置密码
示例:
MariaDB [(none)]> GRANT SELECT ON *.* TO testB@'192.168.4.60' IDENTIFIED BY '123456';
2、重命名
使用格式:
RENAME USER old_user TO new_user[, old_user TO new_user] ...
示例:
MariaDB [(none)]> RENAME USER testA@'192.168.4.60' TO testC@'192.168.4.60';
3、删除用户
使用格式:
DROP USER 'user'@'host' [, 'user'@'host'] ...
示例:
MariaDB [class]> DROP USER testC@'192.168.4.60';
4、修改用户密码
(1)使用mysqladmin命令修改密码
使用格式:
mysqladmin -uUSERNAME -hHOST -p password 'NEW_PASS'
示例:
[root@localhost~]#mysqladmin -uroot -p password
Enter password:
New password:
Confirm new password:
[root@localhost~]#mysqladmin -utest -p password 'testabd'
Enter password:
mysqladmin: Can't turn off logging; error: 'Access denied; you need (at least one of) the SUPER privilege(s) for this operation'
修改普通用户需要超级管理权限
(2)修改mysql.user表
使用格式:
UPDATE mysql.user SET Password=PASSWORD('cleartext password') WHERE User='USERNAME' AND Host='HOST';
示例:
MariaDB [(none)]> UPDATE mysql.user SET PASSWORD=PASSWORD('testabcd') WHERE User='test';
(3)使用SET语句修改密码
使用格式:
SET PASSWORD [FOR 'user'@'host'] = PASSWORD('cleartext password');
示例:
MariaDB [(none)]> SET PASSWORD FOR test@'localhost'=PASSWORD('testabcd');
(4)GRANT语句修改普通用户密码
示例:
MariaDB [(none)]> GRANT SELECT ON *.* TO 'test'@'localhost' IDENTIFIED BY '123456';
5、忘记管理员密码如何解决?
(1)先确保mysql服务停止
[root@localhost~]#systemctl stop mariadb.service
(2)确保其他用户连接不到mysql服务
(3)修改启动配置文件
[root@localhost~]#vim /usr/lib/systemd/system/mariadb.service
(4)启动服务并修改密码
[root@localhost~]#systemctl restart mariadb.service
MariaDB [(none)]> UPDATE mysql.user SET PASSWORD=PASSWORD('123456') WHERE user='root';
(5)修改更改文件
[root@localhost~]#systemctl stop mariadb.service
[root@localhost~]#vim /usr/lib/systemd/system/mariadb.service
删除刚添加的参数
(6)重启服务测试
[root@localhost~]#systemctl restart mariadb.service
[root@localhost~]#mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 3
Server version: 5.5.56-MariaDB MariaDB Server
授权
使用格式:
GRANT priv_type [(column_list)] [, priv_type [(column_list)]] ...
ON [object_type] priv_level
TO user_specification [, user_specification] ...
[REQUIRE {NONE | ssl_option [[AND] ssl_option] ...}]
[WITH with_option ...]
priv_type:表示权限类型
column_list:设置列
user:设置用户
password:用户密码
WITH关键字后面带有一个或多个with_option参数
GRANT OPTION:被授权用户可以使用的参数
MAX_QUERIES_PER_HOUR count:设置每小时允许执行的count查询
MAX_UPDATES_PER_HOUR count:设置每小时count更新
MAX_CONNECTIONS_PER_HOUR count:设置每小时可以建立count连接
MAX_USER_CONNECTIONS count:设置单个用户可以同时具有count连接数
示例:
MariaDB [class]> GRANT SELECT(number) ON class.class TO 'test'@'localhost' IDENTIFIED BY '123456';
查看授权
使用格式:
SHOW GRANTS [FOR 'user'@'host']
示例:
MariaDB [class]> SHOW GRANTS FOR test@'localhost'G;
*************************** 1. row ***************************
Grants for test@localhost: GRANT SELECT ON *.* TO 'test'@'localhost' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9'
*************************** 2. row ***************************
Grants for test@localhost: GRANT SELECT (number) ON `class`.`class` TO 'test'@'localhost'
MariaDB [class]> SHOW GRANTS ; #默认查询root的权限
取消授权
使用格式
REVOKE priv_type [(column_list)][, priv_type [(column_list)]] ...
ON [object_type] priv_level
FROM 'user'@'host' [, 'user'@'host'] ...
示例:
MariaDB [class]> REVOKE SELECT(number) ON class.class FROM test@'localhost';
以上是关于MySQL用户管理的主要内容,如果未能解决你的问题,请参考以下文章