MongoDB on Ubuntu - LabMazurokCom/Blockchain GitHub Wiki
SSH
If you want to perform installation on remote machine then first connect to this machine via SSH. Start the terminal and enter the following command:
ssh REMOTE_ADDRESS -l USER_NAME
You will then be asked to print USER_NAME's password.
To stop connection print exit.
Installing MongoDB Community Edition
Follow the instructions here.
Creating user with admin privilege
Staart mongod process by running
sudo service mongod start
You can stop the process with the command
sudo service mongod stop
You can check the process status reading the log file which (located by default here: /var/log/mongodb/mongod.log).
Then run the following commands:
mongo
use admin
db.createUser({
user: "USER_NAME",
pwd: "USER_PASSWORD",
roles: ["userAdminAnyDatabase", "dbAdminAnyDatabase", "readWriteAnyDatabase"]
})
You can authenticate to any particular database by running
use DB_NAME
db.auth("USER_NAME", "USER_PASSWORD")
If the authentication is successful you'll see the following message:
1
Otherwise this one:
Error: Authentication failed.
0
You cal list all users of any database by running
use DB_NAME
db.getUsers()
Allowing remote connections
By default Mongo uses port 27017 and forbids remote access. To allow remote access you should change the configuration file (its default location is /etc/mongod.conf):
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1 <--- change ip to 0.0.0.0
On DigitalOcean go to Networking->Firewalls, create new firewall with Custom inbound rule for chosen port. Then add the droplet (on which Mongo runs) to this firewall.
Starting the server
By default
- configuration file lives here:
/etc/mongod.conf(you can change this file with the commandnano /etc/mongod.conf) - log file lives here:
/var/log/mongodb/mongod.log(you can read this file with the commandcat /var/log/mongodb/mongod.log) - Mongo tries to save data here:
/data/db(you can create this directory with the commandmkdir datafollowed bymkdir data/db) - Mongo waits for connections on port 27017
You may need to append commands with sudo. You may also need to grant access to the listed above directories to current user.
You can specify other path for configuration file with and other port number with --config and --port arguments of mongod command. List of ports that are already in use can be found by running this command: netstat -antp. You'll have to specify the new port when running mongo command to enter Mongo Shell. It will look like mongo --port PORT.
You can specify other paths for log file and data storage inside configuration file with systemLog and storage options. See documentation on configuration file options
You can make mongod running in background and requiring authentication with the following command:
mongod --fork --logpath PATH_TO_LOG_FILE --auth --port PORT --bind_ip 0.0.0.0
This command requires --logpath to be specified. As mentioned above, the default path is /var/log/mongodb/mongod.log and default port (--port is not required) so the command may look like
mongod --fork --logpath /var/log/mongodb/mongod.log --auth
See detailed documentation on mongod.
If you have accidentally run mongod with wrong arguments you can stop it:
- find the process id (second column) by running
ps -ef | grep mongodorps aux | grep mongod - kill the process:
kill PID
Check that the required port is open for external world by running
nmap 178.128.186.1
If nmap is not installed then install it with apt install nmap.
Creating test database
use test
db.test_col.insertMany([{'a': 1}, {'b': 2}, {'c': 3}])
db.test_col.find()
You should see something like this (your ObjectIds will be different):
{ "_id" : ObjectId("5b4366c780aef6886b682a4d"), "a" : 1 }
{ "_id" : ObjectId("5b4366c780aef6886b682a4e"), "b" : 2 }
{ "_id" : ObjectId("5b4366c780aef6886b682a4f"), "c" : 3 }
Connecting with Python
You will need pymongo package.
import pymongo
auth_string = "mongodb://USER_NAME:USER_PASSWORD@HOST:PORT/DB_NAME"
client = pymongo.MongoClient(auth_string)
print(client.list_database_names())
db = client['test']
col = db['test_col']
for doc in col.find():
print(doc)
You should see output similar to this:
{'_id': ObjectId('5b4366c780aef6886b682a4d'), 'a': 1.0}
{'_id': ObjectId('5b4366c780aef6886b682a4e'), 'b': 2.0}
{'_id': ObjectId('5b4366c780aef6886b682a4f'), 'c': 3.0}
PORT is not required. If default port 27017 is used then connection string might look like this:
"mongodb://USER_NAME:USER_PASSWORD@HOST/DB_NAME"