主頁 > 知識庫 > MySQL新建用戶中的%到底包不包括localhost?

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

熱門標(biāo)簽:淄博400電話申請 百度地圖標(biāo)注后不顯示 俄國地圖標(biāo)注app 昆明電信400電話辦理 溫州瑞安400電話怎么申請 南昌高頻外呼系統(tǒng)哪家公司做的好 電話機(jī)器人市場趨勢 電銷機(jī)器人各個(gè)細(xì)節(jié)介紹 電銷機(jī)器人 行業(yè)

正常解釋

%代表任何客戶機(jī)都可以連接
localhost代表只可以本機(jī)連接

一般情況能訪問本地?cái)?shù)據(jù)庫的都是加了權(quán)限了,一般都是禁止別的機(jī)器訪問本地的mysql端口的,如果允許也是要加上指定ip才可以訪問,這樣才能保證數(shù)據(jù)庫不會(huì)被遠(yuǎn)程訪問。

1 前言

操作MySQL的時(shí)候發(fā)現(xiàn),有時(shí)只建了%的賬號,可以通過localhost連接,有時(shí)候卻不可以,網(wǎng)上搜索也找不到滿意的答案,干脆手動(dòng)測試一波

2 兩種連接方法

這里說的兩種連接方法指是執(zhí)行mysql命令時(shí),-h參數(shù)填的是localhost還是IP, 兩種連接方式的區(qū)別如下

-h 參數(shù)為 localhost
當(dāng)-h參數(shù)為localhost的時(shí)候,實(shí)際上是使用socket連接的(默認(rèn)連接方式), 實(shí)例如下

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

-h 參數(shù)為 IP

當(dāng)-h參數(shù)為IP的時(shí)候,實(shí)際上是使用TCP連接的, 實(shí)例如下

[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/IP

3 不同版本的差別

測試方法就是看能不能連接,如果不想看測試過程可以拉到最后看結(jié)論

3.1 MySQL 8.0

創(chuàng)建用戶

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

結(jié)果顯示8.0版本的MySQL, % 包括localhost

3.2 MySQL 5.7

創(chuàng)建 % 用戶

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
...

結(jié)果顯示5.7版本的MySQL, % 包括localhost

3.3 MySQL 5.6

創(chuàng)建用戶

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
......
--------------

結(jié)果顯示MySQL 5.6的%不包括localhost

3.4 MySQL 5.1

創(chuàng)建用戶

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

結(jié)果顯示 5.1 版本的%不包括localhost

3.5 MariaDB 10.3

創(chuàng)建用戶

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

結(jié)果顯示MariaDB 10.3的%不包括localhost

4 結(jié)論

版本 用戶中的%是否包括localhost
MySQL8.0 包括
MySQL5.7 包括
MySQL5.6 不包括
MySQL5.1 不包括
MariaDB 10.3 不包括

好了,這篇文章就介紹到了,希望大家以后多多支持腳本之家。

您可能感興趣的文章:
  • 如何快速修改MySQL用戶的host屬性
  • mysql允許所有host訪問的方法
  • 完美解決MySQL通過localhost無法連接數(shù)據(jù)庫的問題
  • MySQL 可以用localhost 連接,但不能用IP連接的問題解決方法
  • 淺談Mysql連接數(shù)據(jù)庫時(shí)host和user的匹配規(guī)則

標(biāo)簽:吐魯番 安徽 拉薩 葫蘆島 甘南 嘉峪關(guān) 洛陽

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MySQL新建用戶中的%到底包不包括localhost?》,本文關(guān)鍵詞  MySQL,新建,用戶,中的,到底,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《MySQL新建用戶中的%到底包不包括localhost?》相關(guān)的同類信息!
  • 本頁收集關(guān)于MySQL新建用戶中的%到底包不包括localhost?的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章