官网下载:https://www.mongodb.com/downloadcent
或者软件包:mongodb-linux-x86_64-rhel62-4.0.2.tgz - 坚果云 - 云盘|网盘|企业网盘|同步|备份|无限空间|免费网络硬盘|企业云盘
安装启动MongoDB
首先需要安装相关软件包
yum -y groupinstall "Development tools"
yum -y install openssl openssl-devel
tar zxf mongodb-linux-x86_64-rhel62-4.0.2.tgz
mv mongodb-linux-x86_64-rhel62-4.0.2 /usr/local/mongodb
创建MongoDB的数据存储目录,日志存储目录 ,以及日志文件
mkdir -p ./data/db
mkdir -p ./logs
touch ./logs/mongodb.log
chmod -R 777 ./logs/mongodb.log
当MongoDB处于频繁访问的状态时,如果shell启动进程所占用的资源设置过低的话,将会产生错误导致无法连接到MongoDB实例。需设置ulimit -n和ulimit -u的值大于20000。
ulimit -n 25000
ulimit -u 25000
创建MongoDB配置文件,定义启动所需相关参数
cd /usr/local/mongodb
touch mongodb.conf
#端口号
port=27017
#db目录
dbpath=/usr/local/mongodb/data/db
#日志目录
logpath=/usr/local/mongodb/logs/mongodb.log
#后台
fork=true
#日志输出
logappend=true
#允许远程IP连接
bind_ip=0.0.0.0
启动测试
1)启动
./bin/mongod --config mongodb.conf
2)连接
./bin/mongo
设置内核参数,关闭NUMA
echo 0 >/proc/sys/vm/zone_reclaim_mode //当某个节点可用内存不足时系统会从其他节点分配内存
sysctl -w vm.zone_reclaim_mode=0 //永久设置
启动MongoDB
/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/mongodb.conf
测试
[root@localhost mongodb]# ./bin/mongod --config mongodb.conf
2020-04-19T19:06:26.930+0800 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
about to fork child process, waiting until server is ready for connections.
forked process: 5752
child process started successfully, parent exiting
[root@localhost mongodb]# ps -a
PID TTY TIME CMD
5778 pts/2 00:00:00 ps
[root@localhost mongodb]# ./bin/mongo
MongoDB shell version v4.0.2
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.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:
2020-04-19T19:06:28.234+0800 I CONTROL [initandlisten]
2020-04-19T19:06:28.234+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-04-19T19:06:28.234+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2020-04-19T19:06:28.234+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2020-04-19T19:06:28.234+0800 I CONTROL [initandlisten]
2020-04-19T19:06:28.234+0800 I CONTROL [initandlisten]
2020-04-19T19:06:28.234+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2020-04-19T19:06:28.234+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2020-04-19T19:06:28.234+0800 I CONTROL [initandlisten]
2020-04-19T19:06:28.234+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2020-04-19T19:06:28.234+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2020-04-19T19:06:28.234+0800 I CONTROL [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
> use qydb
switched to db qydb
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
> db.createCollection("tab001")
{ "ok" : 1 }
> show das
2020-04-19T19:25:28.759+0800 E QUERY [js] Error: don't know how to show [das] :
shellHelper.show@src/mongo/shell/utils.js:1055:11
shellHelper@src/mongo/shell/utils.js:766:15
@(shellhelp2):1:1
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
qydb 0.000GB
> db.tab001.insert({"name":"Leon"})
WriteResult({ "nInserted" : 1 })
> db.tab001.find()
{ "_id" : ObjectId("5e9c355eabb348ca8451f0ca"), "name" : "Leon" }
>
查看进程
netstat -anpt | grep mongod
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 33475/mongod
##停止MongoDB的方法
方法一:查看进程,使用kill命令;不能使用kill -9
ps aux | grep mongod
root 33475 0.3 10.0 1537520 100864 ? Sl 04:29 0:00 /usr/local/mongodb/bin/mongod --config /usr/local/mongodb/mongodb.conf
kill -2 33475
方法二:在客户端进去,使用shutdown命令
> use admin; switched to db admin > db.shutdownServer(); server should be down...
MongoDB 后台启动
配置 后台启动
# vi /usr/local/mongodb/mongodb.conf
后台启动命令
#后台Daemon启动, fork=true
fork=true
启动:
/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/mongodb.conf
查询mongo 进程
ps -ef|grep mongo
kill -9 608 进程id
添加防火墙规则
firewall-cmd --zone=public --add-port=27017/tcp --permanent
systemctl restart firewalld
firewall-cmd --query-port=27017/tcp
设置为开机自动启动
vim /etc/rc.local
添加:
rm -rf /usr/local/mongodb/data/db/mongod.lock
/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/mongodb.conf
推荐一款mongodb可视化工具
Robo 3T :Robo 3T | Free, open-source MongoDB GUI (formerly Robomongo)