Does the % in the newly created MySQL user include localhost?

Does the % in the newly created MySQL user include localhost?

Normal explanation

% means any client can connect
localhost means that only the local computer can be connected

Generally, those who can access the local database have been given permissions. Generally, other machines are prohibited from accessing the local MySQL port. If it is allowed, a specified IP address must be added to ensure that the database cannot be accessed remotely.

1 Introduction

When operating MySQL, I found that sometimes I only created % of the account and could connect through localhost, but sometimes I couldn't. I couldn't find a satisfactory answer by searching online, so I just tested it manually.

2 Two connection methods

The two connection methods mentioned here refer to whether the -h parameter is filled with localhost or IP when executing the mysql command. The differences between the two connection methods are as follows

-h parameter is localhost
When the -h parameter is localhost, it actually uses a socket connection (the default connection method). The example is as follows

[mysql@mysql-test-83 ~]$ /usr/local/mysql57/bin/mysql -utest_user -p -hlocalhost
Enter password:
========= Omitted ============

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

From the Current user, we can see that the user is xx@localhost, and the connection method is Localhost via UNIX socket

-h parameter is IP

When the -h parameter is IP, it actually uses TCP connection. The example is as follows

[mysql@mysql-test-83 ~]$ /usr/local/mysql57/bin/mysql -utest_user -p -h127.0.0.1
Enter password:
========= Omitted ============

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: [email protected]
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

From Current user, you can see that the user is [email protected], and the connection method is TCP/IP

3 Differences between different versions

The test method is to see if it can be connected. If you don’t want to see the test process, you can scroll to the end to see the conclusion.

3.1 MySQL 8.0

Create User

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)
Login using 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
========= Omitted ============

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

Login using 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
========= Omitted ============

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: [email protected]
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

The result shows MySQL version 8.0, % including localhost

3.2 MySQL 5.7

Create % User

db83-3306>>create user test_user@'%' identified by 'test_user';
Query OK, 0 rows affected (0.00 sec)

Login using localhost

[mysql@mysql-test-83 ~]$ /usr/local/mysql57/bin/mysql -utest_user -p -hlocalhost
========= Omitted ============

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

Login using IP

[mysql@mysql-test-83 ~]$ /usr/local/mysql57/bin/mysql -utest_user -p -h127.0.0.1
Enter password:
========= Omitted ============

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: [email protected]
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
...

The result shows MySQL version 5.7, including localhost

3.3 MySQL 5.6

Create User

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)

Login using 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)

Login using 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)
========= Omitted ============

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: [email protected]
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
......
--------------

The results show that the % of MySQL 5.6 does not include localhost

3.4 MySQL 5.1

Create User

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)

Login using localhost

[root@chengqm ~]# mysql -utest_user -p
Enter password:
ERROR 1045 (28000): Access denied for user 'test_user'@'localhost' (using password: YES)
Login using 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
========= Omitted ============

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: [email protected]
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

The result shows that the 5.1 version of % does not include localhost

3.5 MariaDB 10.3

Create User

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)

Login using 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)

Login using 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
========= Omitted ============

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: [email protected]
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

The results show that the % of MariaDB 10.3 does not include localhost

4 Conclusion

Version Does the % in user include localhost?
MySQL 8.0 include
MySQL 5.7 include
MySQL 5.6 Not included
MySQL 5.1 Not included
MariaDB 10.3 Not included

Well, this article ends here. I hope you will support 123WORDPRESS.COM in the future.

You may also be interested in:
  • How to quickly modify the host attribute of a MySQL user
  • How to allow all hosts to access mysql
  • Perfect solution to the problem that MySQL cannot connect to the database through localhost
  • Solution to the problem that MySQL can connect using localhost but cannot connect using IP
  • A brief discussion on the matching rules of host and user when Mysql connects to the database

<<:  Example of how to configure cross-domain failure repair in nginx

>>:  A brief discussion on using Vue to complete the mobile apk project

Recommend

Use image to submit the form instead of using button to submit the form

Copy code The code is as follows: <form method...

Using loops in awk

Let's learn about different types of loops th...

How to solve the phantom read problem in MySQL

Table of contents Preface 1. What is phantom read...

Detailed analysis of binlog_format mode and configuration in MySQL

There are three main ways of MySQL replication: S...

MySQL trigger definition and usage simple example

This article describes the definition and usage o...

How to automatically delete records before a specified time in Mysql

About Event: MySQL 5.1 began to introduce the con...

Docker custom network implementation

Table of contents 1. Customize the network to rea...

HTML+CSS+JavaScript to create a simple tic-tac-toe game

Table of contents Implementing HTML Add CSS Imple...

Detailed explanation of mixins in Vue.js

Mixins provide distributed reusable functionality...

Detailed explanation of the role of the new operator in Js

Preface Js is the most commonly used code manipul...

How to check PCIe version and speed in Linux

PCIE has four different specifications. Let’s tak...

MySQL query data by hour, fill in 0 if there is no data

Demand background A statistical interface, the fr...