hostnamectl set-hostname dbatest01
cp /etc/hosts /etc/hosts.bak
cat >>/etc/hosts<
echo 'add by mongodb' >> /etc/security/limits.conf
echo 'mongodb soft nofile 64000' >> /etc/security/limits.conf
echo 'mongodb hard nofile 64000' >> /etc/security/limits.conf
echo 'mongodb soft nproc 32000' >> /etc/security/limits.conf
echo 'mongodb hard nproc 32000' >> /etc/security/limits.conf
echo 'mongod soft stack 32768' >> /etc/security/limits.conf
echo 'mongod hard stack 32768' >> /etc/security/limits.conf
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag
--加入开机启动
echo '
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag'>>/etc/rc.local
chmod +x /etc/rc.local
默认防火墙是关闭状态
sudo ufw disable #禁用
如果启用防火墙需进行如下配置:
如果数据库端口和ssh端口不是3306和22,需视情况更改
sudo ufw enable #开启防火墙
sudo ufw allow 3306/tcp #仅tcp
sudo ufw allow 22/tcp #仅tcp
Ubuntu 默认未安装SELinux,省略设置。
root@HKSZF-ZW-172-19-146-175:/etc/selinux# getenforce
Command 'getenforce' not found, but can be installed with:
apt install selinux-utils
会自行处理依赖关系
--查看
root@HKSZF-ZW-172-19-146-173:/topsoft/soft/single/package# ls -l
total 49632
-rw-r--r-- 1 root root 4387392 Jan 22 2024 mongodb-org-mongos_3.6.23_amd64.deb
-rw-r--r-- 1 root root 9680898 Jan 22 2024 mongodb-org-server_3.6.23_amd64.deb
-rw-r--r-- 1 root root 5284074 Jan 22 2024 mongodb-org-shell_3.6.23_amd64.deb
-rw-r--r-- 1 root root 31458362 Jan 22 2024 mongodb-org-tools_3.6.23_amd64.deb
--安装deb包
root@HKSZF-ZW-172-19-146-173:/topsoft/soft/single/package# dpkg -i *.deb
Selecting previously unselected package mongodb-org-mongos.
(Reading database ... 59893 files and directories currently installed.)
Preparing to unpack mongodb-org-mongos_3.6.23_amd64.deb ...
Unpacking mongodb-org-mongos (3.6.23) ...
Selecting previously unselected package mongodb-org-server.
Preparing to unpack mongodb-org-server_3.6.23_amd64.deb ...
Unpacking mongodb-org-server (3.6.23) ...
Selecting previously unselected package mongodb-org-shell.
Preparing to unpack mongodb-org-shell_3.6.23_amd64.deb ...
Unpacking mongodb-org-shell (3.6.23) ...
Selecting previously unselected package mongodb-org-tools.
Preparing to unpack mongodb-org-tools_3.6.23_amd64.deb ...
Unpacking mongodb-org-tools (3.6.23) ...
Setting up mongodb-org-mongos (3.6.23) ...
Setting up mongodb-org-server (3.6.23) ...
Adding system user `mongodb' (UID 111) ...
Adding new user `mongodb' (UID 111) with group `nogroup' ...
Not creating home directory `/home/mongodb'.
Adding group `mongodb' (GID 117) ...
Done.
Adding user `mongodb' to group `mongodb' ...
Adding user mongodb to group mongodb
Done.
Setting up mongodb-org-shell (3.6.23) ...
Setting up mongodb-org-tools (3.6.23) ...
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for ureadahead (0.100.0-19.1) ...
会自动创建mongodb用户和组
配置文件位置:/etc/mongod.conf
mkdir -p /topsoft/mongodb/{data,log,conf}
chown -R mongodb:mongodb /topsoft/mongodb
cp /etc/mongod.conf /etc/mongod.conf_bak_`date +%F`
sed -i "s#/var/lib/mongodb#/topsoft/mongodb/data#g" /etc/mongod.conf
sed -i "s#/var/log/mongodb/mongod.log#/topsoft/mongodb/log/mongod.log#g" /etc/mongod.conf
sed -i "s#127.0.0.1#127.0.0.1,172.19.146.173#g" /etc/mongod.conf
journal:
enabled: true 下增加
engine: wiredTiger
cat /etc/mongod.conf
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /topsoft/mongodb/logs/mongod.log
# Where and how to store data.
storage:
dbPath: /topsoft/mongodb/data
journal:
enabled: true
engine: wiredTiger
# how the process runs
#processManagement:
# fork: true # fork and run in background
# pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
# timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1,172.19.146.173 # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
## Enterprise-Only Options
#auditLog:
#snmp:
cat > /lib/systemd/system/mongod.service << "EOF"
[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
Documentation=https://docs.mongodb.org/manual
[Service]
User=mongodb
Group=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf
# 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
chmod +x /lib/systemd/system/mongod.service
systemctl start mongod
systemctl enable mongod #设置开机启动
--问题描述
oot@HKSZF-ZW-172-19-146-173:/topsoft/mongodb/logs# systemctl restart mongod
root@HKSZF-ZW-172-19-146-173:/topsoft/mongodb/logs# systemctl status mongod
● mongod.service - High-performance, schema-free document-oriented database
Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Mon 2024-01-22 04:20:44 EST; 7s ago
Docs: https://docs.mongodb.org/manual
Process: 3216 ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf (code=exited, status=2)
Main PID: 3216 (code=exited, status=2)
Jan 22 04:20:44 HKSZF-ZW-172-19-146-173 systemd[1]: Started High-performance, schema-free document-oriented database.
Jan 22 04:20:44 HKSZF-ZW-172-19-146-173 systemd[1]: mongod.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
Jan 22 04:20:44 HKSZF-ZW-172-19-146-173 systemd[1]: mongod.service: Unit entered failed state.
Jan 22 04:20:44 HKSZF-ZW-172-19-146-173 systemd[1]: mongod.service: Failed with result 'exit-code'.
--解决办法
配置文件参数不对,参数上面
--查看进程 确认配置文件位置
--停止服务
systemctl stop mongod
systemctl status mongod
在Ubuntu上,MongoDB通过apt软件包管理工具进行安装和卸载。我们可以使用以下命令卸载MongoDB软件包:
--查看安装的包
root@HKSZF-ZW-172-19-146-173:/topsoft/mongodb# dpkg -l | grep mongo
ii mongodb-org-mongos 3.6.23 amd64 MongoDB sharded cluster query router
ii mongodb-org-server 3.6.23 amd64 MongoDB database server
ii mongodb-org-shell 3.6.23 amd64 MongoDB shell client
ii mongodb-org-tools 3.6.23 amd64 MongoDB tools
--卸载
sudo apt-get purge mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools
这将卸载MongoDB的所有相关软件包。
某些场景下是,建议先dpkg -l查
sudo apt-get purge mongodb mongodb-clients mongodb-server mongodb-dev
默认情况下,数据目录在/var/lib/mongodb。
--删除配置文件
sudo rm /etc/mongodb.conf
--删除数据目录
sudo rm -r /var/lib/mongodb
MongoDB在安装过程中创建了一个名为mongodb的用户和一个名为mongodb的用户组。为了完全卸载MongoDB,我们还需要删除它们。
sudo userdel mongodb
sudo groupdel mongodb
完成上述步骤后,我们还可以清理一些可能的残留文件和目录。
运行以下命令清理MongoDB的日志文件:
sudo rm /var/log/mongodb/mongodb.log
然后,运行以下命令清理MongoDB的临时文件目录:
sudo rm -r /tmp/mongodb-*
通过按照上述步骤,我们可以从Ubuntu系统中完全卸载MongoDB数据库。记住,在卸载之前请备份您的数据,以防意外发生。如果您想重新安装MongoDB,请参考MongoDB的官方文档。
MongoDB的认证功能是用于保护数据库的数据安全性的一项重要功能。当认证功能启用时,连接MongoDB数据库的客户端需要提供合法的用户名和密码才能成功连接和操作数据库。这样可以防止未经授权的访问和数据泄露。
要启用MongoDB的认证功能,我们需要进行以下几个步骤:
security:
authorization: enabled
sudo systemctl restart mongod
use admin
db.createUser(
{
user: "adminUser",
pwd: "adminPassword",
roles: [ { role: "root", db: "admin" } ]
}
)
这里的”adminUser”和”adminPassword”可以根据实际情况进行修改。
mongo -u adminUser -p adminPassword --authenticationDatabase admin
如果不再需要认证功能,或者由于某些原因需要禁用认证功能,可以按照以下步骤进行:
#security:
# authorization: enabled
sudo systemctl restart mongod
请注意,在禁用认证功能后,任何可以访问MongoDB服务器的人都可以进行任意操作,这将带来一定的安全风险。因此,在生产环境中,请谨慎考虑是否禁用认证功能。