Implementation steps for installing MongoDB under Linux

  • 2020-05-13 04:39:03
  • OfStack

Implementation steps for installing MongoDB under Linux

Mongo DB is a non-relational database (NoSql) that is very popular in the IT industry. Its flexible data storage method is favored by current IT employees. Mongo DB is a good implementation of the object-oriented idea (OO idea). In Mongo DB, every record is an Document object. The biggest advantage of Mongo DB is that all data persistence operations do not require developers to manually write SQL statements, and it is easy to implement CRUD operations by directly calling methods. This article shows you how to quickly install mongodb for your reference.

1. Install mongodb configuration

Step 1: sets the system environment and ensures that the default port 27107 is available


### The current environment 
# cat /etc/issue
Red Hat Enterprise Linux Server release 6.5 (Santiago)

# vi /etc/selinux/config
SELINUX=disabled

Step 2: download the installation file


 Download address:  https://www.mongodb.org/downloads. 
 Or use it directly at the command prompt curl The command to download 
curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz

Step 3: extract the downloaded file


# pwd
/usr/local/src
# tar -xvf mongodb-linux-x86_64-rhel62-3.0.6.gz ### Note, this article is directly downloaded from the website, so the file is .gz

Step 4: copy unzipped files to run directory


# mkdir -p /var/lib/mongodb
# cp -R -n /usr/local/src/mongodb-linux-x86_64-rhel62-3.0.6/. /var/lib/mongodb/

Step 5: setting environment variables


e.g. export PATH=<mongodb-install-directory>/bin:$PATH
# vi ~/.bash_profile 
 export PATH=/var/lib/mongodb/bin:$PATH
# source ~/.bash_profile

Step 6: create the data directory


# mkdir -p /data/mongodata

2. Start and verify mongodb


### Start the mongo
# mongod --dbpath /data/mongodata

### The following contents are the relevant information output after startup 
2015-10-28T10:03:33.100+0800 I JOURNAL [initandlisten] journal dir=/data/mongodata/journal
2015-10-28T10:03:33.101+0800 I JOURNAL [initandlisten] recover : no journal files present, no recovery needed
2015-10-28T10:03:33.264+0800 I JOURNAL [initandlisten] preallocateIsFaster=true 2.18
2015-10-28T10:03:33.398+0800 I JOURNAL [durability] Durability thread started
2015-10-28T10:03:33.398+0800 I JOURNAL [journal writer] Journal writer thread started
2015-10-28T10:03:33.401+0800 I CONTROL [initandlisten] MongoDB starting : pid=10191 port=27017 dbpath=/data/mongodata 64-bit host=java_2
2015-10-28T10:03:33.401+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2015-10-28T10:03:33.401+0800 I CONTROL [initandlisten] 
2015-10-28T10:03:33.402+0800 I CONTROL [initandlisten] 
2015-10-28T10:03:33.402+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2015-10-28T10:03:33.402+0800 I CONTROL [initandlisten] **    We suggest setting it to 'never'
2015-10-28T10:03:33.402+0800 I CONTROL [initandlisten] 
2015-10-28T10:03:33.402+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2015-10-28T10:03:33.402+0800 I CONTROL [initandlisten] **    We suggest setting it to 'never'
2015-10-28T10:03:33.402+0800 I CONTROL [initandlisten] 
2015-10-28T10:03:33.402+0800 I CONTROL [initandlisten] db version v3.0.6
2015-10-28T10:03:33.402+0800 I CONTROL [initandlisten] git version: 1ef45a23a4c5e3480ac919b28afcba3c615488f2
2015-10-28T10:03:33.402+0800 I CONTROL [initandlisten] build info: Linux ip-10-67-194-123 2.6.32-220.el6.x86_64 #1 SMP Wed Nov 9 08:03:13 EST 2011 x86_64 BOOST_LIB_VERSION=1_49
2015-10-28T10:03:33.402+0800 I CONTROL [initandlisten] allocator: tcmalloc
2015-10-28T10:03:33.402+0800 I CONTROL [initandlisten] options: { storage: { dbPath: "/data/mongodata" } }
2015-10-28T10:03:33.404+0800 I INDEX  [initandlisten] allocating new ns file /data/mongodata/local.ns, filling with zeroes...
2015-10-28T10:03:33.491+0800 I STORAGE [FileAllocator] allocating new datafile /data/mongodata/local.0, filling with zeroes...
2015-10-28T10:03:33.491+0800 I STORAGE [FileAllocator] creating directory /data/mongodata/_tmp
2015-10-28T10:03:33.497+0800 I STORAGE [FileAllocator] done allocating datafile /data/mongodata/local.0, size: 64MB, took 0.001 secs
2015-10-28T10:03:33.511+0800 I NETWORK [initandlisten] waiting for connections on port 27017

### stop mongo , direct use ctrl + c
^C2015-10-28T10:09:21.510+0800 I CONTROL [signalProcessingThread] got signal 2 (Interrupt), will terminate after current cmd ends
2015-10-28T10:09:21.511+0800 I CONTROL [signalProcessingThread] now exiting
2015-10-28T10:09:21.511+0800 I NETWORK [signalProcessingThread] shutdown: going to close listening sockets...
2015-10-28T10:09:21.511+0800 I NETWORK [signalProcessingThread] closing listening socket: 5
2015-10-28T10:09:21.511+0800 I NETWORK [signalProcessingThread] closing listening socket: 6
2015-10-28T10:09:21.511+0800 I NETWORK [signalProcessingThread] removing socket file: /tmp/mongodb-27017.sock
2015-10-28T10:09:21.511+0800 I NETWORK [signalProcessingThread] shutdown: going to flush diaglog...
2015-10-28T10:09:21.511+0800 I NETWORK [signalProcessingThread] shutdown: going to close sockets...
2015-10-28T10:09:21.512+0800 I STORAGE [signalProcessingThread] shutdown: waiting for fs preallocator...
2015-10-28T10:09:21.512+0800 I STORAGE [signalProcessingThread] shutdown: final commit...
2015-10-28T10:09:21.512+0800 I JOURNAL [signalProcessingThread] journalCleanup...
2015-10-28T10:09:21.512+0800 I JOURNAL [signalProcessingThread] removeJournalFiles
2015-10-28T10:09:21.515+0800 I JOURNAL [signalProcessingThread] Terminating durability thread ...
2015-10-28T10:09:21.615+0800 I JOURNAL [journal writer] Journal writer thread stopped
2015-10-28T10:09:21.615+0800 I JOURNAL [durability] Durability thread stopped
2015-10-28T10:09:21.615+0800 I STORAGE [signalProcessingThread] shutdown: closing all files...
2015-10-28T10:09:21.618+0800 I STORAGE [signalProcessingThread] closeAllFiles() finished
2015-10-28T10:09:21.618+0800 I STORAGE [signalProcessingThread] shutdown: removing fs lock...
2015-10-28T10:09:21.618+0800 I CONTROL [signalProcessingThread] dbexit: rc: 0

### Fix two warnings during startup about use root User starts mongo Ignore the warning 
# echo "never" > /sys/kernel/mm/transparent_hugepage/enabled
# echo "never" > /sys/kernel/mm/transparent_hugepage/defrag

### Restart it again, put it in the background process, 
# mongod --dbpath /data/mongodata &

### View the process after it has started 
# ps -ef|grep mongo |grep -v grep
root   11115 27956 0 10:11 pts/2  00:00:00 mongod --dbpath /data/mongodata
# lsof -i:27017
COMMAND  PID USER  FD  TYPE  DEVICE SIZE/OFF NODE NAME
mongod 11115 root  5u IPv4 50567119   0t0 TCP *:27017 (LISTEN)

### use mongo Connect to the mongod
# mongo
MongoDB shell version: 3.0.6
connecting to: test
2015-10-28T10:14:30.685+0800 I NETWORK [initandlisten] connection accepted from 127.0.0.1:53907 #1 (1 connection now open)
Server has startup warnings: 
2015-10-28T10:11:49.217+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2015-10-28T10:11:49.217+0800 I CONTROL [initandlisten] 
> help
    db.help()          help on db methods
    db.mycoll.help()       help on collection methods
    sh.help()          sharding helpers
    rs.help()          replica set helpers
    help admin          administrative help
    help connect         connecting to a db help
    help keys          key shortcuts
    help misc          misc things to know
    help mr           mapreduce


    show dbs           show database names
    show collections       show collections in current database
    show users          show users in current database
    show profile         show most recent system.profile entries with time >= 1ms
    show logs          show the accessible logger names
    show log [name]       prints out the last segment of log in memory, 'global' is default
    use <db_name>        set current database
    db.foo.find()        list objects in collection foo
    db.foo.find( { a : 1 } )   list objects in foo where a == 1
    it              result of the last line evaluated; use to further iterate
    DBQuery.shellBatchSize = x  set default number of items to display on shell
    exit             quit the mongo shell
> db.getCollection("version");
test.version
> exit
bye

3. mongodb related tools


### Under the installation file README , describes the commonly used mongodb Related command line tools 
# more /usr/local/mongodb/README 
MongoDB README
Welcome to MongoDB!
COMPONENTS

 bin/mongod - The database process.
 bin/mongos - Sharding controller.
 bin/mongo - The database shell (uses interactive javascript).

UTILITIES

 bin/mongodump     - MongoDB dump tool - for backups, snapshots, etc..
 bin/mongorestore   - MongoDB restore a dump
 bin/mongoexport    - Export a single collection to test (JSON, CSV)
 bin/mongoimport    - Import from JSON or CSV
 bin/mongofiles    - Utility for putting and getting files from MongoDB GridFS
 bin/mongostat     - Show performance statistics

RUNNING

 For command line options invoke:

  $ ./mongod --help

 To run a single server database:

  $ mkdir /data/db
  $ ./mongod
  $
  $ # The mongo javascript shell connects to localhost and test database by default:
  $ ./mongo 
  > help

DRIVERS

 Client drivers for most programming languages are available at mongodb.org. Use the 
 shell ("mongo") for administrative tasks.

### Gets a single command usage 
# <command> --help
# mongod --help|more
Options:

General options:
 -h [ --help ]        show this usage information
 --version          show version information

# mongod --version
db version v3.0.6
git version: 1ef45a23a4c5e3480ac919b28afcba3c615488f2 

If you have any questions, please leave a message or come to the site community to exchange discussion, thank you for reading, hope to help you, thank you for your support of the site!


Related articles: