mongodb window安装到thinkphp操作

 使用场景:

一般MySQL在单表数据量超过500万后,性能就会下降的比较快

mongodb  日志收集分析。千万级 亿级

1.安装参照

https://blog.csdn.net/muguli2008/article/details/80591256

2.去C:\Windows\System32  目录下  右击cmd 选择已管理员身份运行

   否则报错 Error connecting to the Service Control Manager: 拒绝访问。 (5)

3.

mongod.exe --dbpath=d:/mongo/db --logpath=d:/mongo/log/log.txt --install


C:\Windows\system32>sc create mongodb binPath= "C:\Program Files\MongoDB\Server\4.2\bin\mongod.exe --service --dbpath D:\mongodb\data --logpath=d:\mongodb\log\mongodb.log --logappend --directoryperdb"
[SC] CreateService 失败 1073:

指定的服务已存在。


C:\Windows\system32>
C:\Windows\system32>mongo
MongoDB shell version v4.2.5-41-g4a806ff
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("7297da0e-33dc-442d-b9ce-beeda26faa7c") }
MongoDB server version: 4.2.5-41-g4a806ff
Server has startup warnings:
2020-04-08T20:23:23.127+0800 I  CONTROL  [initandlisten]
2020-04-08T20:23:23.127+0800 I  CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-04-08T20:23:23.127+0800 I  CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2020-04-08T20:23:23.127+0800 I  CONTROL  [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
>

4. 

5.操作库 建库,建表,改表,删表,查表

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
> use test;
switched to db test
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
> db
test
> db.createCollection('goods');
{ "ok" : 1 }
> show tables;
goods
> db.goods.renameColltion('shop');
2020-04-08T20:43:44.699+0800 E  QUERY    [js] uncaught exception: TypeError: db.goods.renameColltion is not a function :
@(shell):1:1
> db.goods.renameCollection('shop')
{ "ok" : 1 }
> show tables;
shop
> db.createCollection('user');
{ "ok" : 1 }
> db.createCollection('user1');
{ "ok" : 1 }
> show tables;
shop
user
user1
> db.shop.drop();
true
> show tables;
user
user1
>

6.php安装mongodb扩展  

参考:https://blog.csdn.net/weixin_36429334/article/details/73467830

 nts(非线程) 还是 ts (线程),然后查看操作位数

 下载链接: pecl mangodb下载  下载对应版本

打开php.ini 配置文件增加行 : extension=php_mongodb.dll

7.thinkphp操作mongodb

参考:https://www.kancloud.cn/manual/thinkphp5/167865 

composer require topthink/think-mongo=1.*
return [
    // 数据库类型
    'type'           => '\think\mongo\Connection',
    // 设置查询类
    'query'			 => '\think\mongo\Query',
    // 服务器地址
    'hostname'       => '127.0.0.1',
    // 集合名
    'database'       => 'demo',
    // 用户名
    'username'       => '',
    // 密码
    'password'       => '',
    // 端口
    'hostport'       => '',
];

 可以使用Db类直接操作MongoDb

//        phpinfo();
        $data=array();
        $data['id'] = '589461c0fc122812b4007411';
        $data['name'] = '多少积分可垃圾费两款';
        Db::table('user')->insert($data);


        $user = Db::table('user')->select();
        dump($user);

返回的结果

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章