在centos6.5中连接mysql失败


Failed to connect mysql in centos 6.5

我在centos6.5系统中使用php-mysql-apache,所有需求都存在,我想我可以在localhost中发布我的html文件,但当我尝试在php脚本中连接到mysql时。它没有出现,并且显示mysql连接错误。

# rpm -qa |grep php 
  php-pdo-5.3.3-26.el6.x86_64
  php-common-5.3.3-26.el6.x86_64
  php-5.3.3-26.el6.x86_64
  php-mysql-5.3.3-26.el6.x86_64
  php-cli-5.3.3-26.el6.x86_64
  php-ldap-5.3.3-26.el6.x86_64  
# rpm -qa |grep mysql
  mysql-libs-5.1.71-1.el6.x86_64
  mysql-community-release-el6-5.noarch
  mysql-5.1.71-1.el6.x86_64
  mysql-server-5.1.71-1.el6.x86_64
  php-mysql-5.3.3-26.el6.x86_64

我还检查了我的mysql密码和用户名是否正确,我尝试连接如下:

$connect= mysql_connect("localhost","root","password");
if(!$connect){
    echo "Failed to connect to MySQL " ;
}

php.ini文件的"动态扩展"部分如下:

;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;
; If you wish to have an extension loaded automatically, use the following
; syntax:
;
;   extension=modulename.extension
;
; For example
;
;   extension=msql.so
;
; ... or with a path:
;
;   extension=/path/to/extension/msql.so
;
; If you only provide the name of the extension, PHP will look for it in its
; default extension directory.
  extension=mysql.so
  extension=mysqli.so
;;;
; Note: packaged extension modules are now loaded via the .ini files
; found in the directory /etc/php.d; these are loaded by default.
;;;;

这几行是我亲手加上去的;

 extension=mysql.so
 extension=mysqli.so

那么我的错在哪里呢?你有在localhost中运行php脚本的想法吗?

不要使用mysql_connect,因为它已被弃用,使用MySQLi或PDO进行连接–这确实很痛苦,但请检查Apache错误日志或启用PHP来显示脚本中的所有错误,这可能会有所帮助。