MongoDB installation under Linux

Source: Internet
Author: User
Tags mongodb server

1.Download MongoDB

2. unzip the file into a directory and rename it:

    1. [Root@localhost var]# tar-xzvf mongodb-linux-x86_64-enterprise-rhel62-3.4.9.tgz

    2. [Root@localhost var]# mv Mongodb-linux-x86_64-enterprise-rhel62-3.4.9/usr/local/mongodb


the Mongod under the bin is the service-side process of MongoDB, MONGO is its client, and other commands are used for MongoDB for other purposes such as MongoDB file export.

3. start MongoDB.

to set up a good MongoDB storage data files and log files directory, here is established under/data:

    1. [Root@localhost var]# cd/data/db

    2. [Root@localhost db]# mkdir mongodb_data

    3. [Root@localhost db]# mkdir mongodb_log

    4. [Root@localhost db]# ls

    5. Mongodb_data Mongodb_log

use Mongod to start MongoDB under the bin under the MongoDB installation directory,

    1. ./mongod--dbpath=/data/db/mongodb_data/--logpath=/data/db/mongodb_log/mongodb.log--logappend&

After you wait for the startup to succeed, you can see if the boot was successful, the default port number is 27017, and you can also specify an unused port at startup.

Start by looking at the port number to see if MongoDB is started.

  1. [Root@localhost /db]# NETSTAT-LANP | grep "27017"

  2. tcp         0       0  0.0 . 0.0 : 27017                 0.0 . 0.0 :*                     LISTEN            2442 /mongod                 

UNIX 2 [ACC] STREAM LISTENING 18203 2442/mongod/tmp/mongodb-27017.sock

As you can see, it started successfully and now accesses the database using the MONGO client.

    1. [Email protected] bin]#./mongo

    2. MongoDB Shell version v3.4.9

    3. Connecting to:mongodb://127.0.0.1:27017

    4. MongoDB Server version:3.4.9

    5. Connecting To:test

    6. >

Installation Successful

4. additional work.

put the Mongod in the service self-start item

To edit the/etc/rc.d/rc.local, add the following code and save it.

#add MONGONDB Service

rm-rf/data/db/mongodb_data/* &&/usr/local/mongodb/bin/mongod--dbpath=/data/db/mongodb_data/--logpath=/ Data/db/mongodb_log/mongodb.log--logappend&

We restart the computer to see if MongoDB is started, restart can be directly using the MONGO command login, the final discovery can be successful.

also , we use the MONGO command to log in to MongoDB and go to the directory where the MONGO command is located and execute./mongo, is that a bit of a problem? Therefore, we can simplify this by copy the command file to/usr/bin so that the MONGO command can be used in any directory.

    1. [Root@localhost bin]# ls

    2. Bsondump dbbak MONGO Mongod mongodump mongoexport mongofiles mongoimport mongorestore mongos mongosniff mongost At

    3. [Root@localhost bin]# CP mongo/usr/bin/

go to any directory try the MONGO command:

    1. [Root@localhost bin]# CD/

    2. [Root@localhost /]# MONGO

    3. MongoDB Shell version: 1.8. 1

    4. Connecting To:test

    5. >

You can see that the login was successful, which means we can use the MONGO command just like the LS command.

connecting to a database ( if test does not exist, create a test directly ) :

Use test

To display the database:

Show DBS

Insert a record (after switching to the specified database, you need to insert at least one document before the database name is displayed in show DBS):

Db.items.insert ({"Name": "Yiibai Tutorials"})

to create a user and password (on the admin Library):

Db.createuser ({User: "Lich", pwd: "goodjob1234", roles: [{role: "Useradminanydatabase", DB: "Admin"}]})

Verify user permissions (return 1 for user presence):

Db.auth (' Lich ', ' goodjob1234 ')

View All users in the admin library :
Use admin;
Db.system.users.find ();

View All tables:

Show tables;

In addition to the default boot mode above, you can also configure the mongod.conf the way to start MongoDB (not tested successfully)

StartMongodbhave a2Way, one is to start directly by default, and the other is to specify a configuration file. The startup mode is as follows:
1:/etc/init.d/mongod StartorService Mongod Start
2:mongod--config/etc/mongodb.conf
let's look at the configuration file:
Vi/etc/mongod.conf

#Log File Location
Logpath=/var/log/mongo/mongod.log

#write to log in append mode
Logappend=true

#whether to run in daemon mode
Fork = True

#default27017
#port = 27017

#Database File Location
Dbpath=/var/lib/mongo

#Enable recurring RecordsCpuUtilization and/ owait
#cpu = True

#whether to run in a secure authentication mode, default is non-secure way of authentication
#noauth = True
#auth = True

#Verbose logging output
#verbose = True

# Inspect all client data for validity in receipt (useful for
# Developing drivers)Validating client requests when developing drivers
#objcheck = True

# Enable DB Quota Management
#Enable database quota management
#quota = True
#SetOplogRecord Level
# Set oplogging level where n is
# 0=off (default)
# 1=w
# 2=r
# 3=both
# 7=w+some Reads
#diaglog =0

# diagnostic/debugging optionDynamic Debug Items
#nocursors = True

# Ignore Query HintsIgnore query hints
#nohints = True
#Disabledhttpinterface, the default islocalhost:28017
#nohttpinterface = True

#close the server-side script, which will greatly limit the functionality
# Turns off server-side scripting. This would result in greatly limited
# functionality
#noscripting = True
#Close the Scan table, any query will be a scan failure
# Turns off table scans. Any query this would do a table scan fails.
#notablescan = True
#Turn off data file pre-allocation
# Disable data File preallocation.
#noprealloc = True
#specify for the new database. NSsize of the file, Unit: MB
# specify. ns file size for new databases.
# nssize =

# Replication OptionsReplication Options
# in replicated MONGO databases, specify the replica set name here
#replSet =setname
# maximum size in megabytes for replication operation log
#oplogSize =1024
# Path to a key file storing authentication info for connections
# between replica set members
#specify the path to the key file where authentication information is stored
#keyFile =/path/to/keyfile

Or use the following configuration: (You can do a copy set test configuration, note the port number and the name of the replica set, the replica set name must be consistent)

28001.conf

bind_ip=192.168.20.144

port=28001

Logpath=/data/db/mongodb_log/28001.log

Logappend=true

dbpath=/data/db/mongodb_data28001

Replset=imooc

Fork=true

28002.conf

bind_ip=192.168.20.144

port=28002

Logpath=/data/db/mongodb_log/28002.log

Logappend=true

dbpath=/data/db/28002

Replset=imooc

Fork=true

28003.conf

bind_ip=192.168.20.144

port=28003

Logpath=/data/db/mongodb_log/28003.log

Logappend=true

dbpath=/data/db/28003

Replset=imooc

Fork=true

Specify port enable MONGO:

Cd/usr/local/mongo/bin

./mongo 192.168.20.144:28001/admin


MongoDB installation under Linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.