pymongo無法連接到mongo集群,連本地沒問題.
用命令行方式一切正常。
本地mongo無驗證,集群有權(quán)限驗證,
用命令行方式一切正常
$ mongo localhost
MongoDB shell version: 3.2.20
> db.version()
3.0.6
> use hello
switched to db hello
> db
hello
> db.hello.count() # 數(shù)據(jù)庫、表名都叫hello
7
集群
$ mongo -u user -p password remote_ip:remote_port/hello --authenticationDatabase admin
hello:PRIMARY> db.version()
3.6.4-1.2
pymongo版本為:3.7.1。 嘗試如下:
In [2]: from pymongo import MongoClient
In [3]: c2= MongoClient('mongodb://localhost/hello')
In [4]: db2=c2.get_database()
In [5]: db2['hello'].count() # 本地mongo一切正常
Out[5]: 7
In [6]: c1=MongoClient("mongodb://user:password@remote_ip:remote_port/hello&authSource=admin")
In [7]: db1=c1.get_database()
In [8]: db1['hello'].count()
/home/work/venv36/bin/ipython:1: DeprecationWarning: count is deprecated. Use estimated_document_count or count_documents instead. Please note that $where must be replaced by $expr, $near must be replaced by $geoWithin with $center, and $nearSphere must be replaced by $geoWithin with $centerSphere
#!/home/work/fsj/venv36/bin/python3.6
---------------------------------------------------------------------------
OperationFailure Traceback (most recent call last)
<ipython-input-8-bf661fbd7e9c> in <module>()
----> 1 db1['hello'].count()
~/fsj/venv36/lib/python3.6/site-packages/pymongo/collection.py in count(self, filter, session, **kwargs)
1764 collation = validate_collation_or_none(kwargs.pop('collation', None))
1765 cmd.update(kwargs)
-> 1766 return self._count(cmd, collation, session)
1767
1768 def create_indexes(self, indexes, session=None, **kwargs):
~/fsj/venv36/lib/python3.6/site-packages/pymongo/collection.py in _count(self, cmd, collation, session)
1561 def _count(self, cmd, collation=None, session=None):
1562 """Internal count helper."""
-> 1563 with self._socket_for_reads(session) as (sock_info, slave_ok):
1564 res = self._command(
1565 sock_info,
/usr/local/lib/python3.6/contextlib.py in __enter__(self)
79 def __enter__(self):
80 try:
---> 81 return next(self.gen)
82 except StopIteration:
83 raise RuntimeError("generator didn't yield") from None
~/fsj/venv36/lib/python3.6/site-packages/pymongo/mongo_client.py in _socket_for_reads(self, read_preference)
1099 server = topology.select_server(read_preference)
1100
-> 1101 with self._get_socket(server) as sock_info:
1102 slave_ok = (single and not sock_info.is_mongos) or (
1103 read_preference != ReadPreference.PRIMARY)
/usr/local/lib/python3.6/contextlib.py in __enter__(self)
79 def __enter__(self):
80 try:
---> 81 return next(self.gen)
82 except StopIteration:
83 raise RuntimeError("generator didn't yield") from None
~/fsj/venv36/lib/python3.6/site-packages/pymongo/mongo_client.py in _get_socket(self, server)
1056 def _get_socket(self, server):
1057 try:
-> 1058 with server.get_socket(self.__all_credentials) as sock_info:
1059 yield sock_info
1060 except NetworkTimeout:
/usr/local/lib/python3.6/contextlib.py in __enter__(self)
79 def __enter__(self):
80 try:
---> 81 return next(self.gen)
82 except StopIteration:
83 raise RuntimeError("generator didn't yield") from None
~/fsj/venv36/lib/python3.6/site-packages/pymongo/pool.py in get_socket(self, all_credentials, checkout)
1004 sock_info = self._get_socket_no_auth()
1005 try:
-> 1006 sock_info.check_auth(all_credentials)
1007 yield sock_info
1008 except:
~/fsj/venv36/lib/python3.6/site-packages/pymongo/pool.py in check_auth(self, all_credentials)
675
676 for credentials in cached - authset:
--> 677 auth.authenticate(credentials, self)
678 self.authset.add(credentials)
679
~/fsj/venv36/lib/python3.6/site-packages/pymongo/auth.py in authenticate(credentials, sock_info)
561 mechanism = credentials.mechanism
562 auth_func = _AUTH_MAP.get(mechanism)
--> 563 auth_func(credentials, sock_info)
564
565
~/fsj/venv36/lib/python3.6/site-packages/pymongo/auth.py in _authenticate_default(credentials, sock_info)
538 return _authenticate_scram(credentials, sock_info, 'SCRAM-SHA-1')
539 elif sock_info.max_wire_version >= 3:
--> 540 return _authenticate_scram(credentials, sock_info, 'SCRAM-SHA-1')
541 else:
542 return _authenticate_mongo_cr(credentials, sock_info)
~/fsj/venv36/lib/python3.6/site-packages/pymongo/auth.py in _authenticate_scram(credentials, sock_info, mechanism)
260 ('payload', Binary(b"n,," + first_bare)),
261 ('autoAuthorize', 1)])
--> 262 res = sock_info.command(source, cmd)
263
264 server_first = res['payload']
~/fsj/venv36/lib/python3.6/site-packages/pymongo/pool.py in command(self, dbname, spec, slave_ok, read_preference, codec_options, check, allowable_errors, check_keys, read_concern, write_concern, parse_write_concern_error, collation, session, client, retryable_write, publish_events)
577 compression_ctx=self.compression_context,
578 use_op_msg=self.op_msg_enabled,
--> 579 unacknowledged=unacknowledged)
580 except OperationFailure:
581 raise
~/fsj/venv36/lib/python3.6/site-packages/pymongo/network.py in command(sock, dbname, spec, slave_ok, is_mongos, read_preference, codec_options, session, client, check, allowable_errors, address, check_keys, listeners, max_bson_size, read_concern, parse_write_concern_error, collation, compression_ctx, use_op_msg, unacknowledged)
148 helpers._check_command_response(
149 response_doc, None, allowable_errors,
--> 150 parse_write_concern_error=parse_write_concern_error)
151 except Exception as exc:
152 if publish:
~/fsj/venv36/lib/python3.6/site-packages/pymongo/helpers.py in _check_command_response(response, msg, allowable_errors, parse_write_concern_error)
153
154 msg = msg or "%s"
--> 155 raise OperationFailure(msg % errmsg, code, response)
156
157
OperationFailure: Authentication failed.
按照文檔https://api.mongodb.com/pytho... 檢查沒問題。
本地是3.0,集群3.6,authentication mechanism 都應(yīng)該是默認(rèn)的SCRAM-SHA-1
請問怎么處理呢
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
浪潮集團(tuán)項目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗,技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。