CodeIgniter Error mysql_connect of: No such file or directory Solution

  • 2021-07-18 07:22:52
  • OfStack

First of all, CodeIgniter connection database can not be connected, which always shows connection errors, but there is no error information, so it is difficult to debug.

The solution is to add this code at the end of the application/config/database. php file:


echo '<pre>';
print_r($db['default']);
echo '</pre>'; echo 'Trying to connect to database: ' .$db['default']['database'];
$dbh=mysql_connect
(
    $db['default']['hostname'],
    $db['default']['username'],
    $db['default']['password'])
or die('Cannot connect to the database because: ' . mysql_error());
mysql_select_db ($db['default']['database']); echo '<br />   Connected OK:'  ;
die( 'file: ' .__FILE__ . '--> Line: ' .__LINE__);

Error report is displayed, the problem is mysql_connect (): No such file or directory error report.
Because CI has been used before, there is no such error. Google 1 found that MySQL was installed by brew, and PHP could not obtain relevant data because of path problems.

Solution:
If you already have/tmp/mysql. sock but don't have/var/mysql/mysql. sock, you should:


cd /var
mkdir mysql
cd mysql
ln -s /tmp/mysql.sock mysql.sock

If you have/var/mysql/mysql. sock but don't have mysql. sock name:


cd /tmp
ln -s /var/mysql/mysql.sock mysql.sock


Related articles: