一、下载软件
[root@db1 opt]# ls
mongodb-linux-x86_64-rhel70-4.0.14.tgz
二、解压
[root@db1 opt]# tar xf mongodb-linux-x86_64-rhel70-4.0.14.tgz
三、下载依赖软件
[root@db1 mongodb]# yum install -y openssl libcurl
四、创建目录
[root@db1 mongodb]# mkdir /opt/mongo_27017/{conf,log,pid} -p
[root@db1 mongodb]# mkdir /data/mongo_27017
五、编写配置文件
[root@db1 mongo_27017]# cat >/opt/mongo_27017/conf/mongodb.conf<<EOF
> systemLog:
> destination: file
> logAppend: true
> path: /opt/mongo_27017/log/mongodb.log
>
> storage:
> journal:
> enabled: true
> dbPath: /data/mongo_27017
> directoryPerDB: true
> wiredTiger:
> engineConfig:
> cacheSizeGB: 0.5
> directoryForIndexes: true
> collectionConfig:
> blockCompressor: zlib
> indexConfig:
> prefixCompression: true
>
> processManagement:
> fork: true
> pidFilePath: /opt/mongo_27017/pid/mongod.pid
>
> net:
> port: 27017
> bindIp: 127.0.0.1,10.0.0.51
> EOF
六、启动
[root@db1 mongo_27017]# /opt/mongodb/bin/mongod -f /opt/mongo_27017/conf/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 13407
child process started successfully, parent exiting
七、编写环境变量
[root@db1 mongo_27017]# echo 'export PATH=/opt/mongodb/bin:$PATH' >> /etc/profile
[root@db1 mongo_27017]# source /etc/profile
八、进入mongo
[root@db1 mongo_27017]# mongo
MongoDB shell version v4.0.14
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("5c33859d-42b6-4772-9705-5887811814e7") }
MongoDB server version: 4.0.14
Server has startup warnings:
2020-06-29T10:02:02.024+0800 I CONTROL [initandlisten]
2020-06-29T10:02:02.024+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-06-29T10:02:02.024+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2020-06-29T10:02:02.024+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2020-06-29T10:02:02.024+0800 I CONTROL [initandlisten]
2020-06-29T10:02:02.024+0800 I CONTROL [initandlisten]
2020-06-29T10:02:02.024+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2020-06-29T10:02:02.024+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2020-06-29T10:02:02.024+0800 I CONTROL [initandlisten]
2020-06-29T10:02:02.024+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2020-06-29T10:02:02.024+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2020-06-29T10:02:02.024+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()
---
>
九、警告优化(1)
> db.disableFreeMonitoring()
十、关闭mongo
[root@db1 mongo_27017]# mongod -f /opt/mongo_27017/conf/mongodb.conf --shutdown
killing process with pid: 13407
十二、警告优化(2)
[root@db1 mongo_27017]# groupadd mongo -g 1000
[root@db1 mongo_27017]# useradd mongo -g 1000 -u 1000 -M -s /sbin/nologin
[root@db1 mongo_27017]# id mongo
uid=1000(mongo) gid=1000(mongo) groups=1000(mongo)
[root@db1 mongo_27017]#
[root@db1 mongo_27017]# cat >/usr/lib/systemd/system/mongod.service<<EOF
> [Unit]
> Description=MongoDB Database Server
> Documentation=https://docs.mongodb.org/manual
> After=network.target
>
> [Service]
> User=mongo
> Group=mongo
> ExecStart=/opt/mongodb/bin/mongod -f /opt/mongo_27017/conf/mongodb.conf
> ExecStartPre=/usr/bin/chown -R mongo:mongo /opt/mongo_27017/
> ExecStartPre=/usr/bin/chown -R mongo:mongo /data/mongo_27017/
>
> PermissionsStartOnly=true
> PIDFile=/opt/mongo_27017/pid/mongod.pid
> Type=forking
> # file size
> LimitFSIZE=infinity
> # cpu time
> LimitCPU=infinity
> # virtual memory size
> LimitAS=infinity
> # open files
> LimitNOFILE=64000
> # processes/threads
> LimitNPROC=64000
> # locked memory
> LimitMEMLOCK=infinity
> # total threads (user+kernel)
> TasksMax=infinity
> TasksAccounting=false
> # Recommended limits for for mongod as specified in
> # http://docs.mongodb.org/manual/reference/ulimit/#recommended-settings
>
> [Install]
> WantedBy=multi-user.target
> EOF
[root@db1 mongo_27017]# systemctl daemon-reload
[root@db1 mongo_27017]# systemctl start mongod.service
[root@db1 mongo_27017]# ps -ef|grep mongo
mongo 14379 1 87 10:19 ? 00:00:02 /opt/mongodb/bin/mongod -f /opt/mongo_27017/conf/mongodb.conf
root 14414 7245 0 10:19 pts/0 00:00:00 grep --color=auto mongo
十三、警告优化(3)
[root@db1 mongo_27017]# echo 'never' > /sys/kernel/mm/transparent_hugepage/enabled
[root@db1 mongo_27017]# echo 'never' > /sys/kernel/mm/transparent_hugepage/defrag
十四、警告优化(4)
[root@db1 mongo_27017]# cat > /etc/profile<<EOF
> ulimit -f unlimited
> ulimit -t unlimited
> ulimit -v unlimited
> ulimit -n 64000
> ulimit -m unlimited
> ulimit -u 64000
> EOF
[root@db1 mongo_27017]# source /etc/profile
[root@db1 mongo_27017]# systemctl stop mongod
[root@db1 mongo_27017]# systemctl start mongod
十五、登录查看
[root@db1 mongo_27017]# mongo
MongoDB shell version v4.0.14
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("9976307d-8074-4b1d-ad38-efb309b36a85") }
MongoDB server version: 4.0.14
Server has startup warnings:
2020-06-29T10:27:26.028+0800 I CONTROL [initandlisten]
2020-06-29T10:27:26.028+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2020-06-29T10:27:26.028+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2020-06-29T10:27:26.028+0800 I CONTROL [initandlisten]
>
十六、查看库、表
> show databases
admin 0.000GB #系统预留库,mongodb的系统预留库
config 0.000GB #本地预留库,存储关键日志
local 0.000GB #配置信息库
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
> show tables
> show collections
> db #当前在哪个库
test
十七、切换库(创建)
> use wang
switched to db wang
> db
wang
mongo默认库在test库下,不需要新建库,直接use,此时库是虚的,只要插入数据才会真正创建
十八、shell命令行查看
[root@db1 ~]# echo "show dbs"|mongo
MongoDB shell version v4.0.14
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("03c9ecd4-8cb9-4690-a648-111182913841") }
MongoDB server version: 4.0.14
admin 0.000GB
config 0.000GB
local 0.000GB
bye
0 Comments