1 머리말
MySQL
을 운영할 때 가끔 %
에 해당하는 계정만 생성되는 것을 발견했습니다. localhost
연결이 안되는 경우가 있는데, 온라인 검색을 통해서도 만족스러운 답변을 찾을 수 없어서 그냥 수동으로 시도해봅니다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 -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 参数为 IP
当-h
参数为IP
的时候,实际上是使用TCP
连接的, 实例如下
[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 不同版本的差别
测试方法就是看能不能连接,如果不想看测试过程可以拉到最后看结论
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
, %
包括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 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
, %
包括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 -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
的%
不包括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 -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
| 추천 학습: "mysql 동영상 튜토리얼》||||
---|---|---|---|---|---|
여기서 언급한 두 가지 연결 방법은 mysql 실행을 의미합니다. 명령에서 -h 매개변수는 localhost 또는 IP 로 채워집니다. 두 연결 방법의 차이점은 다음과 같습니다 |
-h 매개변수가 localhost 인 경우 실제로는 소켓 을 사용하여 연결됩니다(기본값) 연결 방법), 예시 아래와 같이 | rrreee||||
-h 매개변수가 IP 인 경우 실제로는 TCP를 사용합니다. 로 연결하는 예시는 다음과 같습니다 | rrreee
현재 사용자 를 보면 해당 사용자가 xx@127.0.0.1 이고, 연결 방법을 알 수 있습니다. is TCP/IP
|
||||
테스트 방법은 연결이 가능한지 확인하는 것입니다. 테스트 과정을 보고 싶지 않으시면 스크롤을 끝까지 내리시면 됩니다. 결론을 참조하세요 | 결과는 localhost code>3.2 MySQL 5.7을 포함한 결과가 표시됩니다5.7 |
||||
결과에 따르면 % 의 5.1 버전에는 localhost
|
결과에 따르면 MariaDB 10.3의 % 는 그렇지 않습니다. include localhost4 결론 |
version | 사용자의 % 에 localhost
|
---|---|
MySQL8.0 |
MySQL5.7🎜🎜포함🎜🎜🎜🎜MySQL5.6🎜🎜🎜포함되지 않음🎜🎜 🎜 🎜🎜MySQL5.1🎜🎜 🎜 포함되지 않음🎜🎜🎜🎜🎜MariaDB 10.3🎜🎜🎜포함되지 않음🎜🎜🎜🎜🎜🎜🎜