Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Trouble connecting to my sql db

SkyWalker_89

New member
Joined
Dec 23, 2024
Messages
5
I hope someone can help me here, I recently upgraded my account here and got the possiblity to have a sql database. My problem is that I try to connect to the database using:
$link = mysql_connect( localhost", MyUser", "MyPassW")

I get the following:

Warning: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) in /home/kastir2/public_html/index.php on line 212

Warning: MySQL Connection Failed: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) in /home/kastir2/public_html/index.php on line 212
Unable to connect to database.

Does anyone know why I get this ?
 
The error you're encountering is related to the connection between PHP and MySQL. Let's break down the potential causes and how to fix them:

Issues and Solutions​


  1. Typo in the Connection String
    • Your mysql_connect call has a syntax error:
      $link = mysql_connect( localhost", MyUser", "MyPassW")
      Correct it:
      $link = mysql_connect("localhost", "MyUser", "MyPassW");
  2. MySQL Server Not Running
    • The error suggests that the MySQL server may not be running. To check:
      • On Linux: Run service mysql status or systemctl status mysql.
      • On macOS or Windows: Ensure the MySQL service is active through the respective service manager.
  3. Incorrect MySQL Socket Path
    • PHP is trying to connect via the socket file /tmp/mysql.sock. If your MySQL server is configured to use a different socket path, update your php.ini configuration file:
      mysql.default_socket = /path/to/your/mysql.sock
      Or specify the socket directly in your connection:
      $link = mysql_connect("localhost:/path/to/mysql.sock", "MyUser", "MyPassW");
  4. MySQL User Permissions
    • Verify that the user MyUser has the appropriate permissions to connect to the database from localhost. Use:
      GRANT ALL PRIVILEGES ON *.* TO 'MyUser'@'localhost' IDENTIFIED BY 'MyPassW';<br>FLUSH PRIVILEGES;
  5. Outdated mysql_* Functions
    • The mysql_* functions are deprecated and removed in PHP 7.0+. Consider switching to mysqli_* or PDO:
      $link = mysqli_connect("localhost", "MyUser", "MyPassW");<br>if (!$link) { die("Connection failed: " . mysqli_connect_error());}echo "Connected successfully!";
  6. Database Configuration Check
    • Confirm that your database server's my.cnf or my.ini file contains:
      [mysqld]<br>socket=/tmp/mysql.sock
      Restart the server after editing:
      sudo service mysql restart

Additional Debugging​


  • Logs: Check the MySQL error logs and PHP error logs for additional details.
  • Firewall: Ensure no firewall or SELinux rules block MySQL connections.

Try these steps, and let me know if you are still having trouble.
 
Last edited:
Back
Top