诚之和:如何理解MySQL用户中的百分号%是否包含localhost
这篇文章主要介绍“如何理解MySQL用户中的百分号%是否包含localhost”,在日常操作中,相信很多人在如何理解MySQL用户中的百分号%是否包含localhost问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何理解MySQL用户中的百分号%是否包含localhost”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
1 前言
mysql下载后为啥localhost打不开操作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: 9Current database:
Current user: test_user@localhost
SSL: Not in useCurrent pager: stdout
Using 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
当-h参数为IP的时候,实际上是使用TCP连接的, 实例如下
[mysql@mysql-test-83 ~]$ /usr/local/mysql57/bin/mysql -utest_user -p -h227.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: ***************.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可以看到用户是********.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 -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&
使用 IP 登录
[root@mysql-test-72 ~]# /usr/local/mysql80/bin/mysql -utest_user -p -h227.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: ***************.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
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: 9Current database:
Current user: test_user@localhost
SSL: Not in useCurrent pager: stdout
Using outfile: ''Using delimiter: ;
Server version: 5.7.21-log MySQL Community Server (GPL)
Protocol version: 10Connection: Localhost via UNIX&
使用 IP 登录
[mysql@mysql-test-83 ~]$ /usr/local/mysql57/bin/mysql -utest_user -p -h227.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: ***************.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:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论