NoSQL: MongoDB
Configuration
# mongod.conf # for documentation of all options, see: # http://docs.mongodb.org/manual/reference/configuration-options/ # Where and how to store data. storage: dbPath: /Users/zhliu33/database/mongo/data journal: enabled: true # engine: # wiredTiger: # where to write logging data. systemLog: destination: file logAppend: true path: /Users/zhliu33/database/mongo/log/mongo.log # network interfaces net: bindIp: 0.0.0.0, 127.0.0.1, ::1 port: 27017 ipv6: false # how the process runs processManagement: timeZoneInfo: /usr/share/zoneinfo fork: true setParameter: enableLocalhostAuthBypass: true security: authorization: enabled #operationProfiling: #replication: #sharding: ## Enterprise-Only Options: #auditLog: #snmp:
Instructions
#--------------------------------------------------------------------- # Install #--------------------------------------------------------------------- # macOS https://github.com/mongodb/homebrew-brew #--------------------------------------------------------------------- # Authentication # https://www.mongodb.com/docs/manual/core/authentication/ #--------------------------------------------------------------------- mongod --auth --port 27017 --dbpath /var/lib/mongodb mongosh --port 27017 use admin db.createUser({ user: "root", pwd: "root", roles: ["root"]}) db.createUser( { user: "myadmin", pwd: "myadmin", roles: [ { role: "userAdminAnyDatabase", db: "admin" }, { role: "readWriteAnyDatabase", db: "admin" }, { role: "root", db: "admin" } ] } ) db.createUser( { user: "myTester", pwd: passwordPrompt(), // or cleartext password roles: [ { role: "readWrite", db: "test" }, { role: "read", db: "reporting" } ] } ) # grant role db.grantRolesToUser( "myadmin", [ { role: "clusterAdmin", db: "admin" } ] ) # 查看用户roles & privileges db.getUser('myadmin') db.getRole( "clusterAdmin", { showPrivileges: true } ) # change password db.changeUserPassword("reporting", "SOh3TbYhxuLiW8ypJPxmt1oOfL") The userAdminAnyDatabase role allows this user to: create users grant or revoke roles from users create or modify customs roles # 关闭mongodb db.adminCommand( { shutdown: 1 } ) # Authentication After Connection use admin db.auth("root", "root") # List all users use admin db.system.users.find() To list all users of a sharded cluster that were created through a mongos, connect to a mongos and run the preceding command. MongoDB stores users that are created through a mongos in the admin database of the config servers. To list all shard local users, connect to the respective shard directly and run the preceding command. MongoDB stores shard local users in the admin database of the shard itself. These shard local users are independent from the users added to the sharded cluster through a mongos. Shard local users are local to the shard and are inaccessible to mongos. #--------------------------------------------------------------------- # https://docs.mongodb.com/manual/tutorial/deploy-replica-set/ #--------------------------------------------------------------------- mkdir -pv repl/instance{1..3} mongod --bind_ip 0.0.0.0 --port 27017 --dbpath repl/instance1 --replSet rs mongod --bind_ip 0.0.0.0 --port 27018 --dbpath repl/instance2 --replSet rs mongod --bind_ip 0.0.0.0 --port 27019 --dbpath repl/instance3 --replSet rs mongosh --port 27017 let config={ _id : "rs", members: [ { _id: 0, host: "bemoan:27017" }, { _id: 1, host: "bemoan:27018" }, { _id: 2, host: "bemoan:27019" } ] } rs.initiate(config) rs.conf() rs.status() use rty show tables for(let i=0;i<100;++i){ db.user.insertOne({_id:i,name:'rty_'+i,age:10+i}) } db.user.find().skip(10).limit(5) db.user.countDocuments() db.user.deleteMany({}) db.user.insertOne({name:'rty'}) mongosh --port 27018 (secondary) # https://docs.mongodb.com/manual/reference/method/Mongo.setReadPref/ db.getMongo().setReadPref('primaryPreferred') # /etc/mongod.conf replication: replSetName: "rs0" net: bindIp: localhost,<hostname(s)|ip address(es)> #--------------------------------------------------------------------- # mongod #--------------------------------------------------------------------- mongod --bind_ip localhost,My-Example-Associated-Hostname --config /etc/mongod.conf #--------------------------------------------------------------------- # Sharding #--------------------------------------------------------------------- mkdir -pv shard/{primary,secondary,config}{1..3} mongod --dbpath shard/primary1 --bind_ip 0.0.0.0 --shardsvr --replSet rs1 --port 27017 mongod --dbpath shard/secondary1 --bind_ip 0.0.0.0 --shardsvr --replSet rs1 --port 27027 let config={ _id: 'rs1', members: [ {_id:0, host: 'bemoan:27017'}, {_id:1, host: 'bemoan:27027'} ] } mongod --dbpath shard/primary2 --bind_ip 0.0.0.0 --shardsvr --replSet rs2 --port 27018 mongod --dbpath shard/secondary2 --bind_ip 0.0.0.0 --shardsvr --replSet rs2 --port 27028 let config={ _id: 'rs2', members: [ {_id:0, host: 'bemoan:27018'}, {_id:1, host: 'bemoan:27028'} ] } mongod --dbpath shard/primary3 --bind_ip 0.0.0.0 --shardsvr --replSet rs3 --port 27019 mongod --dbpath shard/secondary3 --bind_ip 0.0.0.0 --shardsvr --replSet rs3 --port 27029 let config={ _id: 'rs3', members: [ {_id:0, host: 'bemoan:27019'}, {_id:1, host: 'bemoan:27029'} ] } mongod --dbpath shard/config1 --bind_ip 0.0.0.0 --configsvr --replSet configsvr --port 27010 mongod --dbpath shard/config2 --bind_ip 0.0.0.0 --configsvr --replSet configsvr --port 27011 mongod --dbpath shard/config3 --bind_ip 0.0.0.0 --configsvr --replSet configsvr --port 27012 let config={ _id: 'configsvr', configsvr: true, members: [ {_id:0, host: 'bemoan:27010'}, {_id:1, host: 'bemoan:27011'}, {_id:2, host: 'bemoan:27012'} ] } mongos --port 27013 --configdb configsvr/bemoan:27010,bemoan:27011,bemoan:27012 --bind_ip 0.0.0.0 mongosh --port 27013 # 设置分片 sh.addShard( "rs1/bemoan:27017,bemoan:27027") sh.addShard( "rs2/bemoan:27018,bemoan:27028") sh.addShard( "rs3/bemoan:27019,bemoan:27029") # 指定分片库 sh.enableSharding('vapour') # Shard a Collection sh.shardCollection("<database>.<collection>", { <shard key field> : "hashed" } ) sh.shardCollection('vapour.user',{_id:'hashed'}) #--------------------------------------------------------------------- # mongodump & mongorestore #--------------------------------------------------------------------- CONNECTION_STRING='mongodb://root:yvy6bkc2cqv%40DMJ0ynh@dds-uf6208c66fef82d41375-pub.mongodb.rds.aliyuncs.com:3717,dds-uf6208c66fef82d42574-pub.mongodb.rds.aliyuncs.com:3717/?replicaSet=mgset-65096576' mongodump --authenticationDatabase=admin --gzip -j8 -o backup -d alicloud $CONNECTION_STRING CONNECTION_STRING='mongodb://root:root@localhost:27017/?authMechanism=SCRAM-SHA-1&authSource=admin' mongorestore --authenticationDatabase=admin --gzip -j8 --drop $CONNECTION_STRING backup #--------------------------------------------------------------------- # mongodump & mongorestore #--------------------------------------------------------------------- mongosh --quiet --eval 'db.shutdownServer()' mongodb://root:root@localhost:27017/admin
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2022-03-26 Vue: 全局函数和变量
2022-03-26 Vue: 自定义hooks转img为base64
2022-03-26 Vue: 拖动HTMLElement
2022-03-26 Vue: 兄弟组件利用自定义Bus传参
2021-03-26 OOM Killer
2021-03-26 Java Spring cloud classic architecture
2021-03-26 Samba