MySQL用户和权限管理「建议收藏」

MySQL用户和权限管理「建议收藏」MySQL的用户账号: 由两部分组成:用户名和主机名 格式:'user_name'@'host' host必须要用引号括起来 注意:host可以是一个主机名也可以是具体

MySQL用户和权限管理

MySQL的用户账号:
  • 由两部分组成:用户名和主机名

  • 格式:”user_name”@”host” host必须要用引号括起来

注意:host可以是一个主机名也可以是具体的ip地址、网段等。

当host为主机名时:
#例如:
user1@"web1.redhat.org"

当host是ip地址或者网段时:
#例如:
tom@172.16.%.%
nacy@"192.168.1.%"
bob@"10.0.0.0/255.255.0.0"
创建用户:
create user "user_name"@"host"  [IDENTIFIED BY "password"]

注意:
host必须用引号括起来,user_name部分可以忽略引号

创建的用户默认只有登录数据库的权限。

创建用户的时候指定密码:create user user_name identified by “password”

修改用户名:
rename user old_name to new_name;
删除用户:
drop user "user_name"@"host"
修改用户密码:

注意

  • 新版mysql中用户密码可以保存在mysql.user表的authentication_string字段中

  • 如果mysql.user表的authentication_string和password字段都保存密码authentication_string优先生效

  • 使用update来操作表修改密码,需要使用FLUSH PRIVILEGES刷新权限才会生效

修改用户密码

文档:https://dev.mysql.com/doc/refman/5.7/en/set-password.html

5.6版本修改密码

mysql5.6的alter user命令只能用来修改用户密码的过期时间。

(1)update mysql.user set password=password("123456") where User="xxx" and Host = "xxx";

(2)set password for xxx@xxx = password("xxx");
5.7及以上版本

MySQL 5.7.6版本起,user表仅使用authentication_string列代替之前版本中的password列来存储密码

  • 使用没有的字符串 PASSWORD()
SET PASSWORD FOR "jeffrey"@"localhost" = "password";
  • 使用PASSWORD()函数(pawword()在 MySQL 5.7 中已弃用,在8.0删除了这个函数)
SET PASSWORD FOR "jeffrey"@"localhost" = PASSWORD("password");
  • 更改用户密码的首选语句:alter user
ALTER USER user IDENTIFIED BY "auth_string";
忘记管理员密码的解决办法:
  1. 启动mysqld进程时,为其使用如下选项:
--skip-grant-tables:不做权限和账号验证。

--skip-networking:禁止远程连接(空密码远程连接上去危险性大),自己能连上因为不走端口号,走的是socket文件
  1. 使用UPDATE命令修改管理员密码

  2. 关闭mysqld进程,移除上述两个选项,重启mysqld

范例:Mariadb 和MySQL5.6版之前激活成功教程root密码

#修改配置文件
[root@centos8 ~]#vim /etc/my.cnf
[mysqld]
skip-grant-tables
skip-networking

#重启mysql
[root@centos8 ~]#systemctl restart mysqld|mariadb

#进入MySQL修改root密码
[root@centos8 ~]#mysql #连接到MySQL
#方法1
#mariadb 旧版和MySQL5.6版之前
MariaDB [(none)]> update mysql.user set password=password("ubuntu") where user="root";

#mariadb 新版
MariaDB [(none)]> update mysql.user set authentication_string=password("ubuntu") where user="root";

#方法2
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> alter user root@"localhost" identified by "ubuntu";

#注释掉对应文件
[root@centos8 ~]#vim /etc/my.cnf
[mysqld]
#skip-grant-tables
#skip-networking

#重启mysql
[root@centos8 ~]#systemctl restart mysqld|mariadb

范例: MySQL5.7和8.0 激活成功教程root密码

#修改配置文件
[root@centos8 ~]#vim /etc/my.cnf
[mysqld]
skip-grant-tables 
skip-networking  #MySQL8.0不需要

#重启MySQL服务
[root@centos8 ~]#systemctl restart mysqld
#方法1
mysql> update mysql.user set authentication_string="" where user="root" and
host="localhost";

#方法2
mysql> flush privileges; #再执行下面任意一个命令
mysql> alter user root@"localhost" identified by "ubuntu";

#进入MySQL后修改root密码为新密码
mysql> set password for root@"localhost"="ubuntu";

#注释掉对应语句
[root@centos8 ~]#vim /etc/my.cnf
[mysqld]
#skip-grant-tables
#skip-networking

#重启mysql
[root@centos8 ~]#systemctl restart mysqld
更加简单激活成功教程密码的方法

停止数据库以后,删除掉mysql的所有数据。

重新启动数据库的时候就会重新初始化,相当于重装系统。
注意:测试环境测试。
权限管理和DCL语句

注意
新建用户的默认权限:USAGE,仅仅能登录数据库系统

权限分类

数据库、表、字段、管理类、程序类

  • 数据库:对数据库的相关操作权限

  • 表:针对表的相关操作

  • 管理类:查看数据库、创建用户等操作

  • 程序类:针对函数、存储过程、触发器等的操作

  • 所有权限:ALL PRIVILEGES 或 ALL

授权:grant .. to ..
grant 权限 on db.tb to user [WITH GRANT OPTION]

#*.*:表示所有数据库和所有表
#WITH GRANT OPTION:允许把自己的权限授权给别人

例如:

mysql> grant all on *.* to tom@"%";
Query OK, 0 rows affected (0.00 sec)

mysql> show grants for tom;
+------------------------------------------+
| Grants for tom@%                         |
+------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO "tom"@"%" |
+------------------------------------------+
1 row in set (0.00 sec)

在授权的时候创建用户(8.0已经删除这种语法)

grant privilege on db.tb to user identified by "password"

例如:

mysql> grant all on *.* to bob@"%" identified by "redhat";
Query OK, 0 rows affected, 1 warning (0.00 sec)
取消权限:revoke .. from ..
revoke privilege on db.tb for user

例如:

mysql> revoke all on *.* from bob@"%";
Query OK, 0 rows affected (0.00 sec)

mysql> show grants for bob@"%";
+---------------------------------+
| Grants for bob@%                |
+---------------------------------+
| GRANT USAGE ON *.* TO "bob"@"%" |
+---------------------------------+
1 row in set (0.00 sec)

查看指定用户的全权限:show grants for ..

例如:

mysql> show grants for tom@"%";
+---------------------------------+
| Grants for tom@%                |
+---------------------------------+
| GRANT USAGE ON *.* TO "tom"@"%" |
+---------------------------------+
1 row in set (0.00 sec)

原文地址:https://www.cnblogs.com/heyongshen/archive/2022/09/11/16675885.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/4782.html

(0)
上一篇 2023-06-05
下一篇 2023-06-05

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注