簡體   English   中英

連接失敗:連接被拒絕...來自 PHP 代碼的 MySQL 數據庫,但我可以從 phpmyadmin 連接

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

所以我可以從 web 瀏覽器登錄到 phpmyadmin 就好了。 但是,當我嘗試使用 PHP 代碼中的相同用戶名和密碼登錄時,我得到“連接失敗:連接被拒絕”。 我正在通過 Google Cloud 在 Ubunutu 終端上運行我的 LAMP 堆棧。

如果我將服務器名稱從“127.0.0.1”更改為“localhost”,我會得到“連接失敗:用戶'cadeuser'@'localhost'的訪問被拒絕(使用密碼:YES)

這種方法適用於我的本地機器(我有一個完全不同的 MySQL 數據庫和 XAMPP),但不適用於谷歌雲。 我對此很陌生,所以請輕松使用 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);
}

運行“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

這是我的 phpmyadmin 用戶查看用戶

這是我關於 Google Cloud防火牆規則的防火牆內容

解決方案我通過在 /etc/mysql/mariadb.conf.d/50-server.cnf 的 [ /etc/mysql/mariadb.conf.d/50-server.cnf ] 或 [mysqld] 下添加“skip-grant-tables”來修復它

我假設是因為 googlecloud 具有不同的 IP 地址,或者您的防火牆可能會阻止您。 嘗試ifconfig和/或查看您的防火牆ufw 您還授予用戶“cadeuser”完全權限嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM