PHP 5.3.3 升級手記(APACHE 2.2 + MYSQL)

看了我上一篇日誌就知道,最近在搞cURL這東西。。。。

最開始沒想弄命令行的,因爲相關教程和文章都比較少。於是很多人都在用php上的curl庫,於是我也嘗試裝了下。

 

最開始我的PHP是5.2.幾,當時是用msi安裝包安裝的(win7系統)當然apache也是安裝包安裝的- -(麼辦法,本人小菜。。)

當時在裝添加mysql.dll時就蛋疼了一把。據說msi它會選擇性安裝東西,很多東西就不裝了。。。於是後來我又把tar包解壓覆蓋了下= =

 

但是後來裝cURL時又悲劇了,嘗試網上n多教程都會讓apache顯示“The requested operation has faild..”

看來網上有人說建議用解壓包的,於是就下載了php5.3,也當作升級了。。。

 

解壓到另外一個地方,然後就開始各種蛋疼地設置,還有修改環境變量

總結下來大概有幾個地方:

1.修改php.ini:

extension_dir = "F:/php/ext"

 


extension=php_gd2.dll
extension=php_curl.dll

extension=php_mbstring.dll

extension=php_mysql.dll
extension=php_mysqli.dll

extension=php_xmlrpc.dll

 

上面除了curl和mysql以外,都是按網上推薦的設置的。。。

 

2.環境變量

windows的path:加一條F:/php

PHPRC=F:/php

 

3.複製

libeay32.dll

php5ts.dll

ssleay32.dll

到system32文件夾

 

4.複製

php5ts.dll

到apache2.2/bin下

 

 

5.修改apache的設置文件,添加如下

PHPIniDir "F:/PHP/"
LoadModule php5_module "F:/PHP/php5apache2_2.dll"
AddType application/x-httpd-php .php

 

似乎就可以了。

注意一下,關於php下載的版本

應該下載

VC6 x86 Thread Safe

版本,VC6版本給apache,非線程安全的版本會缺少一些dll文件- -(不知道爲什麼…)

 

嗯,有機會我估計要重裝apache成解壓版本了。。。

 

 

當一切都配置好了以後,悲劇就出現了。

MYSQL連接不上了。。。

上網查了下原因,是在連接mysql服務器的時候,使用了“localhost”

而在5.3裏應該用"127.0.0.1".....

比如:

 

具體的原因是因爲php 5.3使用了mysqlnd這種東西。

它的好處是加速了數據讀取,以及增加對IPV6的支持

悲劇就悲劇在這裏。因爲它支持了ipv6,當你設置了localhost以後,它會首先在IPV6中尋找ip並且連接。。

所以有2種解決辦法:

1.修改localhost 成127.0.0.1,讓php明確連接ipv4的服務器

2.在host文件中添加如下語句:

這似乎也是VISTA 和win7的一個小問題吧。。。。

 


附一個老外寫的原文:

PHP 5.3 and MySQL connectivity problem

I have seen so many post in our IIS forum mentioning that after moving to PHP5.3 they are not able to connect to MySQL database. So this means a simple program like the one below doesn’t work:

 

And trust me most of the users cannot even dig this further and just mention that they are getting a 500 or some sort of script timeout.

There are two workaround to overcome this situation:

  • Most elegant one is to replace the database server name ‘localhost’ with IP address of the machine or ‘127.0.0.1’ which is reserved for localhost.
  • Disable IPV6. This can be done by opening the file ‘%systemroot%/System32/drivers/etc/hosts’ and commenting out the line ‘::1             localhost’. I would not advise this as there might be some other components which is dependent on IPV6 and may break or stop working.

Now some people also deduce from the above workaround that it is a Windows OS bug or a PHP bug. Neither is correct. I have explained this on PHP Windows internal alias couple of times as well as on our forums. Let me explain it again.

Most of the Operating System today is building support for IPV6. IPV4 has its own set of limitation and hence the world is moving to IPV6. I am not going to explain differences between two, but for the sake of this discussion assume that they are different. A close look at PHP MySQL driver code indicates that mysql_connect() function uses mysql_real_connect() to connect to MySQL. The API mysql_real_connect() is an API which comes from MySQL dll (MySQL development team and is part of MySQL database installation). So this means the support for IPV6 in PHP MySQL driver is not possible unless there is a support for IPV6 in MySQL database. So the limitation (or no support for IPV6 or whatever word may be used to describe this situation) is coming from MySQL database limitation. So now it is clear that this is not a PHP bug in the sense that nothing can be changed in PHP code base to fix it.

Let's move on to operating system concern now. A typical host file (irrespective of OS) has two columns which contains the IP address and host name binding to it. In order to support both IPV4 and IPV6 (at least on Windows) localhost is getting mapped to two IP addresses. There can be other reasons for mapping same localhost to multiple IP address. What is important is that, there is a case where we have multiple IP address pointing to same host name. A close inspection of MySQL code reveals that the function mysql_real_connect() actually tries to connect to only the first IP address returned for the hostname and if this fails, no further attempt to connect using other IP address associated with same host name is made. This can be fixed and I believe a patch is already lying in some development branch of MySQL and please follow MySQL release to know which version has this fix integrated. I do not have any idea about that. But an important thing is that this bug can surface on any OS where you have above condition satisfied for the host file. Yes WINDOWS, Ubuntu, Solaris all will and may get impacted. So this means that this is not a problem with Windows OS or for that reason any other OS.

Hope this explain things. Thanks for the patient reading and till we meet again ‘Good Bye’.

Thanks,

Don.

PS: In this post you learnt one reason for using IP address as your database host name. There is another compelling reason. Read the blog by Kanwal here to know more about it.

 

發佈了28 篇原創文章 · 獲贊 10 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章