This error means that you are using localhost as your database hostname. Please check the control panel of your hosting account to see the actual database hostname you should use.
mysql_connect() establishes a connection to a MySQL server. The following defaults are assumed for missing optional parameters: server = 'localhost:3306', username = name of the user that owns the server process and password = empty password. The server parameter can also include a port number.
MySQLi Procedural Query php $servername = "localhost"; $username = "username"; $password = "password"; $db = "dbname"; // Create connection $conn = mysqli_connect($servername, $username, $password,$db); // Check connection if (!$
Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in. Since PHP 5.5 has removed support for mysql extension in favor of mysqli. It's highly recommended to upgrade to phpGrid 6.0 to address mysql extension deprecation.
For some reason mysql on OS X gets the locations of the required socket file a bit wrong, but thankfully the solution is as simple as setting up a symbolic link.
You may have a socket (appearing as a zero length file) as /tmp/mysql.sock
or /var/mysql/mysql.sock
, but one or more apps is looking in the other location for it. Find out with this command:
ls -l /tmp/mysql.sock /var/mysql/mysql.sock
Rather than move the socket, edit config files, and have to remember to keep edited files local and away from servers where the paths are correct, simply create a symbolic link so your Mac finds the required socket, even when it's looking in the wrong place!
If you have /tmp/mysql.sock
but no /var/mysql/mysql.sock
then...
cd /var
sudo mkdir mysql
sudo chmod 755 mysql
cd mysql
sudo 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 will need permissions to create the directory and link, so just prefix the commands above with sudo if necessary.
I also had this error, but could only fix it through the suggestion here.
To summarize, use:
127.0.0.1
Instead of:
localhost
The reason is that "localhost" is a special name for the MySQL driver making it use the UNIX socket to connect to MySQL instead of the a TCP socket.
I was having the same problem and this is how I fixed it:
I had this and it didn't work:
$con = mysql_connect('localhost', 'root', '1234');
I did this and it worked:
$con = mysql_connect(':/Applications/MAMP/tmp/mysql/mysql.sock', 'root', '1234');
Instead of using the mysql server, I connected directly to the Unix Socket. Worked for me.
MySQL socket is located, in general, in /tmp/mysql.sock
or /var/mysql/mysql.sock
, but probably PHP looks in the wrong place.
Check where is your socket with:
sudo /usr/libexec/locate.updatedb
When the updatedb is terminated:
locate mysql.sock
Then locate your php.ini:
php -i | grep php.ini
this will output something like:
Configuration File (php.ini) Path => /opt/local/etc/php54
Loaded Configuration File => /opt/local/etc/php54/php.ini
Edit your php.ini
sudo vim /opt/local/etc/php54/php.ini
Change the lines:
pdo_mysql.default_socket=/tmp/mysql.sock
mysql.default_socket=/tmp/mysql.sock
mysqli.default_socket = /tmp/mysql.sock
where /tmp/mysql.sock is the path to your socket.
Save your modifications and exit ESC + SHIFT: x
Restart Apache
sudo apachectl stop
sudo apachectl start
I am on XAMPP on Mac OS X, and Brian Lowe's solution above worked with a slight modification.
The mysql.sock file is actually in "/Applications/xampp/xamppfiles/var/mysql/" folder. So had to link it up both in /tmp and /var/mysql. I haven't checked which one is used by PHP command line, but this did the fix, so I am happy :-)
sudo su
ln -s /Applications/xampp/xamppfiles/var/mysql/mysql.sock /tmp/mysql.sock
mkdir /var/mysql
ln -s /Applications/xampp/xamppfiles/var/mysql/mysql.sock /var/mysql/mysql.sock
Mac OS X EL Capitan + MAMP Pro Do this
cd /var
sudo mkdir mysql
sudo chmod 755 mysql
cd mysql
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock mysql.sock
Then do this
cd /tmp
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock mysql.sock
Hope this saves you some time.
The reason is that php cannot find the correct path of mysql.sock
.
Please make sure that your mysql is running first.
Then, please confirm that which path is the mysql.sock
located, for example /tmp/mysql.sock
then add this path string to php.ini:
Finally, restart Apache.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With