grant 授权
作用: 添加用户并设置权限
命令格式:
grant 权限列表 on 库名.表名 to 用户名@"客户端地址" identified by "密码" with grant option;
详细介绍:
权限列表:select、
update(字段名,...字段N)(可以更新的字段列表)、
(drop,delete)(逗号分隔)、
usage(无权限,哈哈!这个官方怎么想出来的)、
all(所有命令|权限)
库名.表名:
mysql.music(mysql库下的music表)、
mysql.*(mysql库下的所有表)、
*.*(所有库的所有表)
用户名:root
客户端地址:
%(所有主机)、
192.168.84.%(网段内的所有主机)、
192.168.84.10(一台主机)、
localhost(数据库服务器本机)
密码:password
with grant option: 是否有授权的权限,也就是使用grant命令的权限
实例:
grant all on *.* to root@"%" identified by "qwer1234!" ;
实例详解:
授权root用户可以在所有主机登录和对所有库所有表的权限
其他实例:
MariaDB [(none)]> grant all on *.* to mydb@"%" identified by "qwer1234!";
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]>
有网络访问限制的记得关掉
对授权用户进行管理
查看及密码修改
select user(); # 查看当前登录用户名以及客户端地址
MariaDB [(none)]> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
show grants; #显示当前登录用户访问权限
show grants for mydb@"%"; # 查看指定用户的权限
MariaDB [(none)]> show grants;
+---------------------------------------------------------------------+
| Grants for root@localhost |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------+
2 rows in set (0.00 sec)
set password=password("123qwer!"); # 修改当前登录用户的密码
MariaDB [(none)]> set password=password("123qwer!");
Query OK, 0 rows affected (0.00 sec)
修改授权用户的密码,有执行失败的例子:可以理解一下(grant all on *.* to mydb@"%" identified by "qwer1234!";)
MariaDB [(none)]> set password for mydb@"192.168.84.20" =password("123qwer!");
ERROR 1133 (42000): Can't find any matching row in the user table
MariaDB [(none)]> set password for mydb@"192.168.84.10" = password("123qwer!");
ERROR 1133 (42000): Can't find any matching row in the user table
MariaDB [(none)]> set password for mydb@"%" = password("123qwer!");
Query OK, 0 rows affected (0.00 sec)
# 删除授权用户,就不执行了(grant all on *.* to mydb@"%" identified by "qwer1234!";)
MariaDB [(none)]> drop user mydb@"%";
授权的信息以数据的形式存在mysql软件自带数据库mysql中
user
记录授权用户及权限
db
记录用户对数据库的访问权限
tables_priv
记录用户对数据库表的访问权限
columns_pirv
记录用户对数据库表字段的访问权限
user
db
revoke 撤销权限(命令)
命令格式
revoke 权限列表 on 库名.表 from 用户名@"客户端地址";
root密码忘记怎么办
- 停止mysql
- 跳过授权表启动mysql
vi /ect/my.conf
[mysqld]
skip-grant-tables
validate_password_policy=0
validate_password_length=1
- 修改root密码
方法一:
MariaDB [(none)]> update mysql.user set authentication_string=password("123456") where host="hostlocal" and user="root";
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0
方法二(有点哈哈)
[root@centos7 ~]# mysqladmin -hlocalshot -uroot -p password "qwer";
Enter password:
- 启动mysql