為什麼我的正確連接嘗試在 apache2 下失敗但在 cli 下失敗? (Why is my correct connection attempt failing under apache2 but not under cli?)


問題描述

為什麼我的正確連接嘗試在 apache2 下失敗但在 cli 下失敗? (Why is my correct connection attempt failing under apache2 but not under cli?)

我有一個安裝了 Apache 和 MySQL 的實時託管服務器。如果我在cli上運行

mysql ‑u <username> ‑p

然後輸入密碼,我就成功登錄了。如果我這樣做

mysql ‑u <username> ‑h localhost ‑p ‑P 3306 <dbname>

然後輸入密碼,那麼我成功連接到MySQL並且數據庫也被成功選擇。所以,我實現了一個小的 php 文件,它看起來像這樣:

<?php

$result = mysqli_real_connect(mysqli_init(), 'localhost', '<username>', '<password>', '<databasename>', 3306);

echo var_dump($result);

我通過 cli 運行它:

php mytestfile.php

它成功連接到數據庫並顯示結果of

bool(true)

但是,如果我嘗試從網絡瀏覽器加載相同的文件,那麼結果是

bool(false)

這清楚地表明從 cli 進行連接的相同嘗試是有效的,而嘗試通過 apache2 連接失敗。試過:

1。重啟Apache

service apache2 restart

2. 重啟MySQL

service restart apache2

3. 使用完全相同的 ini 文件

mv /etc/php/7.4/apache2/php.ini /etc/php/7.4/apache2/php.ini_old

cp /etc/php/7.4/cli/php.ini /etc/php/7.4/apache2/php.ini

service restart apache2

4。在線搜索答案

我找到了一些與selinux相關的答案,說我們需要允許網絡訪問MySQL,但是,它們似乎與這個問題無關,因為我正在連接到本地主機,所以MySQL 與 Apache 在同一台機器上運行。

5. 比較套接字文件路徑

mysql.sock 位於兩個php.ini 中完全相同的位置

6。比較 20‑mysqli.ini

它們是相同的

7。找出執行腳本的用戶是誰

get_current_user()

當我從 cli 和 Apache 執行它時都會產生 root。

注意

這不是新安裝. 這長期正常工作,但在一周內,在測試一些與 curl 相關的邏輯時,我震驚地發現我的愛好項目被破壞了。所以,決定在周末調查這個問題。然而,我所有的嘗試都失敗了。可能是什麼問題?

注意,這不是一個固執己見的問題,除了上面已經檢查和列出的內容之外,我還會尋找要檢查的內容。如果答案包含可能導致我遇到的問題的一個或多個元素的列表,則答案在客觀上是正確的。將選擇包含最詳細描述和最多可能問題(除了我已經檢查過的問題)的答案,只要我遇到的具體問題在列出的問題中。

我不打算重新安裝操作系統、Apache 或 MySQL。我知道如果我從服務器中清除所有內容並重新安裝,那麼這將起作用。但是,由於這是一個業餘項目,我可以讓它無限期關閉,以便進行實驗並找出問題所在。

我收到的錯誤消息是 Permission Denied.

我不打算重新安裝操作系統、Apache 或 MySQL。我知道如果我從服務器中清除所有內容並重新安裝,那麼這將起作用。但是,由於這是一個業餘項目,我可以讓它無限期關閉,以便進行實驗並找出問題所在。

我收到的錯誤消息是 Permission Denied.

我不打算重新安裝操作系統、Apache 或 MySQL。我知道如果我從服務器中清除所有內容並重新安裝,那麼這將起作用。但是,由於這是一個業餘項目,我可以讓它無限期關閉,以便進行實驗並找出問題所在。

我收到的錯誤消息是 Permission Denied.


參考解法

方法 1:

Managed to resolve the problem, but I'm not 100% sure that I correctly determined what the problem was. Basically, in order to fix the issue, and knowing that modernizing will never hurt (especially when things are already broken), I have upgraded PHP to version 8.0, but then the/some problem still persisted, albeit a different error message was provided, which was saying that some folder was missing. Then, I changed the host in the connection settings from localhost to 127.0.0.1 and it works.

So, my best guess about what the problem might have been is that for some reason localhost has stopped being a correct host name for some reason, while 127.0.0.1 is a correct host name, so a step that I have definitely missed during the investigation was to check what the allowed hosts are, because I assumed that this was unchanged. However, even though I strongly suspect that this was the solution to the problem, I do not factually know whether that's the case. Before upgrading PHP from 7.4 to 8.0 the error message when attempting to run things via Apache was Permission Denied and running the very same PHP script via cli was working impeccably. After upgrading PHP to version 8.0, the same script responded with an error message that stated that some folder was missing, without telling me what that folder was, yet, changing hostname from localhost to 127.0.0.1 did the trick.

Steps I have taken in order to resolve the problem, inspired by https://www.tecmint.com/install‑php‑8‑on‑ubuntu/ :

apt update
apt upgrade
apt install  ca‑certificates apt‑transport‑https software‑properties‑common
add‑apt‑repository ppa:ondrej/php
apt update
apt install php8.0 libapache2‑mod‑php8.0
apt install php8.0‑mysqli

Note: I was doing these operations with the root user. If you are doing an upgrade with another user, then you will need to do so with a sudoer and you might need a sudo keyword before the operations above.

Now, running php ‑v yields

enter image description here

However, the trick was to change my settings file so it will use 127.0.0.1 as the host value instead of localhost. This is extremely strange for me, since the very same settings were working properly before, only I have access to the database and I certainly did not change the allowed connections and the fact that via cli I was able to connect to localhost added a lot to the strangeness.

So, all in all, due to the fact that my experiments yielded results earlier than getting any useful answers or hints, I decided to write an answer on my own. However, I am happy to accept an answer which contains a better understanding of the issue than my own, since this answer admittedly does not contain an explanation of what the issue is and hence, this answer is only acceptable until someone comes up with a proper explanation of what might have happened that caused the issue that's now fixed.

方法 2:

As I said in my previous comments, your mysql wasn't allowing incoming connections from that specific host. "localhost" is different from 127.0.0.1. "localhost" means local socket connection while 127.0.0.1 is treated as TCP/IP.

Executing a php script directly in the cli might yield different results than executing it with apache. One execution could use try to use local socket connections while the other tries to use TCP. The way apache executes php (ie. mod_php, php_fpm, etc) may also play a role in this, even if they both use the same php.ini.

As for why it was working before and stopped suddenly; something probably changed without you notice it. I've experienced issues mysql/mariadb config tables before and even filled a bug report, although not directly related to your issue.

(by Lajos ArpadLajos ArpadEnrico Dias)

參考文件

  1. Why is my correct connection attempt failing under apache2 but not under cli? (CC BY‑SA 2.5/3.0/4.0)

#mysqli #apache2 #MySQL #PHP #ubuntu






相關問題

如何第一次創建 mysqli 數據庫? (how can I creates a mysqli database for first the first time?)

使用 jquery autosuggest 從多個表中選擇 (select from multiple tables with jquery autosuggest)

無法連接 - 數據庫或編碼錯誤 (Could Not Connect - Database or Coding Error)

mysqli忽略表中的第一行 (mysqli ignoring the first row in a table)

從數據庫導入的鏈接以 UTF8 截斷 (imported link from database cut off at UTF8)

試圖獲取非對象的屬性 (Trying to get property of non-object)

獲取 php 和 mysqli 中插入數據的 ID (Get Id of inserted data in php and mysqli)

WAMP 和 mysqli::real_connect(): (HY000/2002)? (WAMP and mysqli::real_connect(): (HY000/2002)?)

如何將 MySQLi 準備好的語句的結果放入關聯數組中? (How can I put the results of a MySQLi prepared statement into an associative array?)

測試選擇查詢是否找到結果 (Testing if a Select Query found results)

為什麼我的正確連接嘗試在 apache2 下失敗但在 cli 下失敗? (Why is my correct connection attempt failing under apache2 but not under cli?)

在帶有數組的 IN 語句之後,在 WHERE 語句中使用更多佔位符 (Use more placeholders in WHERE statement after IN statement with arrays)







留言討論