MySQL用户中的%到底包不包括localhost?
1 前言
操作MySQL
的时候发现,有时只建了%
的账号,可以通过localhost
连接,有时候却不可以,网上搜索也找不到满意的答案,干脆手动测试一波
推荐学习:《mysql视频教程》
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用户中的百分号%是否包含localhost?
—–文章转载自PHP中文网如有侵权请联系ymkuzhan@126.com删除
转载请注明来源:分析MySQL用户中的百分号%是否包含localhost?
本文永久链接地址:https://www.ymkuzhan.com/46756.html
本文永久链接地址:https://www.ymkuzhan.com/46756.html
下载声明:
本站资源如无特殊说明默认解压密码为www.ymkuzhan.com建议使用WinRAR解压; 本站资源来源于用户分享、互换、购买以及网络收集等渠道,本站不提供任何技术服务及有偿服务,资源仅提供给大家学习研究请勿作它用。 赞助本站仅为维持服务器日常运行并非购买程序及源码费用因此不提供任何技术支持,如果你喜欢该程序,请购买正版! 版权声明:
下载本站资源学习研究的默认同意本站【版权声明】若本站提供的资源侵犯到你的权益,请提交版权证明文件至邮箱ymkuzhan#126.com(将#替换为@)站长将会在三个工作日内为您删除。 免责声明:
您好,本站所有资源(包括但不限于:源码、素材、工具、字体、图像、模板等)均为用户分享、互换、购买以及网络收集而来,并未取得原始权利人授权,因此禁止一切商用行为,仅可用于个人研究学习使用。请务必于下载后24小时内彻底删除,一切因下载人使用所引起的法律相关责任,包括但不限于:侵权,索赔,法律责任,刑事责任等相关责任,全部由下载人/使用人,全部承担。以上说明,一经发布视为您已全部阅读,理解、同意以上内容,如对以上内容持有异议,请勿下载,谢谢配合!支持正版,人人有责,如不慎对您的合法权益构成侵犯,请联系我们对相应内容进行删除,谢谢!