Can't connect to local MySQL server through socket '/var/run

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
D_zone
Forum Newbie
Posts: 14
Joined: Mon Nov 02, 2009 9:22 am

Can't connect to local MySQL server through socket '/var/run

Post by D_zone »

I Have these error and I can't login to my admin page because of that.

Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /var/www.sit.com/lib/adodb5/drivers/adodb-mysql.inc.php on line 364

Fatal error: Call to a member function SetFetchMode() on a non-object in /var/www.sit.com/core/db.php on line 12

adodb-mysql.inc.php

Code: Select all

// returns concatenated string
	// much easier to run "mysqld --ansi" or "mysqld --sql-mode=PIPES_AS_CONCAT" and use || operator
	function Concat()
	{
		$s = "";
		$arr = func_get_args();
		
		// suggestion by andrew005@mnogo.ru
		$s = implode(',',$arr); 
		if (strlen($s) > 0) return "CONCAT($s)";
		else return '';
	}
	
	function OffsetDate($dayFraction,$date=false)
	{		
		if (!$date) $date = $this->sysDate;
		
		$fraction = $dayFraction * 24 * 3600;
		return $date . ' + INTERVAL ' .	 $fraction.' SECOND';
		
//		return "from_unixtime(unix_timestamp($date)+$fraction)";
	}
	
	// returns true or false
	function _connect($argHostname, $argUsername, $argPassword, $argDatabasename)
	{
		if (!empty($this->port)) $argHostname .= ":".$this->port;
		
		if (ADODB_PHPVER >= 0x4300)
			$this->_connectionID = mysql_connect($argHostname,$argUsername,$argPassword,
												$this->forceNewConnect,$this->clientFlags);  LINE 364
		else if (ADODB_PHPVER >= 0x4200)
			$this->_connectionID = mysql_connect($argHostname,$argUsername,$argPassword,
												$this->forceNewConnect);
		else
			$this->_connectionID = mysql_connect($argHostname,$argUsername,$argPassword);
	
		if ($this->_connectionID === false) return false;
		if ($argDatabasename) return $this->SelectDB($argDatabasename);
		return true;	
	}
db.php

Code: Select all

<?php

class DB {
	
	private static $instance = null;

	public static function getInstance($dbname, $debug = false) {
		if (!isset(self::$instance[$dbname]) || is_null(self::$instance[$dbname])) {
			if (isset($GLOBALS['db'][$dbname])) {
				$ADODB_COUNTRECS = false;
				self::$instance[$dbname] = &ADONewConnection($GLOBALS['db'][$dbname]['dsn']);
		        self::$instance[$dbname]->SetFetchMode(ADODB_FETCH_ASSOC);
				self::$instance[$dbname]->Execute("SET NAMES {$GLOBALS['db'][$dbname]['charset']}");
			}
		}
		self::$instance[$dbname]->debug = $debug;
		return self::$instance[$dbname];
	}
	
}

?>
I've been pulling my hair with this problem, I really need to find the solution!
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Can't connect to local MySQL server through socket '/var

Post by Weirdan »

what's the value of argHostName ? if you specify it as 'localhost' mysql library will attempt to connect using unix socket in /var/run . To connect via TCP/IP specify it as '127.0.0.1'
D_zone
Forum Newbie
Posts: 14
Joined: Mon Nov 02, 2009 9:22 am

Re: Can't connect to local MySQL server through socket '/var

Post by D_zone »

So should i replaced the $argHostname '192.168.1.6' in very function?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Can't connect to local MySQL server through socket '/var

Post by Weirdan »

most likely you should change the dsn you're using and replace 'localhost' with '127.0.0.1'
Post Reply