【问题标题】:Is it possible to connect to local MongoDB with flutter dart?是否可以使用颤振飞镖连接到本地 MongoDB?
【发布时间】:2019-11-21 04:17:13
【问题描述】:

每次我尝试连接到本地 MongoDB 时,都会不断收到此异常: SocketException:操作系统错误:连接被拒绝,errno = 111,地址 = localhost,端口 = 38748

有趣的事实:异常中的端口在每次尝试后都会增加 2 并且总是错误的(我什至启动服务器时使用的端口接下来会抛出异常)

MongoDB 服务器正在运行 - 我 NETWORK [initandlisten] 等待端口 27017 上的连接

依赖已设置 -

依赖: mongo_dart: ^0.3.5 扑: sdk:颤动

import 'package:mongo_dart/mongo_dart.dart' show Db, DbCollection;
class DBConnection {

  static DBConnection _instance;

  final String _host = "localhost";
  final String _port = "27017";
  final String _dbName = "debtservice";
  Db _db;

  static getInstance(){
    if(_instance == null) {
      _instance = DBConnection();
    }
    return _instance;
  }

  Future<Db> getConnection() async{
    if (_db == null){
      try {
        _db = Db(_getConnectionString());
        await _db.open();
      } catch(e){
        print(e);
      }
    }
    return _db;
  }

  _getConnectionString(){
    return "mongodb://$_host:$_port/$_dbName";
  }

  closeConnection() {
    _db.close();
  }

}

我已经尝试在普通 dart 中运行此代码并且它运行了。

【问题讨论】:

标签: mongodb flutter dart serversocket flutter-dependencies


【解决方案1】:

更新mongo配置文件

 sudo nano  /etc/mongod.conf

 bindIp:127.0.0.1 ##### replace this line with below 

 bindIP:0.0.0.0  #### 

更改后,检查MongoDB的状态

 sudo service mongod status

现在 MongoDB 与 Flutter 完美配合。

【讨论】:

    【解决方案2】:

    好的,我现在解决了问题...

    我必须获取我家用设备的无线 LAN-Adapter IP 并使用 --bind_ip -ip- 启动 mongodb。连接字符串中还必须提供 ip。

    仍然感谢 MichaelM。

    【讨论】:

      【解决方案3】:

      在设备或模拟器上运行时,您的 _host 不会是 localhost - 您需要指定运行 MongoDB 的计算机的实际 IP 地址或网络主机名。

      【讨论】:

      • 我在使用 mongoDb 时遇到了同样的问题,即使我将 localhost 更改为 wifi ip 地址 192.168.1.1 看到 ipconfig : IPv4 Address。 . . . . . . . . . . :192.168.1.4 子网掩码。 . . . . . . . . . . :255.255.255.0 默认网关。 . . . . . . . . : 192.168.1.1
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-24
      • 2023-02-24
      • 1970-01-01
      • 2019-02-05
      • 2022-11-12
      • 2020-12-25
      • 2020-11-05
      相关资源
      最近更新 更多