MySQL用户中的%到底包不包括localhost?
1 前言
操作MySQL的时候发现,有时只建了%的账号,可以通过localhost连接,有时候却不可以,网上搜索也找不到满意的答案,干脆手动测试一波
2 两种连接方法
这里说的两种连接方法指是执行mysql命令时,-h参数填的是localhost还是IP, 两种连接方式的区别如下
-h 参数为 localhost
当-h参数为localhost的时候,实际上是使用socket连接的(默认连接方式), 实例如下
[mysql@mysql-test-83 ~]$ /usr/local/mysql57/bin/mysql -utest_user -p -hlocalhost
Enter password:
========= 省略 ===========
mysql> status
/usr/local/mysql57/bin/mysql Ver 14.14 Distrib 5.7.21, for linux-glibc2.12 (x86_64) using EditLine wrapper
Connection id: 9
Current database:
Current user: test_user@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.21-log MySQL Community Server (GPL)
Protocol version: 10
Connection: Localhost via UNIX socket
从Current user可以看到用户是xx@localhost, 连接方式为Localhost via UNIX socket
-h 参数为 IP
当-h参数为IP的时候,实际上是使用TCP连接的, 实例如下
[mysql@mysql-test-83 ~]$ /usr/local/mysql57/bin/mysql -utest_user -p -h127.0.0.1
Enter password:
========= 省略 ===========
mysql> status
--------------
/usr/local/mysql57/bin/mysql Ver 14.14 Distrib 5.7.21, for linux-glibc2.12 (x86_64) using EditLine wrapper
Connection id: 11
Current database:
Current user: test_user@127.0.0.1
SSL: Cipher in use is DHE-RSA-AES256-SHA
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.21-log MySQL Community Server (GPL)
Protocol version: 10
Connection: 127.0.0.1 via TCP/IP
Server characterset: utf8
从Current user可以看到用户是xx@127.0.0.1, 连接方式为TCP/IP
3 不同版本的差别
测试方法就是看能不能连接,如果不想看测试过程可以拉到最后看结论
3.1 MySQL 8.0
创建用户
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.11 |
+-----------+
1 row in set (0.00 sec)
mysql> create user test_user@'%' identified by 'test_user';
Query OK, 0 rows affected (0.07 sec)
使用 localhost 登录
[root@mysql-test-72 ~]# /usr/local/mysql80/bin/mysql -utest_user -p -hlocalhost
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.11 MySQL Community Server - GPL
========= 省略 ===========
mysql> status
--------------
/usr/local/mysql80/bin/mysql Ver 8.0.11 for linux-glibc2.12 on x86_64 (MySQL Community Server - GPL)
Connection id: 9
Current database:
Current user: test_user@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 8.0.11 MySQL Community Server - GPL
Protocol version: 10
Connection: Localhost via UNIX socket
...
使用 IP 登录
[root@mysql-test-72 ~]# /usr/local/mysql80/bin/mysql -utest_user -p -h127.0.0.1
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.11 MySQL Community Server - GPL
========= 省略 ===========
mysql> status
--------------
/usr/local/mysql80/bin/mysql Ver 8.0.11 for linux-glibc2.12 on x86_64 (MySQL Community Server - GPL)
Connection id: 8
Current database:
Current user: test_user@127.0.0.1
SSL: Cipher in use is DHE-RSA-AES128-GCM-SHA256
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 8.0.11 MySQL Community Server - GPL
Protocol version: 10
Connection: 127.0.0.1 via TCP/IP
结果显示8.0版本的MySQL, % 包括localhost
3.2 MySQL 5.7
创建 % 用户
db83-3306>>create user test_user@'%' identified by 'test_user';
Query OK, 0 rows affected (0.00 sec)
使用 localhost 登录
[mysql@mysql-test-83 ~]$ /usr/local/mysql57/bin/mysql -utest_user -p -hlocalhost
========= 省略 ===========
mysql> status
/usr/local/mysql57/bin/mysql Ver 14.14 Distrib 5.7.21, for linux-glibc2.12 (x86_64) using EditLine wrapper
Connection id: 9
Current database:
Current user: test_user@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.21-log MySQL Community Server (GPL)
Protocol version: 10
Connection: Localhost via UNIX socket
....
使用 IP 登录
[mysql@mysql-test-83 ~]$ /usr/local/mysql57/bin/mysql -utest_user -p -h127.0.0.1
Enter password:
========= 省略 ===========
mysql> status
--------------
/usr/local/mysql57/bin/mysql Ver 14.14 Distrib 5.7.21, for linux-glibc2.12 (x86_64) using EditLine wrapper
Connection id: 11
Current database:
Current user: test_user@127.0.0.1
SSL: Cipher in use is DHE-RSA-AES256-SHA
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.21-log MySQL Community Server (GPL)
Protocol version: 10
Connection: 127.0.0.1 via TCP/IP
Server characterset: utf8
...
结果显示5.7版本的MySQL, % 包括localhost
3.3 MySQL 5.6
创建用户
db83-3306>>select version();
+------------+
| version() |
+------------+
| 5.6.10-log |
+------------+
1 row in set (0.00 sec)
db83-3306>>create user test_user@'%' identified by 'test_user';
Query OK, 0 rows affected (0.00 sec)
使用 localhost 登录
[mysql@mysql-test-83 ~]$ /usr/local/mysql57/bin/mysql -utest_user -p -hlocalhost
Enter password:
ERROR 1045 (28000): Access denied for user 'test_user'@'localhost' (using password: YES)
使用 IP 登录
[mysql@mysql-test-83 ~]$ /usr/local/mysql57/bin/mysql -utest_user -p -h127.0.0.1
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.10-log MySQL Community Server (GPL)
========= 省略 ===========
mysql> status
--------------
/usr/local/mysql57/bin/mysql Ver 14.14 Distrib 5.7.21, for linux-glibc2.12 (x86_64) using EditLine wrapper
Connection id: 3
Current database:
Current user: test_user@127.0.0.1
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.6.10-log MySQL Community Server (GPL)
Protocol version: 10
Connection: 127.0.0.1 via TCP/IP
......
--------------
结果显示MySQL 5.6的%不包括localhost
3.4 MySQL 5.1
创建用户
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.1.73 |
+-----------+
1 row in set (0.00 sec)
mysql> create user test_user@'%' identified by 'test_user';
Query OK, 0 rows affected (0.00 sec)
使用 localhost 登录
[root@chengqm ~]# mysql -utest_user -p
Enter password:
ERROR 1045 (28000): Access denied for user 'test_user'@'localhost' (using password: YES)
使用 IP 登录
[root@chengqm ~]# mysql -utest_user -p -h127.0.0.1
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4901339
Server version: 5.1.73 Source distribution
========= 省略 ===========
mysql> status
--------------
mysql Ver 14.14 Distrib 5.1.73, for redhat-linux-gnu (x86_64) using readline 5.1
Connection id: 4901339
Current database:
Current user: test_user@127.0.0.1
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.1.73 Source distribution
Protocol version: 10
Connection: 127.0.0.1 via TCP/IP
结果显示 5.1 版本的%不包括localhost
3.5 MariaDB 10.3
创建用户
db83-3306>>select version();
+---------------------+
| version() |
+---------------------+
| 10.3.11-MariaDB-log |
+---------------------+
1 row in set (0.000 sec)
db83-3306>>create user test_user@'%' identified by 'test_user';
Query OK, 0 rows affected (0.001 sec)
使用 localhost 登录
[mysql@mysql-test-83 ~]$ /usr/local/mariadb/bin/mysql -utest_user -p -hlocalhost
Enter password:
ERROR 1045 (28000): Access denied for user 'test_user'@'localhost' (using password: YES)
使用 IP 登录
[mysql@mysql-test-83 ~]$ /usr/local/mariadb/bin/mysql -utest_user -p -h127.0.0.1
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 10.3.11-MariaDB-log MariaDB Server
========= 省略 ===========
MariaDB [(none)]> status
--------------
/usr/local/mariadb/bin/mysql Ver 15.1 Distrib 10.3.11-MariaDB, for Linux (x86_64) using readline 5.1
Connection id: 12
Current database:
Current user: test_user@127.0.0.1
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server: MariaDB
Server version: 10.3.11-MariaDB-log MariaDB Server
Protocol version: 10
Connection: 127.0.0.1 via TCP/IP
结果显示MariaDB 10.3的%不包括localhost
4 结论
| 版本 | 用户中的%是否包括localhost |
|---|---|
| MySQL8.0 | 包括 |
| MySQL5.7 | 包括 |
| MySQL5.6 | 不包括 |
| MySQL5.1 | 不包括 |
| MariaDB 10.3 | 不包括 |
MySQL用户中的%到底包不包括localhost?的更多相关文章
- MySQL用户与权限管理
执行mysql select 查询报错: SELECT command denied to user 'root'@'localhost' for table "xxx" 问题原因 ...
- 创建MySQL用户 赋予某指定库表的权限 flush privileges才能生效!!!!;@'localhost'授权本地,@'%'授权远程
update ERROR 1364 (HY000): Field 'ssl_cipher' doesn't have a default value 建议使用GRANT语句进行授权,语句如下: gra ...
- MySQL用户授权【转】
MySQL 赋予用户权限命令的简单格式可概括为:grant 权限 on 数据库对象 to 用户 一.grant 普通数据用户,查询.插入.更新.删除 数据库中所有表数据的权利. grant selec ...
- 创建MySQL用户 赋予某指定库表的权限
摘自: http://renxiangzyq.iteye.com/blog/763837 update ERROR 1364 (HY000): Field 'ssl_cipher' doesn't h ...
- html页面通过http访问mysql数据库中的内容,实现用户登录的功能
需求: 通过html编写用户登录页面,页面内容包括用户名.密码和登录按钮,点击登录后访问login.php文件,使用按钮默认的submit提交用户名和密码,在login.php中访问mysql数据库, ...
- linux中重置服务器的mysql用户密码
本文章前提条件是自己经把mysql登录密码给忘记了,这个时间我们解决方法有很多,重新安装mysql数据库一切重来,另一种是通过下面文章重新设置root密码,下面我们一起来看看方法二吧. 最 近 ...
- 查看MYSQL数据库中所有用户及拥有权限
查看MYSQL数据库中所有用户 mysql> SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM m ...
- 在mysql数据库中创建oracle scott用户的四个表及插入初始化数据
在mysql数据库中创建oracle scott用户的四个表及插入初始化数据 /* 功能:创建 scott 数据库中的 dept 表 */ create table dept( deptno int ...
- 在mysql数据库中创建Oracle数据库中的scott用户表
在mysql数据库中创建Oracle数据库中的scott用户表 作者:Eric 微信:loveoracle11g create table DEPT ( DEPTNO int(2) not null, ...
随机推荐
- k8s+crio+podman搭建集群
前言 在传统的k8s集群中,我们都是使用docker engine做为底层的容器管理软件的,而docker engine因为不是k8s亲生的解决方案,所以实际使用中会有更多的分层.之前我们也讲过,k8 ...
- 无用的IP黑名单
无效的IP黑名单,有些还没有收集,在阿里云或者腾讯云的安全组里面设置,拦截不必要的IP,免得遭到攻击,也避免的CPU和内存过高 来源 备注82.102.21.217 拒绝 随机访问目录攻击,频繁69. ...
- Solution -「最大权闭合子图」做题随笔
T1 小 M 的作物 先从简化题目入手,考虑先去掉 \(c\) 的额外收益.然后尝试将所有作物种在 \(B\), 则目前得到了 \(\sum \limits_{i = 1} ^n b_i\) 的收益. ...
- isNotBlank()方法和isNotEmpty()方法的区别
- Splash (渲染JS服务)介绍安装
一. splash介绍 1.Splash 是一个带有 HTTP API 的 javascript 渲染服务.它是一个带有 HTTP API 的轻量级浏览器,使用 Twisted 和 QT5 在 Pyt ...
- CS内网横向移动 模拟渗透实操 超详细
@Webkio 前言: 最近在跟朋友搞一些项目玩,所以最近没怎么更新内容接下来我将在虚拟中模拟内网多层靶场,进行内网渗透实验,超详细,适合小白.本文仅供网安学习,不可利用于未授权渗透工作中,否则后果自 ...
- MySQL客户端工具的使用与MySQL SQL语句
MySQL客户端工具的使用 1.MySQL程序的组成 客户端 mysql:CLI交互式客户端程序 mycli:CLI交互式客户端程序;使用sql语句时会有提示信息 mysql_secure_insta ...
- Docker在手,天下我有,在Win10系统下利用Docker部署Gunicorn+Flask打造独立镜像
原文转载自「刘悦的技术博客」https://v3u.cn/a_id_164 书接上回,之前一篇:Win10环境下使用Flask配合Celery异步推送实时/定时消息(Socket.io)/2020年最 ...
- Odoo14 Error: While parsing modifiers for page: for modifier "invisible": fieldValue.indexOf is not a function
1 Traceback: 2 Error: While parsing modifiers for page: for modifier "invisible": fieldVal ...
- google nexus5x 刷机抓包逆向环境配置(二)
本文仅供学习交流使用,如侵立删! google nexus5x 刷入永久TWEP和Magisk(面具root) 操作环境 nexus5x kaliLinux win10 准备 下载链接:https:/ ...