下载:
curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.0.6.tgz
解压:
tar -zxvf mongodb-linux-x86_64-3.0.6.tgz
目录重命名:
mv mongodb-linux-x86_64-3.0.6/ /usr/local/mongodb
注册mongodb的环境变量:
vi /etc/profile
添加环境变量
export MONGODB_HOME=/usr/local/mongodb
PATH=$MONGODB_HOME/bin:
生效环境变量:
source /etc/profile
进入mongodb的安装目录
cd /usr/local/mongodb/bin
vi master.cnf
添加内容如下(主节点配置master.cnf)(注意:以下目录需要手工创建,最下面的权限auth和keyFile需要先注释,mongodb需要先以无权限模式进去后,添加用户,否则无法添加用户):
#master.conf
dbpath=/mnt/mongodb/master/data/master
logpath=/mnt/mongodb/master/log/master.log
pidfilepath=/mnt/mongodb/master/master.pid
directoryperdb=true
logappend=true
replSet=mySet
bind_ip=127.0.0.1
port=27061
oplogSize=1024
fork=true
noprealloc=true
#auth=true
#keyFile=/mnt/mongodb/master/mongodb-keyfile
继续添加配置文件(从节点配置slaver.cnf)
vi slaver.cnf
#slaver.conf
dbpath=/mnt/mongodb/slaver/data/slaver
logpath=/mnt/mongodb/slaver/log/slaver.log
pidfilepath=/mnt/mongodb/slaver/slaver.pid
directoryperdb=true
logappend=true
replSet=mySet
bind_ip=127.0.0.1
port=27062
oplogSize=1024
fork=true
noprealloc=true
#auth=true
#keyFile=/mnt/mongodb/slaver/mongodb-keyfile
仲裁节点配置():
vi arbiter.conf
#arbiter.conf
dbpath=/mnt/mongodb/arbiter/data/arbiter
logpath=/mnt/mongodb/arbiter/log/arbiter.log
pidfilepath=/mnt/mongodb/arbiter/arbiter.pid
directoryperdb=true
logappend=true
replSet=mySet
bind_ip=127.0.0.1
port=27063
oplogSize=1024
fork=true
noprealloc=true
#auth=true
#keyFile=/mnt/mongodb/arbiter/mongodb-keyfile
注意:绑定的IP在启动并初始化完成以后,需要改为云机器的公网ip
cd /usr/local/mongodb/bin
./mongod -f master.cnf
注意:启动过程可能失败注意看日志文件/mnt/mongodb/master/log/master.log分析错误原因
常见的错误有:
1、防火墙限制端口:
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 27091 -j ACCEPT;
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 27092 -j ACCEPT;
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 27093 -j ACCEPT;
注意:bind_ip开始启动的时候要设置为127.0.0.1,否则,可能导致启动失败
cd /usr/local/mongodb/bin
./mongo 127.0.0.1:27061
use admin
conf=
{
“_id” : “mySet”,
“members” : [
{ “_id” : 0, “host” : “127.0.0.1:27061”, priority:2},
{ “_id” : 1, “host” : “127.0.0.1:27062”, priority:1},
{ “_id” : 2, “host” : “127.0.0.1:27063”, priority:1, arbiterOnly:true}
]
}
rs.initiate(conf)
切换到admin数据库
use admin
添加角色:
db.createUser( {user: “zhangsan”,pwd: “123456”,roles: [ { role: “userAdminAnyDatabase”, db: “admin” } ]})
参照这边文章:
https://blog.csdn.net/qq_35209838/article/details/85124370
上面添加的张三角色是全局数据库管理员:
下面添加的李四角色是myDb的数据写入权限角色:
db.createUser({user: “lisi”, pwd: “123456”, roles: [{ “role” : “readWrite”, “db” : “myDb” }]});
db.createUser({user: “zhangsan”, pwd: “123456”, roles: [{ “role” : “readWrite”, “db” : “admin” }]});
db.createUser({user: “zhangsan01”, pwd: “123456”, roles: [{ “role” : “clusterAdmin”, “db” : “admin” }]});
db.createUser({user: “zhangsan02”, pwd: “123456”, roles: [ “readWrite”, “dbAdmin” ]});
db.createUser({user: “zhangsan03”, pwd: “123456”, roles: [{ “role” : “root”, “db” : “admin” }]});
db.createUser({user: “zhangsan40”, pwd: “123456”, roles: [{ “role” : “root”, “db” : “admin” }]});
db.createUser({user: “zhangsan41”, pwd: “123456”, roles: [{ “role” : “clusterAdmin”, “db” : “admin” }]});
db.createUser({user: “zhangsan42”, pwd: “123456”, roles: [{ “role” : “readWrite”, “db” : “admin” }]});
db.createUser({user: “zhangsan43”, pwd: “123456”, roles: [{ “role” : “dbAdmin”, “db” : “admin” }]});
db.createUser({user: “zhangsan44”, pwd: “123456”, roles: [ “readWrite”, “dbAdmin” ]});
db.createUser({user: “zhangsan45”, pwd: “123456”, roles: [{ “role” : “root”, “db” : “admin” }]}); //失败
db.createUser({user: “zhangsan46”, pwd: “123456”, roles: [{ “role” : “clusterAdmin”, “db” : “spider_dev” }]}); //失败
db.createUser({user: “zhangsan47”, pwd: “123456”, roles: [{ “role” : “readWrite”, “db” : “admin” }]});
db.createUser({user: “zhangsan48”, pwd: “123456”, roles: [{ “role” : “dbAdmin”, “db” : “admin” }]});
db.createUser({user: “zhangsan50”, pwd: “123456”, roles: [ “readWrite”, “dbAdmin” ]});
db.createUser({user: “zhangsan51”, pwd: “123456”, roles: [ “dbAdminAnyDatabase”]});
db.createUser({user: “zhangsan52”, pwd: “123456”, roles: [ “clusterAdmin”]});
添加方式参照上面
例如:arbitor
/usr/local/mongodb/bin/mongod --shutdown --port 27063 --dbpath /usr/local/mongodb/arbiter/data/arbiter/
连接到MongoDB服务 如果是远程连接可以替换“localhost”为服务器所在IP地址
package com.spider.search.service.util;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoDatabase;
import java.util.ArrayList;
import java.util.List;
public class MongoConnUtil {
private MongoDatabase mongoDatabase;
private MongoClient mongoClient;
public MongoDatabase initConn(){
//连接到MongoDB服务 如果是远程连接可以替换“localhost”为服务器所在IP地址
//ServerAddress()两个参数分别为 服务器地址 和 端口 134.175.107.11
ServerAddress serverAddress01 = new ServerAddress("122.41.187.38",27091);
ServerAddress serverAddress02 = new ServerAddress("122.41.187.38",27092);
List addrs = new ArrayList();
addrs.add(serverAddress01);
addrs.add(serverAddress02);
//MongoCredential.createScramSha1Credential()三个参数分别为 用户名 数据库名称 密码
MongoCredential credential01 = MongoCredential.createScramSha1Credential("zhangsan01", "myDb", "123456".toCharArray());
MongoCredential credential02 = MongoCredential.createScramSha1Credential("zhangsan01", "myDb", "123456".toCharArray());
List credentials = new ArrayList();
credentials.add(credential01);
credentials.add(credential02);
//通过连接认证获取MongoDB连接
this.mongoClient = new MongoClient(addrs,credentials);
//连接到数据库 spider_dev
this.mongoDatabase = this.mongoClient.getDatabase("myDb");
return mongoDatabase;
}
public void connClose(){
this.mongoClient.close();
}
}
import com.mongodb.BasicDBObject;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.spider.search.service.util.MongoConnUtil;
import org.apache.commons.lang3.StringUtils;
import org.bson.Document;
public class MyTest{
public static void main(String[] argu){
Document document = new Document();
Document document02 = null;
MongoConnUtil mongoConnUtil = new MongoConnUtil();
MongoDatabase mongoDatabase = mongoConnUtil.initConn();
try {
MongoCollection collection04 = mongoDatabase.getCollection("keyWords");
BasicDBObject query04 = new BasicDBObject();
if (null != document.get("urlId") && StringUtils.isNotEmpty(String.valueOf(document.get("urlId")))) {
query04.put("urlId", document.get("urlId"));
}
if (null != document.get("keyWord") && StringUtils.isNotEmpty(String.valueOf(document.get("keyWord")))) {
query04.put("keyWord", document.get("keyWord"));
}
if (null != document.get("counts") && StringUtils.isNotEmpty(String.valueOf(document.get("counts")))) {
query04.put("counts", document.get("counts"));
}
FindIterable findIterable04 = collection04.find(query04);
MongoCursor mongoCursor04 = findIterable04.iterator();
int icount = 0;
if (mongoCursor04.hasNext()) {
document02 = mongoCursor04.next();
icount++;
}
if (icount <= 0) {
document02 = null;
}
}catch (Exception e){
System.out.println(e);
}
System.out.println(document02);
int icount = 1;
}
}
https://github.com/sijunx/mySpider
里面代码有点多需要搜索下