OS: Ubuntu16.04 (Sakura VPS) MongoDB : 2.6.10 python : 3.5
Install MongoDB
1)Public key setting
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
2) mongodb.Create list
echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
3)Actually install
sudo apt-get update
#sudo apt-get install mongodb-10gen
4) mongod.Create service
sudo vim /lib/systemd/system/mongod.service
▼mongod.Contents of service
[Unit]
Description=MongoDB Database Service
Wants=network.target
After=network.target
[Service]
ExecStart=/usr/bin/mongod --config /etc/mongod.conf
ExecReload=/bin/kill -HUP $MAINPID
Restart=always
User=mongodb
Group=mongodb
StandardOutput=syslog
StandardError=syslog
[Install]
WantedBy=multi-user.target
(Reference URL) http://qiita.com/pelican/items/bb9b5290bb73acedc282
Install the pymongo package that operates MongoDB from python
pip3 install pymongo
sudo systemctl start mongod
from pymongo import MongoClient
if __name__ == "__main__":
#Connect to MongoDB
mongo = MongoClient('localhost:27017')
#Database selection
db = mongo_client['test']
db['test'].insert_one({ "name" : "A", "age": 30 })
For more detailed operations, refer to the URL below. http://goo.gl/xVmzuI
Recommended Posts