Editing
My MongoDB notes
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
[https://www.mongodb.com/ MongoDB] is a general purpose, document-based, distributed database built for modern application developers and for the cloud era. == Ubuntu == == install == [https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/ Install and run MongoDB Community Edition] on Ubuntu 18.04 LTS The site has excellent documentation so I am simply providing overview of steps. # Import the public key used by the package management system. # Create a list file for MongoDB. # Reload local package database. # Install the MongoDB packages. == Run MongoDB Community Edition == # Start MongoDB with <code>sudo service mongod start</code> # Verify that MongoDB has started successfully with <code>sudo grep port /var/log/mongodb/mongod.log</code> or <code>sudo service mongod status</code> # Stop MongoDB with <code>sudo service mongod stop</code> # Restart MongoDB with <code>sudo service mongod restart</code> # Begin using MongoDB shell with <code>mongo</code> Verify MongoDB starts on Ubuntu 18.04 start up (MongoDB Community Edition default install does not start on Ubuntu 18.04 startup) <pre> $ sudo systemctl list-unit-files | grep mongod mongod.service enabled <--- If this value is not enabled, run below command to activate.</pre> Configure MongoDB to start on Ubuntu 18.04 start up <pre>$ sudo systemctl enable mongod.service Created symlink /etc/systemd/system/multi-user.target.wants/mongod.service β /lib/systemd/system/mongod.service. </pre> == mongo shell == [https://docs.mongodb.com/manual/mongo/ mongo shell manual] Running mongo at shell <code>$ mongo</code> Exit mongo shell <code>> exit</code> Display mongo shell help <code>> help</code> == add MongoDB user and enable remote management access == [https://docs.mongodb.com/manual/core/security-users/ User documentation] Steps to setup first user and enable access control [https://docs.mongodb.com/manual/tutorial/enable-authentication/ resource] create user administrator (sample file, use your own user and pwd values) <pre>use admin db.createUser( { user: "myUserAdmin", pwd: "abc123", roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ] } )</pre> Enable MongoDB instance to start with access control enabled by editing MongoDB config <code>$ sudo vi /etc/mongod.conf</code> Enable security.authorization configuration option <pre>security: authorization: enabled </pre> Bind IP address that enables remote connection to MongoDB (optional step) <pre># network interfaces net: port: 27017 bindIp: 127.0.0.1,192.168.1.109 <-- An IP address on your server that you want MongoDB to listen for TCP connections.</pre> Shutdown instance within mongo shell <code>db.adminCommand( { shutdown: 1 } )</code> '''After restart attempt MongoDB instance failed to start.''' The MongoDB log had this: <pre>$ sudo tail /var/log/mongodb/mongod.log 2019-08-11T15:41:04.940-0500 I CONTROL [initandlisten] distarch: x86_64 2019-08-11T15:41:04.940-0500 I CONTROL [initandlisten] target_arch: x86_64 2019-08-11T15:41:04.940-0500 I CONTROL [initandlisten] options: { config: "/etc/mongod.conf", net: { bindIp: "127.0.0.1,192.168.1.109", port: 27017 }, processManagement: { timeZoneInfo: "/usr/share/zoneinfo" }, security: { authorization: "enabled" }, storage: { dbPath: "/var/lib/mongodb", journal: { enabled: true } }, systemLog: { destination: "file", logAppend: true, path: "/var/log/mongodb/mongod.log" } } 2019-08-11T15:41:04.940-0500 E NETWORK [initandlisten] Failed to unlink socket file /tmp/mongodb-27017.sock Unknown error 2019-08-11T15:41:04.940-0500 F - [initandlisten] Fatal Assertion 40486 at src/mongo/transport/transport_layer_asio.cpp 685 2019-08-11T15:41:04.940-0500 F - [initandlisten] ***aborting after fassert() failure</pre> Root owns file and we want mongodb user to own file <pre>$ ls -l /tmp/mongodb-27017.sock srwx------ 1 root root 0 Aug 11 15:31 /tmp/mongodb-27017.sock </pre> The fix is to delete file so it can be recreated with correct permissions. <code>$ sudo rm /tmp/mongodb-27017.sock</code> Verify that TCP socket is closed or you will get ''Failed to set up listener: SocketException: Address already in use'' upon start up <pre>$ netstat -an | grep 27017 tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN </pre> TCP port 27017 in use so you have to close the port <code>$ sudo kill $(sudo lsof -t -i:27017)</code> Verify TCP port in use (should get no output) <code>$ netstat -an | grep 27017</code> Upon start attempt received another failure due to file permissions. Verify that mongodb user and group own all files and directories <code>$ ls -la /var/lib/mongodb</code> If you see root on any file or directory except '..' then change permissions using <code>$ sudo chown -R mongodb:mongodb /var/lib/mongodb</code> Now start MongoDB instance and verify access control and TCP port in bound to bindIp values from above. <pre>$ sudo systemctl start mongod $ sudo systemctl status mongod β mongod.service - MongoDB Database Server Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled) Active: active (running) since Sun 2019-08-11 16:14:13 CDT; 4s ago Docs: https://docs.mongodb.org/manual Main PID: 19662 (mongod) CGroup: /system.slice/mongod.service ββ19662 /usr/bin/mongod --config /etc/mongod.conf Aug 11 16:14:13 hammerhead systemd[1]: Started MongoDB Database Server. $ netstat -an | grep 27017 tcp 0 0 192.168.1.109:27017 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN unix 2 [ ACC ] STREAM LISTENING 13117024 /tmp/mongodb-27017.sock </pre> === Connect and authenticate as user administrator === Authenticate during connection to mongo <code>mongo --port 27017 -u "myUserAdmin" --authenticationDatabase "admin" -p</code> or <pre> $ mongo --port 27017 > use admin > db.auth("myUserAdmin", "abc123" )</pre> == Create additional users (example) == After being authenticated... <pre>> use test switched to db test > db.createUser( ... { ... user: "testdbuser", ... pwd: "xyz123", ... roles: [ { role: "readWrite", db: "test" }, ... { role: "read", db: "reporting" } ] ... } ... ) Successfully added user: { "user" : "testdbuser", "roles" : [ { "role" : "readWrite", "db" : "test" }, { "role" : "read", "db" : "reporting" } ] }</pre> == grant additional roles to user == [https://docs.mongodb.com/manual/reference/method/db.grantRolesToUser/#db.grantRolesToUser db.grantRolesToUser(username, roles, writeConcern)] == Manual references == [https://docs.mongodb.com/manual/reference/connection-string/ connection string ] page describes the URI formats for defining connections between applications and MongoDB instances in the official MongoDB drivers == Windows 10 (dev environment) == Download [https://www.mongodb.com/download-center/community MongoDB Community Edition] zip package Extract downloaded file and rename to root directory to mongodb. Move mongodb directory to user profile. On my Windows 10 dev machine the absolute path is <code>C:\Users\anon\mongodb</code>. Create directory in same profile that will hold mongodb collections called mongodb-data. The absolute path is <code>C:\Users\anon\mongodb-db</code>. To run MongoDB open shell in <code>C:\Users\anon\mongodb\bin</code> directory and type <code>.\mongod.exe --dbpath=\Users\anon\mongodb-data\</code> == MongoDB GUI == [https://robomongo.org/ Robo 3T] (formerly Robomongo) is the free lightweight GUI for MongoDB enthusiasts. == MongoDB drivers == [https://docs.mongodb.com/ecosystem/drivers/ MongoDB ecosystem drivers] [https://docs.mongodb.com/ecosystem/drivers/node/Node.js MongoDB Node.js drivers] [http://mongodb.github.io/node-mongodb-native/3.2/api/ MongoDB Node.js API docs] <center>[[Ubuntu|My Ubuntu notes]] | [[Windows 10 Notes|My Windows 10 notes]]</center>
Summary:
Please note that all contributions to GotOpinion may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
GotOpinion:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
Edit source
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information