mongodb二进制安装与yum安装
一.什么是mongodb? ? ??
? ?
? 二.? mongodb安装1. Ubuntu 二进制安装 2. Centos 二进制安装 3 . yum 安装 4.??mongodb 创建用户并开启登陆验证 5 . mongodb 常用命令 ? mongodb官网下载地址:https://www.mongodb.com/download-center/community 1. Ubuntu 二进制安装我们可以创建mongodb的运行用户来运行,也可以使用root直接来运行 如果我们需要使用指定的用户来运行,那么需要更改/opt/momgodb的目录所有者为指定用户。 ?下载安装包: cd /tmp/ && wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu1404-4.0.3.tgz ?解压并移动到/opt/下 tar -xvzf mongodb-linux-x86_64-ubuntu1404-4.0.3.tgz && mv mongodb-linux-x86_64-ubuntu1404-3 /opt/mongodb 创建目录 data(用来存放数据的),log(存放日志的) mkdir /opt/mongodb/data mkdir /opt/mongodb/log 创建mongodb配置文件 mongodb.conf cd/opt/mongodb/ && vim mongodb.conf 配置文件内容 bind_ip=0.0.0.0 port=27037 dbpath=/opt/mongodb/data/ logpath=/opt/mongodb/log/mongodb.log pidfilepath =/opt/mongodb/mongodb.pid logappend=true fork= maxConns=500 配置文件参数解释 1 bind_ip=0.0 # 绑定的ip, 表示本地所有ip 2 port= # 绑定的端口 默认端口是27017 3 dbpath=/opt/mongodb/data/ #数据存放目录 4 logpath=/opt/mongodb/log/mongodb.log #日志存放目录 5 pidfilepath =/opt/mongodb/mongodb.pid #pid文件存放位置 6 logappend= #日志写入为追加模式 7 fork= #是否以守护进程运行 8 maxConns=500 #最大连接数 9 noauth = true/auth=true #是否开启认证,初次不开启,设置完密码后开启。默认是不开启的 ?启动mongodb. /opt/mongodb/bin/mongod -f /opt/mongodb/mongodb.conf 检查端口是否在监听 root@iZ23rx1wgk89:/opt/mongodb# lsof -i: COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME mongod 14285 root 10u IPv4 30394148 0t0 TCP *:27037 (LISTEN) 防火墙开启27037端口 iptables -A INPUT -p tcp -dport 27037 -j ACCEPT service iptables save 2. Centos7 二进制安装centos 7的安装基本和Ubuntu一致,只有少许的命令不同 ?下载安装包: cd /tmp/ && wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.0.3.tgz ?解压并移动到/opt/下 tar -xvzf mongodb-linux-x86_64-rhel70* && mv mongodb-linux-x86_64-rhel70-4.0.3 /opt/mongodb 创建目录 data(用来存放数据的),log(存放日志的) mkdir /opt/mongodb/data
mkdir /opt/mongodb/log
创建mongodb配置文件 mongodb.conf cd /opt/mongodb/ && vim mongodb.conf 配置文件内容 bind_ip=0.0.0.0
port=27037
dbpath=/opt/mongodb/data/
logpath=/opt/mongodb/log/mongodb.log
pidfilepath =/opt/mongodb/mongodb.pid
logappend=true
fork=true
maxConns=500
noauth = true
配置文件参数解释 1 bind_ip=0.0.0.0 # 绑定的ip,0.0.0.0 表示本地所有ip
2 port=27037 # 绑定的端口 默认端口是27017
3 dbpath=/opt/mongodb/data/ #数据存放目录
4 logpath=/opt/mongodb/log/mongodb.log #日志存放目录
5 pidfilepath =/opt/mongodb/mongodb.pid #pid文件存放位置
6 logappend=true #日志写入为追加模式
7 fork=true #是否以守护进程运行
8 maxConns=500 #最大连接数
9 noauth = true/auth=true #是否开启认证,初次不开启,设置完密码后开启。默认是不开启的
?启动mongodb. /opt/mongodb/bin/mongod -f /opt/mongodb/mongodb.conf 检查端口是否在监听 root@iZ23rx1wgk89:/opt/mongodb# lsof -i:27037
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mongod 14285 root 10u IPv4 30394148 0t0 TCP *:27037 (LISTEN)
防火墙开启27037端口 firewall-cmd --add-port=27037/tcp --permanent firewall-cmd --reload 3 . yum 安装首先得配置yum 源,我这里配置的yum源是阿里云的源的mongodb 3.2的,可以根据自己的安装版本配置不同的yum源 4.0 源 :http://mirrors.aliyun.com/mongodb/yum/redhat/7Server/mongodb-org/4.0/x86_64/ vim /etc/yum.repos.d/mongodb.repo
[mongodb] name=MongoDB Repository baseurl=http:mirrors.aliyun.com/mongodb/yum/redhat/7Server/mongodb-org/3.2/x86_64/ gpgcheck=0 enabled=1 安装 yum -y install mongodb-org mongodb-org-server 启动并添加到开机自启 systemctl enable mongod 防火墙开放27017端口 firewall-cmd --add-port=27017/tcp --permanent firewall-cmd --reload 默认绑定的ip是:127.0.0.1 默认绑定的端口是:27017 我们可以编辑配置文件 4 .mongodb 创建用户并开启登陆验证? ?在实际使用过程中,我需要为mongodb添加认证机制。这样才能保障我们数据的安全性。 如果我们是自己使用tar包安装的,那么我们可以将mongo命令添加到解释器路径里。yum安装默认会添加 ln -s /opt/mongodb/bin/mongo /usr/bin/mongo
如果我们更改了默认端口的话,那么我们需要使用 --port 参数指定端口 ./mongo --port 27037
1 [root@djx2 bin]# ./mongo --port 27037 2 MongoDB shell version v4.0.3 3 connecting to: mongodb:127.0.0.1:27037/ 4 Implicit session: session { "id" : UUID(fc5045fe-8d56-4272-a362-f86e4a0d2a96") } 5 MongoDB server version: 3 6 Welcome to the MongoDB shell. 7 For interactive help,type help. 8 For more comprehensive documentation,see 9 http:docs.mongodb.org/ 10 Questions? Try the support group 11 http:groups.google.com/group/mongodb-user 12 Server has startup warnings: 13 2018-11-06T18:21:18.381+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user,which is not recommended. 14 0800 I CONTROL [initandlisten] 15 16 0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'17 0800 I CONTROL [initandlisten] ** We suggest setting it to never' 18 19 0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 20 21 22 --- 23 Enable MongoDBs free cloud-based monitoring service,which will then receive and display 24 metrics about your deployment (disk utilization,CPU,operation statistics,etc). 25 26 The monitoring data will be available on a MongoDB website with a unique URL accessible to you 27 and anyone you share the URL with. MongoDB may use this information to make product 28 improvements and to suggest MongoDB products and deployment options to you. 29 30 To enable free monitoring,run the following command: db.enableFreeMonitoring() 31 To permanently disable this reminder,run the following command: db.disableFreeMonitoring() 32 --- 33 34 > use admin; 35 switched to db admin 36 > db.createUser({ user: "admin",pwd: "9toc7ttpji8",roles: [{ role: "root",db: "admin" }] }) 37 Successfully added user: { 38 user" : admin,39 roles : [ 40 { 41 roleroot42 db" 43 } 44 ] 45 } 46 > 这样我们就添加好了管理员用户。 然后我们需要将我们?mongodb.conf中的noauth 参数注释,添加??auth = true auth = true
并将mongodb重启,我们在进行查询的时候就需要验证用户了。 创建普通用户 切换到kevin库添加普通用户(readWrite有读写权限;read有读权限) > use kevin; switched to db kevin > db.createUser({user: kevin",1)">pwd: kevin@123456":readWrite}]}); Successfully added user: { : [ { } ] } 5.? mongodb的常用命令?见文章? mongodb常用命令 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |