Mysql 本地主机!= 127.0.0.1?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19712307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 19:17:40  来源:igfitidea点击:

Mysql localhost != 127.0.0.1?

mysql

提问by Bunyk

$ mysql -u root -h 127.0.0.1 -e 'show tables' created_from_host;
+-----------------------------+
| Tables_in_created_from_host |
+-----------------------------+
| test                        |
+-----------------------------+

$ mysql -u root -h localhost -e 'show tables' created_from_host;
ERROR 1049 (42000): Unknown database 'created_from_host'

$ cat /etc/hosts
127.0.0.1       localhost.localdomain localhost
127.0.0.1       localhost
::1     localhost6.localdomain6 localhost6

How could it be? And main question - how to grant ALL privileges on ALL databases from ALL hosts for root?

怎么会这样?主要问题 -如何从所有主机为 root 授予对所有数据库的所有权限?

UPD:

更新:

$ mysql -u root -h 127.0.0.1 -pzenoss -e "show grants";
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost                                                                                                              |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*3715D7F2B0C1D26D72357829DF94B81731174B8C' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION                                                                           |
+----------------------------------------------------------------------------------------------------------------------------------------+
$ mysql -u root -h localhost -pzenoss -e "show grants";
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost                                                                                                              |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*3715D7F2B0C1D26D72357829DF94B81731174B8C' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION                                                                           |
+----------------------------------------------------------------------------------------------------------------------------------------+

UPD2:

UPD2:

zends> SHOW GLOBAL VARIABLES LIKE 'skip_networking';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| skip_networking | OFF   |
+-----------------+-------+
1 row in set (0.00 sec)

zends> SELECT user,host FROM mysql.user WHERE user='root'; 
+------+-----------------------+
| user | host                  |
+------+-----------------------+
| root | 127.0.0.1             |
| root | ::1                   |
| root | localhost             |
| root | localhost.localdomain |
+------+-----------------------+
4 rows in set (0.00 sec)

回答by glglgl

As you can see here, a UNIX mysqld uses sockets if used without a host name or with the host name localhost.

正如你在这里看到的,一个 UNIX mysqld 使用套接字,如果没有主机名或主机名使用localhost

So it makes a difference, and in the GRANT system this difference becomes evident.

所以它会有所不同,而在 GRANT 系统中,这种差异变得明显。

回答by Ingo Ratsdorf

I know this tread is old, however was most probably not correctly answered.

我知道这个胎面很旧,但是很可能没有正确回答。

By default, mysql does a name resolve so 127.0.0.1 and localhost will resolve in the same. However, you can switch the name resolution off in my.cnf:

默认情况下,mysql 会解析名称,因此 127.0.0.1 和 localhost 将解析相同。但是,您可以在 my.cnf 中关闭名称解析:

skip-name-resolve = 1

跳过名称解析 = 1

Then localhost and 127.0.0.1 will NOTbe the same anymore. So you either keep the name resolution, or restrict yourself to only using localhost in your grants or only 127.0.0.1 BUT: If you do the latter one, you will have to access our database with those credentials as well.

然后本地主机,127.0.0.1会相同了。因此,您要么保留名称解析,要么限制自己仅在授权中使用 localhost 或仅使用 127.0.0.1 但是:如果您使用后一种方式,您也必须使用这些凭据访问我们的数据库。

回答by Matt Dodge

Log in through 127.0.0.1 and execute this statement:

通过 127.0.0.1 登录并执行此语句:

SHOW GRANTS

You will probably see something like

你可能会看到类似的东西

GRANT ALL ... 'root'@'127.0.0.1'

I just confirmed on my local install and it seems MySQL won't auto-resolve the host name. You can either add another grant for localhostor just use 127.0.0.1

我刚刚确认了我的本地安装,似乎 MySQL 不会自动解析主机名。您可以为localhost127.0.0.1添加另一个授权或仅使用

回答by Nitin Nain

This caused issues in setting up a local dev environment on MacOS.

这导致在 MacOS 上设置本地开发环境时出现问题。

Several script functions, say mysqli_connect()in php might be using localhostto connect to MySQL: You can't (shouldn't have to) go about replacing localhostwith 127.0.0.1in every script.

几个脚本函数,mysqli_connect()在 php 中说可能localhost用于连接到 MySQL:您不能(不应该)在每个脚本中替换localhostwith 127.0.0.1

Tried fixing this by adding bind-address=localhost, socket=/var/mysql/mysql.sock, socket=/tmp/mysql.socketc. to the my.cnf file, but that didn't work.

通过增加固定试过这个bind-address=localhostsocket=/var/mysql/mysql.socksocket=/tmp/mysql.sock等来了my.cnf文件,但没有奏效。

To resolve the issue(on MacOS), note that the mysql.default_socketsetting in /etc/php.inifile points to /var/mysql/mysql.sock, while MySQL's default_socket is /tmp/mysql.sock.

要解决此问题(在 MacOS 上),请注意file中的mysql.default_socket设置/etc/php.ini指向/var/mysql/mysql.sock,而 MySQL 的 default_socket 是/tmp/mysql.sock.

So, either edit your php.inifile, or create a softlink to MySQL's default_socket:

所以,要么编辑你的php.ini文件,要么创建一个指向 MySQL 的 default_socket 的软链接:

mkdir /var/mysql
ln -s /tmp/mysql.sock /var/mysql/mysql.sock

P.S - 127.0.0.1 is treated as a TCP connection and localhost as unix socket connection by MySQL: details here.

PS - 127.0.0.1 被 MySQL 视为 TCP 连接,localhost 被视为 unix 套接字连接:详情请点击此处