mongodb 安装及开机自动启动

       LinuxMongodb数据库安装及开机自启

1)注意事项:

A. 在这里使用的是SecureCRT进行操作。

B. 下面以#开头的都是对下面一行操作的解释

2)前提准备:

A. mongodb官网下载对应的安装包。这里我选择的是mongodb cummunity server下面linux版本中的rhelRed Hat Enterprise Linux的缩写)最新版本。

 mongodb 安装及开机自动启动_第1张图片

当前时间2018-1-28号最新版本:https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.6.2.tgz下载我们的mongodb-linux-x86_64-rhel70-3.6.2.tgz文件。

B. 建立mongodb.conf文件,其中内容如下

mongodb 安装及开机自动启动_第2张图片

bind_ip = 0.0.0.0

port = 47017

dbpath = /usr/local/mongodb3.6.2/bin/data/db

logpath = /usr/local/mongodb3.6.2/bin/data/logs/mongodb.log

logappend = true

fork = true

C. 建立mongod.service文件,其中内容如下

mongodb 安装及开机自动启动_第3张图片

[Unit]

 

Description=mongodb

After=network.target remote-fs.target nss-lookup.target

 

[Service]

Type=forking

ExecStart=/usr/local/mongodb3.6.2/bin/mongod --config /usr/local/mongodb3.6.2/bin/mongodb.conf

ExecReload=/bin/kill -s HUP $MAINPID

ExecStop=/usr/local/mongodb3.6.2/bin/mongod --shutdown --config /usr/local/mongodb3.6.2/bin/mongodb.conf

PrivateTmp=true

 

[Install]

WantedBy=multi-user.target

3)具体操作。

Last login: Sun Jan 28 13:44:07 2018

#进入/usr/local目录下

[root@localhost ~]# cd /usr/local

#查看当前目录路径

[root@localhost local]# pwd

/usr/local

#查看/usr/local下面的所有文件及文件目录

[root@localhost local]# ls

bin  etc  games  include  lib  lib64  libexec  sbin  share  src

#从当前平台导入文件  这里我们选择mongodb-linux-x86_64-rhel70-3.6.2.tgz文件。

[root@localhost local]# rz

rz waiting to receive.

开始 zmodem 传输。  按Ctrl+C 取消。

  100%   96854 KB 2421 KB/s 00:00:40       0 Errors..

#查看目录下的文件及文件夹,这是多出了mongodb-linux-x86_64-rhel70-3.6.2.tgz文件。

[root@localhost local]# ls

bin  etc  games  include  lib  lib64  libexec  mongodb-linux-x86_64-rhel70-3.6.2.tgz  sbin  share  src

#解压缩我们的mongodb-linux-x86_64-rhel70-3.6.2.tgz文件

[root@localhost local]# tar -zxvf mongodb-linux-x86_64-rhel70-3.6.2.tgz

mongodb-linux-x86_64-rhel70-3.6.2/README

mongodb-linux-x86_64-rhel70-3.6.2/THIRD-PARTY-NOTICES

mongodb-linux-x86_64-rhel70-3.6.2/MPL-2

mongodb-linux-x86_64-rhel70-3.6.2/GNU-AGPL-3.0

mongodb-linux-x86_64-rhel70-3.6.2/bin/mongodump

mongodb-linux-x86_64-rhel70-3.6.2/bin/mongorestore

mongodb-linux-x86_64-rhel70-3.6.2/bin/mongoexport

mongodb-linux-x86_64-rhel70-3.6.2/bin/mongoimport

mongodb-linux-x86_64-rhel70-3.6.2/bin/mongostat

mongodb-linux-x86_64-rhel70-3.6.2/bin/mongotop

mongodb-linux-x86_64-rhel70-3.6.2/bin/bsondump

mongodb-linux-x86_64-rhel70-3.6.2/bin/mongofiles

mongodb-linux-x86_64-rhel70-3.6.2/bin/mongoreplay

mongodb-linux-x86_64-rhel70-3.6.2/bin/mongoperf

mongodb-linux-x86_64-rhel70-3.6.2/bin/mongod

mongodb-linux-x86_64-rhel70-3.6.2/bin/mongos

mongodb-linux-x86_64-rhel70-3.6.2/bin/mongo

mongodb-linux-x86_64-rhel70-3.6.2/bin/install_compass

#查看我们的文件

[root@localhost local]# ls

bin  etc  games  include  lib  lib64  libexec  mongodb-linux-x86_64-rhel70-3.6.2  mongodb-linux-x86_64-rhel70-3.6.2.tgz  sbin  share  src

#重命名我们的文件将 mongodb-linux-x86_64-rhel70-3.6.2重名民成 mongodb3.6.2

[root@localhost local]# mv mongodb-linux-x86_64-rhel70-3.6.2 mongodb3.6.2

[root@localhost local]# ls

bin  etc  games  include  lib  lib64  libexec  mongodb3.6.2  mongodb-linux-x86_64-rhel70-3.6.2.tgz  sbin  share  src

#删除mongodb-linux-x86_64-rhel70-3.6.2.tgz

[root@localhost local]# rm -r mongodb-linux-x86_64-rhel70-3.6.2.tgz

rm: remove regular file 鈥榤ongodb-linux-x86_64-rhel70-3.6.2.tgzyes

[root@localhost local]# ls

bin  etc  games  include  lib  lib64  libexec  mongodb3.6.2  sbin  share  src

#进入 mongodb3.6.2

[root@localhost local]# cd mongodb3.6.2/

[root@localhost mongodb3.6.2]# ls

bin  GNU-AGPL-3.0  MPL-2  README  THIRD-PARTY-NOTICES

#进入bin

[root@localhost mongodb3.6.2]# cd bin

[root@localhost bin]# ls

bsondump  install_compass  mongo  mongod  mongodump  mongoexport  mongofiles  mongoimport  mongoperf  mongoreplay  mongorestore  mongos  mongostat  mongotop

#创建data目录

[root@localhost bin]# mkdir -p data

[root@localhost bin]# ls

bsondump  data  install_compass  mongo  mongod  mongodump  mongoexport  mongofiles  mongoimport  mongoperf  mongoreplay  mongorestore  mongos  mongostat  mongotop

[root@localhost bin]# cd data

[root@localhost data]# ls

[root@localhost data]# mkdir -p db

[root@localhost data]# mkdir -p logs

[root@localhost data]# ls

db  logs

[root@localhost data]# cd ..

[root@localhost bin]# ls

bsondump  data  install_compass  mongo  mongod  mongodump  mongoexport  mongofiles  mongoimport  mongoperf  mongoreplay  mongorestore  mongos  mongostat  mongotop

#选择mongodb.conf文件夹。上传至bin目录下。

[root@localhost bin]# rz

rz waiting to receive.

开始 zmodem 传输。  按Ctrl+C 取消。

  100%     169 bytes  169 bytes/s 00:00:01       0 Errors

#查看文件会包含mongodb.conf文件。

[root@localhost bin]# ls

bsondump  install_compass  mongod        mongodump    mongofiles   mongoperf    mongorestore  mongostat

data      mongo            mongodb.conf  mongoexport  mongoimport  mongoreplay  mongos        mongotop

#启动mongodb

[root@localhost bin]# ./mongod -f mongodb.conf

about to fork child process, waiting until server is ready for connections.

forked process: 1669

child process started successfully, parent exiting

#查看进程会包含47017这个mongodb进程,我们在mongodb.conf定义的端口为47017

[root@localhost bin]# netstat -lant

Active Internet connections (servers and established)

Proto Recv-Q Send-Q Local Address           Foreign Address         State      

tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN     

tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN     

tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     

tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     

tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     

tcp        0      0 0.0.0.0:47017           0.0.0.0:*               LISTEN     

tcp        0      0 192.168.0.105:22        192.168.0.102:57064     ESTABLISHED

tcp6       0      0 :::111                  :::*                    LISTEN     

tcp6       0      0 :::22                   :::*                    LISTEN     

tcp6       0      0 ::1:631                 :::*                    LISTEN     

tcp6       0      0 ::1:25                  :::*                    LISTEN   

#进入mongodbshell 

[root@localhost bin]# ./mongo --port=47017

MongoDB shell version v3.6.2

connecting to: mongodb://127.0.0.1:47017/

MongoDB server version: 3.6.2

Welcome to the MongoDB shell.

For interactive help, type "help".

For more comprehensive documentation, see

        http://docs.mongodb.org/

Questions? Try the support group

        http://groups.google.com/group/mongodb-user

Server has startup warnings:

2018-01-28T16:47:05.761+0800 I CONTROL  [initandlisten]

2018-01-28T16:47:05.761+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.

2018-01-28T16:47:05.761+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.

2018-01-28T16:47:05.761+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.

2018-01-28T16:47:05.761+0800 I CONTROL  [initandlisten]

2018-01-28T16:47:05.762+0800 I CONTROL  [initandlisten]

2018-01-28T16:47:05.762+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.

2018-01-28T16:47:05.762+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'

2018-01-28T16:47:05.762+0800 I CONTROL  [initandlisten]

2018-01-28T16:47:05.762+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.

2018-01-28T16:47:05.762+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'

2018-01-28T16:47:05.762+0800 I CONTROL  [initandlisten]

2018-01-28T16:48:08.932+0800 E -        [main] Error loading history file: FileOpenFailed: Unable to fopen() file /root/.dbshell: No such file or directory

> show dbs

admin  0.000GB

local  0.000GB

> exit

Bye

[root@localhost bin]# ps -ef|grep mongod

root       1669      1  0 16:47 ?        00:00:00 ./mongod -f mongodb.conf

root       1726   1510  0 16:49 pts/0    00:00:00 grep --color=auto mongod

[root@localhost bin]# kill -9 1669

[root@localhost bin]# netstat -lant

Active Internet connections (servers and established)

Proto Recv-Q Send-Q Local Address           Foreign Address         State      

tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN     

tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN     

tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     

tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     

tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     

tcp        0     52 192.168.0.105:22        192.168.0.102:57064     ESTABLISHED

tcp6       0      0 :::111                  :::*                    LISTEN     

tcp6       0      0 :::22                   :::*                    LISTEN     

tcp6       0      0 ::1:631                 :::*                    LISTEN     

tcp6       0      0 ::1:25                  :::*                    LISTEN   

#注意以下是开机自启动操作

[root@localhost bin]# cd /usr/lib/systemd/system

#选择mongodb.service文件夹。上传至/usr/lib/systemd/system目录下。

[root@localhost system]# rz

rz waiting to receive.

开始 zmodem 传输。  按Ctrl+C 取消。

  100%     411 bytes  411 bytes/s 00:00:01       0 Errors

 

[root@localhost system]# netstat -lant

Active Internet connections (servers and established)

Proto Recv-Q Send-Q Local Address           Foreign Address         State      

tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN     

tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN     

tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     

tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     

tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     

tcp        0     52 192.168.0.105:22        192.168.0.102:57064     ESTABLISHED

tcp6       0      0 :::111                  :::*                    LISTEN     

tcp6       0      0 :::22                   :::*                    LISTEN     

tcp6       0      0 ::1:631                 :::*                    LISTEN     

tcp6       0      0 ::1:25                  :::*                    LISTEN     

[root@localhost system]# systemctl daemon-reload

[root@localhost system]# systemctl start mongod

[root@localhost system]# systemctl status mongod

[0m mongod.service - mongodb

   Loaded: loaded (/usr/lib/systemd/system/mongod.service; disabled; vendor preset: disabled)

   Active: active (running) since Sun 2018-01-28 16:59:19 CST; 20s ago

  Process: 1856 ExecStart=/usr/local/mongodb3.6.2/bin/mongod --config /usr/local/mongodb3.6.2/bin/mongodb.conf (code=exited, status=0/SUCCESS)

 Main PID: 1860 (mongod)

   CGroup: /system.slice/mongod.service

           鈹斺攢1860 /usr/local/mongodb3.6.2/bin/mongod --config /usr/local/mongodb3.6.2/bin/mongodb.conf

 

Jan 28 16:59:18 localhost.localdomain systemd[1]: Starting mongodb...

Jan 28 16:59:19 localhost.localdomain mongod[1856]: about to fork child process, waiting until server is ready for connections.

Jan 28 16:59:19 localhost.localdomain mongod[1856]: forked process: 1860

Jan 28 16:59:19 localhost.localdomain mongod[1856]: child process started successfully, parent exiting

Jan 28 16:59:19 localhost.localdomain systemd[1]: Started mongodb.

[root@localhost system]# netstat -lant

Active Internet connections (servers and established)

Proto Recv-Q Send-Q Local Address           Foreign Address         State      

tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN     

tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN     

tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     

tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     

tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     

tcp        0      0 0.0.0.0:47017           0.0.0.0:*               LISTEN     

tcp        0     52 192.168.0.105:22        192.168.0.102:57064     ESTABLISHED

tcp6       0      0 :::111                  :::*                    LISTEN     

tcp6       0      0 :::22                   :::*                    LISTEN     

tcp6       0      0 ::1:631                 :::*                    LISTEN     

tcp6       0      0 ::1:25                  :::*                    LISTEN     

[root@localhost system]# systemctl stop mongod

[root@localhost system]# netstat -lant

Active Internet connections (servers and established)

Proto Recv-Q Send-Q Local Address           Foreign Address         State      

tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN     

tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN     

tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     

tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     

tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     

tcp        0     52 192.168.0.105:22        192.168.0.102:57064     ESTABLISHED

tcp6       0      0 :::111                  :::*                    LISTEN     

tcp6       0      0 :::22                   :::*                    LISTEN     

tcp6       0      0 ::1:631                 :::*                    LISTEN     

tcp6       0      0 ::1:25                  :::*                    LISTEN     

[root@localhost system]# systemctl enable mongod

Created symlink from /etc/systemd/system/multi-user.target.wants/mongod.service to /usr/lib/systemd/system/mongod.service.

[root@localhost system]#

自此自启动操作完毕我们重新启动我们的linux   执行命令netstat -lant

我们会发现47017端口是打开的,我们的mongodb自启动是成功的

 

 

你可能感兴趣的:(mongodb 安装及开机自动启动)