php - mysql_connect(): No such file or directory
Get the solution ↓↓↓Solution:
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.
Answer
Solution:
Fortunatly this is worked well for me:
$db = mysql_connect('localhost:/var/lib/mysql/mysql.sock', 'Username', 'Password');
Answer
Solution:
On a MAC OSX just restart the mysql server
sudo /usr/local/mysql/support-files/mysql.server restart
It worked for me
Answer
Solution:
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
Answer
Solution:
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
Answer
Solution:
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);
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: script cache:clear returned with error code 255
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.