简体   繁体   中英

Cannot connect to MySQL database, socket error

I'm new to php , and while trying to make a connection on one of my pages to the database I set up on the phpMyAdmin page of my site.

I get this error:

"Database connection failed: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (46) (2002)"

I don't know what a socket is, or why it's trying to go to what looks like a temp file, so I don't even know where to being to troubleshoot this.

The code I'm using to make the initial connection is this:

<?php
define("DB_SERVER", "localhost");
define("DB_USER", "xxxx");
define("DB_PASS", "xxxx");
define("DB_NAME", "tester");

$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);

// Test if connection occurred.
if(mysqli_connect_errno()) {
    die("Database connection failed: " .
        mysqli_connect_error() .
        " (" . mysqli_connect_errno() . ")"
    );
}
?>

I know I should have access to the MySQL databases, since I logged on and made them myself.

I previously set the page up by using WAMP on my computer, and everything worked fine.

It's just when I tried making it live on the site that I ran into this error.

Any help would be awesome!!

Try using 127.0.0.1 instead of localhost.

If that does not solve it, and you have root access to your server, try the following command

service mysql restart

To restart the mysql server.

The first option will probably work. Again, if you have root access to your server, you should change the mysql config to support sockets, since it's better than the TCP-ip connection.

After contacting my hosting service several times, it seems the error occurred because my hosting service recently changed their specifications and now uses "mysql" instead of "localhost" in the host and server fields. I had used "localhost" before with another host service, so I didn't think to change this, and the latest help articles on my host's website had not updated to reflect this.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM