诚之和:如何理解MySQL用户中的百分号%是否包含localhost
这篇文章主要介绍“如何理解MySQL用户中的百分号%是否包含localhost”,在日常操作中,相信很多人在如何理解MySQL用户中的百分号%是否包含localhost问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何理解MySQL用户中的百分号%是否包含localhost”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!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 -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当-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: 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/IP3 不同版本的差别测试方法就是看能不能连接,如果不想看测试过程可以拉到最后看结论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 -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: 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, % 包括localhost3.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 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 -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: 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, % 包括localhost3.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 -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 -h227.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的%不包括localhost3.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 -pEnter password: ERROR 1045 (28000): Access denied for user 'test_user'@'localhost' (using password: YES)使用 IP 登录[root@chengqm ~]# mysql -utest_user -p -h227.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 版本的%不包括localhost3.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 -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 -h227.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的%不包括localhost4 结论版本用户中的%是否包括localhostMySQL8.0包括MySQL5.7包括MySQL5.6不包括MySQL5.1不包括MariaDB 10.3不包括到此,关于“如何理解MySQL用户中的百分号%是否包含localhost”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!