访问Mongodb官网https://www.mongodb.com/try/download/community,右侧有选择项,这里选择centos7+tgz+4.2.9选择项。linux
[root@guangzhou src]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.9.tgz --2020-09-29 15:01:18-- https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.9.tgz 正在解析主机 fastdl.mongodb.org (fastdl.mongodb.org)... 204.246.164.124, 204.246.164.81, 204.246.164.16, ... 正在链接 fastdl.mongodb.org (fastdl.mongodb.org)|204.246.164.124|:443... 已链接。 已发出 HTTP 请求,正在等待回应... 200 OK 长度:132776427 (127M) [application/gzip] 正在保存至: “mongodb-linux-x86_64-rhel70-4.2.9.tgz” 100%[========================================================================================>] 132,776,427 9.48MB/s 用时 14s 2020-09-29 15:01:33 (9.37 MB/s) - 已保存 “mongodb-linux-x86_64-rhel70-4.2.9.tgz” [132776427/132776427]) [root@guangzhou src]# tar zxvf mongodb-linux-x86_64-rhel70-4.2.9.tgz [root@guangzhou src]# mv mongodb-linux-x86_64-rhel70-4.2.9 /usr/local/mongodb [root@guangzhou src]# cd /usr/local/mongodb/ [root@guangzhou mongodb]# mkdir -p ./data/db [root@guangzhou mongodb]# mkdir log [root@guangzhou mongodb]# mv log logs [root@guangzhou mongodb]# touch ./logs/mongodb.log
新建配置文件:git
[root@guangzhou mongodb]# vim 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
启动mongodb:web
[root@guangzhou mongodb]# ./bin/mongod --config mongodb.conf about to fork child process, waiting until server is ready for connections. forked process: 17024 child process started successfully, parent exiting
链接mongodb:mongodb
[root@guangzhou mongodb]# ./bin/mongo MongoDB shell version v4.2.9 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("2e0543e1-1f66-44cf-bfd3-3af38f6abe80") } MongoDB server version: 4.2.9 Server has startup warnings: 2020-09-29T15:03:49.377+0800 I STORAGE [initandlisten] 2020-09-29T15:03:49.377+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine 2020-09-29T15:03:49.377+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem 2020-09-29T15:03:50.166+0800 I CONTROL [initandlisten] 2020-09-29T15:03:50.166+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database. 2020-09-29T15:03:50.166+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted. 2020-09-29T15:03:50.166+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended. 2020-09-29T15:03:50.166+0800 I CONTROL [initandlisten] 2020-09-29T15:03:50.167+0800 I CONTROL [initandlisten] 2020-09-29T15:03:50.167+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2020-09-29T15:03:50.167+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2020-09-29T15:03:50.167+0800 I CONTROL [initandlisten] 2020-09-29T15:03:50.167+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2020-09-29T15:03:50.167+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2020-09-29T15:03:50.167+0800 I CONTROL [initandlisten] 2020-09-29T15:03:50.167+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 4096 processes, 100001 files. Number of processes should be at least 50000.5 : 0.5 times number of files. 2020-09-29T15:03:50.167+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 admin switched to db admin > show databases; admin 0.000GB config 0.000GB local 0.000GB
#建立root用户 > db.createUser({user:"root",pwd:"freedom",roles:[{role:"userAdminAnyDatabase",db:"admin"},"readWriteAnyDatabase"]}) Successfully added user: { "user" : "root", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" }, "readWriteAnyDatabase" ] }
断开重联连接输入验证信息:shell
[root@guangzhou mongodb]# ./bin/mongo > use admin switched to db admin > db.auth('root','freedom') 1
配置service文件:数据库
[root@guangzhou mongodb]# touch /usr/lib/systemd/system/mongod.service [Unit] Description=mongodb After=network.target remote-fs.target nss-lookup.target [Service] Type=forking ExecStart=/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/conf/mongodb.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/usr/local/mongodb/bin/mongod --shutdown --config /usr/local/mongodb/conf/mongodb.conf User=root Group=root PrivateTmp=true Restart=always RestartSec=1
重启systemd服务,启动mongodb:vim
[root@guangzhou mongodb]# chmod +x /usr/lib/systemd/system/mongod.service [root@guangzhou mongodb]# systemctl daemon-reload [root@guangzhou ~]# systemctl status mongod ● mongod.service - mongodb Loaded: loaded (/usr/lib/systemd/system/mongod.service; disabled; vendor preset: disabled) Active: active (running) since 二 2020-09-29 15:44:53 CST; 5s ago Process: 25525 ExecStart=/usr/local/mongodb/bin/mongod --config /usr/local/mongodb/conf/mongodb.conf (code=exited, status=0/SUCCESS) Main PID: 25527 (mongod) CGroup: /system.slice/mongod.service └─25527 /usr/local/mongodb/bin/mongod --config /usr/local/mongodb/conf/mongodb.conf 9月 29 15:44:52 guangzhou systemd[1]: Starting mongodb... 9月 29 15:44:52 guangzhou mongod[25525]: about to fork child process, waiting until server is ready for connections. 9月 29 15:44:52 guangzhou mongod[25525]: forked process: 25527 9月 29 15:44:53 guangzhou mongod[25525]: child process started successfully, parent exiting 9月 29 15:44:53 guangzhou systemd[1]: Started mongodb. [root@guangzhou ~]# systemctl stop mongod [root@guangzhou ~]# systemctl status mongod ● mongod.service - mongodb Loaded: loaded (/usr/lib/systemd/system/mongod.service; disabled; vendor preset: disabled) Active: inactive (dead) 9月 29 15:33:54 guangzhou systemd[1]: Unit mongod.service entered failed state. 9月 29 15:33:54 guangzhou systemd[1]: mongod.service failed. 9月 29 15:44:52 guangzhou systemd[1]: Starting mongodb... 9月 29 15:44:52 guangzhou mongod[25525]: about to fork child process, waiting until server is ready for connections. 9月 29 15:44:52 guangzhou mongod[25525]: forked process: 25527 9月 29 15:44:53 guangzhou mongod[25525]: child process started successfully, parent exiting 9月 29 15:44:53 guangzhou systemd[1]: Started mongodb. 9月 29 15:45:04 guangzhou systemd[1]: Stopping mongodb... 9月 29 15:45:04 guangzhou mongod[25625]: killing process with pid: 25527 9月 29 15:45:05 guangzhou systemd[1]: Stopped mongodb.
客户端快捷方式建立:centos
[root@guangzhou mongodb]# ln -s /usr/local/mongodb/bin/mongo /usr/local/bin/mongo [root@guangzhou mongodb]# mongo -version MongoDB shell version v4.2.9 git version: 06402114114ffc5146fd4b55402c96f1dc9ec4b5 OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013 allocator: tcmalloc modules: none build environment: distmod: rhel70 distarch: x86_64 target_arch: x86_64
开机启动mongodb: systemctl enable mongodapi
手动启动mongodb:systemctl start mongodsession
中止mongodb: systemctl stop mongod
查看运行状态: systemctl status mongod