Im trying to connect to mysql locally using apache and php. I can connect to mysql using mysql workbench and i can use php with apache, but i can't connect to mysql using php. When i try i get the following:
Warning: mysql_connect(): Connection refused in /Library/WebServer/Documents/login.php on line 25
This is the code:
mysql_connect($connection= mysql_connect("localhost", "RainyCats", "********");
I have also tried it with the root:
mysql_connect($connection= mysql_connect("localhost", "root", "********");
The mysql preference pane in system preferences shows that the server is running, i have granted all privileges to RainyCats.
I have created the symbolic link to the mysql.sock from /private/var/mysql/mysql.sock to /tmp/mysql.sock The my.cnf file is located in etc/my.cnf
I installed mysql using the dmg.
EDIT: I have php version: 5.4.24 and Apache: 2.2.26
Thank you, Rainy
I had the same problem on Mountain Lion OS, it's probably a socket error behind this. It seems that for local servers on Macos, you've got to specify what socket file your mysql-server is using, to your php code:
So first, you will tell me "OK but where the hell is my socket file ?":
$prompt> mysql -u myusername -p (enter the password here) mysql> show variables like '%socket%'; +---------------+-----------------+ | Variable_name | Value | +---------------+-----------------+ | socket | /tmp/mysql.sock | +---------------+-----------------+ 1 row in set (0.00 sec)
Once you've got the path, use it in your php code:
$username = 'myusername'; $password = 'mypassword'; $host = 'localhost:/tmp/mysql.sock'; $database = 'mydatabase'; #connect to the database or die $link = mysql_connect($host, $username, $password); if (!$link) { die('Could not connect: ' . mysql_error()); } mysql_select_db ($database);
Thanks to Alvin, I found this solution on his website :
Hope this helps...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.