?
Solved

Can't connect to mysql from php

Posted on 2006-03-27
10
Medium Priority
?
1,992 Views
Last Modified: 2013-12-13
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!
0
Comment
Question by:mjsdfwe
  • 5
  • 4
10 Comments
 
LVL 2

Expert Comment

by:WaielEid
ID: 16307987
This might help you :

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

sometimes starting mysql in safe mode works
safe_mysqld
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 16308902
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
0
 

Author Comment

by:mjsdfwe
ID: 16309628
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
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 40

Expert Comment

by:Richard Quadling
ID: 16309659
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
0
 

Author Comment

by:mjsdfwe
ID: 16309833
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!
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 16309851
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.
0
 
LVL 40

Accepted Solution

by:
Richard Quadling earned 2000 total points
ID: 16309860
Can you upgrade to PHP5 and then you can use the much better mySQLi
0
 

Author Comment

by:mjsdfwe
ID: 16309960
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?
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 16310111
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.
0
 

Author Comment

by:mjsdfwe
ID: 16310151
Thanks for the help!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question