Can't connect to mysql from php

Hi,

I installed apache-2 (2.2.0), mysql 5.0.19 and php (4.4.2) on RH.  Apache and PHP are compiled from source, apache is working, mysql is working from the command line and php is working. But I can't connect to mysql from php. The error is "Can't connect to local MySQL server through socket '/var/tmp/mysql.sock'..."

The msyql.sock file exists (srwxrwxrwx    1 mysql    mysql           0 Mar 27 20:16 mysql.sock) and php was compiled with
--with-mysqli=/usr/local/mysql/bin/mysql_config.

The php statement is valid: mysql_connect('localhost', '[user]', '[password]') or die('Could not connect: ' . mysql_error()); and that user /password works from the command line.

What am I doing wrong?

Thanks!
mjsdfweAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

WaielEidCommented:
This might help you :

http://www.tech-recipes.com/mysql_tips762.html

sometimes starting mysql in safe mode works
safe_mysqld
Richard QuadlingSenior Software DeveloperCommented:
Create a php script ...

<?php phpinfo(); ?>

Run this through your browser and look at the settings regarding MySQL.

If any of them are incorrect then edit the configuration file to correct them.

From what I can tell for *ix setups, the ini entries are all defaults for mysql.

[MySQL]
; Allow or prevent persistent links.
mysql.allow_persistent = On

; Maximum number of persistent links.  -1 means no limit.
mysql.max_persistent = -1

; Maximum number of links (persistent + non-persistent).  -1 means no limit.
mysql.max_links = -1

; Default port number for mysql_connect().  If unset, mysql_connect() will use
; the $MYSQL_TCP_PORT or the mysql-tcp entry in /etc/services or the
; compile-time value defined MYSQL_PORT (in that order).  Win32 will only look
; at MYSQL_PORT.
mysql.default_port =

; Default socket name for local MySQL connects.  If empty, uses the built-in
; MySQL defaults.
mysql.default_socket =

; Default host for mysql_connect() (doesn't apply in safe mode).
mysql.default_host =

; Default user for mysql_connect() (doesn't apply in safe mode).
mysql.default_user =

; Default password for mysql_connect() (doesn't apply in safe mode).
; Note that this is generally a *bad* idea to store passwords in this file.
; *Any* user with PHP access can run 'echo get_cfg_var("mysql.default_password")
; and reveal this password!  And of course, any users with read access to this
; file will be able to reveal the password as well.
mysql.default_password =

; Maximum time (in secondes) for connect timeout. -1 means no limimt
mysql.connect_timeout = 60

; Trace mode. When trace_mode is active (=On), warnings for table/index scans and
; SQL-Erros will be displayed.
mysql.trace_mode = Off
mjsdfweAuthor Commented:
I don't think it's mysql.sock because locations in my.cnf match and the timestamp changes when I stop/start the daemon.  Also I can connect to db via command line client.

phpinfo() looks right, mysql.sock points to same location, etc.

Questions about mysqli.  Re-reading docs and comments for nth time I found a comment that mysqli is for PHP 5 only.  Is that correct (this install is PHP 4).

When I compile PHP wth --mysql=/usr/local/mysql, make dies with (make works with bundled libs):

ext/mysql/php_mysql.lo: In function `zif_mysql_create_db':
/.../php-4.4.2/ext/mysql/php_mysql.c:1163: undefined reference to `mysql_create_db'

ext/mysql/php_mysql.lo: In function `zif_mysql_drop_db':
/.../php-4.4.2/ext/mysql/php_mysql.c:1205: undefined reference to `mysql_drop_db'

collect2: ld returned 1 exit status
make: *** [sapi/cli/php] Error 1

I've tried the standard and max versions.

thx,
m2
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Richard QuadlingSenior Software DeveloperCommented:
Yes. mysqli is php5 only.

The mysqli extension is designed to work with the version 4.1.3 or above of MySQL. For previous versions, please see the MySQL extension documentation.

This will probably be why it is not working. mysql and mysqli are 2 different extensions for PHP.

My settings are the default for mysqli

[MySQLI]

; Maximum number of links.  -1 means no limit.
mysqli.max_links = -1

; Default port number for mysqli_connect().  If unset, mysqli_connect() will use
; the $MYSQL_TCP_PORT or the mysql-tcp entry in /etc/services or the
; compile-time value defined MYSQL_PORT (in that order).  Win32 will only look
; at MYSQL_PORT.
mysqli.default_port = 3306

; Default socket name for local MySQL connects.  If empty, uses the built-in
; MySQL defaults.
mysqli.default_socket =

; Default host for mysql_connect() (doesn't apply in safe mode).
mysqli.default_host =

; Default user for mysql_connect() (doesn't apply in safe mode).
mysqli.default_user =

; Default password for mysqli_connect() (doesn't apply in safe mode).
; Note that this is generally a *bad* idea to store passwords in this file.
; *Any* user with PHP access can run 'echo get_cfg_var("mysqli.default_password")
; and reveal this password!  And of course, any users with read access to this
; file will be able to reveal the password as well.
mysqli.default_pd =

; Allow or prevent reconnect
mysqli.reconnect = Off
mjsdfweAuthor Commented:
I re-compiled with --with-mysql (so uses bundled libs). php.ini has:

[MySQL]
mysql.allow_persistent = On
mysql.max_persistent = -1
mysql.max_links = -1
mysql.default_port =
mysql.default_socket =
mysql.default_host =
mysql.default_user =
mysql.default_password =
mysql.connect_timeout = 60
mysql.trace_mode = Off

same results...so I played with mysql.sock config

in my.cnf (client and server): socket= /tmp/mysql.sock  (command line client works)

in php.ini: mysql.default_socket = /tmp/mysql.sock, now in phpinfo();
MYSQL_SOCKET  /var/tmp/mysql.sock (*see note below)
mysql.default_socket   /tmp/mysql.sock  <--local    /tmp/mysql.sock <-- master

(* /var has link to /tmp so cd /var/tmp sees /tmp)

php error is now: Client does not support authentication protocol requested by server; consider upgrading MySQL client
phpinfo() Client API version is 3.23.49

where is PHP getting that MYSQL_SOCKET value?

thx!
Richard QuadlingSenior Software DeveloperCommented:
I'm on windows and I would use port rather than socket.

I don't know what information should exist in the socket setting or the socket file.
Richard QuadlingSenior Software DeveloperCommented:
Can you upgrade to PHP5 and then you can use the much better mySQLi

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mjsdfweAuthor Commented:
That's an interesting question. I have a large amount of code written in 4 that has to run on this server.   How bad is the conversion from PHP4 to PHP5?
Richard QuadlingSenior Software DeveloperCommented:
If you are using $HTTP_xxx_VARS then this should become $_xxx.

If you are using a DB abstraction layer, then the db link should be fairly simple.

Try it on another machine.

I'm a ZCE and have only ever used PHP5 professionally, though I did start with PHP4.

Because PHP5 has a lot better OOP support, I never really bothered doing PHP4 and PHP5 code.

Testing is the best way to see what works or not.
mjsdfweAuthor Commented:
Thanks for the help!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.