您好,登錄后才能下訂單哦!
這篇文章主要介紹“如何理解MySQL用戶中的百分號(hào)%是否包含localhost”,在日常操作中,相信很多人在如何理解MySQL用戶中的百分號(hào)%是否包含localhost問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”如何理解MySQL用戶中的百分號(hào)%是否包含localhost”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
操作MySQL
的時(shí)候發(fā)現(xiàn),有時(shí)只建了%
的賬號(hào),可以通過(guò)localhost
連接,有時(shí)候卻不可以,網(wǎng)上搜索也找不到滿意的答案,干脆手動(dòng)測(cè)試一波
這里說(shuō)的兩種連接方法指是執(zhí)行mysql
命令時(shí),-h
參數(shù)填的是localhost
還是IP
, 兩種連接方式的區(qū)別如下
當(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
當(dāng)-h
參數(shù)為IP
的時(shí)候,實(shí)際上是使用TCP
連接的, 實(shí)例如下
[mysql@mysql-test-83 ~]$ /usr/local/mysql57/bin/mysql -utest_user -p -h227.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
測(cè)試方法就是看能不能連接,如果不想看測(cè)試過(guò)程可以拉到最后看結(jié)論
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)
[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 ...
[root@mysql-test-72 ~]# /usr/local/mysql80/bin/mysql -utest_user -p -h227.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
db83-3306>>create user test_user@'%' identified by 'test_user'; Query OK, 0 rows affected (0.00 sec)
[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 ....
[mysql@mysql-test-83 ~]$ /usr/local/mysql57/bin/mysql -utest_user -p -h227.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
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)
[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)
[mysql@mysql-test-83 ~]$ /usr/local/mysql57/bin/mysql -utest_user -p -h227.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
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)
[root@chengqm ~]# mysql -utest_user -p Enter password: ERROR 1045 (28000): Access denied for user 'test_user'@'localhost' (using password: YES)
[root@chengqm ~]# mysql -utest_user -p -h227.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
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)
[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)
[mysql@mysql-test-83 ~]$ /usr/local/mariadb/bin/mysql -utest_user -p -h227.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
版本 | 用戶中的% 是否包括localhost |
---|---|
MySQL8.0 | 包括 |
MySQL5.7 | 包括 |
MySQL5.6 | 不包括 |
MySQL5.1 | 不包括 |
MariaDB 10.3 | 不包括 |
到此,關(guān)于“如何理解MySQL用戶中的百分號(hào)%是否包含localhost”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。