可能触及到我知识的盲区了
情景
本地使用 ssh -L 127.0.0.1:27017:{aliyun.mongodburi}:3717 {user}@{ip} 建立 ssh 通道,将本地的 27017 端口映射到服务器,访问阿里云的 mongodb (无验证),然后在本地运行一些脚本。
映射应该没问题
$ curl 127.0.0.1:27017
It looks like you are trying to access MongoDB over HTTP on the native driver port.
在用 pymongo 时一切正常。
client = MongoClient() # pretty well
今天用了 https://github.com/mongodb/mongo-go-driver,发现在本地无法连接,一直超时。
func initMongoConnection() {
ctx, _ := context.WithTimeout(context.Background(), time.Second*10)
log.Println(config.Mongo.URI) // "mongodb://localhost:27017"
client, err := mongo.Connect(ctx, options.Client().ApplyURI(config.Mongo.URI))
if err != nil {
panic(err)
}
err = client.Ping(ctx, readpref.Primary())
if err != nil {
panic(err) //panic: context deadline exceeded
}
}
编译之后拿到服务器上去跑又没问题(基本可以定位问题出现在通道上)。本地不走通道访问另一台局域网的 MongoDB,也没问题。
本地到 ssh 服务器的连接速度问题应该可以排除
总结就是:
- python + pymongo + ssh 没问题(速度很快,应该可以排除网络问题造成超时)
- golang + mongo-go-driver 没问题
- goalng + mongo-go-driver + ssh 不行
有可能是哪里的问题?