By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,394 Members | 1,443 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,394 IT Pros & Developers. It's quick & easy.

Cannot get mysql_connect to work

P: n/a
Hi,

I'm trying to do something which should be very simple - connect to the
MySQL database. Here is the call, followed by the error msg.

$conn = mysql_connect("localhost", "root", "");

Warning: mysql_connect(): Can't connect to local MySQL server through
socket '/var/lib/mysql/mysql.sock' (13) in /var/www/html/buildhw.php on
line 13
failed

I've also tried this with the host arg as "", but I get the same
result. Logging on as root made no difference either.

Any help would be very much appreciated.

Thanks,
M. McDonnell

Dec 27 '05 #1
Share this Question
Share on Google+
19 Replies


P: n/a
Michael wrote:
Hi,

I'm trying to do something which should be very simple - connect to the
MySQL database. Here is the call, followed by the error msg.

$conn = mysql_connect("localhost", "root", "");

Warning: mysql_connect(): Can't connect to local MySQL server through
socket '/var/lib/mysql/mysql.sock' (13) in /var/www/html/buildhw.php on
line 13
failed

I've also tried this with the host arg as "", but I get the same
result. Logging on as root made no difference either.

Any help would be very much appreciated.

Thanks,
M. McDonnell


You may want to adjust your php.ini for mysql.
Look for [MySQL] and then the lines like this:

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

I suspect your mysql has been compiled not to have the socket file in
/tmp/mysql.sock. In fact, it appears to be in /var/lib/mysql/mysql.sock.
A quick check of /etc/my.cnf will tell you where it should be.

-david-

Dec 28 '05 #2

P: n/a
David,

Thank you for your help. The file /etc/my.cnf is shown below. It looks
like the default location is correct in the sense that the file
/var/lib/mysql/mysql.sock does exist (whether it contains the correct
information, I cannot tell).

Also, please not that I can connect to MySQL through the MySQL monitor
by typing "mysql".

Do you, or anyone else have any other suggestions? I think I'm stuck at
this point...

mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

[mysql.server]
user=mysql
basedir=/var/lib

[safe_mysqld]
err-log=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

Dec 28 '05 #3

P: n/a
Michael wrote:
David,

Thank you for your help. The file /etc/my.cnf is shown below. It looks
like the default location is correct in the sense that the file
/var/lib/mysql/mysql.sock does exist (whether it contains the correct
information, I cannot tell).

Also, please not that I can connect to MySQL through the MySQL monitor
by typing "mysql".

Do you, or anyone else have any other suggestions? I think I'm stuck at
this point...

mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

[mysql.server]
user=mysql
basedir=/var/lib

[safe_mysqld]
err-log=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid


What does the [MySQL] block in your php.ini file look like?

-david-

Dec 28 '05 #4

P: n/a
Michael wrote:
David,

Thank you for your help. The file /etc/my.cnf is shown below. It looks
like the default location is correct in the sense that the file
/var/lib/mysql/mysql.sock does exist (whether it contains the correct
information, I cannot tell).

Also, please not that I can connect to MySQL through the MySQL monitor
by typing "mysql".

Do you, or anyone else have any other suggestions? I think I'm stuck at
this point...

mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

[mysql.server]
user=mysql
basedir=/var/lib

[safe_mysqld]
err-log=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid


Another question:
Are you running php standalone or via a web server?

-david-

Dec 28 '05 #5

P: n/a
David,

The MySQL block in php.ini is shown below. I do have another piece of
information, and that is that the following call does work:

$conn = mysql_connect("127.0.0.1", "mike", ""); // This works.

I still don't understand why "locahost" doesn't work, though (probably
because I don't fully understand what "locahost" actually refers to).

Thanks for any help you can give.

M. McDonnell


==============php.ini MySQL block follows===================

[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 seconds) for connect timeout. -1 means no limit
mysql.connect_timeout = 60

; Trace mode. When trace_mode is active (=On), warnings for table/index
scans and
; SQL-Errors will be displayed.
mysql.trace_mode = Off

Dec 28 '05 #6

P: n/a
Michael wrote:
David,

The MySQL block in php.ini is shown below. I do have another piece of
information, and that is that the following call does work:

$conn = mysql_connect("127.0.0.1", "mike", ""); // This works.

I still don't understand why "locahost" doesn't work, though (probably
because I don't fully understand what "locahost" actually refers to).

Thanks for any help you can give.

M. McDonnell


==============php.ini MySQL block follows===================

[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 seconds) for connect timeout. -1 means no limit
mysql.connect_timeout = 60

; Trace mode. When trace_mode is active (=On), warnings for table/index
scans and
; SQL-Errors will be displayed.
mysql.trace_mode = Off


Micheal,

localhost refers to the machine itself, via the loopback adapter
(127.0.0.1). You should see in your /etc/hosts file something similar to:

127.0.0.1 localhost

Ensure that's there; if not mysql won't be your only failure.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Dec 28 '05 #7

P: n/a
Jerry,

Thanks for your help. I guess I'm a little confused about localhost. It
seems that if I open a file in the browser at
http:/localhost/myFile.html, it actually refers to the file
/var/www/html/myFile.html. So it seems there are two meanings to
localhost? In one context it refers to the local machine via ip
address 127.0.0.1, and in the context of the browser, (with Apache
running?), localhost refers to a specifc directory path on the local
machine. This is why I'm confused. Can you shed any light on this?

Thanks,
M. McDonnell
P.S., my /etc/host file looks ok (see below).
================/etc/host file ================
Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1 McDonnell localhost.localdomain localhost
==========================================

Dec 28 '05 #8

P: n/a
Michael wrote:
Jerry,

Thanks for your help. I guess I'm a little confused about localhost. It
seems that if I open a file in the browser at
http:/localhost/myFile.html, it actually refers to the file
/var/www/html/myFile.html. So it seems there are two meanings to
localhost? In one context it refers to the local machine via ip
address 127.0.0.1, and in the context of the browser, (with Apache
running?), localhost refers to a specifc directory path on the local
machine. This is why I'm confused. Can you shed any light on this?

Thanks,
M. McDonnell
P.S., my /etc/host file looks ok (see below).
================/etc/host file ================
Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1 McDonnell localhost.localdomain localhost
==========================================


No, in either case it's referring to the local machine.

If your domain is example.com, http://www.example.com/myFile.html would
also refer to the file /var/www/html/myFile.html. Here you're accessing
your machine through the web browser from an external source; if you
call http://localhost/myFile.html you're calling the web browser from
the same machine.

localhost always refers to the machine itself. It's the web browser
which is translating the directory.

And if you have the appropriate daemons running, you can

telnet localhost
ftp localhost
ping localhost
etc.

which does exactly the same things as

telnet example.com
ftp example.com
ping example.com

The only difference is the first ones you're calling from the local
machine; for the second group you're calling from either the same or a
different machine.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Dec 28 '05 #9

P: n/a
>Thanks for your help. I guess I'm a little confused about localhost. It

localhost refers to the local machine. The *whole* machine, not
just one directory on it.
seems that if I open a file in the browser at
http:/localhost/myFile.html, it actually refers to the file
/var/www/html/myFile.html.
On the local machine.

If I ftp to localhost and log in as 'gordon', I end up in my home
directory on the local machine. If I ftp to localhost and log in
as 'ralph', I end up in Ralph's home directory on the local machine.
If I http to localhost and request /, I get the web server's document
root directory (or an index file in it). If I ping localhost, I
get *no directory whatever*, I just get ping replies back. If I
log in on MySQL to localhost, I get the database I asked for, and
who cares what directory it's kept in (sometimes databases are
jumbled together in one large file, but if MySQL can figure it out,
that's fine with me) if I get the right one? If I connect an IRC
client to localhost, I end up talking to the IRC server on my local
machine (assuming one exists), and the directory is not relevant.
So it seems there are two meanings to
localhost? In one context it refers to the local machine via ip
address 127.0.0.1, and in the context of the browser, (with Apache
running?), localhost refers to a specifc directory path on the local
machine. This is why I'm confused. Can you shed any light on this?


localhost refers to the local machine. The *whole* local machine.
Any directory is implied or specified by parts of the request other
than the hostname.

Gordon L. Burditt
Dec 28 '05 #10

P: n/a
Jerry and Gordon,

Thank you for taking the time to write out these superb explanations; I
and I'm sure others appreciate it. Just a few more question, if I
may...

1. How does the browser know that it should translate
http:/localhost/myFile.html into /var/www/html/myFile.html? Is there an
ini file somewhere that contains this information?

2. This thread got started because the first mysql call (see below)
failed, but when I changed localhost to 127.0.0.1, as in the second
call, it worked. Given the information I have from you now, I don't see
how the first call could ever work, although the documentation says
that it should. Any ideas?

$conn = mysql_connect("localhost", "root", ""); // This fails.
$conn = mysql_connect("127.0.0.1", "mike", ""); // This works.

Thanks again for your help,
M. McDonnell

Dec 28 '05 #11

P: n/a
>Thank you for taking the time to write out these superb explanations; I
and I'm sure others appreciate it. Just a few more question, if I
may...

1. How does the browser know that it should translate
It doesn't. The WEB SERVER translates. The browser should not
have access to this information (and I would consider it a security
problem), nor does it need it.
http:/localhost/myFile.html into /var/www/html/myFile.html? Is there an
ini file somewhere that contains this information?
You configure a web server for a document root (that's what Apache
calls it. You can run Apache on Windows. On UNIX, this is probably
set in httpd.conf or a file included by it.). The document root
and its subdirectories contain the web pages to serve. IIS certainly
has a similar concept that you can configure somewhere. I don't
do Windows so I have no idea where this is.
2. This thread got started because the first mysql call (see below)
failed, but when I changed localhost to 127.0.0.1, as in the second
call, it worked. Given the information I have from you now, I don't see
how the first call could ever work, although the documentation says
that it should. Any ideas?
MySQL (specifically, the client library) uses local UNIX-domain (at
least on UNIX) sockets (created by the server) when you connect to
"localhost". (This string is recognized specially, whether or not
looking it up in the hosts file yields "127.0.0.1" for the IP
address.) This will have problems if your MySQL client (e.g. PHP)
and your MySQL server (mysqld) disagree over the location of the
socket. The fix here is to get them to agree on the location of
the socket (may be fixable in php.ini).

MySQL uses Internet-domain sockets when you connect to an IP address.
There is no issue of the path name to a socket that can be
misconfigured: you don't get to specify one, and there may not be
any such socket on the local MySQL client machine (the one running
PHP and the web server).
$conn = mysql_connect("localhost", "root", ""); // This fails.
$conn = mysql_connect("127.0.0.1", "mike", ""); // This works.


Gordon L. Burditt
Dec 28 '05 #12

P: n/a
This thread was too long for me to bother reading. Before when I wasn't
using XAMPP <http://apachefriends.org> I had the same kind of problems
so i found all files like mysql.dll or something simular and placed
them in every directory i thought might make it work and it did, so if
you don't get it working then try that.

Dec 28 '05 #13

P: n/a
Michael wrote:
Jerry,

Thanks for your help. I guess I'm a little confused about localhost. It
seems that if I open a file in the browser at
http:/localhost/myFile.html, it actually refers to the file
/var/www/html/myFile.html. So it seems there are two meanings to
localhost? In one context it refers to the local machine via ip
address 127.0.0.1, and in the context of the browser, (with Apache
running?), localhost refers to a specifc directory path on the local
machine. This is why I'm confused. Can you shed any light on this?

Thanks,
M. McDonnell
P.S., my /etc/host file looks ok (see below).
================/etc/host file ================
Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1 McDonnell localhost.localdomain localhost
==========================================

Michael,
The URL http://localhost/myFile.html is composed of a number of sections:
'http:' indicates the protocol to be used (another example would the svn:)
'localhost' (or any fully qualified name) indicates the Internet Address
of the server that browser requests should be sent to. Sometimes there
is also a port associated with the address (e.g. localhost:8000). The
default ports are 80 for http protocol and 443 for https protocol.

The name 'localhost' or 'www.foo.com' is simply an alias for an Internet
address in the form aaa.bbb.ccc.ddd (assuming IPv4). So, your browser
needs to be able to translate www.foo.com into a meaningful
aaa.bbb.ccc.ddd value.

Your system has a file (/etc/hosts) that maps unregistered names (e.g.
localhost) to IP addresses (e.g. 127.0.0.1)

So, by the time your browser is done with it, the URL
http://localhost/myFile.html has been translated to 'use http protocol',
talking to 127.0.0.1 port 80, and ask for the 'myFile.html' file.

Now, your web server has a concept of a document root, that is the
directory where all the web pages and supporting files on kept. On your
system, this is set to /var/www/html. So when the server sees the
browser's request for the file 'myFile.html' is translates this to 'send
back the contents of file '/var/www/html/myFile.html'.

Another thing about IP aliases. You notice that I said 'unregistered
names'. That is because names like 'www.ibm.com' are registered to be
universally (sometimes called globally) unique and are defined in a
world-wide database know as the 'name service'.

If you want to know more about this side of things, check out programs
like 'bind' and files like '/etc/resolv.conf'.

-david-

Dec 28 '05 #14

P: n/a
Michael wrote:
Jerry and Gordon,

Thank you for taking the time to write out these superb explanations; I
and I'm sure others appreciate it. Just a few more question, if I
may...

1. How does the browser know that it should translate
http:/localhost/myFile.html into /var/www/html/myFile.html? Is there an
ini file somewhere that contains this information?

2. This thread got started because the first mysql call (see below)
failed, but when I changed localhost to 127.0.0.1, as in the second
call, it worked. Given the information I have from you now, I don't see
how the first call could ever work, although the documentation says
that it should. Any ideas?

$conn = mysql_connect("localhost", "root", ""); // This fails.
$conn = mysql_connect("127.0.0.1", "mike", ""); // This works.

Thanks again for your help,
M. McDonnell


Michael,

The browser doesn't know - and doesn't care. This is all done by the
web server (Apache, IIS, Tomcat, etc.), based on settings in the browser.

As for your problem - you've changed your signon id, also. In localhost
it is "root", but when you connect to 127.0.0.1, you use "mike".

In neither case do you have a password listed - so if "root" has no
password (very bad idea), it should work also.

Remember - the internet works only on ip addresses. It doesn't know
anything about "localhost", "example.com" or whatever. But your HOSTS
file should provide the translation from "localhost" to 127.0.0.1 for you.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Dec 28 '05 #15

P: n/a
Thank you all vey much for answering these questions so clearly. Your
help is greatly appreciated.

Jerry, regarding the different user name in the mysql_connect call, I
meant to write it as shown below, but I tried "root" as the user also
with the same results. Still a bit of a mystery to me.

$conn = mysql_connect("localhost", "mike", ""); // This fails.
$conn = mysql_connect("127.0.0.1", "mike", ""); // This works.

Dec 28 '05 #16

P: n/a
Michael wrote:
Hi,

I'm trying to do something which should be very simple - connect to the
MySQL database. Here is the call, followed by the error msg.

$conn = mysql_connect("localhost", "root", "");

Warning: mysql_connect(): Can't connect to local MySQL server through
socket '/var/lib/mysql/mysql.sock' (13) in /var/www/html/buildhw.php on
line 13
failed

I've also tried this with the host arg as "", but I get the same
result. Logging on as root made no difference either.

Any help would be very much appreciated.

Thanks,
M. McDonnell

I am having the "same" issue on my server. WinXP Pro, Apache 2.0.55,
MySQL 4.1.15, PHP 5.11.

I have read the whole thread, is there anyone who can point out some
obvious Windows issues, besides PATH= and the location of the .dll.

In addition, PHPinfo does not list MySQL extension as loaded...so
totally perturbed at this point...

--
J Pribe

www.jpribe.com
nntp |at| jpribe |dot| com
Dec 29 '05 #17

P: n/a
Michael wrote:
Jerry and Gordon,

Thank you for taking the time to write out these superb explanations; I
and I'm sure others appreciate it. Just a few more question, if I
may...

1. How does the browser know that it should translate
http:/localhost/myFile.html into /var/www/html/myFile.html? Is there an
ini file somewhere that contains this information?

2. This thread got started because the first mysql call (see below)
failed, but when I changed localhost to 127.0.0.1, as in the second
call, it worked. Given the information I have from you now, I don't see
how the first call could ever work, although the documentation says
that it should. Any ideas?

$conn = mysql_connect("localhost", "root", ""); // This fails.
$conn = mysql_connect("127.0.0.1", "mike", ""); // This works.

Thanks again for your help,
M. McDonnell

OK, so it's connecting through the TCP/IP port, but not through your socket.

Does /var/lib/mysql/mysql.sock exist? If not, where is mysql.sock?

Note in some versions it might be mysqld.sock.

Also, some distros use a different configuration file. I know debian
does, for instance. But I'm not sure which distro you're using, or if
you're using the MySQL package for your distro.

In any case, this looks like a mysql problem more than a PHP one. You
might be better off following up in comp.databases.mysql, where the
mysql gurus hang out.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Dec 29 '05 #18

P: n/a
JPribe wrote:
Michael wrote:
Hi,

I'm trying to do something which should be very simple - connect to the
MySQL database. Here is the call, followed by the error msg.

$conn = mysql_connect("localhost", "root", "");

Warning: mysql_connect(): Can't connect to local MySQL server through
socket '/var/lib/mysql/mysql.sock' (13) in /var/www/html/buildhw.php on
line 13
failed

I've also tried this with the host arg as "", but I get the same
result. Logging on as root made no difference either.

Any help would be very much appreciated.

Thanks,
M. McDonnell

I am having the "same" issue on my server. WinXP Pro, Apache 2.0.55,
MySQL 4.1.15, PHP 5.11.

I have read the whole thread, is there anyone who can point out some
obvious Windows issues, besides PATH= and the location of the .dll.

In addition, PHPinfo does not list MySQL extension as loaded...so
totally perturbed at this point...

Things to check:
1. In php.ini:
- extension=mysql.dll and/or extension=mysqli.dll are uncommented
- extension_dir is set to the path to the ext directory (e.g. C:\php\ext)
2. In apache2/bin:
- libmysql.dll has been copied here from the mysql directory
3. You can connect to mysql from the command prompt: (e.g. mysql -uxxx
-pxxx)

-david-

Dec 29 '05 #19

P: n/a
Michael wrote:
Hi,

I'm trying to do something which should be very simple - connect to the
MySQL database. Here is the call, followed by the error msg.

$conn = mysql_connect("localhost", "root", "");

Warning: mysql_connect(): Can't connect to local MySQL server through
socket '/var/lib/mysql/mysql.sock' (13) in /var/www/html/buildhw.php on
line 13
failed

I've also tried this with the host arg as "", but I get the same
result. Logging on as root made no difference either.

Any help would be very much appreciated.

Thanks,
M. McDonnell


Hi,

are you sure MySQL is running? Try mysqladmin -version and direct connect
with the mysql shell command.

I know e.g. early versions of SuSE Linux did not automatically start the
service.

What is your OS and what version of MySQL are you using?

Regards,
Lars
Dec 29 '05 #20

This discussion thread is closed

Replies have been disabled for this discussion.