2

I have two computers, one install windows 7 , another is CentOS 5.8


In CentOS...

I use yum command to install httpd(apache2.2), php5.3.3 and mysql.

  • yum install httpd
  • yum install php53
  • yum install pdo
  • yum install php53-mysql

First I check the phpinfo, the pdo, pdo_mysql is success extends,

then I also check php -m in CentOS Terminal , have extend pdo and pdo_mysql, too.

enter image description here enter image description here


I run same code between windows 7 and CentOS

window success, but centos fail

Two files, db.php and dbtest.php

Here is db.php:

<?php

    class DB
    {
        private $conn;

        #### construct ####
        public function __construct( $dsn , $db_user , $db_password , $showError = false )
        {                       
            try
            {               
                $this->conn = new PDO( $dsn , $db_user , $db_password );

                if( $showError ) // set error information show or not. 
                {
                    $this->conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
                }
                else
                {
                    $this->conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT );
                }

                $setUtf8 = $this->conn->prepare( 'set names utf8' ); // set encoded by utf8
                $setUtf8->execute();
            }
            catch( PDOException $err )
            {
                return false;
            }
        }
    }
?>

and here is dbtest.php:

<?php

    require_once( "db.php" );

    $link_test = new DB( "mysql:dbname=pdotest;port=3306;host=192.168.1.127", "root" , "123456" );  

    var_dump($link_test);
?>

When dump in windows , the result is:

object(DB)#1 (1) { ["conn":"DB":private]=> object(PDO)#2 (0), { } }

dump in CentOS, the result is:

object(DB)#1 (1) { ["conn":"DB":private]=> NULL }

Can anyone tell me why connect fail in CentOS??


Edit at 2012/10/24 16:10 (Asia/Taipei)

For test many hour, I guess is php pdo have problem...

because in my LAN, I can connect any 3306 port mysql by CentOS Terminal

but in php pdo_mysql class, I can't link any mysql(even CentOS local's mysql )

so...how to check my pdo_mysql extension is operate normally?

PS: Sorry,I have poor English :(

user1770147
  • 63
  • 2
  • 6

3 Answers3

0

PHP/PDO on Linux will attempt to connect via a socket for local connections. You configure the location of this socket in the pdo_mysql.default_socket property in php.ini.

You will of course run into Bug #60155 which is fixed in 5.3.9 (I think) so I would definitely advise upgrading beyond version 5.3.3.

Phil
  • 157,677
  • 23
  • 242
  • 245
0

First, thank you to answer my question : )


Focus on socket, I already tried two ways:

1) Just like you say, I modify php.ini , add pdo_mysql.default_socket

then restart httpd service, but pdo mysql connection still fail

2) I modify dbtest.php line 3 (#3) , part of DSN

Original=> "mysql:dbname=pdotest;port=3306;host=192.168.1.127"
New  => "mysql:unix_socket=/var/lib/mysql/mysql.sock;dbname=pdotest;port=3306;host=192.168.1.127"

, but connection still fail


I also try original mysql_connect to test link db, It's work!

According to your suggest, I will change php to another version(Maybe 5.4.8 or lower then 5.3.3) later, then post the result

Thanks!

user1770147
  • 63
  • 2
  • 6
0

Waste a lot of time...finally I get the solution!!

When I saw this question << Click link to see question

This answer solve my problem.

Selinux default setting is close httpd_can_network_connect and httpd_can_network_connect_db

Just open it! PDO connection will operate normally :)

Command:

setsebool -P httpd_can_network_connect=1
setsebool -P httpd_can_network_connect_db=1

Thank you, Michael Berkowski

Community
  • 1
  • 1
user1770147
  • 63
  • 2
  • 6