RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
MySQL新建用户中%包不包括localhost吗-创新互联

这篇文章主要介绍了MySQL新建用户中%包不包括localhost吗,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

创新互联从2013年创立,先为绥江等服务建站,绥江等地企业,进行企业商务咨询服务。为绥江企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

正常解释

%代表任何客户机都可以连接
localhost代表只可以本机连接

一般情况能访问本地数据库的都是加了权限了,一般都是禁止别的机器访问本地的mysql端口的,如果允许也是要加上指定ip才可以访问,这样才能保证数据库不会被远程访问。

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 -h227.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 -h227.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 -h227.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 -h227.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 -h227.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 -h227.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吗”这篇文章对大家有帮助,同时也希望大家多多支持创新互联成都网站设计公司,关注创新互联成都网站设计公司行业资讯频道,更多相关知识等着你来学习!

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、网站设计器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


新闻名称:MySQL新建用户中%包不包括localhost吗-创新互联
文章位置:http://lswzjz.com/article/hiesd.html