欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-11 07:52:57
正常解释 %代表任何客户机都可以连接 localhost代表只可以本机连接 一般情况能访问本地数据库的都是加了权限了,一般都是禁止别的机器访问本地的mysql...

正常解释

%代表任何客户机都可以连接
localhost代表只可以本机连接

一般情况能访问本地数据库的都是加了权限了,一般都是禁止别的机器访问本地的mysql端口的,如果允许也是要加上指定ip才可以访问,这样才能保证数据库不会被远程访问。

1 前言

操作mysql的时候发现,有时只建了%的账号,可以通过localhost连接,有时候却不可以,网上搜索也找不到满意的答案,干脆手动测试一波

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
mysql8.0 包括
mysql5.7 包括
mysql5.6 不包括
mysql5.1 不包括
mariadb 10.3 不包括

好了,这篇文章就介绍到了,希望大家以后多多支持。