mongodb单个服务部署
mongodb3.2.8安装步骤:
1、系统准备 (1)redhat或cnetos6.2以上系统 (2)系统开发包完整 (3)ip地址和hosts文件解析正常 (4)iptables防火墙&SElinux关闭 (5)关闭大页内存机制 ######################################################################## root用户下 在vi /etc/rc.local最后添加如下代码 if test -f /sys/kernel/mm/transparent_hugepage/enabled; then echo never > /sys/kernel/mm/transparent_hugepage/enabled fi if test -f /sys/kernel/mm/transparent_hugepage/defrag; then echo never > /sys/kernel/mm/transparent_hugepage/defrag fi 其他系统关闭参照官方文档: https://docs.mongodb.com/manual/tutorial/transparent-huge-pages/ 参考地址:“官方下载地址:https://www.mongodb.com/try/download 在这里根据自己的需要,选择下载对应系统的MongoDB数据库版本(注:在MongoDB版本中,是偶数:如3.2.x、3.4.x、3.6.x表示正式版【可用于生产环境】,是奇数:3.1.x、3.3.x、3.5.x表示开发版,而OS系统版本:自动给你推荐你当前适合的MongoDB数据库版本)。 然后点击Download按扭后,进入下载页面: 注:进入上面这个下载页面后,... --------------- 为什么要关闭? Transparent Huge Pages (THP) is a Linux memory management system that reduces the overhead of Translation Lookaside Buffer (TLB) lookups on machines with large amounts of memory by using larger memory pages. However, database workloads often perform poorly with THP, because they tend to have sparse rather than contiguous memory access patterns. You should disable THP on Linux machines to ensure best performance with MongoDB. 透明巨型页面(THP)是一个Linux内存管理系统 这减少了转换查找缓冲区(TLB)的开销 通过使用更大的内存页查找具有大量内存的机器。 然而,使用THP时,数据库工作负载通常表现不佳, 因为它们往往具有稀疏而非连续的内存访问模式。 您应该在Linux机器上禁用THP,以确保MongoDB的最佳性能。 -------------- ############################################################################ 2、mongodb安装 (1)创建所需用户和组 groupadd -g 800 mongod useradd -u 801 -g mongod mongod passwd mongod (2)创建mongodb所需目录结构 mkdir -p /mongodb/bin mkdir -p /mongodb/conf mkdir -p /mongodb/log mkdir -p /mongodb/data (3)上传并解压软件到指定位置 mongodb-linux-x86_64-3.2.8.tgz cd mongodb-linux-x86_64-3.2.8/bin/ cp * /mongodb/bin (4)设置目录结构权限 chown -R mongod:mongod /mongodb (5)设置用户环境变量 su - mongod vi .bash_profile export PATH=/mongodb/bin:$PATH source .bash_profile (6)启动mongodb mongod --dbpath=/mongodb/data --logpath=/mongodb/log/mongodb.log --port=27017 --logappend --fork (7)登录mongodb [mongod@server2 ~]$ mongo MongoDB shell version: 3.2.8 connecting to: test (8)使用配置文件 启动命令: mongod -f /mongodb/conf/mongodb.conf mongod -f /mongodb/conf/mongodb.conf --shutdown ------------ vi /mongodb/conf/mongodb.conf logpath= dbpath= port= logappend= fork= auth= +++++++++++++++++++ (YAML模式:) -- NOTE: YAML does not support tab characters for indentation: use spaces instead. -- systemLog: destination: file path: "/mongodb/log/mongod.log" logAppend: true storage: journal: enabled: true dbPath: "<PATH>" processManagement: fork: true pidFilePath: <string> net: bindIp: <ip> port: <port> setParameter: enableLocalhostAuthBypass: false security: authorization: enabled replication: oplogSizeMB: <NUM> replSetName: "<REPSETNAME>" secondaryIndexPrefetch: "all" sharding: clusterRole: <string> archiveMovedChunks: <boolean> ---for mongos only replication: localPingThresholdMs: <int> sharding: configDB: <string> --- ......... ++++++++++++++++++++++ (9)mongodb的关闭方式 ---kill进程形式 $ kill -2 PID 原理:-2表示向mongod进程发送SIGINT信号。 或 $ kill -4 PID 原理:-4表示向mognod进程发送SIGTERM信号。 ---自带模式 admin> db.shutdownServer() 或 admin> db.adminCommand({shutdown:1}) 或 $ mongod -f mongodb.conf --shutdown killing process with pid: 1621 注:mongod进程收到SIGINT信号或者SIGTERM信号,会做一些处理 > 关闭所有打开的连接 > 将内存数据强制刷新到磁盘 > 当前的操作执行完毕 > ... > 安全停止 !!!切记kill -9 > 数据库直接关闭 > 数据丢失 > 数据文件损失 > 修复数据库(成本高,有风险)
安装执行过程:
创建所需用户和组 [root@mcw01 ~]$ groupadd -g 800 mongod [root@mcw01 ~]$ useradd -u 801 -g mongod mongod [root@mcw01 ~]$ passwd mongod Changing password for user mongod. New password: BAD PASSWORD: The password is shorter than 8 characters Retype new password: passwd: all authentication tokens updated successfully. 创建mongodb所需目录结构 [root@mcw01 ~]$ mkdir -p /mongodb/bin [root@mcw01 ~]$ mkdir -p /mongodb/conf [root@mcw01 ~]$ mkdir -p /mongodb/log [root@mcw01 ~]$ mkdir -p /mongodb/data [root@mcw01 ~]$ rz 上传并解压软件到指定位置 [root@mcw01 ~]$ ls anaconda-ks.cfg mongodb-linux-x86_64-3.2.8.tgz [root@mcw01 ~]$ tar xf mongodb-linux-x86_64-3.2.8.tgz [root@mcw01 ~]$ ls anaconda-ks.cfg mongodb-linux-x86_64-3.2.8 mongodb-linux-x86_64-3.2.8.tgz [root@mcw01 ~]$ mkdir /application [root@mcw01 ~]$ mv mongodb-linux-x86_64-3.2.8 /application/ [root@mcw01 ~]$ cd /application/mongodb-linux-x86_64-3.2.8/ [root@mcw01 /application/mongodb-linux-x86_64-3.2.8]$ ls bin GNU-AGPL-3.0 MPL-2 README THIRD-PARTY-NOTICES [root@mcw01 /application/mongodb-linux-x86_64-3.2.8]$ cd bin/ [root@mcw01 /application/mongodb-linux-x86_64-3.2.8/bin]$ ls #查看解压的mongodb包 bsondump mongo mongod mongodump mongoexport mongofiles mongoimport mongooplog mongoperf mongorestore mongos mongostat mongotop [root@mcw01 /application/mongodb-linux-x86_64-3.2.8/bin]$ cp * /mongodb/bin/ [root@mcw01 /application/mongodb-linux-x86_64-3.2.8/bin]$ cd [root@mcw01 ~]$ chown -R mongod:mongod /mongodb #设置目录结构权限 [root@mcw01 ~]$ su - mongod #设置用户环境变量 [mongod@mcw01 ~]$ vi .bash_profile [mongod@mcw01 ~]$ source .bashprofile -bash: .bashprofile: No such file or directory [mongod@mcw01 ~]$ source .bash_profile 命令行启动mongodb [mongod@mcw01 ~]$ mongod --dbpath=/mongodb/data --logpath=/mongodb/log/mongodb.log --port=27017 --logappend --fork #命令行启动mongodb about to fork child process, waiting until server is ready for connections. forked process: 15507 child process started successfully, parent exiting 登录mongodb [mongod@mcw01 ~]$ mongo MongoDB shell version: 3.2.8 connecting to: test 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: 2022-03-03T10:46:14.929+0800 I CONTROL [initandlisten] 2022-03-03T10:46:14.929+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. 2022-03-03T10:46:14.929+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2022-03-03T10:46:14.930+0800 I CONTROL [initandlisten] 2022-03-03T10:46:14.930+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. 2022-03-03T10:46:14.930+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never' 2022-03-03T10:46:14.930+0800 I CONTROL [initandlisten] 2022-03-03T10:46:14.930+0800 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 4096 processes, 65535 files. Number of processes should be at least 32767.5 : 0.5 times number of files. 2022-03-03T10:46:14.930+0800 I CONTROL [initandlisten] > db test > db.version() 3.2.8 > bye [mongod@mcw01 ~]$ ps -ef|grep mongo root 15479 15361 0 10:44 pts/0 00:00:00 su - mongod mongod 15480 15479 0 10:44 pts/0 00:00:00 -bash mongod 15507 1 1 10:46 ? 00:00:02 mongod --dbpath=/mongodb/data --logpath=/mongodb/log/mongodb.log --port=27017 --logappend --fork mongod 15529 15480 0 10:50 pts/0 00:00:00 ps -ef mongod 15530 15480 0 10:50 pts/0 00:00:00 grep --color=auto mongo [mongod@mcw01 ~]$ kill -2 15507 #杀进程方式停止服务 [mongod@mcw01 ~]$ ps -ef|grep -v grep|grep mongo root 15479 15361 0 10:44 pts/0 00:00:00 su - mongod mongod 15480 15479 0 10:44 pts/0 00:00:00 -bash mongod 15533 15480 0 10:51 pts/0 00:00:00 ps -ef [mongod@mcw01 ~]$ tree /mongodb/ #查看mongodb的目录结构 /mongodb/ ├── bin │ ├── bsondump │ ├── mongo │ ├── mongod │ ├── mongodump │ ├── mongoexport │ ├── mongofiles │ ├── mongoimport │ ├── mongooplog │ ├── mongoperf │ ├── mongorestore │ ├── mongos │ ├── mongostat │ └── mongotop ├── conf ├── data │ ├── collection-0--6278657654986478919.wt │ ├── diagnostic.data │ │ └── metrics.2022-03-03T02-46-14Z-00000 │ ├── index-1--6278657654986478919.wt │ ├── journal │ │ ├── WiredTigerLog.0000000001 │ │ ├── WiredTigerPreplog.0000000001 │ │ └── WiredTigerPreplog.0000000002 │ ├── _mdb_catalog.wt │ ├── mongod.lock │ ├── sizeStorer.wt │ ├── storage.bson │ ├── WiredTiger │ ├── WiredTigerLAS.wt │ ├── WiredTiger.lock │ ├── WiredTiger.turtle │ └── WiredTiger.wt └── log └── mongodb.log 6 directories, 29 files 使用配置文件启动mongodb [mongod@mcw01 ~]$ vi /mongodb/config/mongodb.conf [mongod@mcw01 ~]$ vi /mongodb/ bin/ conf/ data/ log/ [mongod@mcw01 ~]$ vi /mongodb/conf/mongodb.conf [mongod@mcw01 ~]$ cat /mongodb/conf/mongodb.conf logpath= dbpath= port= logappend= fork= auth= [mongod@mcw01 ~]$ mongod -f /mongodb/config/mongodb.conf Error reading config file: No such file or directory try 'mongod --help' for more information [mongod@mcw01 ~]$ mongod -f /mongodb/conf/mongodb.conf Error parsing option "port" as int: No digits try 'mongod --help' for more information [mongod@mcw01 ~]$ vim /mongodb/conf/mongodb.conf [mongod@mcw01 ~]$ cat /mongodb/conf/mongodb.conf #以配置文件启动方式,最少需要设置的配置文件,命令行启动也可以不需要配置文件 logpath=/mongodb/log/mongodb.log dbpath=/mongodb/data port=27017 logappend= fork= auth= [mongod@mcw01 ~]$ mongod -f /mongodb/conf/mongodb.conf #指定配置文件的启动 about to fork child process, waiting until server is ready for connections. forked process: 15645 child process started successfully, parent exiting [mongod@mcw01 ~]$ ps -ef|grep mongod root 15479 15361 0 10:44 pts/0 00:00:00 su - mongod mongod 15480 15479 0 10:44 pts/0 00:00:00 -bash mongod 15645 1 15 10:59 ? 00:00:02 mongod -f /mongodb/conf/mongodb.conf mongod 15662 15480 0 10:59 pts/0 00:00:00 ps -ef mongod 15663 15480 0 10:59 pts/0 00:00:00 grep --color=auto mongod [mongod@mcw01 ~]$ mongod -f /mongodb/conf/mongodb.conf --shutdown #指定配置文件的启动关闭 killing process with pid: 15645 [mongod@mcw01 ~]$ ps -ef|grep mongod root 15479 15361 0 10:44 pts/0 00:00:00 su - mongod mongod 15480 15479 0 10:44 pts/0 00:00:00 -bash mongod 15666 15480 0 11:00 pts/0 00:00:00 ps -ef mongod 15667 15480 0 11:00 pts/0 00:00:00 grep --color=auto mongod [mongod@mcw01 ~]$ [mongod@mcw01 ~]$ [mongod@mcw01 ~]$ mongod -f /mongodb/conf/mongodb.conf about to fork child process, waiting until server is ready for connections. forked process: 15683 child process started successfully, parent exiting [mongod@mcw01 ~]$ mongo MongoDB shell version: 3.2.8 connecting to: test > db.shutdownServer() #mongo命令行停止失败 shutdown command only works with the admin database; try 'use admin' > use admin switched to db admin > db.shutdownServer() 2022-03-03T11:02:22.619+0800 E QUERY [thread1] Error: shutdownServer failed: { "ok" : 0, "errmsg" : "not authorized on admin to execute command { shutdown: 1.0 }", "code" : 13 } : _getErrorWithCode@src/mongo/shell/utils.js:25:13 DB.prototype.shutdownServer@src/mongo/shell/db.js:302:1 @(shell):1:1 > db.adminCommand({shutdown:1}) { "ok" : 0, "errmsg" : "not authorized on admin to execute command { shutdown: 1.0 }", "code" : 13 } > \q 2022-03-03T11:03:24.315+0800 E QUERY [thread1] SyntaxError: illegal character @(shell):1:0 > exit bye [mongod@mcw01 ~]$