您好,欢迎访问一九零五行业门户网

分析MySQL用户中的百分号%是否包含localhost?

mysql用户中的%到底包不包括localhost?
1 前言操作mysql的时候发现,有时只建了%的账号,可以通过localhost连接,有时候却不可以,网上搜索也找不到满意的答案,干脆手动测试一波
推荐学习:《mysql视频教程》
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 -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 不同版本的差别测试方法就是看能不能连接,如果不想看测试过程可以拉到最后看结论
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
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 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
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 -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
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 -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
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 -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
4 结论版本用户中的%是否包括localhost
mysql8.0 包括
mysql5.7 包括
mysql5.6 不包括
mysql5.1 不包括
mariadb 10.3 不包括
以上就是分析mysql用户中的百分号%是否包含localhost?的详细内容。
其它类似信息

推荐信息