We help IT Professionals succeed at work.

mysqli connection problem

fosiul01
fosiul01 asked
on
Medium Priority
521 Views
Last Modified: 2012-06-21
hi
this is php 5.3.8 and mysql 5.5

both has been installed from source .

and from phpinfo.. i can see php has mysqli support. , see  bellow.

mysqli
MysqlI Support      enabled
Client API library version       5.5.17
Active Persistent Links       0
Inactive Persistent Links       0
Active Links       0
Client API header version       5.5.17
MYSQLI_SOCKET       mysqld.sock


and mysql is running as this :

 ps aux | grep mysql
root     15607  0.0  0.0  63876  1252 pts/4    S    17:08   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf
mysql    16244  0.0  2.2 4907936 364452 pts/4  Sl   17:09   0:00 /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/DB/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/DB/data/posrep.err --open-files-limit=8192 --pid-file=/DB/data/posrep.pid --socket=mysqld.sock --port=3306


When i try to connect mysql from console i connect like this :
/usr/local/mysql/bin/mysql -uroot -S /DB/data/mysqld.sock -p


now problem is :
when i am trying to use mysqli wiht php .. its seems like its does not connect or it does not get any output form script.

when i execute bellow code. i get bellow error

Warning: mysqli_connect(): (HY000/2002): Can't connect to local MySQL server through socket 'mysqld.sock' (2) in /usr/local/apache/htdocs/mysql.php on line 3 Can't connect to local MySQL server through socket 'mysqld.sock' (2) Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, boolean given in /usr/local/apache/htdocs/mysql.php on line 14 Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in /usr/local/apache/htdocs/mysql.php on line 18 Warning: mysqli_close() expects parameter 1 to be mysqli, boolean given in /usr/local/apache/htdocs/mysql.php on line 33

now dont understand . where is the issue ??
can any one point me out..
is this php cant connect to mysql via mysqli  or the script has problem ??

Thanks for your help .


<?php
// Connect to database
        $link = mysqli_connect('localhost','root','xxxxx','dbname');

        // Check for Errors
        if(mysqli_connect_errno()){
          echo mysqli_connect_error();
        }

        // Prepare Query
        $query = "SELECT * FROM reportschedule r";

        // Escape Query
        $query = mysqli_real_escape_string($link,$query);


        // Perform Query
        if($result = mysqli_query($link,$query)){

         // Cycle through results
         while($row = mysqli_fetch_object($result)){
           echo $row->column;
         }


         // Free Result Set
         mysqli_free_result($result);


        }

        // Close Connection
        mysqli_close($link);
?>

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
run lsof -i:3306 to make sure you only have one mysql instance listening on port 3306.

Alternatively, use the socket in your connection string:
http://php.net/manual/en/function.mysql-connect.php

$link = mysqli_connect(':/DB/data/mysqld.sock','root','xxxxx','dbname');


usually the sockets should be stored in /var/run this way should the system reboot unexpectedly power loss, etc. the mysql service can start since /var/run is emptied on boot.  In your case, mysql will not start because the socket /DB/data/mysqld.sock will still exist.
Top Expert 2009

Author

Commented:
i tryed that way before .. it did not help

I tryed to put /DB/data/mysqld.sock  in to php.ini , still did not help

then i changed the mysql.sock from /DB/data/ to /tmp/mysql.sock .

then chagned the path in php.ini..
then it was ok...

CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
Permission issues on accessing the file in the original path.
socket should often be in /var/run
Top Expert 2009

Author

Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for fosiul01's comment http:/Q_27428446.html#37075120

for the following reason:

had to add that path into php.ini
Top Expert 2009

Author

Commented:
open it
Top Expert 2009

Author

Commented:
cancel it