16

I have just installed a MySQL server (version 3.23.58) on an old RedHat7. I cannot install a more recent MySQL version because of the dependencies. I cannot update librairies on this RedHat server.

However, I have a problem connecting to the database with PHP. First I used PDO but I realized that PDO was not compatible with MySQL 3.23...

So I used mysql_connect(). Now I have the following error:

Warning: mysql_connect(): No such file or directory in /user/local/apache/htdocs/php/database.php on line 9
Error: No such file or directory

My code is:

$host = 'localhost';
$user = 'root';
$password = '';
$database = 'test';
$db = mysql_connect($host, $user, $password) or die('Error : ' . mysql_error());
mysql_select_db($database);

I checked twice that the database exists and the login and password are correct.

This is strange because the code works fine on my Windows PC with Wampp. I cannot figure out where the problem comes from.

Any idea?

Maxbester
  • 2,435
  • 7
  • 42
  • 70
  • 2
    Start using PDO or mysqli_* instead of mysql_* – Leri Sep 25 '12 at 14:10
  • 8
    @PLB: Have you read the question or just the code? – Madara's Ghost Sep 25 '12 at 14:12
  • 3
    If you insist on using MySQL 3.x and with it the `mysql_*` functions, which soon will be deprecated, you may want to check in your `php.ini` whether the `php_mysql` extension is enabled. – Havelock Sep 25 '12 at 14:12
  • 2
    I really hope for your sake this system isn't exposed to the internet. It sounds like it could be cracked in ten seconds flat. MySQL 3.2 is extremely old and full of holes, and RedHat 7 can't be patched to be secure, it's just not possible. Both of these pieces of software are over twelve years old. – tadman Sep 25 '12 at 14:15
  • 1
    a couple of suggestions; try `mysqli_connect()` just incase there is a difference. second, try connecting to `127.0.0.1`. Third, open a command prompt and run `which mysql`, then check the processlist with `ps -ef | grep mysql` to check that it's installed and running. the `no such file or directory` seems to suggest that mysql is not where PHP thinks it is, or it isn't installed or is misconfigured. – totallyNotLizards Sep 25 '12 at 14:17
  • @Maxbester what output did you get from `which mysql` ? also, if you run `mysql -hlocalhost -uroot -p test` from the server does it work? – totallyNotLizards Sep 25 '12 at 14:27
  • @jammypeach: MySQL is running Havelock: I am going to check this file deeply but I am not sure it will solve the problem. tadman: Of course this computer is not connected to the Internet :) – Maxbester Sep 25 '12 at 14:27
  • @jammypeach: Yes `mysql -hlocalhost -uroot -p test` does work. This is my only mean to run SQL statements so it was the first thing I checked. – Maxbester Sep 25 '12 at 14:30
  • this is a horrible idea and if you do it a kitten will die, but you could run the mysql commands directly using `shell_exec()`. However like I said **DO NOT DO THIS**, not only will a kitten die but your soul will be claimed by microsoft and licensed back to you for an increasing anual sum. And your brain will BSOD. I post this only in the event of the world ending and `shell_exec()` being the only way left to connect to the database of apocalypse averting information. – totallyNotLizards Sep 25 '12 at 14:36
  • I think we have misunderstood each other. Why are you talking of `shell_exec()`?? Could anyone come back to my problem? – Maxbester Sep 25 '12 at 14:42
  • @Maxbester it was just an idea for a workaround, using the `shell_exec()` command you can execute mysql commands as if in the shell. I added it only to show a way of getting around this problem but as I've said, it's very unadvisable. – totallyNotLizards Sep 25 '12 at 18:38
  • Possible duplicate of [Warning: mysql\_connect(): \[2002\] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in](http://stackoverflow.com/questions/4219970/warning-mysql-connect-2002-no-such-file-or-directory-trying-to-connect-vi) – azerafati May 20 '16 at 13:01

6 Answers6

42

Yes you can not connect like that!

@PLB and @jammypeach mysqli is after v4.1, he is using v3 :) Guys read the specs, if you want really to help!

You can't connect, because your socket file is a bit wrong. I remember now that the old RH had this issue before. Your socket is probably as /var/mysql/mysql.sock or /tmp/mysql.sock but one or more apps are looking for the other.

If yours is /tmp/mysql.sock but no /var/mysql/mysql.sock you should:

cd /var 
mkdir mysql
cd mysql
ln -s /tmp/mysql.sock mysql.sock

If you have /var/mysql/mysql.sock but no /tmp/mysql.sock then:

cd /tmp
ln -s /var/mysql/mysql.sock mysql.sock

You'll need permissions to make the changes. Just sudo, if needed before the commands above!

ANOTHER SOLUTION (easier):

Create file and call phpinfo(); Look for 'mysql.default_socket'; or 'pdo_mysql.default_socket'; Open My.ini or My.cnf find the socket value e.g. socket=/tmp/mysql.sock Open your php.ini file (which is also found on your phpinfo() page as ‘Loaded Configuration File‘) and change all the occurrences of the incorrect socket location to the correct socket location from MySQL.

ANOTHER SOLUTION (easiest): DSN for PDO:

mysql:unix_socket=/tmp/mysql.sock;dbname=...

mysql_connect:

$db = mysql_connect('localhost:/tmp/mysql.sock', ...

Your system is really scary when it comes to security, if you're hosting sensitive data, I'd upgrade to the latest versions.

---- UPDATE ----

Aaahhhh PHP 5.0 and MySQL 3.23 :)

PHP 5 has a mysql client packaged that cannot connect to a MySQL database less than version 4.1. Starting with version 4.1, MySQL uses a new way of password hashing that is not compatible with pre-4.1 databases. The server your configuration is connecting to is version 3.23. So you need to get yourself a higher version of MySQL. Sorry, but there is no other practical solution for your case. If I was you, I'd upgrade the whole system and install the most recent OS version, if I had to I'd go with Debian and the most recent stable versions of PHP and MySQL.

GTodorov
  • 1,993
  • 21
  • 24
  • 2
    Well spotted with the specs - in my defence though, I posted those suggestions as a comment because I wasn't sure enough to post an answer. If I'd had the time to read the specs for that version of MySQL I'd probably have posted an answer too ;) – totallyNotLizards Sep 25 '12 at 14:31
  • 1
    Try the update! My bad. @jammypeach Take my apology, didn't want to insult you! – GTodorov Sep 25 '12 at 14:59
  • @GTodorov There is no socket in /tmp and the directory /var/mysql doesn't exist. Is it wrong? In the phpinfo page, mysql.default_socket has no value. By the way, don't worry for the security issues. This computer is not connected to a public network and there is absolutely no important data in it. I am aware of the lack of security ;) – Maxbester Sep 25 '12 at 15:56
  • 1
    Aaahhhh PHP 5.0 :) PHP 5 has a mysql client packaged that cannot connect to a MySQL database less than version 4.1. Starting with version 4.1, MySQL uses a new way of password hashing that is not compatible with pre-4.1 databases. The server your configuration is connecting to is version 3.23. So, you need to get yourself a higher version of MySQL. – GTodorov Sep 25 '12 at 16:21
  • @GTodorov alright! Unfortunately I cannot update the OS version. But I can change the PHP version! I know this is not recommended. Do you know which PHP version would be compatible with MySQL 3.23? Thanks! – Maxbester Sep 26 '12 at 06:39
  • Anything earlier than PHP 4.2 – GTodorov Sep 26 '12 at 14:28
  • Thanks! I hope the documentation is still available! – Maxbester Sep 28 '12 at 09:57
  • I am not sure if you'll be able to find any docs for installing php4 it should be pretty much the same as php5. Take a look here: http://us2.php.net/manual-lookup.php?pattern=php+4&lang=en&scope=quickref – GTodorov Sep 28 '12 at 18:59
  • 2
    This information was the key to my success using MariaDB. phpMyAdmin connected but `mysqli_connect()` wouldn't. I got the expected location of the socket file using `phpinfo()` above and created a symlink (`ln -s ...`) to match the `socket` variable as reported by phpMyAdmin. Thanks, @GTodorov! – Stephan Samuel May 15 '18 at 00:14
3

Fortunatly this is worked well for me:

$db = mysql_connect('localhost:/var/lib/mysql/mysql.sock', 'Username', 'Password');
Sina
  • 431
  • 4
  • 7
2

On a MAC OSX just restart the mysql server

sudo /usr/local/mysql/support-files/mysql.server restart

It worked for me

Shashank Agarwal
  • 712
  • 8
  • 14
2

I had a similar issue that took me several hours to solve. I placed Mysql / Mariadb on a separate drive and the PHP driven website was looking for the socket in the default location. So, I created a symbolic link but I think SELinux would not give the permission. So, then I added these lines to the /etc/php.ini file and it works.

pdo_mysql.default_socket = /data/mysql/mysql.sock
mysql.default_socket = /data/mysql/mysql.sock
mysqli.default_socket = /data/mysql/mysql.sock
Code_Help
  • 303
  • 3
  • 13
1

I use MAMP and has the sample problem. I found that there are no mysql.sock in tmp. so use this code

cd /tmp && ln -s /Applications/MAMP/tmp/mysql/mysql.sock

It worked for me

lzdaniel
  • 11
  • 1
1

Instead of using localhost, use 127.0.0.1 as your host.

$host = '127.0.0.1';
$user = 'root';
$password = '';
$database = 'test';
$db = mysqli_connect($host, $user, $password) or die('Error : ' . mysqli_error());
$tb = mysqli_select_db($db,$database);