搭建mongodb
这里直接将的是搭建mongodb的复制集,一主两从
1️⃣:下载mongodb4.2.23并解压缩到任意目录下
#这里可以是你服务器的任意目录 wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.23.tgz tar -zxvf mongodb-linux-x86_64-rhel70-4.2.23.tgz
2️⃣:创建相关文件夹
mkdir -p /home/M/mongodb/data/db{1,2,3}
3️⃣:编写配置文件
systemLog: destination: file path: /home/M/mongodb/data/db1/mongodb.log #log path logAppend: true storage: dbPath: /home/M/mongodb/data/db1 #data directory net: bindIp: 0.0.0.0 port: 28817 #port security: authorization: enabled keyFile: /home/M/mongodb/mongo.key replication: replSetName: rs0 processManagement: fork: true
这里的keyFile文件主要是秘钥,可以使用如下命令生成
openssl rand -base64 90 -out ./mongo.key
这里配置文件分别置于db{1,2,3}下
4️⃣:编辑/ect/profile文件,末尾加入如下语句
export PATH=$PATH:/home/M/mongodb-linux-x86_64-rhel70-4.2.23/bin #保存退出后执行如下命令 source /etc/profile
5️⃣:执行如下命令配置复制集
mongo 10.0.4.7:28817 rs.config({_id:"rs0",members:[{_id:0,host:"127.0.0.1:28817"},{_id:1,host:"127.0.0.1:28818"},{_id:2,host:"127.0.0.1:28819"}]}) #这里切记登录别用自己的hostname,否则外部是不能访问的,如果想外部访问用外网ip,另外如果是单机的话内网ip也行
6️⃣:设计账号密码并在主节点配置权限
use admin db.createUser({ user:'admin',pwd:'bkwEtSrIA3EXQWgo',roles:[ { role:'userAdminAnyDatabase', db: 'admin'}]}); db.auth('admin','bkwEtSrIA3EXQWgo') db.grantRolesToUser( "admin" , [ { role: "dbOwner", db: "admin" },{ "role": "clusterAdmin", "db": "admin" }, { "role": "userAdminAnyDatabase", "db": "admin" }, { "role": "dbAdminAnyDatabase", "db": "admin" }]) #创建root用户 db.createUser({user:"root",pwd:"bYBBkcZkliadV6M6",roles:[{role:"root",db:"admin"}]}); #赋予root用户操作命令的权限,不然会提示报错没有权限 Error: error: { "operationTime" : Timestamp(1670313676, 1), "ok" : 0, "errmsg" : "not authorized on base_bo to execute command { find: \"test\", filter: {}, lsid: { id: UUID(\"39276352-9b03-48b7-8586-44d7b7714e32\") }, $clusterTime: { clusterTime: Timestamp(1670313666, 1), signature: { hash: BinData(0, B91D68894743B2DFE5C5BA0AA4B8E5F23741B0E3), keyId: 7173561399774281733 } }, $db: \"base_bo\" }", "code" : 13, "codeName" : "Unauthorized", "$clusterTime" : { "clusterTime" : Timestamp(1670313676, 1), "signature" : { "hash" : BinData(0,"VYwnvDNG39YupihA7Jsri923qE8="), "keyId" : NumberLong("7173561399774281733") } } } db.grantRolesToUser ( "root", [ { role: "__system", db: "admin" } ] )
另外给大家安利一个软件,具体页面如下,它的功能很强大