Mongodb 安装

参考文档:在Red Hat上安装_MonogDB 中文网 (mongodb.net.cn)

CentOS 安装社区版

二进制包 安装

二进制包下载地址:MongoDB Community Downloads | MongoDB

# 安装依赖
sudo yum install libcurl openssl

# 下载二进制包,解压
curl https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.6.tgz -O /opt/
tar xf mongodb-linux-x86_64-rhel70-4.2.6.tgz
mkdir /opt/mongodb/{logs,data}

# 配置文件
# curl https://raw.Githubusercontent.com/mongodb/mongo/master/rpm/mongod.conf -O /opt/mongodb/

# 配置环境变量
echo "PATH=$PATH:/opt/mongodb/bin" > /etc/profile.d/mongodb.sh
source /etc/profile.d/mongodb.sh
mongo --version

# 关闭THP,大叶内存
root 用户下
vim /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

[root@jenkins-hk opt]# cat /sys/kernel/mm/transparent_hugepage/enabled
always madvise [never]
[root@jenkins-hk opt]# cat /sys/kernel/mm/transparent_hugepage/defrag
always defer defer+madvise madvise [never]

其他系统关闭,参照官方文档
https://www.mongodb.com/docs/manual/tutorial/transparent-huge-pages/#using-tuned-and-ktune

# 为什么关闭
Disable Transparent Huge Pages (THP)
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 enabled, because they tend to have sparse rather than contiguous memory access patterns. When running MongoDB on Linux, THP should be disabled for best performance.


# 创建用户
groupadd mongod
useradd -g mongod mongod

# 配置mongodb目录权限
mkdir /var/run/mongodb
chown -R mongod:mongod /opt/mongodb /var/run/mongodb

# 如果不想使用 systemctl 来管理,直接命令行启动
su - mongod
mongod --dbpath=/opt/mongodb/data/ --logpath=/opt/mongodb/logs/mongodb.log --port=27017 --logappend --fork


# 登录
mongo

# 关闭
> use admin
> db.shutdownServer()
> quit()
或者
mongod -f /opt/mongodb/mongo.conf --shutdown

简单命令

mongo

# 使用简单命令
> use db;
switched to db db
> db
db
> show tables;
> show databases;
admin   0.000GB
config  0.000GB
local   0.000GB
> use admin;
switched to db admin
> show tables;
system.version
> quit()

# 关闭mongo

使用配置文件



# 配置文件
cat > /opt/mongodb/mongod.conf <<EOF
systemLog:
  destination: file
  logAppend: true
  path: /opt/mongodb/logs/mongod.log

storage:
  dbPath: /opt/mongodb/data
  journal:
    enabled: true

processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongod.pid		# 可以不配置,自动生成到 data 目录中
  timeZoneInfo: /usr/share/zoneinfo

net:
  port: 27017					# 监听端口
  bindIp: 0.0.0.0			# 监听地址

security:
  authorization: enabled				# 是否打开用户名密码验证

---- 
EOF



# 创建systemd启动配置文件,github 启动配置文件地址
# cd /usr/lib/systemd/system/
# curl https://raw.githubusercontent.com/mongodb/mongo/master/rpm/mongod.service -O mongod.service

# 自己修改的
cat > /usr/lib/systemd/system/mongod.service <<EOF
[Unit]
Description=MongoDB Database Server
Documentation=https://docs.mongodb.org/manual
After=network-online.target
Wants=network-online.target

[Service]
User=mongod
Group=mongod
Environment="OPTIONS=-f /opt/mongodb/mongod.conf"
EnvironmentFile=-/etc/sysconfig/mongod
ExecStart=/opt/mongodb/bin/mongod $OPTIONS
ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb
ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb
ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb
PermissionsStartOnly=true
PIDFile=/var/run/mongodb/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 mongod as specified in
# https://docs.mongodb.com/manual/reference/ulimit/#recommended-ulimit-settings

[Install]
WantedBy=multi-user.target
EOF



# 启动mongodb服务并配置随系统启动
systemctl enable --now mongod


# 
mongod -f /opt/mongodb/mongod.conf

配置 limit

# 首先配置对应 limit 
# 大多数类Unix操作系统都限制了会话可能使用的系统资源。这些限制可能会对MongoDB的运行产生负面影响。有关更多信息,请参见UNIX ulimit设置。;
# 连接:https://mongodb.net.cn/manual/reference/ulimit/

limit fsize unlimited unlimited    # (file size)
limit cpu unlimited unlimited      # (cpu time)
limit as unlimited unlimited       # (virtual memory size)
limit memlock unlimited unlimited  # (locked-in-memory size)
limit nofile 64000 64000           # (open files)
limit nproc 64000 64000            # (processes/threads)


# 具体配置步骤


rpm 包安装