"Can't connect to local MySQL server through socket '/tmp/mysqld.sock' (2)"

Here's my code:

<?php

$dbhost=$_POST['dbhost'];               // Host name
$dbusername=$_POST['dbusername'];       // Mysql username
$dbpassword=$_POST['dbpassword'];       // Mysql password
$db_name=$_POST['database'];            // Database name
$tbl_name=$_POST['tablename'];          // Table name

$action=$_POST['action'];


if($action == 'connect')
{
    if(mysql_connect("$dbhost", "$dbusername", "$dbpassword") == true)
    {
        if(mysql_select_db("$db_name") == true)
        {
            echo('Connected to Database.');
        }
        else
        {
            die('Error: Can\'t use ' . DB_NAME . ': ' . mysql_error());
        }
    }
    else
    {
        die('Error: Connection failed ' . mysql_error());
    }
}

if($action == 'post')
{
    $avatarname = $_POST["avatarname"];
    // $rank = $_POST['rank'];
    // $status = $_POST['status'];
    echo($avatarname.' loaded into MySQL Database.');

    $sql = "INSERT INTO $tbl_name (avatarname) VALUES ('$avatarname')";
    if (!mysql_query($sql))
    {
        die('Error: ' . mysql_error());
    }
}

if($action == 'done')
{
    echo('Closing connection..');
    mysql_close();
}

?>

It works up the the connection part, and returns "Connected to Database", but then it throws the error.. My hosting site is 1and1.com if that makes any difference.

Thanks in advance for any help, this has really been trying me..

Recommended Answers

All 14 Replies

If you're paying for hosting and if you don't have access to your own MySQL config files, I think you should contact your hosting company and ask them for help. Most solutions that I find on Google (just copy/paste your error into the Google search bar and you'll find plenty) talk about modifying config files.

Thanks minitauros, I was thinking that's probably what it would come down to. I'll give that a try and see what I can find.

^ Agreed :)

BTW - a little off-topic pointer:

Because if(){} looks to see if a statement is true, and will do an else if not, you do not need == true (PHP will do this automatically)

So this:

if(mysql_connect("$dbhost", "$dbusername", "$dbpassword") == true)

Will do the same as this:

if(mysql_connect("$dbhost", "$dbusername", "$dbpassword"))

Little programming trick for you ;P

commented: Yeah, I thinking it was probably redundant but wasn't certain, thank for the tip! :) +0

Well, I got a new host now, Siteground. Just went with what the vast majority reviews of hosts agreed were the best. Liking them a lot better so far, and I did get some errors sorted but, ran into another:

"Access denied for user 'vangua01'@'localhost' (using password: NO)"

The url sends an application/x-www-form-urlencoded string including:
"action=connect" to the databaseinterface script.

The script returns that connection was successful.

The url then sends the application/x-www-form-urlencoded including:
"action=post"&avatarname=(names go here)

The script reads that the action is post and grabs the names from the url, successfully repeating them, but they are never actually put into the database, this is where the error is thrown.

Updated my databaseinterface.php also attempting to fix the error, here's where I am:

<?php

define('DB_NAME', 'vangua01_access_list');
define('DB_USER', 'vangua01_admin2');
define('DB_PASSWORD', 'askljflnfldsakn');
define('DB_HOST', 'localhost');


$action=$_POST['action'];

if($action == 'connect')
{
    $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

    if (!$link)
    {
        die('Connection failed: ' . mysql_error());
    }

    $db_selected = mysql_select_db(DB_NAME, $link);

    if (!$db_selected)
    {
        die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
    }

    else
    {
        echo('Connected to Database.');
    }
}

if($action == 'post')
{
    $avatarname = $_POST["avatarname"];

    echo('Loading '.$avatarname);

    $sql = "INSERT INTO vanguard_access (avatarname) VALUES ('$avatarname')";
    if (!mysql_query($sql))
    {
        die('Error: ' . mysql_error());
    }
}

if($action == 'done')
{
    echo('Closing connection..');
    mysql_close();
}

?>

Thanks guys

The error says that MySQL socket is corrupted. You may ask your server admin to fix it.

  1. The mysql_connect() function returns a resource if the connection succeeded, or boolean false if not.
  2. Use the mysqli api's for MySQL connectivity - the mysql functions are deprecated for more current PHP versions.

Your updated version is more correct. Your problem regarding the "access denied" error is in the permissions in the database for your user ID. This has been a PITA for me as well in the past, and altering the user id location so that instead of @'localhost' it is something like @'*', so you can log in from anywhere.

Please cross check that you are using correct username and password to connect the database server.

You may also need to check with your web host that they host the database on the same server your script is located. Normally, a web host will NOT have their database server on the same IP. You need to get the correct IP instead of "localhost" as you are using now.

I'll give that a try rubberman and see what I come up with.

I have checked repeatedly that all the usernames, passwords, database names, etc, are all correct and correctly assigned. I've deleted and created new users and databases and tables trying different names, still with no luck.

I talked to the SiteGrounds support for a long time last night trying to resolve this issue, unfortunately they don't have enough knowledge of PHP, etc to really help a lot. But I know my host name etc work because I have a seperate database on the same account and site, for my website registration and login and it all works perfectly. MySQL just doesn't seem to like my other script..

Just for clarity's sake, this script is being triggered from an LSL script inside Second Life. I can bounce messages off the site just fine, and receive them back in the game. But, as you can see, when I try to connect and insert users into my database table from Second Life it gives me that error:

"Access denied for user 'vangua01'@'localhost' (using password: NO)"

Does the 'using password: NO' mean that it thinks I'm not sending it a password? Because on my registration script and seperate database for my website, it says:

'using password: YES'

Here's some MySQL documentation I thought may be relevant:

"Make sure that the server has not been configured to ignore network connections or (if you are attempting to connect remotely) that it has not been configured to listen only locally on its network interfaces. If the server was started with --skip-networking, it will not accept TCP/IP connections at all. If the server was started with --bind-address=127.0.0.1, it will listen for TCP/IP connections only locally on the loopback interface and will not accept remote connections."

There is a place in the control panel of my site to allow remote MySQL control from a host, maybe that's it?

This might not be the answer to your question, but I still want to bring it up for clarification.

When you created the database user on your cpanel, did you grant this user the proper privileges?

Is the user allowed to access and have privilege for the database?

Yep. Added to database and assigned all privileges. Even tried multiple times deleting the user and starting all over to no avail.

Well, I tried using my working database and user etc in this script just for a test, still throws the same error..

"Access denied for user 'vangua01'@'localhost' (using password: NO)"

I know it's been a while, but I had the exact same issue. My password was a bit over complicated and that was the problem. I had a pound sign and exclamation point in it. When I made it simpler, it worked fine.

@rookhaven

you can use special characters, but you have to escape, for example instead of:

mysql -uroot -pabc!def

do:

mysql -uroot -pabc\!def

and it will work, bye!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.