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

1 前言

操做MySQL的時候發現,有時只建了%的帳號,能夠經過localhost鏈接,有時候卻不能夠,網上搜索也找不到滿意的答案,乾脆手動測試一波mysql

2 兩種鏈接方法

這裏說的兩種鏈接方法指是執行mysql命令時,-h參數填的是localhost仍是IP, 兩種鏈接方式的區別以下linux

-h 參數爲 localhost

-h參數爲localhost的時候,其實是使用socket鏈接的(默認鏈接方式), 實例以下sql

[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 socketapp

-h 參數爲 IP

-h參數爲IP的時候,其實是使用TCP鏈接的, 實例以下socket

[mysql@mysql-test-83 ~]$ /usr/local/mysql57/bin/mysql -utest_user -p -h127.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/IPide

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 -h127.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, % 包括localhostcode

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 -h127.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, % 包括localhostip

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 -h127.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%不包括localhostit

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 -h127.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 -h127.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 不包括
相關文章
相關標籤/搜索