CodeIgniter Forums
Upgrading to CI3 - MySQLi connection issues - solution - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Upgrading to CI3 - MySQLi connection issues - solution (/showthread.php?tid=63540)



Upgrading to CI3 - MySQLi connection issues - solution - gregormck - 11-11-2015

Hi - I had an issue connecting to my local MySQL database (MAMP) recently when upgrading my app from 2.2 to 3.03. I could connect in the browser but when running scripts in Terminal I was getting "Warning: mysqli_connect(): (HY000/2002): No such file or directory" errors. For similar issues you can see this Stackoverflow post (not mine): http://stackoverflow.com/questions/20458864/connecting-to-mysqli-through-terminal-in-mac-os-using-mamp

I had my database config as follows:

$db['default']['hostname'] = ':/Applications/MAMP/tmp/mysql/mysql.sock';
$db['default']['username'] = 'username';
$db['default']['password'] = 'password';
$db['default']['database'] = 'database';

Note that I was getting connection errors with 2.2 until I took the advice in this SO post to add the socket path as the hostname (http://stackoverflow.com/a/10958016/802814)

Using the same setting above in 3.03 was giving me the issues I raised above. After a lot of trial and error, I finally found that if I remove the ":" in the hostname it works i.e. $db['default']['hostname'] = '/Applications/MAMP/tmp/mysql/mysql.sock';

The issue was caused by a change in the db_connect function in mysqli_driver.php - specifically the addition of this:

if ($this->hostname[0] === '/')
{
$hostname = NULL;
$port = NULL;
$socket = $this->hostname;
}
else
{
$hostname = ($persistent === TRUE && is_php('5.3'))
? 'p:'.$this->hostname : $this->hostname;
$port = empty($this->port) ? NULL : $this->port;
$socket = NULL;
}

As $this->hostname[0] === : then $socket was not being set and therefore was setting $hostname as 'p::/Applications/MAMP/tmp/mysql/mysql.sock', which doesn't work obviously.

The reason for this post was to share what I've learned and to help anyone else who might have the similar settings to me.

Cheers!