user1956177
user1956177

Reputation: 31

Trouble with PHP $connection command and MySQL

Probably just because I'm not experienced with this sort of thing, but I downloaded MySQL with Apache running, and I'm working with the following code in a PHP file:

public static $connection = NULL;

private static $host = MYSQL_HOST;
private static $username = MYSQL_USER;
private static $passwd = MYSQL_PASS;
private static $db = MYSQL_DBNAME;

public static function init() {
  if(!self::$connection) {
    self::$connection = new mysqli(self::$host, self::$username, self::$passwd, self::$db);
}

}

It comes up with this when I open it in Firefox:

trying to connect via unix:///var/mysql/mysql.sock (says this doesn't exist—which, it doesn't)

I replaced MYSQL_HOST with 'localhost', MYSQL_USER with both 'mysql' and 'root' (stupid, yes), MYSQL_PASS with both my system password and NULL, and MYSQL_DBNAME with a few more things (I am having trouble finding out what my database name is called, even with MySQLWorkbench...I started learning this entire field of computing two days ago). It does say that a MySQL server is running on my machine, just not sure how to put the legos together here. Most of the settings are default (port 3306 and such). A test database migration over MySQLWorkBench failed (something to do with not reading the number of rows correctly), but otherwise it was fine and dandy, from what I saw.

Any help would be very welcome!

Upvotes: 1

Views: 166

Answers (2)

Arjan
Arjan

Reputation: 9874

When you specify localhost as the host name, your computer will try to access the MySQL server using sockets in /var/mysql/mysql.sock. Since that file does not exist, this won't work. However, when you specify 127.0.0.1 as host name, the MySQL connection is set up over TCP/IP.

See also this question.

So the answer is to either find where MYSQL_HOST is defined and change it to be 127.0.0.1, or forget about MYSQL_HOST and just enter 127.0.0.1 instead. The latter is harder to maintain in case you would want to move your site to some other location (server).

Upvotes: 1

DWright
DWright

Reputation: 9500

Try restarting SQL server. This may recreate the missing .sock file. See here for info on restarting: http://theos.in/desktop-linux/tip-that-matters/how-do-i-restart-mysql-server/

Upvotes: 0

Related Questions