search
Homephp教程php手册TP3.2.3教你如何解决mongo无法连接的问题

由于业务需要, 最近我也开始学习了mongo, 公司用的3.2.3版本, 遇到最大的问题就是Call to a member function selectCollection() on a non-object, 其实看过错误提示就会很明白, 这个原因是由于缺少数据库名引起的.
知道了错误原因就很容易解决这个问题了.
首先, 我的数据库配置文件是这样写的:return array(<br>         'DB_TYPE' => 'mysql',<br>         'DB_HOST' => 'localhost',<br>         'DB_NAME' => 'xxx',<br>         'DB_USER' => 'xxx',<br>         'DB_PWD' => 'xxx',<br>         'DB_PORT' => '3306',<br>         'DB_PREFIX' => '',<br>         //密钥<br>         "AUTHCODE" => 'xxx',<br>         "COOKIE_PREFIX" => 'xxx',<br>     'MONGO'=>array(<br>         'DB_TYPE'   => 'mongo', // 数据库类型<br>         'DB_HOST'   => 'localhost', // 服务器地址<br>         'DB_NAME'   => 'xxx', // 数据库名<br>         'DB_USER'   => 'xxx', // 用户名<br>         'DB_PWD'    => 'xxx', // 密码<br>         'DB_PORT'   => '27017', // 端口<br>         'DB_PREFIX' => 'xx'<br>     )<br> );默认使用mysql的链接方式, 只有在某些页面才会用到mongo.
so 在使用的时候是这样: M("Product","","MONGO");大家可以去了解一下这个M方法的源码和具体的参数是干什么用的, 这里有个小问题, 就是中间的参数是表前缀, 但是我为空了系统竟然使用mysql的表前缀,并且我mongo的配置里面也写了表前缀是为空的,都不行, 有知道的大神说一下,小弟感激不尽.
然后就是修改Library\Think\Db\Driver\Mongo.class.php这个文件:
其中switchCollection函数里面有一段代码是这样的:if(!empty($db)) { // 传人Db则切换数据库<br>      // 当前MongoDb对象<br>      $this->_dbName  =  $db;<br>      $this->_mongo = $this->_linkID->selectDb($db);<br> }我们在其后面加个else:if(!empty($db)) { // 传人Db则切换数据库<br>      // 当前MongoDb对象<br>      $this->_dbName  =  $db;<br>      $this->_mongo = $this->_linkID->selectDb($db);<br> }else{
       $config = C('MONGO');
       $db = $config['DB_NAME'];
       $this->_dbName  =  $db;
       $this->_mongo = $this->_linkID->selectDb($db);
 }
开头我也说了, 造成这个错误的原因就是缺少数据库名, so 我们可以加上去, 这样就解决了.

AD:真正免费,域名+虚机+企业邮箱=0元

Statement
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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.