资讯专栏INFORMATION COLUMN

MySQL用户中的%到底包不包括localhost?

greatwhole / 2837人阅读

摘要:前言操作的时候发现,有时只建了的账号,可以通过连接,有时候却不可以,网上搜索也找不到满意的答案,干脆手动测试一波两种连接方法这里说的两种连接方法指是执行命令时,参数填的是还是两种连接方式的区别如下参数为当参数为的时候,实际上是使用连接

1 前言

操作MySQL的时候发现,有时只建了%的账号,可以通过localhost连接,有时候却不可以,网上搜索也找不到满意的答案,干脆手动测试一波

2 两种连接方法

这里说的两种连接方法指是执行mysql命令时,-h参数填的是localhost还是IP, 两种连接方式的区别如下

-h 参数为 localhost

-h参数为localhost的时候,实际上是使用socket连接的(默认连接方式), 实例如下

</>复制代码

  1. [mysql@mysql-test-83 ~]$ /usr/local/mysql57/bin/mysql -utest_user -p -hlocalhost
  2. Enter password:
  3. ========= 省略 ===========
  4. mysql> status
  5. /usr/local/mysql57/bin/mysql Ver 14.14 Distrib 5.7.21, for linux-glibc2.12 (x86_64) using EditLine wrapper
  6. Connection id: 9
  7. Current database:
  8. Current user: test_user@localhost
  9. SSL: Not in use
  10. Current pager: stdout
  11. Using outfile: ""
  12. Using delimiter: ;
  13. Server version: 5.7.21-log MySQL Community Server (GPL)
  14. Protocol version: 10
  15. Connection: Localhost via UNIX socket

Current user可以看到用户是xx@localhost, 连接方式为Localhost via UNIX socket

-h 参数为 IP

-h参数为IP的时候,实际上是使用TCP连接的, 实例如下

</>复制代码

  1. [mysql@mysql-test-83 ~]$ /usr/local/mysql57/bin/mysql -utest_user -p -h127.0.0.1
  2. Enter password:
  3. ========= 省略 ===========
  4. mysql> status
  5. --------------
  6. /usr/local/mysql57/bin/mysql Ver 14.14 Distrib 5.7.21, for linux-glibc2.12 (x86_64) using EditLine wrapper
  7. Connection id: 11
  8. Current database:
  9. Current user: test_user@127.0.0.1
  10. SSL: Cipher in use is DHE-RSA-AES256-SHA
  11. Current pager: stdout
  12. Using outfile: ""
  13. Using delimiter: ;
  14. Server version: 5.7.21-log MySQL Community Server (GPL)
  15. Protocol version: 10
  16. Connection: 127.0.0.1 via TCP/IP
  17. Server characterset: utf8

Current user可以看到用户是xx@127.0.0.1, 连接方式为TCP/IP

3 不同版本的差别

测试方法就是看能不能连接,如果不想看测试过程可以拉到最后看结论

3.1 MySQL 8.0 创建用户

</>复制代码

  1. mysql> select version();
  2. +-----------+
  3. | version() |
  4. +-----------+
  5. | 8.0.11 |
  6. +-----------+
  7. 1 row in set (0.00 sec)
  8. mysql> create user test_user@"%" identified by "test_user";
  9. Query OK, 0 rows affected (0.07 sec)
使用 localhost 登录

</>复制代码

  1. [root@mysql-test-72 ~]# /usr/local/mysql80/bin/mysql -utest_user -p -hlocalhost
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or g.
  4. Your MySQL connection id is 9
  5. Server version: 8.0.11 MySQL Community Server - GPL
  6. ========= 省略 ===========
  7. mysql> status
  8. --------------
  9. /usr/local/mysql80/bin/mysql Ver 8.0.11 for linux-glibc2.12 on x86_64 (MySQL Community Server - GPL)
  10. Connection id: 9
  11. Current database:
  12. Current user: test_user@localhost
  13. SSL: Not in use
  14. Current pager: stdout
  15. Using outfile: ""
  16. Using delimiter: ;
  17. Server version: 8.0.11 MySQL Community Server - GPL
  18. Protocol version: 10
  19. Connection: Localhost via UNIX socket
  20. ...
使用 IP 登录

</>复制代码

  1. [root@mysql-test-72 ~]# /usr/local/mysql80/bin/mysql -utest_user -p -h127.0.0.1
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or g.
  4. Your MySQL connection id is 8
  5. Server version: 8.0.11 MySQL Community Server - GPL
  6. ========= 省略 ===========
  7. mysql> status
  8. --------------
  9. /usr/local/mysql80/bin/mysql Ver 8.0.11 for linux-glibc2.12 on x86_64 (MySQL Community Server - GPL)
  10. Connection id: 8
  11. Current database:
  12. Current user: test_user@127.0.0.1
  13. SSL: Cipher in use is DHE-RSA-AES128-GCM-SHA256
  14. Current pager: stdout
  15. Using outfile: ""
  16. Using delimiter: ;
  17. Server version: 8.0.11 MySQL Community Server - GPL
  18. Protocol version: 10
  19. Connection: 127.0.0.1 via TCP/IP

结果显示8.0版本的MySQL, % 包括localhost

3.2 MySQL 5.7 创建 % 用户

</>复制代码

  1. db83-3306>>create user test_user@"%" identified by "test_user";
  2. Query OK, 0 rows affected (0.00 sec)
使用 localhost 登录

</>复制代码

  1. [mysql@mysql-test-83 ~]$ /usr/local/mysql57/bin/mysql -utest_user -p -hlocalhost
  2. ========= 省略 ===========
  3. mysql> status
  4. /usr/local/mysql57/bin/mysql Ver 14.14 Distrib 5.7.21, for linux-glibc2.12 (x86_64) using EditLine wrapper
  5. Connection id: 9
  6. Current database:
  7. Current user: test_user@localhost
  8. SSL: Not in use
  9. Current pager: stdout
  10. Using outfile: ""
  11. Using delimiter: ;
  12. Server version: 5.7.21-log MySQL Community Server (GPL)
  13. Protocol version: 10
  14. Connection: Localhost via UNIX socket
  15. ....
使用 IP 登录

</>复制代码

  1. [mysql@mysql-test-83 ~]$ /usr/local/mysql57/bin/mysql -utest_user -p -h127.0.0.1
  2. Enter password:
  3. ========= 省略 ===========
  4. mysql> status
  5. --------------
  6. /usr/local/mysql57/bin/mysql Ver 14.14 Distrib 5.7.21, for linux-glibc2.12 (x86_64) using EditLine wrapper
  7. Connection id: 11
  8. Current database:
  9. Current user: test_user@127.0.0.1
  10. SSL: Cipher in use is DHE-RSA-AES256-SHA
  11. Current pager: stdout
  12. Using outfile: ""
  13. Using delimiter: ;
  14. Server version: 5.7.21-log MySQL Community Server (GPL)
  15. Protocol version: 10
  16. Connection: 127.0.0.1 via TCP/IP
  17. Server characterset: utf8
  18. ...

结果显示5.7版本的MySQL, % 包括localhost

3.3 MySQL 5.6 创建用户

</>复制代码

  1. db83-3306>>select version();
  2. +------------+
  3. | version() |
  4. +------------+
  5. | 5.6.10-log |
  6. +------------+
  7. 1 row in set (0.00 sec)
  8. db83-3306>>create user test_user@"%" identified by "test_user";
  9. Query OK, 0 rows affected (0.00 sec)
使用 localhost 登录

</>复制代码

  1. [mysql@mysql-test-83 ~]$ /usr/local/mysql57/bin/mysql -utest_user -p -hlocalhost
  2. Enter password:
  3. ERROR 1045 (28000): Access denied for user "test_user"@"localhost" (using password: YES)
使用 IP 登录

</>复制代码

  1. [mysql@mysql-test-83 ~]$ /usr/local/mysql57/bin/mysql -utest_user -p -h127.0.0.1
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or g.
  4. Your MySQL connection id is 3
  5. Server version: 5.6.10-log MySQL Community Server (GPL)
  6. ========= 省略 ===========
  7. mysql> status
  8. --------------
  9. /usr/local/mysql57/bin/mysql Ver 14.14 Distrib 5.7.21, for linux-glibc2.12 (x86_64) using EditLine wrapper
  10. Connection id: 3
  11. Current database:
  12. Current user: test_user@127.0.0.1
  13. SSL: Not in use
  14. Current pager: stdout
  15. Using outfile: ""
  16. Using delimiter: ;
  17. Server version: 5.6.10-log MySQL Community Server (GPL)
  18. Protocol version: 10
  19. Connection: 127.0.0.1 via TCP/IP
  20. ......
  21. --------------

结果显示MySQL 5.6%不包括localhost

3.4 MySQL 5.1 创建用户

</>复制代码

  1. mysql> select version();
  2. +-----------+
  3. | version() |
  4. +-----------+
  5. | 5.1.73 |
  6. +-----------+
  7. 1 row in set (0.00 sec)
  8. mysql> create user test_user@"%" identified by "test_user";
  9. Query OK, 0 rows affected (0.00 sec)
使用 localhost 登录

</>复制代码

  1. [root@chengqm ~]# mysql -utest_user -p
  2. Enter password:
  3. ERROR 1045 (28000): Access denied for user "test_user"@"localhost" (using password: YES)
使用 IP 登录

</>复制代码

  1. [root@chengqm ~]# mysql -utest_user -p -h127.0.0.1
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or g.
  4. Your MySQL connection id is 4901339
  5. Server version: 5.1.73 Source distribution
  6. ========= 省略 ===========
  7. mysql> status
  8. --------------
  9. mysql Ver 14.14 Distrib 5.1.73, for redhat-linux-gnu (x86_64) using readline 5.1
  10. Connection id: 4901339
  11. Current database:
  12. Current user: test_user@127.0.0.1
  13. SSL: Not in use
  14. Current pager: stdout
  15. Using outfile: ""
  16. Using delimiter: ;
  17. Server version: 5.1.73 Source distribution
  18. Protocol version: 10
  19. Connection: 127.0.0.1 via TCP/IP

结果显示 5.1 版本的%不包括localhost

3.5 MariaDB 10.3 创建用户

</>复制代码

  1. db83-3306>>select version();
  2. +---------------------+
  3. | version() |
  4. +---------------------+
  5. | 10.3.11-MariaDB-log |
  6. +---------------------+
  7. 1 row in set (0.000 sec)
  8. db83-3306>>create user test_user@"%" identified by "test_user";
  9. Query OK, 0 rows affected (0.001 sec)
使用 localhost 登录

</>复制代码

  1. [mysql@mysql-test-83 ~]$ /usr/local/mariadb/bin/mysql -utest_user -p -hlocalhost
  2. Enter password:
  3. ERROR 1045 (28000): Access denied for user "test_user"@"localhost" (using password: YES)
使用 IP 登录

</>复制代码

  1. [mysql@mysql-test-83 ~]$ /usr/local/mariadb/bin/mysql -utest_user -p -h127.0.0.1
  2. Enter password:
  3. Welcome to the MariaDB monitor. Commands end with ; or g.
  4. Your MariaDB connection id is 12
  5. Server version: 10.3.11-MariaDB-log MariaDB Server
  6. ========= 省略 ===========
  7. MariaDB [(none)]> status
  8. --------------
  9. /usr/local/mariadb/bin/mysql Ver 15.1 Distrib 10.3.11-MariaDB, for Linux (x86_64) using readline 5.1
  10. Connection id: 12
  11. Current database:
  12. Current user: test_user@127.0.0.1
  13. SSL: Not in use
  14. Current pager: stdout
  15. Using outfile: ""
  16. Using delimiter: ;
  17. Server: MariaDB
  18. Server version: 10.3.11-MariaDB-log MariaDB Server
  19. Protocol version: 10
  20. 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 不包括

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/17860.html

相关文章

  • 如何用ACM简化你的Spring Cloud微服务环境配置管理

    摘要:摘要本文我们就如何使用阿里云这样的配置管理产品在中替代帮助简化环境配置管理做一个简单的示例,帮助你理解基于来简化微服务环境配置管理的方案,并会简单比较一下与方案的优劣。 摘要: 本文我们就如何使用阿里云ACM这样的配置管理产品在Spring Cloud中替代Spring Cloud Config帮助简化环境配置管理做一个简单的示例,帮助你理解基于ACM来简化微服务环境配置管理的方案,并...

    ?xiaoxiao, 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<