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’

例如:

mysql> create user root@'10.0.0.%' identified by 'redhat';
Query OK, 0 rows affected (0.11 sec)
rename user old_name to new_name;

例如:

mysql> create user bob;
Query OK, 0 rows affected (0.01 sec)

mysql> rename user bob@'%' to bob2@'%';
Query OK, 0 rows affected (0.00 sec)
drop user 'user_name'@'host'

例如:

mysql> drop user bob2@'%';
Query OK, 0 rows affected (0.00 sec)

说明:

  • 新版mysql中用户密码可以保存在mysql.user表的authentication_string字段中
  • 如果mysql.user表的authentication_string和password字段都保存密码authentication_string优先生效
  • 使用update来操作表修改密码,需要使用FLUSH PRIVILEGES刷新权限才会生效

不同版本的mysql修改用户的方式有所不同。

mysql5.6不能使用alter user命令来修改密码。可一使用update何set password来修改密码。

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

使用update修改密码:

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

使用set password修改密码:

set password for xxx@xxx = password('xxx');

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@'xxx' IDENTIFIED BY 'auth_string';
--skip-grant-tables:不做权限和账号验证。

--skip-networking:禁止远程连接(空密码远程连接上去危险性大),自己能连上因为不走端口号,走的是socket文件

范例: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的所有数据。

重新启动数据库的时候就会重新初始化,相当于重装系统。
注意:测试环境测试。

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

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

  • 数据库:对数据库的相关操作权限
  • 表:针对表的相关操作
  • 管理类:查看数据库、创建用户等操作
  • 程序类:针对函数、存储过程、触发器等的操作
  • 所有权限:ALL PRIVILEGES 或 ALL
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 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)

例如:

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

Original: https://www.cnblogs.com/heyongshen/p/16675885.html
Author: 背对背依靠
Title: MySQL——用户和权限管理

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/578600/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • 内存错误和服务器内存RAS功能-DELL篇-1

    简介 内存子系统错误是现代计算系统中最常见的一些错误类型。了解内存错误是如何发生的以及如何预防或避免它们可能是一个复杂的话题–在过去30年里,这个话题挑战了无数的行业研…

    Linux 2023年6月7日
    0139
  • 列表初始化

    C++11将列表初始化(大括号初始化)作为一种通用的初始化方式.可用于所有类型. 数组以前就可以用列表初始化,但 C++11 中的列表初始化新增了一些功能: 初始化数组时,可省略等…

    Linux 2023年6月13日
    081
  • 系统初始化

    一般系统安装好后,按照自己习惯定义 csharp;gutter:true;</p> <h1>alias</h1> <p>echo &…

    Linux 2023年6月7日
    060
  • 天气干燥怎么防止被静电电到

    可以摸一下墙壁或地板,把电放掉,这样摸门把手之类的金属物品就不会被电到了。 可以摸一下墙壁或地板,把电放掉,这样摸门把手之类的金属物品就不会被电到了。亲身实践,十分有效。只是摸墙和…

    Linux 2023年6月6日
    0100
  • debian与windows时间不同步的简单治疗方法

    试过几种方法, 但就这个方法好使点。hwclock -w –localtime Original: https://www.cnblogs.com/leotiger/p…

    Linux 2023年6月13日
    076
  • Windows 域控配置时间同步

    此功能是因内网时间与互联网时间不同步,需我们手动指定互联网NTP服务器来同步时间。一般默认情况下,加域客户端同步的是域主机的时间。如果域控的主机时间不准的话,那么域内的客户端也就随…

    Linux 2023年6月8日
    0154
  • 高等代数:4 矩阵的运算

    4 矩阵的运算 1、数域K上两个矩阵称为 相等,如果它们的行数相等,列数也相等,并且它们的所有元素对应相等。 2、定义1:设(A=(a_{ij}),B=(b_{ij}))都是数域K…

    Linux 2023年6月8日
    0100
  • 搭建ES集群

    添加以下elasticsearch用户: bash;gutter:true; 1 useradd elasticsearch 2 passwd elasticsearch</…

    Linux 2023年6月8日
    088
  • kubeadm搭建单master k8s集群

    kubeadm搭建单master k8s集群 一、 准备环境 软件环境: 软件 版本 操作系统 CentOS7.9_x64 内核 kernel-ml-5.17.3-1.el7 Do…

    Linux 2023年6月13日
    0125
  • 用动态端口,增强winrm,open sshd的,服务器安全

    前言 我开发了一套开源,免费,跨平台的devops脚本批量运维工具。【kaiiit家的饭店】是软件的正式名字。【卡死你3000】是第一版开发代号。 想要增强win被控机密码安全。可…

    Linux 2023年6月14日
    085
  • go-结构体内存布局

    方式一:通过 var 声明结构体 在 Go 语言中当一个变量被声明的时候,系统会自动初始化它的默认值,比如 int 被初始化为 0,指针为 nil。 var 声明同样也会为结构体类…

    Linux 2023年6月13日
    0103
  • 综合布线 子网掩码 IP地址 子网划分

    A类地址:1.0.0.0~126.255.255.255 255.0.0.0 B类地址:128.0.0.0~191.255.255.255 255.255.0.0 C类地址:192…

    Linux 2023年6月6日
    092
  • 【Example】C++ Vector 内存预分配的良好习惯

    为什么要对 Vector 进行内存预分配? 1,Vector 本身是一个内存只会增长不会减小的容器。 2,Vector 存在 size 和 capacity 两种计数,size 即…

    Linux 2023年6月13日
    0108
  • CTF竞赛权威指南(PWN篇)下载地址

    博客网址:www.shicoder.top微信:18223081347欢迎加群聊天 :452380935 这里给大家提供《CTF竞赛权威指南(PWN篇)》的下载地址(不是网上的64…

    Linux 2023年6月13日
    0102
  • Mybatis源码解读-SpringBoot中配置加载和Mapper的生成

    本文 mybatis-spring-boot探讨在springboot工程中mybatis相关对象的注册与加载。 建议先了解mybatis在spring中的使用和springboo…

    Linux 2023年6月7日
    0123
  • Linux 服务器巡检脚本

    bash;gutter:true;</p> <h1>!/bin/bash</h1> <p>cat < $RESULTFILE …

    Linux 2023年6月13日
    0137
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球