hadoop生態搭建(3節點)-13.mongodb配置
# 13.mongodb配置_副本集_認證授權
# ==================================================================安裝 mongodb
tar -zxvf ~/mongodb-linux-x86_64-rhel70-3.4.5.tgz -C /usr/local mv /usr/local/mongodb-linux-x86_64-rhel70-3.4.5 /usr/local/mongodb-3.4.5 rm -r ~/mongodb-linux-x86_64-rhel70-3.4.5.tgz
# 環境變數
# ==================================================================node1 node2 node3
vi /etc/profile # 在export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL下新增 export JAVA_HOME=/usr/java/jdk1.8.0_111 export ZOOKEEPER_HOME=/usr/local/zookeeper-3.4.12 export HADOOP_HOME=/usr/local/hadoop-2.7.6 export MYSQL_HOME=/usr/local/mysql export HBASE_HOME=/usr/local/hbase-1.2.4 export HIVE_HOME=/usr/local/hive-2.1.1 export SCALA_HOME=/usr/local/scala-2.12.4 export KAFKA_HOME=/usr/local/kafka_2.12-0.10.2.1 export FLUME_HOME=/usr/local/flume-1.8.0 export SPARK_HOME=/usr/local/spark-2.3.0 export STORM_HOME=/usr/local/storm-1.1.0 export ERLANG_HOME=/usr/local/erlang export RABBITMQ_HOME=/usr/local/rabbitmq_server-3.7.5 export MONGODB_HOME=/usr/local/mongodb-3.4.5 export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$ZOOKEEPER_HOME/bin:$HADOOP_HOME/bin:$HADOOP_HOME/sbin:$MYSQL_HOME/bin:$HBASE_HOME/bin:$HIVE_HOME/bin:$SCALA_HOME/bin:$KAFKA_HOME/bin:$FLUME_HOME/bin:$SPARK_HOME/bin:$SPARK_HOME/sbin:$STORM_HOME/bin:$ERLANG_HOME/bin:$RABBITMQ_HOME/ebin:$RABBITMQ_HOME/sbin:$MONGODB_HOME/bin export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export HADOOP_INSTALL=$HADOOP_HOME export HADOOP_MAPRED_HOME=$HADOOP_HOME export HADOOP_COMMON_HOME=$HADOOP_HOME export HADOOP_HDFS_HOME=$HADOOP_HOME export YARN_HOME=$HADOOP_HOME export HADOOP_COMMON_LIB_NATIVE_DIR=$HADOOP_HOME/lib/native
# ==================================================================node1
# 使環境變數生效 source /etc/profile # 檢視配置結果 echo $MONGODB_HOME mkdir -p $MONGODB_HOME/{conf,data,pids,logs} vi $MONGODB_HOME/conf/mongod.conf systemLog: destination: file logAppend: true #日誌檔案存放目錄 path: /usr/local/mongodb-3.4.5/logs/mongod.log storage: #資料檔案存放目錄 dbPath: /usr/local/mongodb-3.4.5/data journal: enabled: true processManagement: #以守護程式的方式啟用,即在後臺執行 fork: true pidFilePath: /usr/local/mongodb-3.4.5/pids/mongod.pid net: port: 27017 #埠 replication: replSetName: replSet1 #security: # authorization: enabled # keyFile: /usr/local/mongodb-3.4.5/keyfile/mongodb.keyfile scp -r $MONGODB_HOME node2:/usr/local/ scp -r $MONGODB_HOME node3:/usr/local/
# ==================================================================node2 node3
# 使環境變數生效 source /etc/profile # 檢視配置結果 echo $MONGODB_HOME
shutdown -h now
# 快照 mongodb_副本集_叢集前
# 副本集配置
# ==================================================================node1 node2 node3 mongod -f $MONGODB_HOME/conf/mongod.conf # ==================================================================任意一臺 mongo --port 27017 # 新增副本集配置 > cfg={_id:"replSet1", members:[{_id:1, host:'node1:27017'},{_id:2, host:'node2:27017'}, {_id:3, host:'node3:27017'}]}; > rs.initiate(cfg); > rs.status(); > exit; # ==================================================================node1 mongo node1:27017/admin replSet1:PRIMARY> rs.status(); replSet1:PRIMARY> use admin; replSet1:PRIMARY> db.dropUser("admin"); replSet1:PRIMARY> db.createUser({user: "admin",pwd: "Mongo*123456",roles: [{role:"root",db:"admin"}]}); replSet1:PRIMARY> db.auth("admin", "Mongo*123456"); replSet1:PRIMARY> exit # ==================================================================node2 mongo node2:27017/admin replSet1:SECONDARY> rs.status(); replSet1:SECONDARY> exit; # ==================================================================node3 mongo node3:27017/admin replSet1:SECONDARY> rs.status(); replSet1:SECONDARY> exit; # ==================================================================node1 mkdir -p $MONGODB_HOME/keyfile openssl rand -base64 756 > $MONGODB_HOME/keyfile/mongodb.keyfile chmod 600 $MONGODB_HOME/keyfile/mongodb.keyfile # ==================================================================node2 node3 mkdir -p $MONGODB_HOME/keyfile # ==================================================================node1 # 開啟了keyFile,隱含就開啟了auth,這個時候連線副本集就需要進行認證了 # 重啟Mongo服務 scp -r $MONGODB_HOME/keyfile/mongodb.keyfile node2:$MONGODB_HOME/keyfile/ scp -r $MONGODB_HOME/keyfile/mongodb.keyfile node3:$MONGODB_HOME/keyfile/ # ==================================================================node2 node3 chmod 600 $MONGODB_HOME/keyfile/mongodb.keyfile # ==================================================================node1 node2 node3 vi $MONGODB_HOME/conf/mongod.conf security: authorization: enabled keyFile: /usr/local/mongodb-3.4.5/keyfile/mongodb.keyfile # ==================================================================node1 node2 node3 # 先從再主 先node3 node2 最後node1 mongod -f $MONGODB_HOME/conf/mongod.conf --shutdown mongod -f $MONGODB_HOME/conf/mongod.conf mongo node1:27017 > use admin; > show dbs; # 此刻會報錯誤,沒有授權 > exit; # ==================================================================node1 mongo node1:27017/admin -u admin -p Mongo*123456 replSet1:PRIMARY> rs.status(); # > use logs; # > db.dropUser("loguser"); # > db.createUser({user: "loguser",pwd: "Mongo*123456",roles: [{role: "readWrite", db: "logs"}]}); # > db.auth("loguser", "Mongo*123456"); # > db.createUser({user: "loguser",pwd: "Mongo*123456",roles: [{role : "readWrite",db : "logs"},{role : "readWrite",db : "school"}]}); # > db.updateUser({user: "loguser",pwd: "Mongo*123456",roles: [{role : "readWrite",db : "logs"},{role : "readWrite",db : "school"}]}) > use school; > db.dropUser("schooluser"); > db.createUser({user: "schooluser",pwd: "Mongo*123456",roles: [{role: "readWrite", db: "school"}]}); > db.auth("schooluser", "Mongo*123456"); > exit; mongo node1:27017/school -u schooluser -p Mongo*123456 > use school; > db.student.insert({name:"Tom", age:16}) db.student.insert({name:"Jerry", age:15}) db.student.insert({name:"Mary", age:9}); > db.student.find(); # ==================================================================node2 mongo node2:27017/school -u schooluser -p Mongo*123456 replSet1:SECONDARY> db.student.find(); Error: error: { "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435, "codeName" : "NotMasterNoSlaveOk" } # 發生上述錯誤是因為mongodb預設讀寫都是在Primary上進行的,副本節點不允許讀寫,可以使用如下命令來允許副本讀: > db.getMongo().setSlaveOk(); replSet1:SECONDARY> db.student.find(); { "_id" : ObjectId("59d98fde9740291fac4998fb"), "name" : "Tom", "age" : 16 } { "_id" : ObjectId("59d98fe69740291fac4998fc"), "name" : "Jerry", "age" : 15 } { "_id" : ObjectId("59d98fed9740291fac4998fd"), "name" : "Mary", "age" : 9 } > exit; # ==================================================================node3 mongo node3:27017/school -u schooluser -p Mongo*123456 replSet1:SECONDARY> db.student.find(); Error: error: { "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435, "codeName" : "NotMasterNoSlaveOk" } # 發生上述錯誤是因為mongodb預設讀寫都是在Primary上進行的,副本節點不允許讀寫,可以使用如下命令來允許副本讀: > db.getMongo().setSlaveOk(); replSet1:SECONDARY> db.student.find(); { "_id" : ObjectId("59d98fde9740291fac4998fb"), "name" : "Tom", "age" : 16 } { "_id" : ObjectId("59d98fe69740291fac4998fc"), "name" : "Jerry", "age" : 15 } { "_id" : ObjectId("59d98fed9740291fac4998fd"), "name" : "Mary", "age" : 9 } > exit; # ==================================================================node3 node2 node1 # 先從再主 先node3 node2 最後node1 mongod -f $MONGODB_HOME/conf/mongod.conf --shutdown shutdown -h now # 快照 mongodb_副本集_安全認證
# 另一種方式
# 13.mongodb配置_副本集_分片_認證授權
# ==================================================================規劃
# 埠分配:
mongos: 27017
config: 27000
shard1: 27001
shard2: 27002
shard3: 27003
# ==================================================================安裝 mongodb
tar -zxvf ~/mongodb-linux-x86_64-rhel70-3.4.5.tgz -C /usr/local mv /usr/local/mongodb-linux-x86_64-rhel70-3.4.5 /usr/local/mongodb-3.4.5 rm -r ~/mongodb-linux-x86_64-rhel70-3.4.5.tgz
# 環境變數
# ==================================================================node1 node2 node3
vi /etc/profile # 在export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL下新增 export JAVA_HOME=/usr/java/jdk1.8.0_111 export ZOOKEEPER_HOME=/usr/local/zookeeper-3.4.12 export HADOOP_HOME=/usr/local/hadoop-2.7.6 export MYSQL_HOME=/usr/local/mysql export HBASE_HOME=/usr/local/hbase-1.2.4 export HIVE_HOME=/usr/local/hive-2.1.1 export SCALA_HOME=/usr/local/scala-2.12.4 export KAFKA_HOME=/usr/local/kafka_2.12-0.10.2.1 export FLUME_HOME=/usr/local/flume-1.8.0 export SPARK_HOME=/usr/local/spark-2.3.0 export STORM_HOME=/usr/local/storm-1.1.0 export ERLANG_HOME=/usr/local/erlang export RABBITMQ_HOME=/usr/local/rabbitmq_server-3.7.5 export MONGODB_HOME=/usr/local/mongodb-3.4.5 export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$ZOOKEEPER_HOME/bin:$HADOOP_HOME/bin:$HADOOP_HOME/sbin:$MYSQL_HOME/bin:$HBASE_HOME/bin:$HIVE_HOME/bin:$SCALA_HOME/bin:$KAFKA_HOME/bin:$FLUME_HOME/bin:$SPARK_HOME/bin:$SPARK_HOME/sbin:$STORM_HOME/bin:$ERLANG_HOME/bin:$RABBITMQ_HOME/ebin:$RABBITMQ_HOME/sbin:$MONGODB_HOME/bin export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export HADOOP_INSTALL=$HADOOP_HOME export HADOOP_MAPRED_HOME=$HADOOP_HOME export HADOOP_COMMON_HOME=$HADOOP_HOME export HADOOP_HDFS_HOME=$HADOOP_HOME export YARN_HOME=$HADOOP_HOME export HADOOP_COMMON_LIB_NATIVE_DIR=$HADOOP_HOME/lib/native
# ==================================================================node1
# 使環境變數生效 source /etc/profile # 檢視配置結果 echo $MONGODB_HOME
# ==================================================================node1
mkdir -p $MONGODB_HOME/{conf,data,pids,logs} vi $MONGODB_HOME/conf/mongod.conf systemLog: destination: file logAppend: true #日誌檔案存放目錄 path: /usr/local/mongodb-3.4.5/logs/mongod.log storage: #資料檔案存放目錄 dbPath: /usr/local/mongodb-3.4.5/data journal: enabled: true processManagement: #以守護程式的方式啟用,即在後臺執行 fork: true pidFilePath: /usr/local/mongodb-3.4.5/pids/mongod.pid net: port: 27017 #埠 replication: replSetName: replSet1 #security: # authorization: enabled # keyFile: /usr/local/mongodb-3.4.5/keyfile/mongodb.keyfile scp -r $MONGODB_HOME node2:/usr/local/ scp -r $MONGODB_HOME node3:/usr/local/
# ==================================================================node2 node3
# 使環境變數生效 cd ~ source /etc/profile # 檢視配置結果 echo $MONGODB_HOME
# 分別在每臺機器建立conf、mongos、config、shard1、shard2、shard3六個目錄,因為mongos不儲存資料,只需要建立日誌檔案目錄即可。
mkdir -p $MONGODB_HOME/conf mkdir -p $MONGODB_HOME/mongos/logs mkdir -p $MONGODB_HOME/config/{data,logs} mkdir -p $MONGODB_HOME/shard1/{data,logs} mkdir -p $MONGODB_HOME/shard2/{data,logs} mkdir -p $MONGODB_HOME/shard3/{data,logs} # 配置檔案內容 vi $MONGODB_HOME/conf/config.conf pidfilepath = /usr/local/mongodb-3.4.5/config/logs/configsvr.pid dbpath = /usr/local/mongodb-3.4.5/config/data logpath = /usr/local/mongodb-3.4.5/config/logs/configsvr.log logappend = true bind_ip = 0.0.0.0 port = 27000 fork = true configsvr = true #副本集名稱 replSet=configs #設定最大連線數 maxConns=20000 # 安全認證機制 # keyFile = /usr/local/mongodb-3.4.5/keyfile/keyFile.file # 開啟使用者認證 # auth = true # ==================================================================node1 node2 node3 # 設定第一個分片副本集 vi $MONGODB_HOME/conf/shard1.conf pidfilepath = /usr/local/mongodb-3.4.5/shard1/logs/shard1.pid dbpath = /usr/local/mongodb-3.4.5/shard1/data logpath = /usr/local/mongodb-3.4.5/shard1/logs/shard1.log logappend = true bind_ip = 0.0.0.0 port = 27001 fork = true #開啟web監控 httpinterface = true rest = true #副本集名稱 replSet = shard1 shardsvr = true #設定最大連線數 maxConns = 20000 # 安全認證機制 # keyFile = /usr/local/mongodb-3.4.5/keyfile/keyFile.file # 開啟使用者認證 # auth = true # ==================================================================node1 node2 node3 設定第二個分片副本集 vi $MONGODB_HOME/conf/shard2.conf pidfilepath = /usr/local/mongodb-3.4.5/shard2/logs/shard2.pid dbpath = /usr/local/mongodb-3.4.5/shard2/data logpath = /usr/local/mongodb-3.4.5/shard2/logs/shard2.log logappend = true bind_ip = 0.0.0.0 port = 27002 fork = true # 開啟web監控 httpinterface = true rest = true # 副本集名稱 replSet = shard2 shardsvr = true #設定最大連線數 maxConns = 20000 # 安全認證機制 # keyFile = /usr/local/mongodb-3.4.5/keyfile/keyFile.file # 開啟使用者認證 # auth = true # 設定第三個分片副本集 # ==================================================================node1 node2 node3 vi $MONGODB_HOME/conf/shard3.conf pidfilepath = /usr/local/mongodb-3.4.5/shard3/logs/shard3.pid dbpath = /usr/local/mongodb-3.4.5/shard3/data logpath = /usr/local/mongodb-3.4.5/shard3/logs/shard3.log logappend = true bind_ip = 0.0.0.0 port = 27003 fork = true # 開啟web監控 httpinterface=true rest=true # 副本集名稱 replSet=shard3 shardsvr = true # 設定最大連線數 maxConns=20000 # 安全認證機制 # keyFile = /usr/local/mongodb-3.4.5/keyfile/keyFile.file # 開啟使用者認證 # auth = true # ==================================================================node1 node2 node3 # 配置路由伺服器 mongos # 先啟動配置伺服器和分片伺服器,後啟動路由例項啟動路由例項:(三臺機器) vi $MONGODB_HOME/conf/mongos.conf pidfilepath = /usr/local/mongodb-3.4.5/mongos/logs/mongos.pid logpath = /usr/local/mongodb-3.4.5/mongos/logs/mongos.log logappend = true bind_ip = 0.0.0.0 port = 27017 fork = true #監聽的配置伺服器,只能有1個或者3個 configs為配置伺服器的副本集名字 configdb = configs/node1:27000,node2:27000,node3:27000 #設定最大連線數 maxConns=20000 # 安全認證機制 # keyFile = /usr/local/mongodb-3.4.5/keyfile/keyFile.file # ==================================================================node1 scp -r $MONGODB_HOME node2:/usr/local/ scp -r $MONGODB_HOME node3:/usr/local/ # 檢視配置結果 echo $MONGODB_HOME shutdown -h now # 快照 mongodb叢集前
# 啟動
# 啟動config server # ==================================================================node1 node2 node3 mongod -f $MONGODB_HOME/conf/config.conf # ==================================================================任意一臺 # 登入任意一臺配置伺服器,初始化配置副本集 mongo --port 27000 > config={_id:"configs",members:[{_id:1,host:"node1:27000"},{_id:2,host:"node2:27000"},{_id:3,host:"node3:27000"}]} > rs.initiate(config) # 其中 "_id":"configs" 應與配置檔案中配置的 replicaction.replSetName 一致,"members" 中的 "host" 為三個節點的 ip 和 port # 啟動shard1 server # ==================================================================node1 node2 node3 mongod -f $MONGODB_HOME/conf/shard1.conf # ==================================================================任意一臺 # 登陸任意一臺伺服器,初始化副本集 mongo --port 27001 #定義副本集配置,第三個節點的 "arbiterOnly":true 代表其為仲裁節點。 > config={_id:"shard1",members:[{_id:1,host:"node1:27001"},{_id:2,host:"node2:27001"},{_id:3,host:"node3:27001",arbiterOnly:true}]} > rs.initiate(config); # 啟動shard2 server # ==================================================================node1 node2 node3 mongod -f $MONGODB_HOME/conf/shard2.conf # ==================================================================任意一臺 mongo --port 27002 > config={_id:"shard2",members:[{_id:1,host:"node1:27002",arbiterOnly:true},{_id:2,host:"node2:27002"},{_id:3,host:"node3:27002"}]} > rs.initiate(config); # 啟動shard3 server # ==================================================================node1 node2 node3 mongod -f $MONGODB_HOME/conf/shard3.conf # ==================================================================任意一臺 # 登陸任意一臺伺服器,初始化副本集 mongo --port 27003 > config={_id:"shard3",members:[{_id:1,host:"node1:27003"},{_id:2,host:"node2:27003",arbiterOnly:true},{_id:3,host:"node3:27003"}]} > rs.initiate(config); # ==================================================================node1 node2 node3 # 啟動三臺伺服器的mongos server mongos -f $MONGODB_HOME/conf/mongos.conf # 啟用分片 # 目前搭建了mongodb配置伺服器、路由伺服器,各個分片伺服器,不過應用程式連線到mongos路由伺服器並不能使用分片機制,還需要在程式裡設定分片配置,讓分片生效。 # ==================================================================任意一臺 # 登陸任意一臺mongos mongo --port 27017 #使用admin資料庫 > use admin; # 串聯路由伺服器與分配副本集 > sh.addShard("shard1/node1:27001,node2:27001,node3:27001") > sh.addShard("shard2/node1:27002,node2:27002,node3:27002") > sh.addShard("shard3/node1:27003,node2:27003,node3:27003") #檢視分片伺服器的配置 > db.runCommand({listshards:1}); # 檢視叢集狀態 > sh.status(); # 測試 # 目前配置服務、路由服務、分片服務、副本集服務都已經串聯起來了,但我們的目的是希望插入資料,資料能夠自動分片。連線在mongos上,準備讓指定的資料庫、指定的集合分片生效。 # 指定test分片生效 > db.runCommand({enablesharding:"test"}); # 指定資料庫裡需要分片的集合和片鍵 # 設定分片的集合名稱。且必須指定Shard Key,系統會自動建立索引,然後根據這個shard Key來計算 > db.runCommand({shardcollection:"test.table1",key:{_id:"hashed"}}); # 我們設定test的 table1 表需要分片,根據 id 自動分片到 shard1 ,shard2,shard3 上面去。要這樣設定是因為不是所有mongodb 的資料庫和表 都需要分片 #使用test > use test; #插入測試資料 > for (var i = 1; i <= 5000; i++) db.table1.save({id:i,"field1":"testval"+i}); # WriteResult({ "nInserted" : 1 }) > db.table1.find().count(); > db.table1.find().limit(5); # 檢視分片 > db.table1.stats(); # 可以看到資料分到3個分片,表示已經成功了 #預設是從主節點讀寫資料,副本節點上不允許讀,需要設定副本節點可以讀 repset:SECONDARY> db.getMongo().setSlaveOk(); repset:SECONDARY> show tables; # 檢視機器 主 從 仲裁 mongo --host node1 --port 27001 mongo --host node2 --port 27001 mongo --host node3 --port 27001 mongo --host node1 --port 27002 mongo --host node2 --port 27002 mongo --host node3 --port 27002 mongo --host node1 --port 27003 mongo --host node2 --port 27003 mongo --host node3 --port 27003 shutdown -h now # 快照 mongodb未安全認證
# 開始安全認證
mongod -f $MONGODB_HOME/conf/config.conf mongod -f $MONGODB_HOME/conf/shard1.conf mongod -f $MONGODB_HOME/conf/shard2.conf mongod -f $MONGODB_HOME/conf/shard3.conf mongos -f $MONGODB_HOME/conf/mongos.conf mongo --port 27017 # 新增使用者 注意一定要使用admin資料庫 > use admin; > db.dropUser("admin"); > db.createUser({user: "admin",pwd: "Mongo*123",roles: [{role:"root",db:"admin"}]}); > db.auth("admin", "Mongo*123"); > exit; # ==================================================================node1 # 生成金鑰檔案。 # 在keyFile身份驗證中,副本集中的每個mongod例項都使用keyFile的內容作為共享密碼,只有具有正確金鑰檔案的mongod或者mongos例項可以連線到副本集。金鑰檔案的內容必須在6到1024個字元之間,並且在unix/linux系統中檔案所有者必須有對檔案至少有讀的許可權。 # 可以用任何方式生成金鑰檔案例如: #隨機生成keyFile或者手動寫入,key的長度必須是6-1024的base64字元,unix下必須相同組許可權,windows下不需要 mkdir -p $MONGODB_HOME/keyfile openssl rand -base64 756 > $MONGODB_HOME/keyfile/keyFile.file chmod 600 $MONGODB_HOME/keyfile/keyFile.file # ==================================================================node2 mkdir -p $MONGODB_HOME/keyfile # ==================================================================node3 mkdir -p $MONGODB_HOME/keyfile # ==================================================================node1 # 第一條命令是生成金鑰檔案,第二條命令是使用chmod更改檔案許可權,為檔案所有者提供讀許可權 # 將金鑰複製到叢集中的每臺機器指定位置 scp -r $MONGODB_HOME/keyfile/keyFile.file [email protected]:$MONGODB_HOME/keyfile scp -r $MONGODB_HOME/keyfile/keyFile.file [email protected]:$MONGODB_HOME/keyfile # ==================================================================node2 chmod 600 $MONGODB_HOME/keyfile/keyFile.file # ==================================================================node3 chmod 600 $MONGODB_HOME/keyfile/keyFile.file # ==================================================================node1 # 依次在每臺機器上的mongod(注意是所有的mongod不是mongos)的配置檔案中加入下面一段配置。 # config server,shard1,shard2,shard3都加入下面的配置檔案 # 配置檔案內容 vi $MONGODB_HOME/conf/config.conf keyFile = /usr/local/mongodb-3.4.5/keyfile/keyFile.file auth = true vi $MONGODB_HOME/conf/shard1.conf keyFile = /usr/local/mongodb-3.4.5/keyfile/keyFile.file auth = true vi $MONGODB_HOME/conf/shard2.conf keyFile = /usr/local/mongodb-3.4.5/keyfile/keyFile.file auth = true vi $MONGODB_HOME/conf/shard3.conf keyFile = /usr/local/mongodb-3.4.5/keyfile/keyFile.file auth = true vi $MONGODB_HOME/conf/mongos.conf keyFile = /usr/local/mongodb-3.4.5/keyfile/keyFile.file scp -r $MONGODB_HOME/conf/config.conf [email protected]:$MONGODB_HOME/conf scp -r $MONGODB_HOME/conf/shard1.conf [email protected]:$MONGODB_HOME/conf scp -r $MONGODB_HOME/conf/shard2.conf [email protected]:$MONGODB_HOME/conf scp -r $MONGODB_HOME/conf/shard3.conf [email protected]:$MONGODB_HOME/conf scp -r $MONGODB_HOME/conf/mongos.conf [email protected]:$MONGODB_HOME/conf scp -r $MONGODB_HOME/conf/config.conf [email protected]:$MONGODB_HOME/conf scp -r $MONGODB_HOME/conf/shard1.conf [email protected]:$MONGODB_HOME/conf scp -r $MONGODB_HOME/conf/shard2.conf [email protected]:$MONGODB_HOME/conf scp -r $MONGODB_HOME/conf/shard3.conf [email protected]:$MONGODB_HOME/conf scp -r $MONGODB_HOME/conf/mongos.conf [email protected]:$MONGODB_HOME/conf # ==================================================================node3 node2 node1 # 先從再主 mongod -f $MONGODB_HOME/conf/config.conf --shutdown mongod -f $MONGODB_HOME/conf/shard1.conf --shutdown mongod -f $MONGODB_HOME/conf/shard2.conf --shutdown mongod -f $MONGODB_HOME/conf/shard3.conf --shutdown # ==================================================================node1 node2 node3 # mongodb的啟動順序是,先啟動配置伺服器,在啟動分片,最後啟動mongos. mongod -f $MONGODB_HOME/conf/config.conf mongod -f $MONGODB_HOME/conf/shard1.conf mongod -f $MONGODB_HOME/conf/shard2.conf mongod -f $MONGODB_HOME/conf/shard3.conf mongos -f $MONGODB_HOME/conf/mongos.conf mongo --port 27017 > use admin; > show dbs; # 此刻會報錯誤,沒有授權 > exit; # ==================================================================node1 mongo node1:27017/admin -u admin -p Mongo*123 > show dbs; > db.runCommand({enablesharding:"logs"}); > db.runCommand({shardcollection:"logs.logList",key:{id:1}}) > use logs; > db.dropUser("loguser"); > db.createUser({user:"loguser",pwd:"Mongo*123",roles:[{role:"readWrite",db:"logs"}]}); > db.auth("loguser","Mongo*123"); > db.logList.ensureIndex({key: -1}); # > db.logList.ensureIndex({DateTime: -1}); # > db.logList.ensureIndex({PrimaryData: -1}); > exit; mongo node2:27017/logs -u loguser -p Mongo*123 ...SECONDARY> db.logList.find().count(); Error: error: { "ok" : 0, "errmsg" : "not master and slaveOk=false", "code" : 13435, "codeName" : "NotMasterNoSlaveOk" } # 發生上述錯誤是因為mongodb預設讀寫都是在Primary上進行的,副本節點不允許讀寫,可以使用如下命令來允許副本讀: > db.getMongo().setSlaveOk(); ...SECONDARY> db.student.find(); mongo node1:27017/logs -u loguser -p Mongo*123 > use logs; > show tables; > db.logList.find().count(); #插入測試資料 > for (var i = 1; i <= 5000; i++) db.logList.save({id:i,"field1":"testval" + i}); # WriteResult({ "nInserted" : 1 }) > db.logList.find().count(); > db.logList.find().limit(5); # 檢視分片 > db.logList.stats(); > db.logList.find().skip(0).limit(5); > db.logList.remove({}); shutdown -h now # 快照 mongodb安全認證