MacLochlainns Weblog

Michael McLaughlin's Technical Blog

Site Admin

Archive for the ‘MongoDB’ Category

Linux mongod Service

with one comment

The installation of MongoDB doesn’t do everything for you. In fact, the first time you start the mongod service, like this as the root user or sudoer user with the command:

service mongod start

A sudoer user will be prompted for their password, like

A typical MongoDB instance raises the following errors:

Redirecting to /bin/systemctl start mongod.service
[student@localhost cit425]$ mongo
MongoDB shell version v3.4.11
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.11
Server has startup warnings: 
2018-10-29T10:51:57.515-0600 I STORAGE  [initandlisten] 
2018-10-29T10:51:57.515-0600 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2018-10-29T10:51:57.515-0600 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2018-10-29T10:51:58.264-0600 I CONTROL  [initandlisten] 
2018-10-29T10:51:58.264-0600 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.                                                                                               
2018-10-29T10:51:58.264-0600 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.                                                                              
2018-10-29T10:51:58.264-0600 I CONTROL  [initandlisten]                                                  
2018-10-29T10:51:58.265-0600 I CONTROL  [initandlisten]                                                  
2018-10-29T10:51:58.265-0600 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 15580 processes, 64000 files. Number of processes should be at least 32000 : 0.5 times number of files.

You can fix this by following the MongoDB instructions for the Unix ulimit Settings, which will tell you to create a mongod file in the /etc/systemd/system directory. You should create this file as the root superuser. This is what you should put in the file:

[Unit]
Description=MongoDB
Documentation=man:mongo
 
[Service]
# Other directives omitted
# (file size)
LimitFSIZE=infinity
# (cpu time)
LimitCPU=infinity
# (virtual memory size)
LimitAS=infinity
# (locked-in-memory size)
LimitMEMLOCK=infinity
# (open files)
LimitNOFILE=64000
# (processes/threads)
LimitNPROC=64000

Then, you should be able to restart the mongod service without any warnings with this command:

service mongod restart

As always, I hope this helps somebody.

Written by maclochlainn

October 29th, 2018 at 11:39 am

Ruby GEM Mongo

without comments

While trying to use the Ruby gem utility to install the MongoDB gem, I encountered an error on a new Fedora 27 instance. This is the error message:

Fetching: bson-4.3.0.gem (100%)
Building native extensions.  This could take a while...
ERROR:  Error installing mongo:
        ERROR: Failed to build gem native extension.
 
    current directory: /usr/local/share/gems/gems/bson-4.3.0/ext/bson
/usr/bin/ruby -r ./siteconf20180517-49401-1htl7zc.rb extconf.rb
mkmf.rb can't find header files for ruby at /usr/share/include/ruby.h
 
extconf failed, exit code 1
 
Gem files will remain installed in /usr/local/share/gems/gems/bson-4.3.0 for inspection.
Results logged to /usr/local/lib64/gems/ruby/bson-4.3.0/gem_make.out
[student@localhost ~]$ ruby --version
ruby 2.4.3p205 (2017-12-14 revision 61247) [x86_64-linux]

There wasn’t much on the error but I checked the Ruby installation and ruby-devel package wasn’t installed by default. That’s odd since I choose to install the development components on the workstation.

Not a problem, I simply ran the yum utility as root through a sudoer user to install the ruby-devel package:

yum install -y ruby-devel

You should see a successful installation log like:

As I suspected, it fixed the problem immediately when I ran gem utility to install the mongo gem. The syntax to install the mongo gem is:

gem install mongo

The console output should be:

Building native extensions.  This could take a while...
Successfully installed bson-4.3.0
Fetching: mongo-2.5.3.gem (100%)
Successfully installed mongo-2.5.3
Parsing documentation for bson-4.3.0
Installing ri documentation for bson-4.3.0
Parsing documentation for mongo-2.5.3
Installing ri documentation for mongo-2.5.3
Done installing documentation for bson, mongo after 3 seconds
2 gems installed

You can now write and run a Ruby test MongoDB connection program. Here’s a basic MongoDBTest.rb Ruby file:

#!/usr/bin/ruby
 
# Require libraries.
require 'rubygems'
require 'mongo'
 
# Create a new connection to MongoDB.
$client = Mongo::Client.new(['localhost'])
 
puts 'Connected to MongoDB'
puts '===================='
puts 'Database Names:'
puts '---------------'
$client.database_names.each{|name| puts name}

After you create the MongoDBTest.rb file, you need to change its permissions with the chmod utility as follows:

chmod 755 MongoDBTest.rb

Then, you can run it as follows from the directory where you created the file:

./MongoDBTest.rb

Unless you’ve added something to the base MongoDB instance, it should print:

Connected to MongoDB
====================
Database Names:
---------------
admin
local

As always, I hope this helps somebody looking for a straightforward example and solution.

Written by maclochlainn

May 17th, 2018 at 2:00 pm

MongoDB on Fedora 27

without comments

I was very pleased to see MongoDB 3.4 in the repo for Fedora 27; however, I was quite frankly severely disappointed by the lack of concrete installation steps in the MongoDB in Action, Second Edition. The installation is straightforward as long as you remember that you don’t simply install a mongodb package. You need to install the mongodb and mongodb-server packages as well as some dependents.

As the root user, you can use yum to install them like this:

yum install -y mongo*

After installing the correct packages, you can start the MongoDB Daemon as a service using the root account or a sudoer account (if you get warning messages, you need to create a mongod file):

service mongod start

Finally, you can connect to the MongoDB client, like so:

mongo

Inside the mongo shell, you can check the MongoDB version with the following command:

db.version()

It should print:

3.4.11

Or, you can check the statistics of a new empty instance:

db.stats()

A fresh installation of MongoDB 3.4.11 on Fedora 27 should return:

{
        "db" : "test",
        "collections" : 0,
        "views" : 0,
        "objects" : 0,
        "avgObjSize" : 0,
        "dataSize" : 0,
        "storageSize" : 0,
        "numExtents" : 0,
        "indexes" : 0,
        "indexSize" : 0,
        "fileSize" : 0,
        "ok" : 1
}

You can insert data into MongoDB by creating a database, which is simply a file. You use an existing database or file by using the use command. If the database or file doesn’t exist already, the use command creates the database.

use videodb

You can create a collection or table with the insert function. The following creates an item collection and adds two JSON objects to the item collection.

db.item.insert({item:"Star Wars: A New Hope"})

It returns:

WriteResult({ "nInserted" : 1 })

You can create or add to an item collection or table with the same insert function call. You simply enclose the list of collection elements in the square brackets of a JavaScript array or list. The following inserts the second and third Star Wars films:

db.item.insert([{item:"Star Wars: The Empire Strikes Back"},{item:"Star Wars: Return of the Jedi"}])

It returns:

BulkWriteResult({
        "writeErrors" : [ ],
        "writeConcernErrors" : [ ],
        "nInserted" : 2,
        "nUpserted" : 0,
        "nMatched" : 0,
        "nModified" : 0,
        "nRemoved" : 0,
        "upserted" : [ ]

You can query the collection with the find function, like:

db.item.find()

It returns:

{ "_id" : ObjectId("5b02732435b861850bc51ef1"), "item" : "Star Wars: A New Hope" }
{ "_id" : ObjectId("5b02734635b861850bc51ef2"), "item" : "Star Wars: The Empire Strikes Back" }
{ "_id" : ObjectId("5b02734635b861850bc51ef3"), "item" : "Star Wars: Return of the Jedi" }

You can write large JavaScript files for bulk inserts. For example, the following users.js file:

db.users.insert(
[
  { contact_account: "CA_20170321_0001"
  , first_name: "Jonathan"
  , middle_name: "Eoin"
  , last_name: "MacGregor"
  , addresses:
    {
      street_address: ["1111 Broadway","Suite 101"]
    , city: "Oakland"
    , state: "CA"
    , zip: "94607" 
    }
  }
, { contact_account: "CA_20170328_0001"
  , first_name: "Remington"
  , middle_name: "Alain" 
  , last_name: "Dennison"
  , addresses:
    {
      street_address: ["2222 Broadway","Suite 121"]
    , city: "Oakland"
    , state: "CA"
    , zip: "94607" 
    }
  }
])

You can call the file with the load command from the interactive MongoDB command line:

load("/home/student/Code/mongodb/users.js")

You can query the results after you load the collection elements in the users collection with the following command:

db.users.find()

It returns:

{ "_id" : ObjectId("5b0270d535b861850bc51eef"), "contact_account" : "CA_20170321_0001", "first_name" : "Jonathan", "middle_name" : "Eoin", "last_name" : "MacGregor", "addresses" : { "street_address" : [ "1111 Broadway", "Suite 101" ], "city" : "Oakland", "state" : "CA", "zip" : "94607" } }
{ "_id" : ObjectId("5b0270d535b861850bc51ef0"), "contact_account" : "CA_20170328_0001", "first_name" : "Remington", "middle_name" : "Alain", "last_name" : "Dennison", "addresses" : { "street_address" : [ "2222 Broadway", "Suite 121" ], "city" : "Oakland", "state" : "CA", "zip" : "94607" } }

In both cases, the item and user collections add a unique identifier. The unique identifier is much like a surrogate key in a relational database. That’s why you should always define a unique natural key in the definition of your collection.

As always, I hope this helps.

Written by maclochlainn

May 16th, 2018 at 6:05 pm

How to install MongoDB

without comments

This post shows the yum command to install the MongoDB packages on Linux. More on setup and use will follow.

You install the MongoDB package with the following yum command:

yum install -y mongodb

You should see the following log file, which may also show a missing mirrored site:

Loaded plugins: langpacks, refresh-packagekit
cassandra/signature                                         |  819 B  00:00     
cassandra/signature                                         | 2.9 kB  00:00 !!! 
mysql-connectors-community                                  | 2.5 kB  00:00     
mysql-tools-community                                       | 2.5 kB  00:00     
mysql56-community                                           | 2.5 kB  00:00     
updates/20/x86_64/metalink                                  | 2.6 kB  00:00     
Resolving Dependencies
--> Running transaction check
---> Package mongodb.x86_64 0:2.4.6-1.fc20 will be installed
--> Processing Dependency: v8 for package: mongodb-2.4.6-1.fc20.x86_64
--> Processing Dependency: libv8.so.3()(64bit) for package: mongodb-2.4.6-1.fc20.x86_64
--> Processing Dependency: libtcmalloc.so.4()(64bit) for package: mongodb-2.4.6-1.fc20.x86_64
--> Processing Dependency: libboost_program_options.so.1.54.0()(64bit) for package: mongodb-2.4.6-1.fc20.x86_64
--> Processing Dependency: libboost_iostreams.so.1.54.0()(64bit) for package: mongodb-2.4.6-1.fc20.x86_64
--> Processing Dependency: libboost_filesystem.so.1.54.0()(64bit) for package: mongodb-2.4.6-1.fc20.x86_64
--> Running transaction check
---> Package boost-filesystem.x86_64 0:1.54.0-12.fc20 will be installed
---> Package boost-iostreams.x86_64 0:1.54.0-12.fc20 will be installed
---> Package boost-program-options.x86_64 0:1.54.0-12.fc20 will be installed
---> Package gperftools-libs.x86_64 0:2.1-4.fc20 will be installed
---> Package v8.x86_64 1:3.14.5.10-18.fc20 will be installed
--> Finished Dependency Resolution
 
Dependencies Resolved
 
================================================================================
 Package                   Arch       Version                 Repository   Size
================================================================================
Installing:
 mongodb                   x86_64     2.4.6-1.fc20            fedora       30 M
Installing for dependencies:
 boost-filesystem          x86_64     1.54.0-12.fc20          updates      66 k
 boost-iostreams           x86_64     1.54.0-12.fc20          updates      59 k
 boost-program-options     x86_64     1.54.0-12.fc20          updates     147 k
 gperftools-libs           x86_64     2.1-4.fc20              updates     266 k
 v8                        x86_64     1:3.14.5.10-18.fc20     updates     3.0 M
 
Transaction Summary
================================================================================
Install  1 Package (+5 Dependent packages)
 
Total download size: 34 M
Installed size: 101 M
Downloading packages:
(1/6): boost-iostreams-1.54.0-12.fc20.x86_64.rpm            |  59 kB  00:00     
(2/6): boost-filesystem-1.54.0-12.fc20.x86_64.rpm           |  66 kB  00:00     
(3/6): boost-program-options-1.54.0-12.fc20.x86_64.rpm      | 147 kB  00:00     
(4/6): gperftools-libs-2.1-4.fc20.x86_64.rpm                | 266 kB  00:00     
(5/6): v8-3.14.5.10-18.fc20.x86_64.rpm                      | 3.0 MB  00:00     
(6/6): mongodb-2.4.6-1.fc20.x86_64.rpm                      |  30 MB  00:04     
--------------------------------------------------------------------------------
Total                                              7.6 MB/s |  34 MB  00:04     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction (shutdown inhibited)
  Installing : 1:v8-3.14.5.10-18.fc20.x86_64                                1/6 
  Installing : boost-program-options-1.54.0-12.fc20.x86_64                  2/6 
  Installing : gperftools-libs-2.1-4.fc20.x86_64                            3/6 
  Installing : boost-filesystem-1.54.0-12.fc20.x86_64                       4/6 
  Installing : boost-iostreams-1.54.0-12.fc20.x86_64                        5/6 
  Installing : mongodb-2.4.6-1.fc20.x86_64                                  6/6 
  Verifying  : boost-iostreams-1.54.0-12.fc20.x86_64                        1/6 
  Verifying  : boost-filesystem-1.54.0-12.fc20.x86_64                       2/6 
  Verifying  : gperftools-libs-2.1-4.fc20.x86_64                            3/6 
  Verifying  : mongodb-2.4.6-1.fc20.x86_64                                  4/6 
  Verifying  : boost-program-options-1.54.0-12.fc20.x86_64                  5/6 
  Verifying  : 1:v8-3.14.5.10-18.fc20.x86_64                                6/6 
 
Installed:
  mongodb.x86_64 0:2.4.6-1.fc20                                                 
 
Dependency Installed:
  boost-filesystem.x86_64 0:1.54.0-12.fc20                                      
  boost-iostreams.x86_64 0:1.54.0-12.fc20                                       
  boost-program-options.x86_64 0:1.54.0-12.fc20                                 
  gperftools-libs.x86_64 0:2.1-4.fc20                                           
  v8.x86_64 1:3.14.5.10-18.fc20                                                 
 
Complete!

Hope this helps those looking for the command.

Written by maclochlainn

September 27th, 2017 at 10:16 pm