Преглед на файлове

调整mongodb连接配置,以及使用方式

王刚 преди 2 години
родител
ревизия
c9df154007

+ 1 - 1
Gameserver/Amfphp/Services/AppServer.php

@@ -86,7 +86,7 @@ class AppServer {
      * @param Resp $resp
      */
     private static function LogCmdMongo($resp) {
-        $tablename = 'ylsj2019.tab_op_log' . date('Ym');                        # 当月的日志表名 
+        $tablename = 'tab_op_log' . date('Ym');                        # 当月的日志表名 
         $arr = array(
             'uid' => req()->uid,
             'req' => req()->storage(),

+ 4 - 0
Gameserver/Amfphp/config.php

@@ -90,14 +90,18 @@ abstract class config {
             $this->nosql->host = '192.168.10.16';                               # host/ip
             $this->nosql->port = 6004;                                          # 端口
             $this->nosql->pwd = 'wanggang1985';                                 # 密钥 
+            $this->nosql->db = 0;                                               # db索引
         }
     }
 
     private function _InitMongoDB() {
+        $this->mongo = new \stdClass();
         if (GAME_ONLINE) {
             $this->_InitOuterMongoDB();
         } else {
             $this->mongodb = "mongodb://ylsj2019eXNsag==:wanggang1985@192.168.10.16:27017/?authSource=ylsj2019";
+            $this->mongo->db = "ylsj2019_t2";
+            $this->mongo->conn = "mongodb://gwang:wanggang1985@localhost:23333/?authSource=admin";
         }
     }
 

+ 4 - 1
Gameserver/Amfphp/configs/config_and.php

@@ -30,7 +30,7 @@ class config_and extends config {
     protected function _InitOuterNetPaydb() {
         $this->paydb->host = '172.17.16.5';
         $this->paydb->port = '3306';
-        $this->paydb->name = 'ylsj2019_pay';
+        $this->paydb->name = 'ylsj2019_pay_t2';
         $this->paydb->user = 'ylsjMTY0LjkyLjE5NC4x';
         $this->paydb->password = 'wanggang1985';
     }
@@ -42,6 +42,7 @@ class config_and extends config {
         $this->nosql->host = "172.17.16.5";                                       # host/ip
         $this->nosql->port = 6379;                                              # 端口
         $this->nosql->pwd = 'wanggang1985';                                     # 密钥
+        $this->nosql->db = 2;                                                   # db索引
     }
 
     /**
@@ -49,6 +50,8 @@ class config_and extends config {
      */
     protected function _InitOuterMongoDB() {
         $this->mongodb = "mongodb://ylsjMTY0LjkyLjE5NC4x:wanggang1985@172.17.16.5:23333/?authSource=ylsj2019";
+        $this->mongo->db = "ylsj2019_t2";
+        $this->mongo->conn = "mongodb://gwang:wanggang1985@localhost:27017/?authSource=admin";
     }
 
 }

+ 2 - 0
Gameserver/Amfphp/configs/config_ios.php

@@ -43,6 +43,8 @@ class config_ios extends config {
      */
     protected function _InitOuterMongoDB() {
         $this->mongodb = "mongodb://gwang:wanggang1985@localhost:27017/?authSource=admin";
+        $this->mongo->db = "ylsj2019";
+        $this->mongo->conn = "mongodb://ylsjMTY0LjkyLjE5NC4x:wanggang1985@192.168.10.16:27017/?authSource=ylsj2019";
     }
 
 }

+ 2 - 0
Gameserver/Amfphp/configs/config_web.php

@@ -42,6 +42,8 @@ class config_web extends config {
      */
     protected function _InitOuterMongoDB() {
         $this->mongodb = "mongodb://gwang:wanggang1985@localhost:27017/?authSource=admin";
+        $this->mongo->db = "ylsj2019";
+        $this->mongo->conn = "mongodb://ylsjMTY0LjkyLjE5NC4x:wanggang1985@192.168.10.16:27017/?authSource=ylsj2019";
     }
 
 }

+ 3 - 2
Gameserver/Amfphp/main.php

@@ -65,7 +65,7 @@ function gMem() {
 //            $cmem->auth($nosql->pwd);
 //        } else {
         $cmem = new CRedisUtil();
-        $cmem->conn($nosql->host, $nosql->port, $nosql->pwd);
+        $cmem->conn($nosql->host, $nosql->port, $nosql->pwd, $nosql->db);
 
 //        }
     }
@@ -80,7 +80,8 @@ function gMongo() {
     static $mu = null;
     if (null == $mu) {
         $mu = new MongoUtil();
-        $mgr = $mu->conn(config::Inst()->mongodb);
+        $mc = config::Inst()->mongo;
+        $mgr = $mu->conn($mc->conn, $mc->db);
         if ($mgr === false) {
             CLog::err("MongoDB连接异常!");
         }

+ 36 - 36
Gameserver/Amfphp/process/UserProc.php

@@ -57,17 +57,18 @@ class UserProc {
                 Err(ErrCode::cmd_err);
         }
     }
-    
+
     /*
      * 6016 删除角色
      */
-    public static function deleteUserUId(){
+
+    public static function deleteUserUId() {
         $mem = gMem();
-        
+
         $uid = req()->uid;
         $zoneid = req()->zoneid;
         $list = array();
-        
+
         $zoneKey = MemKey_User::Union_PlayedZoneInfo_normal($uid);
         $list[] = $zoneKey;
         $publicKey = MemKey_User::Union_PublicState_hash($uid);
@@ -86,20 +87,20 @@ class UserProc {
         $list[] = $PVP_DefensiveLogKey;
         $SigKey = MemKey_User::Sig($zoneid, $uid);
         $list[] = $SigKey;
-        
+
         $CurIdKey = MemKey_User::Mail_CurId_int($zoneid, $uid);
         $list[] = $CurIdKey;
         $SysRecordKey = MemKey_User::Mail_SysRecord_set($zoneid, $uid);
         $list[] = $SysRecordKey;
         $QueueKey = MemKey_User::Mail_Queue_hash($zoneid, $uid);
         $list[] = $QueueKey;
-        
+
         foreach ($list as $key) {
-            if($mem->exists($key)){
+            if ($mem->exists($key)) {
                 $mem->delete($key);
             }
         }
-        
+
         return Resp::ok();
     }
 
@@ -339,7 +340,7 @@ class UserProc {
     /**
      * 6000 【移动端】 获取分区列表
      */
-    public static function GetZoneList() {        
+    public static function GetZoneList() {
         $defaultZone = new Ins_ZoneInfo(1, 0, "");                             # 新用户默认分区
         $bGetRecommended = false;
         if (count(req()->paras) > 0) {                                           # 是否只拉取推荐分区
@@ -457,9 +458,9 @@ class UserProc {
         }
         return $resp;
     }
-    
-    public static function newFieldSupplement(&$game) {       
-        if(!StlUtil::dictHasProperty($game->store,'storage')){
+
+    public static function newFieldSupplement(&$game) {
+        if (!StlUtil::dictHasProperty($game->store, 'storage')) {
             $key = 1;
             $key2 = 2;
 
@@ -467,8 +468,8 @@ class UserProc {
             $game->store->storage->$key = new Ins_storage();
             $game->store->storage->$key2 = new Ins_storage();
         }
-        
-        if($game->store->storage == null){
+
+        if ($game->store->storage == null) {
             $key = 1;
             $key2 = 2;
 
@@ -476,51 +477,50 @@ class UserProc {
             $game->store->storage->$key = new Ins_storage();
             $game->store->storage->$key2 = new Ins_storage();
         }
-        
-        if(!StlUtil::dictHasProperty($game->store,'battleItem')){
+
+        if (!StlUtil::dictHasProperty($game->store, 'battleItem')) {
             $game->store->battleItem = ObjectInit();
         }
-        
-        if($game->store->battleItem == null){
+
+        if ($game->store->battleItem == null) {
             $game->store->battleItem = ObjectInit();
         }
-        
-        if(!StlUtil::dictHasProperty($game->newMap,'huichengquanRecord')){
+
+        if (!StlUtil::dictHasProperty($game->newMap, 'huichengquanRecord')) {
             $game->newMap->huichengquanRecord = ObjectInit();
         }
-        
-        if(is_array($game->newMap->huichengquanRecord)){
+
+        if (is_array($game->newMap->huichengquanRecord)) {
             $game->newMap->huichengquanRecord = ObjectInit();
         }
-        
+
         $dixiachengId = 503001;
-        if(!StlUtil::dictHasProperty($game->newMap->unlockedFootholds, $dixiachengId)){
+        if (!StlUtil::dictHasProperty($game->newMap->unlockedFootholds, $dixiachengId)) {
             $mo = GameConfig::gate_getItem($dixiachengId);
             $footHold = new Ins_FootHold();
             $footHold->mapId = $mo->gateId;
             $footHold->curMapType = $mo->mapType;
-            $game->newMap->unlockedFootholds->$dixiachengId = $footHold; 
-        }   
-        
+            $game->newMap->unlockedFootholds->$dixiachengId = $footHold;
+        }
+
         foreach ($game->newMap->unlockedFootholds as $mapid => $val) {
-            if($val->mapId != $mapid){
+            if ($val->mapId != $mapid) {
                 $game->newMap->unlockedFootholds->$mapid->mapId = $mapid;
                 break;
             }
         }
-        
-        if(!StlUtil::dictHasProperty($game->newMap, 'curFootholdId')){
+
+        if (!StlUtil::dictHasProperty($game->newMap, 'curFootholdId')) {
             $game->newMap->curFootholdId = $game->newMap->curMapId;
         }
-        
-        if($game->newMap->lastMapId == 0){
+
+        if ($game->newMap->lastMapId == 0) {
             $game->newMap->lastMapId = 503099;
         }
-        
-       if(!StlUtil::dictHasProperty($game->store, 'dailyTask_HandOver')){
+
+        if (!StlUtil::dictHasProperty($game->store, 'dailyTask_HandOver')) {
             $game->store->dailyTask_HandOver = 0;
         }
-                         
     }
 
     /**
@@ -779,7 +779,7 @@ class UserProc {
      *           version 1.0.0 Mysql storagef 1. 分表不做了, 2. 存储过程不用了 3. 先能用再说
      */
     public static function backupUserInfoMongo() {
-        $collectionName = "ylsj2019.userInfoBack";                              # 表名
+        $collectionName = "userInfoBack";                              # 表名
         $key = MemKey_User::Info_hash(req()->zoneid, req()->uid);
         $doc = array('key' => $key, #                                           # 最新文档
             'ts' => TimeUtil::dtCurrent(), #                                    # 更新时间

+ 5 - 3
Gameserver/Amfphp/test.php

@@ -9,11 +9,13 @@ include __DIR__ . '/main.php';
 echoLine("phpver: " . PHP_VERSION . PHP_EOL);
 echoLine("tsDay:" . totalDays());
 
-SelfChecker::CheckAll();
+//SelfChecker::CheckAll();
 //
 //set_time_limit(15);                                                           # 设置执行超时时间
 //
 
- 
 
- 
+foreach (gMongo()->find("c1") as $d) {
+    var_dump($d);
+}
+var_dump(gMongo()->find("c1")->toArray());

+ 9 - 7
Gameserver/Amfphp/util/MongoUtil.php

@@ -21,6 +21,7 @@ class MongoUtil {
      * @var \MongoDB\Driver\Manager
      */
     private $mc;
+    private $dbPrefix = "";
 
     /**
      * 返回一个新的option构造器,请使用链式写法追加所有option, 最后用GetOption()获取最终的option数组
@@ -36,9 +37,10 @@ class MongoUtil {
      * @param type $connStr
      * @return \MongoDB\Driver\Manager|false
      */
-    public function conn($connStr) {
+    public function conn($connStr, $dbPrefix = "") {
         try {
             $this->mc = new \MongoDB\Driver\Manager($connStr);
+            $this->dbPrefix = $dbPrefix;
             return $this->mc;
         } catch (\Exception $e) {
             Clog::err($e->getMessage());
@@ -47,12 +49,12 @@ class MongoUtil {
     }
 
     /**
-     * 查询
+     * 查询,返回的是光标,也可以用toArray()转换为数组使用
      * @author gwang<wanggangzero@qq.com>
      * @param string $collection 集合
      * @param array $filter 同过滤器语法
      * @param array $options 请利用QueryOptionBuilder()来辅助定制选项
-     * @return \MongoDB\Driver\Cursor|false 
+     * @return \MongoDB\Driver\Cursor|false 访问光标或者false, 可以用foreach来访问Cursor
      */
     public function find($collection, $filter = [], $options = []) {
         if (empty($this->mc)) {
@@ -60,7 +62,7 @@ class MongoUtil {
         }
         try {
             $query = new \MongoDB\Driver\Query($filter, $options);
-            $cursor = $this->mc->executeQuery($collection, $query);
+            $cursor = $this->mc->executeQuery($this->dbPrefix . '.' . $collection, $query);
             return $cursor;
         } catch (\Exception $ex) {
             CLog::err($ex->getMessage());
@@ -85,7 +87,7 @@ class MongoUtil {
             $bulk = new \MongoDB\Driver\BulkWrite();
             $bulk->insert($addArr);
             $writeConcern = new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY, 6000);
-            $result = $this->mc->executeBulkWrite($collection, $bulk, $writeConcern);
+            $result = $this->mc->executeBulkWrite($this->dbPrefix . '.' . $collection, $bulk, $writeConcern);
             if ($result->getInsertedCount()) {
                 return true;
             }
@@ -115,7 +117,7 @@ class MongoUtil {
             $bulk = new \MongoDB\Driver\BulkWrite();
             $bulk->delete($whereArr, $options);
             $writeConcern = new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY, 30000);
-            $result = $conn->executeBulkWrite($collection, $bulk, $writeConcern);
+            $result = $conn->executeBulkWrite($this->dbPrefix . '.' . $collection, $bulk, $writeConcern);
             return $result->getDeletedCount();
         } catch (\Exception $e) {
             CLog::err($e->getMessage());                                        # 记录错误日志
@@ -145,7 +147,7 @@ class MongoUtil {
             $bulk = new \MongoDB\Driver\BulkWrite();
             $bulk->update($whereArr, $newPropertiesArr, $options);
             $writeConcern = new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY, 30000);
-            $result = $conn->executeBulkWrite($collection, $bulk, $writeConcern);
+            $result = $conn->executeBulkWrite($this->dbPrefix . '.' . $collection, $bulk, $writeConcern);
             return $result->getUpsertedCount() + $result->getModifiedCount();
         } catch (\Exception $e) {
             CLog::err($e->getMessage());                                        # 记录错误日志