1、部分项目的MySQL连接时候发现,有时只建了%的账号,可以通过localhost连接,有时候连不上。
[root@centos data]# mysql -h 127.0.0.1 -utest1 -p'AAAA###1188'
Warning: Using a password on the command line interface can be insecure.
2、2种连接方法
执行mysql命令时,-h参数填的是localhost 还是IP, 区别如下
当-h参数为localhost的时候,实际上是使用socket连接的(默认连接方式)
[root@centos wwwlogs]# mysql -hlocalhost -utest1 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 239
Server version: 5.6.50-log Source distribution
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> status
--------------
mysql Ver 14.14 Distrib 5.6.50, for Linux (x86_64) using EditLine wrapper
Connection id: 239
Current database:
Current user: shishi_chancai@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.6.50-log Source distribution
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8mb4
Db characterset: utf8mb4
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /tmp/mysql.sock
Uptime: 20 hours 49 min 0 sec*~~
Threads: 2 Questions: 205630 Slow queries: 0 Opens: 208 Flush tables: 1 Open tables: 201 Queries per second avg: 2.743
--------------
mysql> exit
Bye
当-h参数为IP的时候,实际上是使用TCP连接的
[root@centos wwwlogs]# mysql -h127.0.0.1 -utest1 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 240
Server version: 5.6.50-log Source distribution
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> status
--------------
mysql Ver 14.14 Distrib 5.6.50, for Linux (x86_64) using EditLine wrapper
Connection id: 240
Current database:
Current user: shishi_chancai@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.6.50-log Source distribution
Protocol version: 10
Connection: 127.0.0.1 via TCP/IP***
Server characterset: utf8mb4
Db characterset: utf8mb4
Client characterset: utf8
Conn. characterset: utf8
TCP port: 3306
Uptime: 20 hours 49 min 14 sec
Threads: 2 Questions: 205635 Slow queries: 0 Opens: 208 Flush tables: 1 Open tables: 201 Queries per second avg: 2.743
--------------
3、结论
版本 授权用户中的%是否包括localhost
MySQL8.0 包括
MySQL5.7 包括
MySQL5.6 不包括
MySQL5.1 不包括
MariaDB 10.3 不包括
4、备注
MySQL5.6 版本授权中
localhost 包含127.0.0.1 和 localhost
127.0.0.1 包含127.0.0.1 不包含 localhost