###前提・実現したいこと
1.MongoDBを起動したい。
2.pythonからもMongoDBを起動しDBにデータを格納したいがエラーとなりできない
###発生している問題・エラーメッセージ
1.MongoDBを起動できない
2.pythonからMongoDBを起動しようとするとエラーとなる
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 61] Connection refused
1.MongoDBを起動できない
mongodとターミナル上でコマンドを打つと、以下となりshutting downとあることから起動していないと判断。
I CONTROL [initandlisten] MongoDB starting : pid=7000 port=27017 dbpath=/data/db 64-bit host=test
I CONTROL [initandlisten] db version v3.4.9
I CONTROL [initandlisten] git version: 876ebee8c7dd0e2d992f36a848ff4dc50ee6603e
I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2l 25 May 2017
I CONTROL [initandlisten] allocator: system
I CONTROL [initandlisten] modules: none
I CONTROL [initandlisten] build environment:
I CONTROL [initandlisten] distarch: x86_64
I CONTROL [initandlisten] target_arch: x86_64
I CONTROL [initandlisten] options: {}
I STORAGE [initandlisten] exception in initAndListen: 20 Attempted to create a lock file on a read-only directory: /data/db, terminating
I NETWORK [initandlisten] shutdown: going to close listening sockets...
I NETWORK [initandlisten] shutdown: going to flush diaglog...
I CONTROL [initandlisten] now exiting
I CONTROL [initandlisten] shutting down with code:100
2.pythonからMongoDBを起動しようとするとエラーとなる
仮想環境に入り、pymongoをインストール。pythonのインタラクティブシェル上で、以下のコマンドを打つが
データをcollection.insert_oneで格納しようとすると記載のエラーとなる。
from pymongo import MongoClient
client = MongoClient('localhost' , 27017)
type(client)
<class 'pymongo.mongo_client.MongoClient'>
db = client.test
type(db)
<class 'pymongo.database.Database'>
collection = db.spots
type(collection)
<class 'pymongo.collection.Collection'>
collection.insert_one({'name':'名前' , 'station':'駅'})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/test/Documents/python/learning/lib/python3.6/site-packages/pymongo/collection.py", line 667, in insert_one
with self._socket_for_writes() as sock_info:
File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/contextlib.py", line 81, in enter
return next(self.gen)
File "/Users/test/Documents/python/learning/lib/python3.6/site-packages/pymongo/mongo_client.py", line 868, in _get_socket
server = self._get_topology().select_server(selector)
File "/Users/test/Documents/python/learning/lib/python3.6/site-packages/pymongo/topology.py", line 214, in select_server
address))
File "/Users/test/Documents/python/learning/lib/python3.6/site-packages/pymongo/topology.py", line 189, in select_servers
self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 61] Connection refused
###該当のソースコード
from pymongo import MongoClient client = MongoClient('localhost' , 27017) db = client.test collection = db.spots collection.insert_one({'name':'名前' , 'station':'駅'})
###試したこと
https://teratail.com/questions/69643
と全く同じ状況にあるようにも思うのですが、mongodで起動できなかったことから、なぜ起動できないのかを
解決しないといけないと思ったのですが、そのなぜが分からずでして・・・
mongod --versionで
db version v3.4.9
と出てくるのでそもそもインストールできていないというわけではないと考えています。
また、あらかじめ、 mkdir -p /data/db
でディレクトリを作成しています。
dataとdbフォルダそれぞれを
ls -l
で確認するといずれも「drwxr-xr-x」となっているので
書き込み権限がない、というわけでもないのかなと思っています。
###補足情報(言語/FW/ツール等のバージョンなど)
プログラミング初心者です。
MacOSでターミナルを使っています。
回答3件
あなたの回答
tips
プレビュー