阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

MySQL连接权限测试

382次阅读
没有评论

共计 6214 个字符,预计需要花费 16 分钟才能阅读完成。

本实验完成以下测试需求:
需求一:验证 ’%’ 是否包含 ’localhost’
需求二:验证 localhost 和 127.0.0.1 分别使用的连接协议
版本:mariadb 10.1.12

一. 验证 ’%’ 是否包含 ’localhost’
先建立帐号和授权:
MariaDB [(none)]> grant all privileges on *.* to ‘lmsapps’@’%’ identified by “Lms166apps”;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> exit
Bye
[apps@mvxl2972 ~]$ MySQL -ulmsapps -pLms166apps  –socket=/tmp/mysql3306.sock
ERROR 1045 (28000): Access denied for user ‘lmsapps’@’localhost’ (using password: YES)
当只有 ’lmsapps’@’%’ 有权限时,无法登入。
MariaDB [(none)]> select user,host from mysql.user;
+———+———–+
| user    | host      |
+———+———–+
| lmsapps | %        |
| root    | 127.0.0.1 |
| root    | ::1      |
|        | localhost |
| root    | localhost |
|        | mvxl2972  |
| root    | mvxl2972  |
+———+———–+
7 rows in set (0.00 sec)

再增加 lmsapps@’localhost’ 用户:
MariaDB [(none)]> grant all privileges on *.* to lmsapps@’localhost’ identified by ‘Lms166apps’;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> select user,host from mysql.user;
+———+———–+
| user    | host      |
+———+———–+
| lmsapps | %        |
| root    | 127.0.0.1 |
| root    | ::1      |
|        | localhost |
| lmsapps | localhost |
| root    | localhost |
|        | mvxl2972  |
| root    | mvxl2972  |
+———+———–+
[apps@mvxl2972 ~]$ mysql -ulmsapps -pLms166apps  –socket=/tmp/mysql3306.sock
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 28
Server version: 10.1.12-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

MariaDB [(none)]>

现在能正常登入。
说明 ’%’ 是不包含 ’localhost’

二. 验证 localhost 和 127.0.0.1 分别使用的连接协议

1. 在 skip_name_resolve 为开启状态下:
MariaDB [(none)]> show variables like ‘skip%’;
+—————————+——-+
| Variable_name            | Value |
+—————————+——-+
| skip_external_locking    | ON    |
| skip_name_resolve        | ON    |
| skip_networking          | OFF  |
| skip_parallel_replication | OFF  |
| skip_replication          | OFF  |
| skip_show_database        | OFF  |

先 drop 用户 ’lmsapps’@’%’
MariaDB [(none)]> drop user ‘lmsapps’@’%’;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> exit
Bye
[apps@mvxl2972 ~]$ mysql -ulmsapps -pLms166apps -h 127.0.0.1
ERROR 1045 (28000): Access denied for user ‘lmsapps’@’127.0.0.1’ (using password: YES)

 

mysql -ulmsapps -pLms166apps -h 127.0.0.1

通过 127.0.0.1 连接时使用 TCP/IP 协议:
[apps@mvxl2972 ~]$ mysql -ulmsapps -pLms166apps -h 127.0.0.1
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 34
Server version: 10.1.12-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

MariaDB [(none)]> \s
————–
mysql  Ver 15.1 Distrib 10.1.12-MariaDB, for Linux (x86_64) using readline 5.1

Connection id:          34
Current database:
Current user:          lmsapps@127.0.0.1
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ”
Using delimiter:        ;
Server:                MariaDB
Server version:        10.1.12-MariaDB MariaDB Server
Protocol version:      10
Connection:            127.0.0.1 via TCP/IP
Server characterset:    utf8
Db    characterset:    utf8
Client characterset:    utf8
Conn.  characterset:    utf8
TCP port:              3306
Uptime:                1 day 22 hours 14 min 17 sec

Threads: 2  Questions: 68  Slow queries: 0  Opens: 0  Flush tables: 1  Open tables: 11  Queries per second avg: 0.000

不加 127.0.0.1 时,默认连接使用 socket 协议:
[apps@mvxl2972 ~]$ mysql -ulmsapps -pLms166apps
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 35
Server version: 10.1.12-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

MariaDB [(none)]> \s
————–
mysql  Ver 15.1 Distrib 10.1.12-MariaDB, for Linux (x86_64) using readline 5.1

Connection id:          35
Current database:
Current user:          lmsapps@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ”
Using delimiter:        ;
Server:                MariaDB
Server version:        10.1.12-MariaDB MariaDB Server
Protocol version:      10
Connection:            Localhost via UNIX socket
Server characterset:    utf8
Db    characterset:    utf8
Client characterset:    utf8
Conn.  characterset:    utf8
UNIX socket:            /tmp/mysql3306.sock
Uptime:                1 day 22 hours 14 min 54 sec

Threads: 2  Questions: 72  Slow queries: 0  Opens: 0  Flush tables: 1  Open tables: 11  Queries per second avg: 0.000

加 localhost 连接使用 socket 协议:
[apps@mvxl2972 ~]$ mysql -ulmsapps -pLms166apps -h localhost
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 59
Server version: 10.1.12-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

MariaDB [(none)]> \s
————–
mysql  Ver 15.1 Distrib 10.1.12-MariaDB, for Linux (x86_64) using readline 5.1

Connection id:          59
Current database:
Current user:          lmsapps@localhost
SSL:                    Not in use
Current pager:          stdout
Using outfile:          ”
Using delimiter:        ;
Server:                MariaDB
Server version:        10.1.12-MariaDB MariaDB Server
Protocol version:      10
Connection:            Localhost via UNIX socket
Server characterset:    utf8
Db    characterset:    utf8
Client characterset:    utf8
Conn.  characterset:    utf8
UNIX socket:            /tmp/mysql3306.sock
Uptime:                1 day 22 hours 26 min 16 sec

Threads: 4  Questions: 371  Slow queries: 0  Opens: 0  Flush tables: 1  Open tables: 11  Queries per second avg: 0.002

2. 在 skip_name_resolve 为停用状态下:
更改配置文件中 skip_name_resolve 为 off,并重启 mysql.
MariaDB [(none)]> show variables like ‘skip%’;
+—————————+——-+
| Variable_name            | Value |
+—————————+——-+
| skip_external_locking    | ON    |
| skip_name_resolve        | OFF    |
| skip_networking          | OFF  |
| skip_parallel_replication | OFF  |
| skip_replication          | OFF  |
| skip_show_database        | OFF  |

停掉 skip_name_resolve 后,用 127.0.0.1 登入转变成了 localhost,用 lmsapps@’127.0.0.1’ 的密码无法登入:
-bash-4.1$ mysql -ulmsapps -p -h 127.0.0.1 -P 3306
Enter password:
ERROR 1045 (28000): Access denied for user ‘lmsapps’@’localhost’ (using password: YES)

-bash-4.1$ mysql -ulmsapps -p -h 127.0.0.1
Enter password:
ERROR 1045 (28000): Access denied for user ‘lmsapps’@’localhost’ (using password: YES)

而用 lmsapps@’localhost’ 登入才成功,并且使用 TCP/IPt 协议:
-bash-4.1$ mysql -ulmsapps -p -h localhost
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
MariaDB [(none)]>

实验总结:
1. 授权中的 ’%’ 不包含 ’localhost’;
2.skip_name_resolve 开启情况下,127.0.0.1 连接时用
‘lmsapps’@’%’ 帐号使用 TCP/IP 协义,而默认或用 localhost 连接时使用 socket 协议;
3.skip_name_resolve 关闭情况下,127.0.0.1 连接会转换成用
‘lmsapps’@’localhost’ 帐号使用 TCP/IP 协义,而默认或用 localhost 连接时使用 socket 协议;

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-12/138200.htm

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计6214字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中

星哥玩云

星哥玩云
星哥玩云
分享互联网知识
用户数
4
文章数
19351
评论数
4
阅读量
7986295
文章搜索
热门文章
星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛 NAS-6:抖音视频同步工具,视频下载自动下载保存 前言 各位玩 NAS 的朋友好,我是星哥!...
星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛 NAS-3:安装飞牛 NAS 后的很有必要的操作 前言 如果你已经有了飞牛 NAS 系统,之前...
我把用了20年的360安全卫士卸载了

我把用了20年的360安全卫士卸载了

我把用了 20 年的 360 安全卫士卸载了 是的,正如标题你看到的。 原因 偷摸安装自家的软件 莫名其妙安装...
再见zabbix!轻量级自建服务器监控神器在Linux 的完整部署指南

再见zabbix!轻量级自建服务器监控神器在Linux 的完整部署指南

再见 zabbix!轻量级自建服务器监控神器在 Linux 的完整部署指南 在日常运维中,服务器监控是绕不开的...
飞牛NAS中安装Navidrome音乐文件中文标签乱码问题解决、安装FntermX终端

飞牛NAS中安装Navidrome音乐文件中文标签乱码问题解决、安装FntermX终端

飞牛 NAS 中安装 Navidrome 音乐文件中文标签乱码问题解决、安装 FntermX 终端 问题背景 ...
阿里云CDN
阿里云CDN-提高用户访问的响应速度和成功率
随机文章
星哥带你玩飞牛NAS-4:飞牛NAS安装istore旁路由,家庭网络升级的最佳实践

星哥带你玩飞牛NAS-4:飞牛NAS安装istore旁路由,家庭网络升级的最佳实践

星哥带你玩飞牛 NAS-4:飞牛 NAS 安装 istore 旁路由,家庭网络升级的最佳实践 开始 大家好我是...
开源MoneyPrinterTurbo 利用AI大模型,一键生成高清短视频!

开源MoneyPrinterTurbo 利用AI大模型,一键生成高清短视频!

  开源 MoneyPrinterTurbo 利用 AI 大模型,一键生成高清短视频! 在短视频内容...
浏览器自动化工具!开源 AI 浏览器助手让你效率翻倍

浏览器自动化工具!开源 AI 浏览器助手让你效率翻倍

浏览器自动化工具!开源 AI 浏览器助手让你效率翻倍 前言 在 AI 自动化快速发展的当下,浏览器早已不再只是...
多服务器管理神器 Nexterm 横空出世!NAS/Win/Linux 通吃,SSH/VNC/RDP 一站式搞定

多服务器管理神器 Nexterm 横空出世!NAS/Win/Linux 通吃,SSH/VNC/RDP 一站式搞定

多服务器管理神器 Nexterm 横空出世!NAS/Win/Linux 通吃,SSH/VNC/RDP 一站式搞...
飞牛NAS玩转Frpc并且配置,随时随地直连你的私有云

飞牛NAS玩转Frpc并且配置,随时随地直连你的私有云

飞牛 NAS 玩转 Frpc 并且配置,随时随地直连你的私有云 大家好,我是星哥,最近在玩飞牛 NAS。 在数...

免费图片视频管理工具让灵感库告别混乱

一言一句话
-「
手气不错
让微信公众号成为 AI 智能体:从内容沉淀到智能问答的一次升级

让微信公众号成为 AI 智能体:从内容沉淀到智能问答的一次升级

让微信公众号成为 AI 智能体:从内容沉淀到智能问答的一次升级 大家好,我是星哥,之前写了一篇文章 自己手撸一...
星哥带你玩飞牛NAS硬件03:五盘位+N5105+双网口的成品NAS值得入手吗

星哥带你玩飞牛NAS硬件03:五盘位+N5105+双网口的成品NAS值得入手吗

星哥带你玩飞牛 NAS 硬件 03:五盘位 +N5105+ 双网口的成品 NAS 值得入手吗 前言 大家好,我...
300元就能买到的”小钢炮”?惠普7L四盘位小主机解析

300元就能买到的”小钢炮”?惠普7L四盘位小主机解析

  300 元就能买到的 ” 小钢炮 ”?惠普 7L 四盘位小主机解析 最近...
4盘位、4K输出、J3455、遥控,NAS硬件入门性价比之王

4盘位、4K输出、J3455、遥控,NAS硬件入门性价比之王

  4 盘位、4K 输出、J3455、遥控,NAS 硬件入门性价比之王 开篇 在 NAS 市场中,威...
星哥带你玩飞牛 NAS-10:备份微信聊天记录、数据到你的NAS中!

星哥带你玩飞牛 NAS-10:备份微信聊天记录、数据到你的NAS中!

星哥带你玩飞牛 NAS-10:备份微信聊天记录、数据到你的 NAS 中! 大家对「数据安全感」的需求越来越高 ...