解决PHP使用普通帐号链接mongodb报错问题

起先使用mongodb来开发,未加上帐号密码验证功能. mongodb一直正常,准备放到线上之时加上帐号密码功能,整个过程以下 html

增长mongodb帐号
增长用户a_ttlsa_com对a_ttlsa_com这个库有读写权限,密码为ttlsa.com.passwd mongodb

# mongo
 MongoDB shell version: 2.4.5
 connecting to: test
 > use a_ttlsa_com
 switched to db a _ttlsa_com
 > db.addUser('a_ttlsa_com','ttlsa.com.passwd')
 {
 "user" : "a_ttlsa_com",
 "readOnly" : false,
 "pwd" : "48f7704260bf8c966fcdec40915f5c22",
 "_id" : ObjectId("52327181611f841827ccae9d")
 }
 >

PHP链接mongodb配置
我这边是yii框架,链接方式大同小异 shell

'mongodb' => array(
    'class'            => 'EMongoDB',
    'connectionString' => 'mongodb://a_ttlsa_com:ttlsa.com.passwd@192.168.50.101',
    'dbName'           => 'a_ttlsa_com',
    'fsyncFlag'        => true,
    'safeFlag'         => true,
    'useCursor'        => false
),

打开页面,出现以下报错 数据库

EMongoDB failed to open connection: Failed to connect to: 192.168.50.101:27017:Authentication failed on database 'admin' with username 'a_ttlsa_com': auth fails bash

用户密码密码都是正确,为何会去链接admin数据库,是否是有点莫名其妙,其实这边犯了一个错误。mongodb的链接字符串后面要加上库名,不然他默认会去链接admin库,因此配置文件后面的dbName实际上和链接彻底没有关系. 框架

配置文件修改成以下 运维

'mongodb' => array(
    'class'            => 'EMongoDB',
    'connectionString' => 'mongodb://a_ttlsa_com:ttlsa.com.passwd@192.168.50.101/a_ttlsa_com',
    'dbName'           => 'a_ttlsa_com',
    'fsyncFlag'        => true,
    'safeFlag'         => true,
    'useCursor'        => false
),

恢复正常. yii

来源网站:  运维生存时间    网址:http://www.ttlsa.com/html/2981.html 网站

相关文章
相关标签/搜索