# shell-mongodb-cluster **Repository Path**: brianchou/shell-mongodb-cluster ## Basic Information - **Project Name**: shell-mongodb-cluster - **Description**: Shell脚本部署mongodb cluster集群 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 0 - **Created**: 2021-06-05 - **Last Updated**: 2023-06-10 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # MongoDB Cluster 部署 上传`shell-mongodb-cluster`至每个服务器的`/opt`目录,默认集群目录`/opt/mongodb_cluster`,如果要修改则需要修改`auto_install_mongodb.sh` 、`auto_init_cluster.sh`和`mongodb` 如果修改默认端口,则需要修改`auto_install_mongodb.sh` 和`auto_init_cluster.sh`文件 `auto_install_mongodb.sh` 和 `auto_init_cluster.sh` 脚本里的`IPS`数组根据实际情况修改 `cacheSizeGB默认0.5` 可以根据实际情况修改 `auto_install_mongodb.sh`中的`CACHE_SIZE=0.5` 如果`passwd.js`文件不在`/opt/shell-mongodb-cluster`目录下,那么需要修改`auto_init_cluster.sh`中的`PASSWD_PATH` mongodb文件为`mongodb-linux-x86_64-rhel70-4.0.3.tgz` 如果不是这个版本,则需要修改`auto_install_mongodb.sh`文件中的`VERSION`值 ## 1. 节点1执行 节点1服务器为IPS数组的第一个元素 ```shell ssh-keygen -f /root/.ssh/id_rsa -P '' # 将节点1的公钥拷贝到节点2和3 ssh-copy-id root@节点2IP ssh-copy-id root@节点3IP ``` ## 2. 每个节点执行 ```shell cd /opt/shell-mongodb-cluster chmod +x *.sh ./auto_install_mongodb.sh ``` 在执行到启动mongos.conf文件时,mongodb用户进程不会自动退出,需要`ctrl+c`终止,这里有点问题,未解决。 ```shell ==================== 启动 mongos.conf ==================== 2021-06-06T10:31:07.573+0800 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none' about to fork child process, waiting until server is ready for connections. forked process: 4023 ``` ## 3. 节点1执行 管理员账号及密码在`passwd.js`文件中,内容如下,默认端口为20000,默认用户为root,默认密码为123456,如果前面修改了默认端口,则此处也需要修改。 ```json conn = new Mongo("127.0.0.1:20000"); db = conn.getDB("admin"); printjson(db.createUser({user:"root",pwd:"123456",roles:[ { "role" : "root", "db" : "admin" } ]})); ``` 初始化集群默认在节点1执行,注意:节点1一定是`IPS`数组的第一个元素 ```shell ./auto_init_cluster.sh ``` 添加一个具有`readWrite`权限的用户 ```shell source /etc/profile mongo --port 20000 mongos> use admin mongos> db.auth('root','123456') mongos> use test; mongos> db.createUser({user: "test",pwd: "123456",roles: [{ role: "readWrite", db: "test" }]}); mongos> exit ``` ## 4. 每个节点执行 ```shell source /etc/profile # 如果要直接使用mongo相关命令,需要执行一次source /etc/profile service mongodb restart # 重启数据库 # config,shard_server1到3,mongos 共5个进程 ps -ef|grep -v grep|grep mongo ``` `MongoDB集群验证`节点1插入数据 ```shell mongo --port 20000 mongos> use test; mongos> db.auth('test','123456') mongos> db.col.insert({title: 'MongoDB',description: 'MongoDB NoSQL',by: 'BrianChou',url: 'https://hello',tags: ['mongodb', 'database', 'NoSQL'],likes: 100}) ``` 节点2或3查看数据 ```shell mongo --port 20000 mongos> use test; mongos> db.auth('test','123456') mongos> db.col.find() ``` ## 5. 重新安装 需要先停止应用,然后再安装 ```shell ps -ef|grep -v grep|grep mongo|awk '{print $2}'|xargs kill -9 rm -rf /opt/mongodb_cluster ``` 修改`test`账号的密码 ```shell mongos> use admin; switched to db admin mongos> db.auth('root','123456') 1 mongos> use test; switched to db test mongos> db.updateUser('test',{pwd: '654321'}) mongos> show users { "_id" : "test.test", "user" : "test", "db" : "test", "roles" : [ { "role" : "readWrite", "db" : "test" } ], "mechanisms" : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ] } ```