欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php链接mongo数据库

程序员文章站 2024-01-02 10:22:16
...
$conn = new Mongo("mongodb://admin:admin@localhost:27017/local"); ///这个就是第11行

这么写完之后报错

( ! ) Deprecated: main(): The Mongo class is deprecated, please use the MongoClient class in D:\bzyweb\test2\conn.php on line 11
Call Stack
# Time Memory Function Location
1 0.0010 238592 {main}( ) ..\conn.php:0

( ! ) Fatal error: Uncaught exception 'MongoConnectionException' with message 'Failed to connect to: localhost:27017: Authentication failed on database 'local' with username 'admin': auth fails' in D:\bzyweb\test2\conn.php on line 11
( ! ) MongoConnectionException: Failed to connect to: localhost:27017: Authentication failed on database 'local' with username 'admin': auth fails in D:\bzyweb\test2\conn.php on line 11
Call Stack
# Time Memory Function Location
1 0.0010 238592 {main}( ) ..\conn.php:0
2 0.0010 239720 __construct ( ) ..\conn.php:11


回复讨论(解决方案)

Deprecated: main(): The Mongo class is deprecated, please use the MongoClient class
反对:main():Mongo类是过时的,请使用mongoclient类

echo "mongo contact:
";
//$DB = new PDO("mongodb:host=localhost;port=27017;dbname=aaa","admin","admin");
$conn = new MongoClient("mongodb://admin:admin@localhost2:27017/local");
?>

错误提示
mongo contact:

( ! ) Fatal error: Uncaught exception 'MongoConnectionException' with message 'MongoClient::__construct(): ' in D:\bzyweb\test2\conn.php on line 4
( ! ) MongoConnectionException: MongoClient::__construct(): in D:\bzyweb\test2\conn.php on line 4
Call Stack
# Time Memory Function Location
1 0.0000 240416 {main}( ) ..\conn.php:0
2 0.0000 240672 __construct ( ) ..\conn.php:4

你的下图是什么?mongo 管理器?
你看看他是怎么写的

下图是一个mongoweb的管理地址,我看了好多写法,好像链接都是那么写的,不清楚为什么我的出错了,所以贴出来数据库的结构,我想这样能表述的更清晰一点

$dsn="mongodb://localhost:27017/admin:admin/aaa";
$m=new MongoClient($dsn);
没有报错了

上一篇:

下一篇: