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);

返回的結果

 

 

 

 

 

 

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