MongoDB basic installation and admin command script summary

  • 2020-05-27 07:30:31
  • OfStack

The installation
1. Install MongoDB for Linux
1.1 create data directory and log files:


mkdir -p /data/node/
touch /data/mongodb.log

1.2 installation of mongodb:


tar zxf mongodb-linux-x86_64-2.4.9.tgz 
mv mongodb-linux-x86_64-2.4.9 /opt/mongodb
echo "export PATH=$PATH:/opt/mongodb/bin" >>/etc/profile
source /etc/profile

1.3 create a new slave node profile:


cat >> ~/.mongodb.conf <<EOF

fork = ture
port = 11000
dbpath = /data/node
logpath = /data/mongodb.log
logappend = true
EOF

1.4 start MongoDB


mongod --config ~/.mongodb.conf

2. Install MongoDB for Windows
2.1 download and unzip


mongodb-win32-x86_64-2008plus-2.4.9.zip

2.2 adding services


# mongod -f d:\mongodb\mongodb.cfg --serviceName MongoBD --install

2.3 startup service


# net start mongodb 

2.4 delete service


# mongod --remove

2.5 MongoDB launch profile


mongodb.cfg -->
logpath=d:\data\mongo.log 
dbpath=d:\data
logappend=true
auth=true
#fork=true

management
There are quick ways to manage MongoDB, whether it's a backup or a multi-node system with replication. The system will automatically complete various configurations.
(1) MongoDB is a normal command line program called with mongod.
(2) MongoDB provides built-in management interface and monitoring functions, which are easy to integrate with the third party monitoring package.
(3) MongoDB supports basic, database-level user authentication, including read-only users, and independent administrator privileges.
(4) multiple ways to backup MongoDB.

1. Start and stop MongoDB
1.1 command line startup
Command line startup, mongod --help can see all options.
(1) -- the default value of dbpath is /data/db/. Each Mongod process needs a separate data directory, and to have three instances of mongod, you must have three separate data directories. When mongodb starts, the mongod.lock file is created in the data directory to prevent other mongod processes from using the data directory.
(2) -- port
Specify the listening port, which defaults to 27017, to run multiple mongod processes, and you need to specify a different port number.
(3) -- fork
Run mongodb as a daemon to create the server process.
(4) -- logpath
Specify the log output path, not the output command line, which overrides the existing file and clears the original journal record. To keep it, use the --logappend option.
(5) -- config
Specifies a configuration file to load various options not specified on the command line.
1.2 configuration files
MongoBD supports getting configuration information from files. The configuration file can be specified using the -f or --config option. Such as:


tar zxf mongodb-linux-x86_64-2.4.9.tgz 
mv mongodb-linux-x86_64-2.4.9 /opt/mongodb
echo "export PATH=$PATH:/opt/mongodb/bin" >>/etc/profile
source /etc/profile
0

tar zxf mongodb-linux-x86_64-2.4.9.tgz 
mv mongodb-linux-x86_64-2.4.9 /opt/mongodb
echo "export PATH=$PATH:/opt/mongodb/bin" >>/etc/profile
source /etc/profile
1

1.3 stop MongoDB
Database shutdown method:
(1) kill-2 SIGTERM or kill-2 SIGINT can safely exit, waiting for the currently running operation or file pre-allocation, closing all open connections, flushing the cached data to disk, and finally stopping.
Cannot kill-9 (SIGKILL), this will cause data file corruption.
(2) use the administrative command {"shutdown" : 1}


tar zxf mongodb-linux-x86_64-2.4.9.tgz 
mv mongodb-linux-x86_64-2.4.9 /opt/mongodb
echo "export PATH=$PATH:/opt/mongodb/bin" >>/etc/profile
source /etc/profile
2

2. Monitor
2.1 use the management interface
When you start MongoDB, you start a very basic HTTP server that is listening on a port 1000 larger than the primary server. The rendered information can be viewed either through shell or through the web page.
To take advantage of the management interface, turn on REST support with the --reset option. You can also close the administration interface at startup using --nohttpinterface.
2.2 serverStatus
serverStatus presents the internal details of MongoDB, such as the version of the server, the running time, and the current number of connections.


MongoDB shell version: 2.4.9
connecting to: 127.0.0.1:10001/test
> db.runCommand({serverStatus : 1})

"globalLock" represents how much time (in microseconds) the global write lock takes up on the server. mem" contains how much data the server memory maps, the virtual memory and resident memory footprint of the server process;
(1) "indexCounters" means the number of times B tree is retrieved in disk and memory;
(2)"backgroudFlushing" means how many times fsync has been done backstage and how much time has been spent;
(3) "opcounters" contains the number of times of each major operation.
2.3 mongostat
mongostat outputs the important information provided by serverStatus. Output a new 1 line per second, which is better than the static count you saw earlier. Respectively insert/s, commands/s, vsize and % locked.
2.4 the third plug-in
MongbDB plugins for Nagios, Munin, Ganglia, Cacti.

3. The security
MongoDB supports authentication of a single connection.
3.1 basic knowledge of certification
Each database in an MongoDB instance can have many users. Once authentication is enabled, only database authenticated users can perform read and write operations.
After authentication, the administrator can read and write all the databases and execute specific administrative commands.
You need to have an administrator account before you can enable security authentication.


tar zxf mongodb-linux-x86_64-2.4.9.tgz 
mv mongodb-linux-x86_64-2.4.9 /opt/mongodb
echo "export PATH=$PATH:/opt/mongodb/bin" >>/etc/profile
source /etc/profile
4

Creating a read-only user in shell sets the third parameter of adduser to true. Calls to addUser() must have write permissions to the database.
Not only can addUser add new users, it can also change user passwords or read-only states.
Restart the server, add the --auth option, and turn on the security check.


tar zxf mongodb-linux-x86_64-2.4.9.tgz 
mv mongodb-linux-x86_64-2.4.9 /opt/mongodb
echo "export PATH=$PATH:/opt/mongodb/bin" >>/etc/profile
source /etc/profile
5

3.2 working principles of certification
Database user accounts are stored as documents in the system.users collection.


tar zxf mongodb-linux-x86_64-2.4.9.tgz 
mv mongodb-linux-x86_64-2.4.9 /opt/mongodb
echo "export PATH=$PATH:/opt/mongodb/bin" >>/etc/profile
source /etc/profile
6

You can perform


tar zxf mongodb-linux-x86_64-2.4.9.tgz 
mv mongodb-linux-x86_64-2.4.9 /opt/mongodb
echo "export PATH=$PATH:/opt/mongodb/bin" >>/etc/profile
source /etc/profile
7

Delete accounts.
When a user authenticates, the server binds the authentication and connection to track the authentication.
3.3 other installation considerations
There are many options to lock MongoDB instances in addition to authentication. Even with authentication, the MongoDB transport protocol is not encrypted. If encryption is required, use the SHH tunnel or similar to do the encryption between the client and the server.
The MongoDB server is recommended to be placed in a firewall or Intranet, but if it needs to be accessed externally, you can specify that mongod is bound to the local IP address using the --bindip option.
You can use --noscripting to completely disable server-side JavaScript execution.


Related articles: