简体   繁体   English

连接失败:连接被拒绝...来自 PHP 代码的 MySQL 数据库,但我可以从 phpmyadmin 连接

[英]Connection failed: Connection refused... for MySQL database from PHP code, but I can connect from phpmyadmin

So I can login to phpmyadmin just fine from a web browser.所以我可以从 web 浏览器登录到 phpmyadmin 就好了。 But when I try to use the same username and password from my PHP code to login I get " Connection failed: Connection refused ".但是,当我尝试使用 PHP 代码中的相同用户名和密码登录时,我得到“连接失败:连接被拒绝”。 I am running my LAMP stack on an Ubunutu terminal via Google Cloud.我正在通过 Google Cloud 在 Ubunutu 终端上运行我的 LAMP 堆栈。

If I change the servername from "127.0.0.1" to "localhost" I get " Connection failed: Access denied for user 'cadeuser '@'localhost' (using password: YES) "如果我将服务器名称从“127.0.0.1”更改为“localhost”,我会得到“连接失败:用户'cadeuser'@'localhost'的访问被拒绝(使用密码:YES)

This method of doing it works on my local machine (which I have an entirely different MySQL database with XAMPP), but not with the Google Cloud one.这种方法适用于我的本地机器(我有一个完全不同的 MySQL 数据库和 XAMPP),但不适用于谷歌云。 I am very new to this so go easy please.我对此很陌生,所以请轻松使用 go。

$servername = "127.0.0.1";
$myfile = fopen("../../reach.txt", "r") or die("Unable to open file!");

$username = fgets($myfile);
$password = fgets($myfile);

$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

Running "IFCONFIG" I get运行“IFCONFIG”我得到

ens4: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1460
        inet 10.128.0.2  netmask 255.255.255.255  broadcast 0.0.0.0
        inet6 fe80::4001:aff:fe80:2  prefixlen 64  scopeid 0x20<link>
        ether 42:01:0a:80:00:02  txqueuelen 1000  (Ethernet)
        RX packets 107514  bytes 176647898 (176.6 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 100127  bytes 15653331 (15.6 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 6153  bytes 564232 (564.2 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6153  bytes 564232 (564.2 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Here is my phpmyadmin users view users这是我的 phpmyadmin 用户查看用户

Here is my firewall stuff on Google Cloud firewall rules这是我关于 Google Cloud防火墙规则的防火墙内容

SOLUTION I fixed it by adding "skip-grant-tables" under [mariadb] or [mysqld] at /etc/mysql/mariadb.conf.d/50-server.cnf解决方案我通过在 /etc/mysql/mariadb.conf.d/50-server.cnf 的 [ /etc/mysql/mariadb.conf.d/50-server.cnf ] 或 [mysqld] 下添加“skip-grant-tables”来修复它

I assume because the googlecloud one has a different IP address, or possibly your firewall might be preventing you.我假设是因为 googlecloud 具有不同的 IP 地址,或者您的防火墙可能会阻止您。 Try ifconfig and/or look at your firewall ufw .尝试ifconfig和/或查看您的防火墙ufw Also have you given the user 'cadeuser' full permissions?您还授予用户“cadeuser”完全权限吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM