1 yum安装
1.1 取一台干净的虚拟机centos-7.x -minimal开启yum缓存
vim /etc/yum.conf
keepcache=1 #1为保存 0为不保存
1.2 yum安装mongodb
vim /etc/yum.repos.d/mongodb-org-4.4.repo
内容
[mongodb-org-4.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc
yum install -y mongodb-org
2 离线安装
2.1 安装包说明
安装包缓存在/var/cache/yum/7/x86_64/
服务器端安装包
mongodb-org-4.4.11-1.el7.x86_64.rpm
mongodb-org-mongos-4.4.11-1.el7.x86_64.rpm
mongodb-org-server-4.4.11-1.el7.x86_64.rpm
工具及客户端安装包
mongodb-database-tools-100.5.1.x86_64.rpm
mongodb-org-database-tools-extra-4.4.11-1.el7.x86_64.rpm
mongodb-org-shell-4.4.11-1.el7.x86_64.rpm
mongodb-org-tools-4.4.11-1.el7.x86_64.rpm
工具安装依赖包
cyrus-sasl-2.1.26-23.el7.x86_64.rpm
cyrus-sasl-gssapi-2.1.26-23.el7.x86_64.rpm
cyrus-sasl-lib-2.1.26-23.el7.x86_64.rpm
cyrus-sasl-plain-2.1.26-23.el7.x86_64.rpm
2.2 将安装包复制到目标机器
使用yum本机安装
yum localinstall -y \
cyrus-sasl-2.1.26-23.el7.x86_64.rpm \
cyrus-sasl-gssapi-2.1.26-23.el7.x86_64.rpm \
cyrus-sasl-lib-2.1.26-23.el7.x86_64.rpm \
cyrus-sasl-plain-2.1.26-23.el7.x86_64.rpm \
mongodb-database-tools-100.5.1.x86_64.rpm \
mongodb-org-database-tools-extra-4.4.11-1.el7.x86_64.rpm \
mongodb-org-shell-4.4.11-1.el7.x86_64.rpm \
mongodb-org-tools-4.4.11-1.el7.x86_64.rpm
2.3 也可自行下载安装包
https://www.mongodb.com/try/download/community
wget https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.4/x86_64/RPMS/mongodb-org-server-4.4.10-1.el7.x86_64.rpm
wget https://repo.mongodb.org/yum/redhat/7/mongodb-org/4.4/x86_64/RPMS/mongodb-org-shell-4.4.10-1.el7.x86_64.rpm
使用rpm方式安装
rpm -ivh mongodb-org-server-4.4.10-1.el7.x86_64.rpm
rpm -ivh mongodb-org-shell-4.4.10-1.el7.x86_64.rpm
3 修改配置文件
3.1 修改IP
vim /etc/mongod.conf
修改内容
bindIp 0.0.0.0
启动服务
systemctl start mongod.service
禁用防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
3.2 修改数据文件路径地大坑
vim /etc/mongod.conf
dbPath: /data/mongod
systemctl restart mongod.service
重启服务失败,参考官网文档
On RHEL 7.0, if you change the data path, the default SELinux
policies will prevent mongod from having write access on the new
data path if you do not change the security context.
执行如下命令
chmod 777 /data/mongod
chown mongod:mongod /data/mongod
mkdir -p /data/mongod
chcon -Rv --type=mongod_var_lib_t /data/mongod 或 chcon -R -t mongod_var_lib_t /data/mongod
在oracle linux系统上出现
![](https://img.haomeiwen.com/i7848013/e397d9ebd41158b4.png)
can't apply partial context to unlabeled file '/home/suozhiyuan/'
vim /etc/selinux/config
原来是关闭: SELINUX=disabled
改成打开: SELINUX=enforcing
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
#改成SELINUX=enforcing
# SELINUXTYPE= can take one of these three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
systemctl restart mongod.service
终于正常了!!!!!!!
3.3 使用 mongo shell 创建用户
mongo
>use admin;
>db.createUser({
user:'root',
pwd:'123456',
roles:[{role:"root",db:'admin'}]
}
);
3.4 插入数据
>db.dept.insert([
{_id:NumberInt(1),name:'开发部',code:'Y0001',type:1},
{_id:NumberInt(2),name:'测试部',code:'Y0002',type:2},
{_id:NumberInt(3),name:'销售部',code:'Y0003',type:3},
{_id:NumberInt(4),name:'运营部',code:'Y0004',type:3},
]);
>db.user.insert([
{_id:NumberInt(1),name:'张三',dept_id:NumberInt(1),salary:2000},
{_id:NumberInt(2),name:'张三',dept_id:NumberInt(1),salary:2000},
{_id:NumberInt(3),name:'张三',dept_id:NumberInt(1),salary:3000},
{_id:NumberInt(4),name:'李四',dept_id:NumberInt(2),salary:3000},
{_id:NumberInt(5),name:'张三',dept_id:NumberInt(2),salary:4000},
{_id:NumberInt(6),name:'张三',dept_id:NumberInt(2),salary:4000},
{_id:NumberInt(7),name:'张三',dept_id:NumberInt(3),salary:2400},
{_id:NumberInt(8),name:'张三',dept_id:NumberInt(3),salary:2500},
{_id:NumberInt(9),name:'张三',dept_id:NumberInt(4),salary:2600},
{_id:NumberInt(10),name:'张三',dept_id:NumberInt(4),salary:2700},
]);
网友评论