Link to home
Start Free TrialLog in
Avatar of pieter78
pieter78

asked on

MySQL error 2002 - Can't connect (...) through socket '/tmp/mysql.sock'

Trying to install a local webserver for development purposes.. I am a little experienced in PHP/MySQL programming, but so far I did it under windows.. however, as I need to run a .NET localhost under WinXP now, I made a dual boot with SuSE 9.0 and tried to configure Apache/PHP/MySQL there... in the end the best solution I can think of, but MySQL is driving me crazy. I know the question has been asked many times before (here & everywhere), I know mysql.com has an online manual with suggestions, but so far, nothing helped... Here we go:

Software = Linux kernel 2.4.21 + Apache 2.0.49 + PHP 4.3.5 + MySQL 4.0.18
Apache/PHP installation as described on: http://hulan.info/blog/item/install-compile-apache-2-php-4-3-5-and-mysql-under-linux

Apache and PHP work perfect, phpinfo() runs smooth... but MySQL gives the 2002 error as described in the title: Can't connect (...) through socket '/tmp/mysql.sock'

My socket-file mysql.sock is located in /var/lib/mysql/mysql.sock and I already tried to change the [client] and [mysqld] settings in /etc/my.cnf without result. Of course, in /tmp there is no socket-file.

As I started installation with the MySQL RPM's (server + client + shared), later I tried a little with the .tar.gz file (I guess it is the binary) to play with the chown options as described in the manual. However, I got it running in the shell with the "mysqld_safe &" command from /usr/bin, but once when experimenting I assigned a password as was described, so now I have to use this:
mysqladmin -u root version -p
(enter password)

and then I finally get the message that it is running, UNIX socket /var/lib/mysql/mysql.sock and it also gives the uptime.

Now my question: I used phpmyadmin under windows, and it worked. But now, when I do so here, I still get the error 2002 (socket in /tmp!!) although mysqladmin shows me an uptime and so on (socket in /var/lib/mysql)... I don't understand it anymore.

1) WHY?? How can I get a normal mysql startup (I am not sure if mysqld_safe is the regular way, guess not)
2) What is wrong with my setup so that I cannot get to my db from PHP? (I did not change the socket settings in config.inc.php as I never had to do before on both localhost under windows, and remote host on the web)
3) And besides, how can I get rid of the password here, I want mysqladmin to show me the things immediately (just for testing on localhost, as stated before)

Any good help would really be appreciated!! Many thanks in advance!!
SOLUTION
Avatar of paullamhkg
paullamhkg

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of paullamhkg
paullamhkg

by checking in www.mysql.com for error 2002, got something for you http://dev.mysql.com/doc/mysql/en/Can_not_connect_to_server.html which may help :)
Avatar of pieter78

ASKER

Hi, thanx 4 reacting...

My mysql-dir is located /var/lib/mysql
There, I cannot find the mysql_install_db

However, if I run "whereis mysql_install_db" (as root) I get the /usr/bin path, so I changed dir to there.

Then entered as a command just "mysql_install_db" (I am not sure if I get the difference exactly between let's say "command"  and "./command" so I just used the first way here)

Here is the shell output (my pc is named walhalla):
---------------------------------------------------------
walhalla:/usr/bin # mysql_install_db
Installing all prepared tables
040510 21:06:36  /usr/sbin/mysqld: Shutdown Complete


To start mysqld at boot time you have to copy support-files/mysql.server
to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h walhalla password 'new-password'
See the manual for more instructions.

NOTE:  If you are upgrading from a MySQL <= 3.22.10 you should run
the /usr/bin/mysql_fix_privilege_tables. Otherwise you will not be
able to use the new GRANT command!

You can start the MySQL daemon with:
cd / ; /usr/bin/mysqld_safe &

You can test the MySQL daemon with the benchmarks in the 'sql-bench' directory:
cd sql-bench ; perl run-all-tests

Please report any problems with the /usr/bin/mysqlbug script!

The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at https://order.mysql.com

walhalla:/usr/bin #
---------------------------------------------------------

And this is the output of "mysqladmin version":

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
walhalla:/usr/bin # mysqladmin version -u root -p
Enter password:
mysqladmin  Ver 8.40 Distrib 4.0.18, for pc-linux on i686
Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL license

Server version          4.0.18-standard
Protocol version        10
Connection              Localhost via UNIX socket
UNIX socket             /var/lib/mysql/mysql.sock
Uptime:                 20 sec

Threads: 1  Questions: 1  Slow queries: 0  Opens: 6  Flush tables: 1  Open tables: 0  Queries per second avg: 0.050
walhalla:/usr/bin #
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Maybe you can already see something from there, I don't know where to look anymore. The other page you advised me, was one I saw before when browsing the MySQL help files. From there, I set the password, I thought ;-)  But it didn't gave me what I thought...

As you can see, I get an uptime from the server here, Further, when I am playing around, I can use "mysql -u root -p" to login, and e.g. executing "SHOW DATABASES;" gives me a nice overview like this:

********************************************
mysql> show databases;
+----------+
| Database |
+----------+
| d_test   |
| mysql    |
| pieter   |
| test     |
+----------+
4 rows in set (0.01 sec)

mysql>

********************************************

Maybe I am looking in the wrong direction, this is something I already saw before (which means, I could already login to mysql in the shell). Just from there I cannot get into it using PHP (as far as I tried: phpMyAdmin).

Looking 4wd to read your thougths...
Btw, of course, before I run "mysqladmin version" I started the MySQL engine :-)   I used "mysqld_safe &" in the /usr/bin path as described.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Was worth the try... I used "whereis php.ini" to find it in /etc. So I opened /etc/php.ini (using vim) and found the line...
it was already there exactly as you stated. So, unfortunately, it did not help.

I tried to create a database using a PHP file (to exclude a wrong config of phpMyAdmin), but still get the same error... it keeps looking for /tmp/mysql.sock although I do not know where else I can redirect or tell my pc it has to look somewhere else.

As stated, problem occurs only with PHP, from the shell I can start the daemon with "mysqld_safe &" and login to mysql.  Any other ideas (just mention it, I am desperate enough to give it a try  ;-)  ) ??
Sorry just back to a short business trip to China.

try to reconfigure your php again, this time try to use the php.tar.gz to install

                $ ./configure  --with-apxs=/apache/bin/apxs --enable-versioning --with-mysql \
                $ make
                $ make install

and try again this should work,
I can try again in the weekend, these days I am unfortunately running out of time myself... but let me just show you how I installed PHP, because I used the tar.gz file with the instructions as provided in the page I linked in my question all above:

-----------------------------------------
PHP 4 (as Apache 2 server module!):

tar -zxvf php-4.3.5.tar.gz
cd ../php-4.3.5
./configure \
  --with-apxs2=/usr/local/apache2/bin/apxs \
  --with-mysql \
  --with-iconv=/usr/local \
  --enable-track-vars \
  --with-gettext \
  --with-config-file-path=/usr/local/apache2/conf \
  --enable-trans-id
make
make install
cp php.ini-dist /usr/local/apache2/conf/php.ini
-----------------------------------------

Maybe this already makes some things clear to you (at least, I guess it makes more sense to you than it does to me ;-) )
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
pieter78
try compile in this way to see it can help

./configure \
  --with-apxs2=/usr/local/apache2/bin/apxs \
  --with-mysql=/path/to/mysql \                               <-- here to tell php where to find myql, normal no need to do this but have a try
  --with-iconv=/usr/local \
  --enable-track-vars \
  --with-gettext \
  --with-config-file-path=/usr/local/apache2/conf \
  --enable-trans-id
YES!!! It works.... these two were the answer to the problem
- in php.ini: mysql.default_socket =  /var/lib/mysql/mysql.sock
- As I read you configure option, you did that, so you need to edit
  /usr/local/apache2/conf/php.ini

Indeed... I already read this tip one the MySQL site, but I always looked in the wrong pat... although my phpino() is clear about this, but I looked over it... :-)     I admit, I am blonde!

Just one small question left: what is the best way to start MySQL? Is it "mysqld_safe &" (I could make an entry via WebMin so it starts automatically) or should I use something different? It is just meant for debugging purposes (development), not for a real webserver.
(yes, just after install there were auto-start entries, but I once deleted them during bug-hunting...)
and, just in case: what command is used to stop the MySQL server?
MySQL comes with a default rc-script named mysql.server or mysql.server.sh.
Use that with start or stop argument to start/stop your mysqld (it calls mysqld_safe itself).
Fantastic! Now it all works like I had it in mind when I started... many many thanks!!
Hi there!

I have had much the same problem with my MySQL installation. I continually get the "Can't connect" error, sometimes with Err 13 and sometimes with Error 2 depending on how I try to connect.

I can't use the mysqladmin tool - I get this:
[root@tiber mysql]# ./bin/mysqladmin version -u root -p
Enter password:
./bin/mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)'
Check that mysqld is running and that the socket: '/tmp/mysql.sock' exists!

If I do a ps -ax | grep sql I get this:
 1809 ?        S      0:00 /bin/sh ./bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/tiber.tgis.co.uk.pid
 1851 ?        S      0:00 /usr/local/mysql/bin/mysqld --defaults-extra-file=/usr/local/mysql/data/my.cnf --basedir=/usr/local/mysql --datadir=/var/lib/mysql --user=mysql --pid-file=/var/lib/mysql/tiber.tgis.co.uk.pid --skip-locking --socket=/var/lib/mysql/mysql.sock
 1861 ?        S      0:00 /usr/local/mysql/bin/mysqld --defaults-extra-file=/usr/local/mysql/data/my.cnf --basedir=/usr/local/mysql --datadir=/var/lib/mysql --user=mysql --pid-file=/var/lib/mysql/tiber.tgis.co.uk.pid --skip-locking --socket=/var/lib/mysql/mysql.sock
 1862 ?        S      0:00 /usr/local/mysql/bin/mysqld --defaults-extra-file=/usr/local/mysql/data/my.cnf --basedir=/usr/local/mysql --datadir=/var/lib/mysql --user=mysql --pid-file=/var/lib/mysql/tiber.tgis.co.uk.pid --skip-locking --socket=/var/lib/mysql/mysql.sock
 1863 ?        S      0:00 /usr/local/mysql/bin/mysqld --defaults-extra-file=/usr/local/mysql/data/my.cnf --basedir=/usr/local/mysql --datadir=/var/lib/mysql --user=mysql --pid-file=/var/lib/mysql/tiber.tgis.co.uk.pid --skip-locking --socket=/var/lib/mysql/mysql.sock
 1864 ?        S      0:00 /usr/local/mysql/bin/mysqld --defaults-extra-file=/usr/local/mysql/data/my.cnf --basedir=/usr/local/mysql --datadir=/var/lib/mysql --user=mysql --pid-file=/var/lib/mysql/tiber.tgis.co.uk.pid --skip-locking --socket=/var/lib/mysql/mysql.sock
 1865 ?        S      0:00 /usr/local/mysql/bin/mysqld --defaults-extra-file=/usr/local/mysql/data/my.cnf --basedir=/usr/local/mysql --datadir=/var/lib/mysql --user=mysql --pid-file=/var/lib/mysql/tiber.tgis.co.uk.pid --skip-locking --socket=/var/lib/mysql/mysql.sock
 1949 ?        S      0:01 /usr/local/mysql/bin/mysqld --defaults-extra-file=/usr/local/mysql/data/my.cnf --basedir=/usr/local/mysql --datadir=/var/lib/mysql --user=mysql --pid-file=/var/lib/mysql/tiber.tgis.co.uk.pid --skip-locking --socket=/var/lib/mysql/mysql.sock
 1950 ?        S      0:00 /usr/local/mysql/bin/mysqld --defaults-extra-file=/usr/local/mysql/data/my.cnf --basedir=/usr/local/mysql --datadir=/var/lib/mysql --user=mysql --pid-file=/var/lib/mysql/tiber.tgis.co.uk.pid --skip-locking --socket=/var/lib/mysql/mysql.sock
 1953 ?        S      0:00 /usr/local/mysql/bin/mysqld --defaults-extra-file=/usr/local/mysql/data/my.cnf --basedir=/usr/local/mysql --datadir=/var/lib/mysql --user=mysql --pid-file=/var/lib/mysql/tiber.tgis.co.uk.pid --skip-locking --socket=/var/lib/mysql/mysql.sock
 1960 ?        S      0:00 /usr/local/mysql/bin/mysqld --defaults-extra-file=/usr/local/mysql/data/my.cnf --basedir=/usr/local/mysql --datadir=/var/lib/mysql --user=mysql --pid-file=/var/lib/mysql/tiber.tgis.co.uk.pid --skip-locking --socket=/var/lib/mysql/mysql.sock
 2284 pts/0    S      0:00 grep sql

Strangely, the my.cnf file doesn't exist in the /usr/local/mysql/data directory at all - it's in /etc

I I try and connect using Apache to the SQL database, I get this:

Warning!
The following errors must be rectified before continuing!
Please go back and try again!

Connection error:
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (13)

Stumped, confused and VERY frustrated! Hope someone can help.

Regards
Chris
kenwardc ,
read seriously what you've posted, in particular anything about "socket", and you clearly see what's wrong ;-)
Hi there

Sorry - you have me baffled. I'm very new to Linux (Windows is my experise) so can't see anything in what you said that can help me? Could I have some more clues please? <g>

Regards
Chris
>  I'm very new to Linux (Windows is my experise)
hmm, AFAIK reading is the same everywhere, and totally OS-independent :-))
If you server is configured to use socket A, and your client uses socket B, then why do you wonder that they complain?

BTW, you should open your own question for this (according EE policies)
Ahoffman

Thanks for your response. I can understand what you mean but I don't know HOW to change the system setup and this is why I asked the question in the first place. Can you help me with the setup here?

Not sure what you mean about "open your own question" - should I have created a brand new topic? If this is the case I'll do that with pleasure. I have only subscribed today to EE so am still learning my way around. Some friendly help would be good.

OK - so how do I change the client? Any help gratefully accepted.

Regards
Chris
even I still gave the answer, somehow ...
please open a new question in this Topic Area (TA): Linux Setup
post your question (like above) there, and you'll get a clear answer
Done! Realise now how this site works. ! ;-)