mongodb 安裝及開機自動啟動
Linux下Mongodb資料庫安裝及開機自啟
1)注意事項:
A. 在這裡使用的是SecureCRT進行操作。
B. 下面以#開頭的都是對下面一行操作的解釋
2)前提準備:
A. 到mongodb官網下載對應的安裝包。這裡我選擇的是mongodb cummunity server下面linux版本中的rhel(Red Hat Enterprise Linux的縮寫)最新版本。
當前時間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檔案,其中內容如下
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檔案,其中內容如下
[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目錄下
[[email protected] ~]# cd /usr/local
#檢視當前目錄路徑
[[email protected] local]# pwd
/usr/local
#檢視/usr/local下面的所有檔案及檔案目錄
[[email protected] local]# ls
bin etc games include lib lib64 libexec sbin share src
#從當前平臺匯入檔案 這裡我們選擇mongodb-linux-x86_64-rhel70-3.6.2.tgz檔案。
[[email protected] 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檔案。
[[email protected] 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檔案
[[email protected] 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
#檢視我們的檔案
[[email protected] 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
[[email protected] local]# mv mongodb-linux-x86_64-rhel70-3.6.2 mongodb3.6.2
[[email protected] 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
[[email protected] local]# rm -r mongodb-linux-x86_64-rhel70-3.6.2.tgz
rm: remove regular file 鈥榤ongodb-linux-x86_64-rhel70-3.6.2.tgz鈥yes
[[email protected] local]# ls
bin etc games include lib lib64 libexec mongodb3.6.2 sbin share src
#進入 mongodb3.6.2
[[email protected] local]# cd mongodb3.6.2/
[[email protected] mongodb3.6.2]# ls
bin GNU-AGPL-3.0 MPL-2 README THIRD-PARTY-NOTICES
#進入bin
[[email protected] mongodb3.6.2]# cd bin
[[email protected] bin]# ls
bsondump install_compass mongo mongod mongodump mongoexport mongofiles mongoimport mongoperf mongoreplay mongorestore mongos mongostat mongotop
#建立data目錄
[[email protected] bin]# mkdir -p data
[[email protected] bin]# ls
bsondump data install_compass mongo mongod mongodump mongoexport mongofiles mongoimport mongoperf mongoreplay mongorestore mongos mongostat mongotop
[[email protected] bin]# cd data
[[email protected] data]# ls
[[email protected] data]# mkdir -p db
[[email protected] data]# mkdir -p logs
[[email protected] data]# ls
db logs
[[email protected] data]# cd ..
[[email protected] bin]# ls
bsondump data install_compass mongo mongod mongodump mongoexport mongofiles mongoimport mongoperf mongoreplay mongorestore mongos mongostat mongotop
#選擇mongodb.conf資料夾。上傳至bin目錄下。
[[email protected] bin]# rz
rz waiting to receive.
開始 zmodem 傳輸。 按Ctrl+C 取消。
100% 169 bytes 169 bytes/s 00:00:01 0 Errors
#檢視檔案會包含mongodb.conf檔案。
[[email protected] bin]# ls
bsondump install_compass mongod mongodump mongofiles mongoperf mongorestore mongostat
data mongo mongodb.conf mongoexport mongoimport mongoreplay mongos mongotop
#啟動mongodb
[[email protected] 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
[[email protected] 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
#進入mongodb的shell端
[[email protected] 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
[[email protected] 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
[[email protected] bin]# kill -9 1669
[[email protected] 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
#注意以下是開機自啟動操作
[[email protected] bin]# cd /usr/lib/systemd/system
#選擇mongodb.service資料夾。上傳至/usr/lib/systemd/system目錄下。
[[email protected] system]# rz
rz waiting to receive.
開始 zmodem 傳輸。 按Ctrl+C 取消。
100% 411 bytes 411 bytes/s 00:00:01 0 Errors
[[email protected] 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
[[email protected] system]# systemctl daemon-reload
[[email protected] system]# systemctl start mongod
[[email protected] 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.
[[email protected] 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
[[email protected] system]# systemctl stop mongod
[[email protected] 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
[[email protected] system]# systemctl enable mongod
Created symlink from /etc/systemd/system/multi-user.target.wants/mongod.service to /usr/lib/systemd/system/mongod.service.
[[email protected] system]#
自此自啟動操作完畢我們重新啟動我們的linux 執行命令netstat -lant
我們會發現47017埠是開啟的,我們的mongodb自啟動是成功的