一、单机yum安装
1、关闭selinux,设置firewalld
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
firewall-cmd --permanent --add-port=27017/tcp;firewall-cmd --reload
2、更改yum源
rm -rf /etc/yum.repos.d/*
vi /etc/yum.repos.d/base.repo
[rocky-base]
name=rocky-base
baseurl=http://mirrors.163.com/rocky/8.6/BaseOS/x86_64/os/
enable=1
gpgcheck=0
[rocky-appstream]
name=rocky-appstream
baseurl=http://mirrors.163.com/rocky/8.6/AppStream/x86_64/os/
gpgcheck=0
enable=1
[rocky-extras]
name=rocky-extras
baseurl=http://mirrors.163.com/rocky/8.6/extras/x86_64/os/
gpgcheck=0
enable=1
vi /etc/yum.repos.d/mongodb.repo
[mongodb-org-6.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/6.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc
dnf -y install net-tools bash-completion vim wget
reboot
安装mongodb
yum install -y mongodb-org
systemctl start mongod
mongosh
二、二进制文件安装
最小化安装rockylinux8.6
1、关闭selinux,防火墙放通策略及安装软件包
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
firewall-cmd --permanent --add-port=27017/tcp;firewall-cmd --reload
dnf -y install net-tools bash-completion vim wget
reboot
2、下载mongodb软件及mongodb-shell
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel80-6.0.2.tgz
wget https://downloads.mongodb.com/compass/mongosh-1.6.0-linux-x64.tgz
tar -xvf mongodb-linux-x86_64-rhel80-6.0.2.tgz
mv mongodb-linux-x86_64-rhel80-6.0.2 /usr/local/mongodb
3、查看安装
4、编辑配置文件
mkdir -p /data/mongodb
vim /data/mongodb/mongodb.conf
systemLog:
destination: file
logAppend: true
path: /data/mongodb/mongodb.log
storage:
dbPath: /data/mongodb
journal:
enabled: true
processManagement:
fork: true
net:
port: 27017
bindIp: 0.0.0.0
5、启动与检查
/usr/local/mongodb/bin/mongod -f /data/mongodb/mongodb.conf
ls /data/mongodb
ps -aux | grep mongo
netstat -antup | grep mongo
6、安装mongodb-shell
tar -xvf mongosh-1.6.0-linux-x64.tgz
mv mongosh-1.6.0-linux-x64/bin/mongosh /usr/bin
mongosh
7、优化
Soft rlimits for open file descriptors too low
vim /etc/security/limits.conf
加入如下
* - nproc 65536
* - nofile 65536
logout
ulimit -n
ulimit -a
vm.max_map_count is too low
vi /etc/sysctl.conf
加入如下
vm.max_map_count=655360
sysctl -p
/sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
echo 'never' > /sys/kernel/mm/transparent_hugepage/enabled
echo 'never' > /sys/kernel/mm/transparent_hugepage/defrag
vim /etc/rc.d/rc.local
加入
echo 'never' > /sys/kernel/mm/transparent_hugepage/enabled
echo 'never' > /sys/kernel/mm/transparent_hugepage/defrag
chmod +x /etc/rc.d/rc.local
You are running this process as the root user, which is not recommended
Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
kill (px -aux |grep mongo)
useradd mongodb
echo password | passwd --stdin mongodb
chown -R mongodb:mongodb /data/mongodb
vim /data/mongodb/mongodb.conf
加入
security:
authorization: enabled
reboot
mongodb用户登录
/usr/local/mongodb/bin/mongod -f /data/mongodb/mongodb.conf
mongosh
8、关闭
use admin
db.shutdownServer()
三、基本命令
show dbs;
show databases;
1、mongodb创建库、创建集合、插入数据
use mydb
db.myuser.insertOne({ name: 'john', age:28 })
show dbs
show collections
db.myuser.insertOne( {'name': 'jerry', age: 27} )
db.myuser.insertOne( {'name': 'mary', age: 26} )
db.myuser.find()
db.myuser.find( { name: 'john' } )
db.myuser.find( { age: 26 } )
集合的field不固定,可以插入如下数据
db.myuser.insert( {age: 29} )
db.myuser.insert( {'location': 'urumqi'} )
db.myuser.find()
更新集合数据
db.myuser.update({ age: 29 }, {$set: { age: 30 }})
db.myuser.update({ 'name': 'mary' }, { $set: { 'name': 'hanmei' } })
删除集合数据
db.myuser.remove({ name: 'hanmei' })
db.myuser.find()
删除集合所有数据
db.myuser.remove( {} )
db.myus