Home Backend Development PHP Tutorial CodeIgniter错误mysql_connect(): No such file or directory解决方法_PHP

CodeIgniter错误mysql_connect(): No such file or directory解决方法_PHP

May 31, 2016 pm 07:29 PM
codeigniter mysql_connect()

CI框架

首先CodeIgniter连接数据库连不上,总是显示连接错误,但是又没有error信息,难以debug。

解决方案是:在application/config/database.php文件的最后加上这一段代码:

代码如下:


echo '

';<br>print_r($db['default']);<br>echo '
Copy after login
';

 

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 '
   Connected OK:'  ;
die( 'file: ' .__FILE__ . '--> Line: ' .__LINE__);

 

显示报错,问题是mysql_connect(): No such file or directory报错。
因为以前也有用过CI都没有这个错误,谷歌一下发现是因为MySQL是brew安装的,因为路径问题导致PHP无法获取相关数据。

解决方案:
如果你已经有了 /tmp/mysql.sock 但是没有  /var/mysql/mysql.sock 你应该:

代码如下:


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

 

如果你有了 /var/mysql/mysql.sock   但是没有  mysql.sock name:

代码如下:


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

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement custom middleware in CodeIgniter How to implement custom middleware in CodeIgniter Jul 29, 2023 am 10:53 AM

How to implement custom middleware in CodeIgniter

CodeIgniter middleware: Accelerate application responsiveness and page rendering CodeIgniter middleware: Accelerate application responsiveness and page rendering Jul 28, 2023 pm 06:51 PM

CodeIgniter middleware: Accelerate application responsiveness and page rendering

PHP development: Using CodeIgniter to implement MVC pattern and RESTful API PHP development: Using CodeIgniter to implement MVC pattern and RESTful API Jun 16, 2023 am 08:09 AM

PHP development: Using CodeIgniter to implement MVC pattern and RESTful API

How to use the database query builder (Query Builder) in the CodeIgniter framework How to use the database query builder (Query Builder) in the CodeIgniter framework Jul 28, 2023 pm 11:13 PM

How to use the database query builder (Query Builder) in the CodeIgniter framework

How to use CodeIgniter5 framework in php? How to use CodeIgniter5 framework in php? Jun 01, 2023 am 11:21 AM

How to use CodeIgniter5 framework in php?

CodeIgniter middleware: Provides secure file upload and download functions CodeIgniter middleware: Provides secure file upload and download functions Aug 01, 2023 pm 03:01 PM

CodeIgniter middleware: Provides secure file upload and download functions

How to use the PHP framework CodeIgniter to quickly build a backend management system How to use the PHP framework CodeIgniter to quickly build a backend management system Jun 27, 2023 am 09:46 AM

How to use the PHP framework CodeIgniter to quickly build a backend management system

Use PHP framework CodeIgniter to develop a real-time chat application to provide convenient communication services Use PHP framework CodeIgniter to develop a real-time chat application to provide convenient communication services Jun 27, 2023 pm 02:49 PM

Use PHP framework CodeIgniter to develop a real-time chat application to provide convenient communication services

See all articles