MysqL用户中的%到底包不包括localhost?
1 前言 *** 作MySQL
的时候发现,有时只建了%
的账号,可以通过localhost
连接,有时候却不可以,网上搜索也找不到满意的答案,干脆手动测试一波
推荐学习:《mysql视频教程》
这里说的两种连接方法指是执行MysqL
命令时,-h
参数填的是localhost
还是IP
, 两种连接方式的区别如下
当-h
参数为localhost
的时候,实际上是使用socket
连接的(默认连接方式), 实例如下
[MysqL@MysqL-test-83 ~]$ /usr/local/MysqL57/bin/MysqL -utest_user -p -hlocalhostEnter password: ========= 省略 ===========MysqL> status/usr/local/MysqL57/bin/MysqL Ver 14.14 distrib 5.7.21, for linux-glibc2.12 (x86_64) using Editline wrapperConnection ID: 9Current database: Current user: test_user@localhostSSL: Not in useCurrent pager: stdoutUsing outfile: ''Using delimiter: ;Server version: 5.7.21-log MysqL Community Server (GPL)Protocol version: 10Connection: Localhost via UNIX socket
从Current user
可以看到用户是xx@localhost
, 连接方式为Localhost via UNIX socket
当-h
参数为IP
的时候,实际上是使用TCP
连接的, 实例如下
[MysqL@MysqL-test-83 ~]$ /usr/local/MysqL57/bin/MysqL -utest_user -p -h127.0.0.1Enter password: ========= 省略 ===========MysqL> status--------------/usr/local/MysqL57/bin/MysqL Ver 14.14 distrib 5.7.21, for linux-glibc2.12 (x86_64) using Editline wrapperConnection ID: 11Current database: Current user: test_user@127.0.0.1SSL: Cipher in use is DHE-RSA-AES256-SHACurrent pager: stdoutUsing outfile: ''Using delimiter: ;Server version: 5.7.21-log MysqL Community Server (GPL)Protocol version: 10Connection: 127.0.0.1 via TCP/IPServer characterset: utf8
从Current user
可以看到用户是xx@127.0.0.1
, 连接方式为TCP/IP
测试方法就是看能不能连接,如果不想看测试过程可以拉到最后看结论
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 -hlocalhostEnter password: Welcome to the MysqL monitor. Commands end with ; or \g.Your MysqL connection ID is 9Server 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: 9Current database: Current user: test_user@localhostSSL: Not in useCurrent pager: stdoutUsing outfile: ''Using delimiter: ;Server version: 8.0.11 MysqL Community Server - GPLProtocol version: 10Connection: Localhost via UNIX socket...使用 IP 登录
[root@MysqL-test-72 ~]# /usr/local/MysqL80/bin/MysqL -utest_user -p -h127.0.0.1Enter password: Welcome to the MysqL monitor. Commands end with ; or \g.Your MysqL connection ID is 8Server 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: 8Current database: Current user: test_user@127.0.0.1SSL: Cipher in use is DHE-RSA-AES128-GCM-SHA256Current pager: stdoutUsing outfile: ''Using delimiter: ;Server version: 8.0.11 MysqL Community Server - GPLProtocol version: 10Connection: 127.0.0.1 via TCP/IP
结果显示8.0
版本的MysqL
, %
包括localhost
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 wrapperConnection ID: 9Current database: Current user: test_user@localhostSSL: Not in useCurrent pager: stdoutUsing outfile: ''Using delimiter: ;Server version: 5.7.21-log MysqL Community Server (GPL)Protocol version: 10Connection: Localhost via UNIX socket....使用 IP 登录
[MysqL@MysqL-test-83 ~]$ /usr/local/MysqL57/bin/MysqL -utest_user -p -h127.0.0.1Enter password: ========= 省略 ===========MysqL> status--------------/usr/local/MysqL57/bin/MysqL Ver 14.14 distrib 5.7.21, for linux-glibc2.12 (x86_64) using Editline wrapperConnection ID: 11Current database: Current user: test_user@127.0.0.1SSL: Cipher in use is DHE-RSA-AES256-SHACurrent pager: stdoutUsing outfile: ''Using delimiter: ;Server version: 5.7.21-log MysqL Community Server (GPL)Protocol version: 10Connection: 127.0.0.1 via TCP/IPServer characterset: utf8...
结果显示5.7
版本的MysqL
, %
包括localhost
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 -hlocalhostEnter 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.1Enter password: Welcome to the MysqL monitor. Commands end with ; or \g.Your MysqL connection ID is 3Server 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 wrapperConnection ID: 3Current database: Current user: test_user@127.0.0.1SSL: Not in useCurrent pager: stdoutUsing outfile: ''Using delimiter: ;Server version: 5.6.10-log MysqL Community Server (GPL)Protocol version: 10Connection: 127.0.0.1 via TCP/IP......--------------
结果显示MysqL 5.6
的%
不包括localhost
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 -pEnter password: ERROR 1045 (28000): Access denIEd for user 'test_user'@'localhost' (using password: YES)使用 IP 登录
[root@chengqm ~]# MysqL -utest_user -p -h127.0.0.1Enter password: Welcome to the MysqL monitor. Commands end with ; or \g.Your MysqL connection ID is 4901339Server 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.1Connection ID: 4901339Current database: Current user: test_user@127.0.0.1SSL: Not in useCurrent pager: stdoutUsing outfile: ''Using delimiter: ;Server version: 5.1.73 Source distributionProtocol version: 10Connection: 127.0.0.1 via TCP/IP
结果显示 5.1
版本的%
不包括localhost
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 -hlocalhostEnter 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.1Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g.Your MariaDB connection ID is 12Server 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.1Connection ID: 12Current database: Current user: test_user@127.0.0.1SSL: Not in useCurrent pager: stdoutUsing outfile: ''Using delimiter: ;Server: MariaDBServer version: 10.3.11-MariaDB-log MariaDB ServerProtocol version: 10Connection: 127.0.0.1 via TCP/IP
结果显示MariaDB 10.3
的%
不包括localhost
版本 | 用户中的% 是否包括localhost |
---|---|
MysqL8.0 | 包括 |
MysqL5.7 | 包括 |
MysqL5.6 | 不包括 |
MysqL5.1 | 不包括 |
MariaDB 10.3 | 不包括 |
总结
以上是内存溢出为你收集整理的分析MySQL用户中的百分号%是否包含localhost?全部内容,希望文章能够帮你解决分析MySQL用户中的百分号%是否包含localhost?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)