使用ansible剧本rpm方式离线安装mongodb分片集群
【说明】
使用ansible安装一个分片集群,三台服务器,三个mongos,三个config,三个分片节点,每三个分片有三个副本(每个节点运行三个端口的mongod)
IP |
作用1 |
作用2 |
作用3 |
作用4 |
作用5 |
10.x.x.21 |
Mongos |
Config server |
分片1主节点 |
分片2从节点 |
分片3从节点 |
10.x.x.188 |
Mongos |
Config server |
分片2主节点 |
分片1从节点 |
分片3从节点 |
10.x.x.187 |
Mongos |
Config server |
分片3主节点 |
分片2从节点 |
分片1从节点 |
对应端口 |
27017 |
27019 |
2700 |
2701 |
2702 |
[mongo_servers] 10.x.x.21 ansible_user=root ansible_ssh_pass=xxxxxxxx cluster_role=mongo1 mongod_port=2700 10.x.x.187 ansible_user=root ansible_ssh_pass=xxxxxxxx cluster_role=mongo2 mongod_port=2701 10.x.x.188 ansible_user=root ansible_ssh_pass=xxxxxxxx cluster_role=mongo3 mongod_port=2702 #The list of servers where replication should happen, including the master server. [replication_servers] 》》》这里注意cluster_role跟上面是反着的,因为分片节点上面每个节点都会保留三个分片的数据(副本节点) 10.x.x.21 ansible_user=root ansible_ssh_pass=xxxxxxxx cluster_role=mongo3 10.x.x.187 ansible_user=root ansible_ssh_pass=xxxxxxxx cluster_role=mongo1 10.x.x.188 ansible_user=root ansible_ssh_pass=xxxxxxxx cluster_role=mongo2 #The list of mongodb configuration servers, make sure it is 1 or 3. [mongoc_servers] 10.x.x.21 ansible_user=root ansible_ssh_pass=xxxxxxxx cluster_role=mongo1 10.x.x.187 ansible_user=root ansible_ssh_pass=xxxxxxxx cluster_role=mongo2 10.x.x.188 ansible_user=root ansible_ssh_pass=xxxxxxxx cluster_role=mongo3 #The list of servers where mongos servers would run. [mongos_servers] 10.x.x.21 ansible_user=root ansible_ssh_pass=xxxxxxxx cluster_role=mongo1 10.x.x.187 ansible_user=root ansible_ssh_pass=xxxxxxxx cluster_role=mongo2 10.x.x.188 ansible_user=root ansible_ssh_pass=xxxxxxxx cluster_role=mongo3
使用GitHub上面的一个脚本来修改:https://github.com/a-h/ansible-mongodb-cluster/tree/master
修改了mongodb7版本的信息,使用hosts具体IP信息来运行剧本,修改mongodb的配置参数,修改mongos的配置参数,在mongodb时候创建管理用户,修改卸载内容,添加mongosh内容,修改测试分片数据脚本
特别注意:这里清理的目录会被删掉,确认好里面没有其他重要文件
说明:由于剧本信息较多,如有需要请说明。
【剧本说明】
[root@mysqlbackup:/root/auto/ansible-mongodb-cluster]$ tree mongodb7/ mongodb7/ ├── clean.yml ├── group_vars │ └── all.yml ├── hosts ├── mongod.yml ├── roles │ ├── common │ │ ├── files │ │ │ ├── Centos-ali.repo │ │ │ ├── epel.repo.j2 │ │ │ ├── mongodb-org-3.2.repo │ │ │ ├── pip.conf │ │ │ └── RPM-GPG-KEY-EPEL-6 │ │ ├── handlers │ │ │ └── main.yml │ │ ├── tasks │ │ │ ├── main.yml │ │ │ └── main.yml_bak │ │ ├── templates │ │ │ ├── hosts.j2 │ │ │ ├── hosts.j2_bak │ │ │ └── iptables.j2 │ │ └── vars │ ├── mongoc │ │ ├── files │ │ │ └── secret │ │ ├── tasks │ │ │ └── main.yml │ │ └── templates │ │ ├── mongoc.conf.j2 │ │ ├── mongoc_init.js.j2 │ │ └── mongoc.service │ ├── mongod │ │ ├── files │ │ │ └── secret │ │ ├── tasks │ │ │ ├── main.yml │ │ │ └── main.yml_bak │ │ └── templates │ │ ├── mongod.conf.j2 │ │ ├── mongod_init.js.j2 │ │ ├── mongod.service.j2 │ │ ├── mongod_user.js.j2 │ │ └── shard_init.j2 │ ├── mongos │ │ ├── files │ │ │ └── secret │ │ ├── tasks │ │ │ └── main.yml │ │ ├── templates │ │ │ ├── mongos.conf.j2 │ │ │ ├── mongos_init.js.j2 │ │ │ └── mongos.service.j2 │ │ └── vars │ │ └── main.yml │ └── shard_test │ ├── tasks │ │ └── main.yml │ └── templates │ └── shard_test.js ├── shard_test.yml └── site.yml
【运行结果】
[root@mysqlbackup:/root/auto/ansible-mongodb-cluster-master/mongodb7]$ ansible-playbook -i hosts site.yml PLAY [all] ********************************************************************************************************************************************************************************************** TASK [Gathering Facts] ********************************************************************************************************************************************************************************** ok: [10.x.x.21] ok: [10.x.x.188] ok: [10.x.x.187] TASK [common : download rpm] **************************************************************************************************************************************************************************** ok: [10.x.x.21] => (item={u'name': u'mongodb-org-database-tools-extra-7.0.12-1.el7.x86_64.rpm'}) ok: [10.x.x.188] => (item={u'name': u'mongodb-org-database-tools-extra-7.0.12-1.el7.x86_64.rpm'}) ok: [10.x.x.187] => (item={u'name': u'mongodb-org-database-tools-extra-7.0.12-1.el7.x86_64.rpm'}) ok: [10.x.x.21] => (item={u'name': u'mongodb-org-database-7.0.12-1.el7.x86_64.rpm'}) ok: [10.x.x.188] => (item={u'name': u'mongodb-org-database-7.0.12-1.el7.x86_64.rpm'}) ok: [10.x.x.21] => (item={u'name': u'mongodb-org-tools-7.0.12-1.el7.x86_64.rpm'}) ok: [10.x.x.187] => (item={u'name': u'mongodb-org-database-7.0.12-1.el7.x86_64.rpm'}) ok: [10.x.x.188] => (item={u'name': u'mongodb-org-tools-7.0.12-1.el7.x86_64.rpm'}) ok: [10.x.x.187] => (item={u'name': u'mongodb-org-tools-7.0.12-1.el7.x86_64.rpm'}) ok: [10.x.x.21] => (item={u'name': u'mongodb-org-server-7.0.12-1.el7.x86_64.rpm'}) ok: [10.x.x.188] => (item={u'name': u'mongodb-org-server-7.0.12-1.el7.x86_64.rpm'}) ok: [10.x.x.187] => (item={u'name': u'mongodb-org-server-7.0.12-1.el7.x86_64.rpm'}) ok: [10.x.x.21] => (item={u'name': u'mongodb-org-mongos-7.0.12-1.el7.x86_64.rpm'}) ok: [10.x.x.188] => (item={u'name': u'mongodb-org-mongos-7.0.12-1.el7.x86_64.rpm'}) ok: [10.x.x.187] => (item={u'name': u'mongodb-org-mongos-7.0.12-1.el7.x86_64.rpm'}) ok: [10.x.x.188] => (item={u'name': u'mongodb-mongosh-2.2.12.x86_64.rpm'}) ok: [10.x.x.187] => (item={u'name': u'mongodb-mongosh-2.2.12.x86_64.rpm'}) ok: [10.x.x.188] => (item={u'name': u'mongodb-database-tools-100.10.0-1.x86_64.rpm'}) ok: [10.x.x.21] => (item={u'name': u'mongodb-mongosh-2.2.12.x86_64.rpm'}) ok: [10.x.x.187] => (item={u'name': u'mongodb-database-tools-100.10.0-1.x86_64.rpm'}) ok: [10.x.x.21] => (item={u'name': u'mongodb-database-tools-100.10.0-1.x86_64.rpm'}) TASK [common : install rpm] ***************************************************************************************************************************************************************************** changed: [10.x.x.21] => (item=[u'/tmp/mongodb-org-database-tools-extra-7.0.12-1.el7.x86_64.rpm', u'/tmp/mongodb-org-database-7.0.12-1.el7.x86_64.rpm', u'/tmp/mongodb-org-tools-7.0.12-1.el7.x86_64.rpm', u'/tmp/mongodb-org-server-7.0.12-1.el7.x86_64.rpm', u'/tmp/mongodb-org-mongos-7.0.12-1.el7.x86_64.rpm', u'/tmp/mongodb-mongosh-2.2.12.x86_64.rpm', u'/tmp/mongodb-database-tools-100.10.0-1.x86_64.rpm']) changed: [10.x.x.188] => (item=[u'/tmp/mongodb-org-database-tools-extra-7.0.12-1.el7.x86_64.rpm', u'/tmp/mongodb-org-database-7.0.12-1.el7.x86_64.rpm', u'/tmp/mongodb-org-tools-7.0.12-1.el7.x86_64.rpm', u'/tmp/mongodb-org-server-7.0.12-1.el7.x86_64.rpm', u'/tmp/mongodb-org-mongos-7.0.12-1.el7.x86_64.rpm', u'/tmp/mongodb-mongosh-2.2.12.x86_64.rpm', u'/tmp/mongodb-database-tools-100.10.0-1.x86_64.rpm']) changed: [10.x.x.187] => (item=[u'/tmp/mongodb-org-database-tools-extra-7.0.12-1.el7.x86_64.rpm', u'/tmp/mongodb-org-database-7.0.12-1.el7.x86_64.rpm', u'/tmp/mongodb-org-tools-7.0.12-1.el7.x86_64.rpm', u'/tmp/mongodb-org-server-7.0.12-1.el7.x86_64.rpm', u'/tmp/mongodb-org-mongos-7.0.12-1.el7.x86_64.rpm', u'/tmp/mongodb-mongosh-2.2.12.x86_64.rpm', u'/tmp/mongodb-database-tools-100.10.0-1.x86_64.rpm']) TASK [common : Create the mongod user] ****************************************************************************************************************************************************************** ok: [10.x.x.21] ok: [10.x.x.188] ok: [10.x.x.187] TASK [common : Create the data directory for the namenode metadata] ************************************************************************************************************************************* changed: [10.x.x.21] changed: [10.x.x.187] changed: [10.x.x.188] TASK [common : create log directory for mongodb] ******************************************************************************************************************************************************** ok: [10.x.x.21] ok: [10.x.x.188] ok: [10.x.x.187] TASK [common : create run directory for mongodb] ******************************************************************************************************************************************************** ok: [10.x.x.21] ok: [10.x.x.188] ok: [10.x.x.187] TASK [common : Generate /etc/hosts content] ************************************************************************************************************************************************************* ok: [10.x.x.21] ok: [10.x.x.188] ok: [10.x.x.187] TASK [common : Read content from remote /tmp/hosts_content] ********************************************************************************************************************************************* changed: [10.x.x.21 -> 10.x.x.21] changed: [10.x.x.188 -> 10.x.x.188] changed: [10.x.x.187 -> 10.x.x.187] TASK [common : Use the content read from the remote file] *********************************************************************************************************************************************** ok: [10.x.x.21] ok: [10.x.x.188] ok: [10.x.x.187] PLAY [mongo_servers] ************************************************************************************************************************************************************************************ TASK [Gathering Facts] ********************************************************************************************************************************************************************************** ok: [10.x.x.188] ok: [10.x.x.21] ok: [10.x.x.187] TASK [create data directory for mongodb] **************************************************************************************************************************************************************** changed: [10.x.x.21 -> 10.x.x.21] => (item=10.x.x.21) changed: [10.x.x.187 -> 10.x.x.21] => (item=10.x.x.21) changed: [10.x.x.188 -> 10.x.x.21] => (item=10.x.x.21) changed: [10.x.x.21 -> 10.x.x.187] => (item=10.x.x.187) changed: [10.x.x.187 -> 10.x.x.187] => (item=10.x.x.187) changed: [10.x.x.188 -> 10.x.x.187] => (item=10.x.x.187) changed: [10.x.x.21 -> 10.x.x.188] => (item=10.x.x.188) changed: [10.x.x.187 -> 10.x.x.188] => (item=10.x.x.188) changed: [10.x.x.188 -> 10.x.x.188] => (item=10.x.x.188) TASK [Create the mongodb startup file] ****************************************************************************************************************************************************************** ok: [10.x.x.21 -> 10.x.x.21] => (item=10.x.x.21) ok: [10.x.x.187 -> 10.x.x.21] => (item=10.x.x.21) ok: [10.x.x.188 -> 10.x.x.21] => (item=10.x.x.21) ok: [10.x.x.187 -> 10.x.x.187] => (item=10.x.x.187) ok: [10.x.x.21 -> 10.x.x.187] => (item=10.x.x.187) ok: [10.x.x.188 -> 10.x.x.187] => (item=10.x.x.187) ok: [10.x.x.187 -> 10.x.x.188] => (item=10.x.x.188) ok: [10.x.x.21 -> 10.x.x.188] => (item=10.x.x.188) ok: [10.x.x.188 -> 10.x.x.188] => (item=10.x.x.188) TASK [mongod : authorization] *************************************************************************************************************************************************************************** ok: [10.x.x.21] ok: [10.x.x.187] ok: [10.x.x.188] TASK [Create the mongodb configuration file] ************************************************************************************************************************************************************ changed: [10.x.x.187 -> 10.x.x.21] => (item=10.x.x.21) changed: [10.x.x.188 -> 10.x.x.21] => (item=10.x.x.21) changed: [10.x.x.21 -> 10.x.x.21] => (item=10.x.x.21) changed: [10.x.x.21 -> 10.x.x.187] => (item=10.x.x.187) changed: [10.x.x.188 -> 10.x.x.187] => (item=10.x.x.187) changed: [10.x.x.187 -> 10.x.x.187] => (item=10.x.x.187) changed: [10.x.x.188 -> 10.x.x.188] => (item=10.x.x.188) changed: [10.x.x.187 -> 10.x.x.188] => (item=10.x.x.188) changed: [10.x.x.21 -> 10.x.x.188] => (item=10.x.x.188) TASK [mongod : Copy the keyfile for authentication] ***************************************************************************************************************************************************** changed: [10.x.x.21] changed: [10.x.x.188] changed: [10.x.x.187] TASK [Start the mongodb service] ************************************************************************************************************************************************************************ changed: [10.x.x.21 -> 10.x.x.21] => (item=10.x.x.21) changed: [10.x.x.188 -> 10.x.x.21] => (item=10.x.x.21) changed: [10.x.x.187 -> 10.x.x.21] => (item=10.x.x.21) changed: [10.x.x.21 -> 10.x.x.187] => (item=10.x.x.187) changed: [10.x.x.187 -> 10.x.x.187] => (item=10.x.x.187) changed: [10.x.x.188 -> 10.x.x.187] => (item=10.x.x.187) changed: [10.x.x.187 -> 10.x.x.188] => (item=10.x.x.188) changed: [10.x.x.188 -> 10.x.x.188] => (item=10.x.x.188) changed: [10.x.x.21 -> 10.x.x.188] => (item=10.x.x.188) TASK [mongod : wait_for] ******************************************************************************************************************************************************************************** ok: [10.x.x.21] ok: [10.x.x.188] ok: [10.x.x.187] TASK [Create the file to initialize the mongod users] *************************************************************************************************************************************************** ok: [10.x.x.21] ok: [10.x.x.188] ok: [10.x.x.187] TASK [Initialize the mongod users] ********************************************************************************************************************************************************************** changed: [10.x.x.187] changed: [10.x.x.188] changed: [10.x.x.21] TASK [mongod : set authorization] *********************************************************************************************************************************************************************** ok: [10.x.x.21] ok: [10.x.x.187] ok: [10.x.x.188] TASK [Create the mongodb authorization configuration file] ********************************************************************************************************************************************* changed: [10.x.x.21 -> 10.x.x.21] => (item=10.x.x.21) changed: [10.x.x.187 -> 10.x.x.21] => (item=10.x.x.21) changed: [10.x.x.188 -> 10.x.x.21] => (item=10.x.x.21) changed: [10.x.x.188 -> 10.x.x.187] => (item=10.x.x.187) changed: [10.x.x.187 -> 10.x.x.187] => (item=10.x.x.187) changed: [10.x.x.21 -> 10.x.x.187] => (item=10.x.x.187) changed: [10.x.x.187 -> 10.x.x.188] => (item=10.x.x.188) changed: [10.x.x.188 -> 10.x.x.188] => (item=10.x.x.188) changed: [10.x.x.21 -> 10.x.x.188] => (item=10.x.x.188) TASK [reStart the mongodb service] ********************************************************************************************************************************************************************** changed: [10.x.x.21 -> 10.x.x.21] => (item=10.x.x.21) changed: [10.x.x.187 -> 10.x.x.21] => (item=10.x.x.21) changed: [10.x.x.188 -> 10.x.x.21] => (item=10.x.x.21) changed: [10.x.x.21 -> 10.x.x.187] => (item=10.x.x.187) changed: [10.x.x.187 -> 10.x.x.187] => (item=10.x.x.187) changed: [10.x.x.188 -> 10.x.x.187] => (item=10.x.x.187) changed: [10.x.x.188 -> 10.x.x.188] => (item=10.x.x.188) changed: [10.x.x.21 -> 10.x.x.188] => (item=10.x.x.188) changed: [10.x.x.187 -> 10.x.x.188] => (item=10.x.x.188) TASK [Create the file to initialize the mongod replica set] ********************************************************************************************************************************************* ok: [10.x.x.21] ok: [10.x.x.188] ok: [10.x.x.187] TASK [mongod : Initialize the replication set] ********************************************************************************************************************************************************** changed: [10.x.x.21] changed: [10.x.x.188] changed: [10.x.x.187] PLAY [mongoc_servers] *********************************************************************************************************************************************************************************** TASK [Gathering Facts] ********************************************************************************************************************************************************************************** ok: [10.x.x.188] ok: [10.x.x.21] ok: [10.x.x.187] TASK [Create data directory for mongoc configuration server] ******************************************************************************************************************************************** changed: [10.x.x.21] changed: [10.x.x.188] changed: [10.x.x.187] TASK [mongoc : Create the mongodb startup file] ********************************************************************************************************************************************************* ok: [10.x.x.21] ok: [10.x.x.188] ok: [10.x.x.187] TASK [mongoc : Create the mongodb configuration file] *************************************************************************************************************************************************** ok: [10.x.x.21] ok: [10.x.x.187] ok: [10.x.x.188] TASK [mongoc : Copy the keyfile for authentication] ***************************************************************************************************************************************************** ok: [10.x.x.21] ok: [10.x.x.188] ok: [10.x.x.187] TASK [mongoc : Start the mongodb service] *************************************************************************************************************************************************************** changed: [10.x.x.21] changed: [10.x.x.188] changed: [10.x.x.187] TASK [mongoc : wait_for] ******************************************************************************************************************************************************************************** ok: [10.x.x.21] ok: [10.x.x.188] ok: [10.x.x.187] TASK [mongoc : Create the file to initialize mongo user] ************************************************************************************************************************************************ [WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{inventory_hostname == groups['mongoc_servers'][0]}} [WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{inventory_hostname == groups['mongoc_servers'][0]}} skipping: [10.x.x.187] [WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{inventory_hostname == groups['mongoc_servers'][0]}} skipping: [10.x.x.188] ok: [10.x.x.21] TASK [mongoc : Create admin User] *********************************************************************************************************************************************************************** [WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{inventory_hostname == groups['mongoc_servers'][0]}} [WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{inventory_hostname == groups['mongoc_servers'][0]}} skipping: [10.x.x.187] [WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{inventory_hostname == groups['mongoc_servers'][0]}} skipping: [10.x.x.188] changed: [10.x.x.21] PLAY [mongos_servers] *********************************************************************************************************************************************************************************** TASK [Gathering Facts] ********************************************************************************************************************************************************************************** ok: [10.x.x.21] ok: [10.x.x.188] ok: [10.x.x.187] TASK [Create the mongos startup file] ******************************************************************************************************************************************************************* ok: [10.x.x.21] ok: [10.x.x.188] ok: [10.x.x.187] TASK [Create the mongos configuration file] ************************************************************************************************************************************************************* ok: [10.x.x.21] ok: [10.x.x.187] ok: [10.x.x.188] TASK [mongos : Copy the keyfile for authentication] ***************************************************************************************************************************************************** ok: [10.x.x.21] ok: [10.x.x.188] ok: [10.x.x.187] TASK [Start the mongos service] ************************************************************************************************************************************************************************* changed: [10.x.x.21] changed: [10.x.x.187] changed: [10.x.x.188] TASK [mongos : wait_for] ******************************************************************************************************************************************************************************** ok: [10.x.x.21] ok: [10.x.x.188] ok: [10.x.x.187] TASK [mongos : Initialize mongo_servers_group] ********************************************************************************************************************************************************** ok: [10.x.x.21] ok: [10.x.x.187] ok: [10.x.x.188] TASK [mongos : Populate mongo_servers_group with mongod_port] ******************************************************************************************************************************************* ok: [10.x.x.21] => (item=10.x.x.21) ok: [10.x.x.21] => (item=10.x.x.187) ok: [10.x.x.187] => (item=10.x.x.21) ok: [10.x.x.21] => (item=10.x.x.188) ok: [10.x.x.187] => (item=10.x.x.187) ok: [10.x.x.188] => (item=10.x.x.21) ok: [10.x.x.188] => (item=10.x.x.187) ok: [10.x.x.187] => (item=10.x.x.188) ok: [10.x.x.188] => (item=10.x.x.188) TASK [mongos : debug] *********************************************************************************************************************************************************************************** ok: [10.x.x.21] => { "mongo_servers_group": [ 2700, 2701, 2702 ] } ok: [10.x.x.187] => { "mongo_servers_group": [ 2700, 2701, 2702 ] } ok: [10.x.x.188] => { "mongo_servers_group": [ 2700, 2701, 2702 ] } TASK [mongos : Copy the file enable sharding] ********************************************************************************************************************************************************** [WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{inventory_hostname == groups['mongos_servers'][0]}} [WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{inventory_hostname == groups['mongos_servers'][0]}} skipping: [10.x.x.187] [WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{inventory_hostname == groups['mongos_servers'][0]}} skipping: [10.x.x.188] changed: [10.x.x.21] TASK [mongos : Create sharding] ************************************************************************************************************************************************************************* [WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{inventory_hostname == groups['mongos_servers'][0]}} [WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{inventory_hostname == groups['mongos_servers'][0]}} skipping: [10.x.x.187] [WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{inventory_hostname == groups['mongos_servers'][0]}} skipping: [10.x.x.188] changed: [10.x.x.21] TASK [mongos : debug] *********************************************************************************************************************************************************************************** ok: [10.x.x.21] => { "msg": { "changed": true, "cmd": "/usr/bin/mongosh \"localhost:27017/admin\" /tmp/mongos_init.js", "delta": "0:00:03.807226", "end": "2024-08-07 16:07:25.260496", "failed": false, "rc": 0, "start": "2024-08-07 16:07:21.453270", "stderr": "", "stderr_lines": [], "stdout": "shardingVersion\n{\n _id: 1,\n clusterId: ObjectId('66b32b29c13841878d598d3c')\n}\n---\nshards\n[\n {\n _id: 'mongo1',\n host: 'mongo1/mongo1:2700,mongo2:2700,mongo3:2700',\n state: 1,\n topologyTime: Timestamp({ t: 1723018042, i: 3 })\n },\n {\n _id: 'mongo2',\n host: 'mongo2/mongo1:2701,mongo2:2701,mongo3:2701',\n state: 1,\n topologyTime: Timestamp({ t: 1723018042, i: 7 })\n },\n {\n _id: 'mongo3',\n host: 'mongo3/mongo3:2702,mongo1:2702,mongo2:2702',\n state: 1,\n topologyTime: Timestamp({ t: 1723018043, i: 6 })\n }\n]\n---\nactive mongoses\n[\n {\n '7.0.12': 3\n }\n]\n---\nautosplit\n{\n 'Currently enabled': 'yes'\n}\n---\nbalancer\n{\n 'Currently enabled': 'yes',\n 'Failed balancer rounds in last 5 attempts': 0,\n 'Currently running': 'no',\n 'Migration Results for the last 24 hours': 'No recent migrations'\n}\n---\ndatabases\n[\n {\n database: {\n _id: 'config',\n primary: 'config',\n partitioned: true\n },\n collections: {}\n }\n]", "stdout_lines": [ "shardingVersion", "{", " _id: 1,", " clusterId: ObjectId('66b32b29c13841878d598d3c')", "}", "---", "shards", "[", " {", " _id: 'mongo1',", " host: 'mongo1/mongo1:2700,mongo2:2700,mongo3:2700',", " state: 1,", " topologyTime: Timestamp({ t: 1723018042, i: 3 })", " },", " {", " _id: 'mongo2',", " host: 'mongo2/mongo1:2701,mongo2:2701,mongo3:2701',", " state: 1,", " topologyTime: Timestamp({ t: 1723018042, i: 7 })", " },", " {", " _id: 'mongo3',", " host: 'mongo3/mongo3:2702,mongo1:2702,mongo2:2702',", " state: 1,", " topologyTime: Timestamp({ t: 1723018043, i: 6 })", " }", "]", "---", "active mongoses", "[", " {", " '7.0.12': 3", " }", "]", "---", "autosplit", "{", " 'Currently enabled': 'yes'", "}", "---", "balancer", "{", " 'Currently enabled': 'yes',", " 'Failed balancer rounds in last 5 attempts': 0,", " 'Currently running': 'no',", " 'Migration Results for the last 24 hours': 'No recent migrations'", "}", "---", "databases", "[", " {", " database: {", " _id: 'config',", " primary: 'config',", " partitioned: true", " },", " collections: {}", " }", "]" ] } } ok: [10.x.x.187] => { "msg": { "changed": false, "skip_reason": "Conditional result was False", "skipped": true } } ok: [10.x.x.188] => { "msg": { "changed": false, "skip_reason": "Conditional result was False", "skipped": true } } PLAY [mongos_servers] *********************************************************************************************************************************************************************************** TASK [Gathering Facts] ********************************************************************************************************************************************************************************** ok: [10.x.x.21] ok: [10.x.x.187] ok: [10.x.x.188] TASK [shard_test : Copy the test script] **************************************************************************************************************************************************************** skipping: [10.x.x.187] skipping: [10.x.x.188] changed: [10.x.x.21] TASK [shard_test : Run test script] ********************************************************************************************************************************************************************* skipping: [10.x.x.187] skipping: [10.x.x.188] changed: [10.x.x.21] TASK [shard_test : debug] ******************************************************************************************************************************************************************************* ok: [10.x.x.21] => { "msg": { "changed": true, "cmd": "/usr/bin/mongosh \"localhost:27017/admin\" /tmp/shard_test.js -u admin -p 123456", "delta": "0:19:29.515139", "end": "2024-08-07 16:27:01.526747", "failed": false, "rc": 0, "start": "2024-08-07 16:07:32.011608", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": [] } } skipping: [10.x.x.187] skipping: [10.x.x.188] PLAY RECAP ********************************************************************************************************************************************************************************************** 10.x.x.187 : ok=43 changed=14 unreachable=0 failed=0 skipped=7 rescued=0 ignored=0 10.x.x.188 : ok=43 changed=14 unreachable=0 failed=0 skipped=7 rescued=0 ignored=0 10.x.x.21 : ok=50 changed=19 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 [root@mysqlbackup:/root/auto/ansible-mongodb-cluster-master/mongodb7]$
【结果】
最后有测试插入数据最终结果
[direct: mongos] admin> sh.status() shardingVersion { _id: 1, clusterId: ObjectId('66b32b29c13841878d598d3c') } --- shards [ { _id: 'mongo1', host: 'mongo1/mongo1:2700,mongo2:2700,mongo3:2700', state: 1, topologyTime: Timestamp({ t: 1723018042, i: 3 }) }, { _id: 'mongo2', host: 'mongo2/mongo1:2701,mongo2:2701,mongo3:2701', state: 1, topologyTime: Timestamp({ t: 1723018042, i: 7 }) }, { _id: 'mongo3', host: 'mongo3/mongo1:2702,mongo2:2702,mongo3:2702', state: 1, topologyTime: Timestamp({ t: 1723018043, i: 6 }) } ] --- active mongoses [ { '7.0.12': 3 } ] --- autosplit { 'Currently enabled': 'yes' } --- balancer { 'Currently enabled': 'yes', 'Currently running': 'no', 'Failed balancer rounds in last 5 attempts': 0, 'Migration Results for the last 24 hours': { '1': 'Success' } } --- databases [ { database: { _id: 'config', primary: 'config', partitioned: true }, collections: { 'config.system.sessions': { shardKey: { _id: 1 }, unique: false, balancing: true, chunkMetadata: [ { shard: 'mongo1', nChunks: 1 } ], chunks: [ { min: { _id: MinKey() }, max: { _id: MaxKey() }, 'on shard': 'mongo1', 'last modified': Timestamp({ t: 1, i: 0 }) } ], tags: [] } } }, { database: { _id: 'test', primary: 'mongo2', partitioned: false, version: { uuid: UUID('92e9a984-f2f7-4f76-9963-9a7055a16c03'), timestamp: Timestamp({ t: 1723018043, i: 10 }), lastMod: 1 } }, collections: { 'test.messages': { shardKey: { createTime: 1 }, unique: false, balancing: true, chunkMetadata: [ { shard: 'mongo1', nChunks: 1 }, { shard: 'mongo2', nChunks: 1 } ], chunks: [ { min: { createTime: MinKey() }, max: { createTime: ISODate('2024-04-30T22:52:30.020Z') }, 'on shard': 'mongo1', 'last modified': Timestamp({ t: 2, i: 0 }) }, { min: { createTime: ISODate('2024-04-30T22:52:30.020Z') }, max: { createTime: MaxKey() }, 'on shard': 'mongo2', 'last modified': Timestamp({ t: 2, i: 1 }) } ], tags: [] }, 'test.user': { shardKey: { _id: 'hashed' }, unique: false, balancing: true, chunkMetadata: [ { shard: 'mongo1', nChunks: 2 }, { shard: 'mongo2', nChunks: 2 }, { shard: 'mongo3', nChunks: 2 } ], chunks: [ { min: { _id: MinKey() }, max: { _id: Long('-6148914691236517204') }, 'on shard': 'mongo3', 'last modified': Timestamp({ t: 1, i: 0 }) }, { min: { _id: Long('-6148914691236517204') }, max: { _id: Long('-3074457345618258602') }, 'on shard': 'mongo3', 'last modified': Timestamp({ t: 1, i: 1 }) }, { min: { _id: Long('-3074457345618258602') }, max: { _id: Long('0') }, 'on shard': 'mongo1', 'last modified': Timestamp({ t: 1, i: 2 }) }, { min: { _id: Long('0') }, max: { _id: Long('3074457345618258602') }, 'on shard': 'mongo1', 'last modified': Timestamp({ t: 1, i: 3 }) }, { min: { _id: Long('3074457345618258602') }, max: { _id: Long('6148914691236517204') }, 'on shard': 'mongo2', 'last modified': Timestamp({ t: 1, i: 4 }) }, { min: { _id: Long('6148914691236517204') }, max: { _id: MaxKey() }, 'on shard': 'mongo2', 'last modified': Timestamp({ t: 1, i: 5 }) } ], tags: [] } } } ] [direct: mongos] admin> use test switched to db test [direct: mongos] test> db.user.find({_id: 1 }).explain() { queryPlanner: { mongosPlannerVersion: 1, winningPlan: { stage: 'SINGLE_SHARD', shards: [ { shardName: 'mongo2', connectionString: 'mongo2/mongo1:2701,mongo2:2701,mongo3:2701', serverInfo: { host: 'rac01', port: 2701, version: '7.0.12', gitVersion: 'b6513ce0781db6818e24619e8a461eae90bc94fc' }, namespace: 'test.user', indexFilterSet: false, parsedQuery: { _id: { '$eq': 1 } }, queryHash: '58F0F49D', planCacheKey: '0028A5F4', maxIndexedOrSolutionsReached: false, maxIndexedAndSolutionsReached: false, maxScansToExplodeReached: false, winningPlan: { stage: 'IDHACK' }, rejectedPlans: [] } ] } }, serverInfo: { host: 'rac01', port: 27017, version: '7.0.12', gitVersion: 'b6513ce0781db6818e24619e8a461eae90bc94fc' }, serverParameters: { internalQueryFacetBufferSizeBytes: 104857600, internalQueryFacetMaxOutputDocSizeBytes: 104857600, internalLookupStageIntermediateDocumentMaxSizeBytes: 104857600, internalDocumentSourceGroupMaxMemoryBytes: 104857600, internalQueryMaxBlockingSortMemoryUsageBytes: 104857600, internalQueryProhibitBlockingMergeOnMongoS: 0, internalQueryMaxAddToSetBytes: 104857600, internalDocumentSourceSetWindowFieldsMaxMemoryBytes: 104857600, internalQueryFrameworkControl: 'trySbeRestricted' }, command: { find: 'user', filter: { _id: 1 }, lsid: { id: UUID('d34c23de-e826-4d77-9102-6da3dca6ad0e') }, '$clusterTime': { clusterTime: Timestamp({ t: 1723019610, i: 1 }), signature: { hash: Binary.createFromBase64('yQ6iysm7mX1p2pdy3eaWLMoa/ac=', 0), keyId: Long('7400306067793510422') } }, '$db': 'test' }, ok: 1, '$clusterTime': { clusterTime: Timestamp({ t: 1723019611, i: 1 }), signature: { hash: Binary.createFromBase64('ZOwt/iVN6eXlll2oMQ0ajq5d3Mg=', 0), keyId: Long('7400306067793510422') } }, operationTime: Timestamp({ t: 1723019608, i: 1 }) } [direct: mongos] test>
db.user.getShardDistribution() Shard mongo1 at mongo1/mongo1:2700,mongo2:2700,mongo3:2700 { data: '627KiB', docs: 16539, chunks: 1, 'estimated data per chunk': '627KiB', 'estimated docs per chunk': 16539 } --- Shard mongo2 at mongo2/mongo1:2701,mongo2:2701,mongo3:2701 { data: '624KiB', docs: 16451, chunks: 1, 'estimated data per chunk': '624KiB', 'estimated docs per chunk': 16451 } --- Shard mongo3 at mongo3/mongo1:2702,mongo2:2702,mongo3:2702 { data: '644KiB', docs: 17010, chunks: 1, 'estimated data per chunk': '644KiB', 'estimated docs per chunk': 17010 } --- Totals { data: '1.85MiB', docs: 50000, chunks: 3, 'Shard mongo1': [ '33.07 % data', '33.07 % docs in cluster', '38B avg obj size on shard' ], 'Shard mongo2': [ '32.9 % data', '32.9 % docs in cluster', '38B avg obj size on shard' ], 'Shard mongo3': [ '34.01 % data', '34.02 % docs in cluster', '38B avg obj size on shard' ] }