Trong kỹ thuật phần mềm, một kết nối là một bộ đệm duy trì các kết nối tới cơ sở dữ liệu. Các kết nối tới cơ sở dữ liệu sau khi sử dụng sẽ không đóng lại ngay mà sẽ được sử dụng lại khi được yêu cầu trong tương lai
anh hùng ebudezain
ahihi đồ ngốc nghếch thanh hùng nè
ahihi đồ ngốc nghếch thanh hùng nè
read data support
ahihi đồ ngốc nghếch thanh hùng nè
Xem kết nối và vấn đề trong mongo
ahihi đồ ngốc nghếch thanh hùng nè
anh hùng đẹp trai
anh hùng đẹp trai
anh hùng đẹp trai
anh hùng đẹp trai
anh hùng thông minh lắm
anh hùng ebudezain
Chúng ta đã biết sử dụng mysql hay postgreSQL thì để kiểm tra mã, bạn có tốt không trong vấn đề kiểm tra kết nối giám sát, thì bạn có thể vào terminal gõ lệnh
Như bài trước mình đã trình bày về cụm MongoDb [https. //viblo. asia/tanphan/posts/oOVlYEmVl8W] hôm nay mình sẽ cùng nhau tạo 1 cụm mongo với docker để xem thực hư nó như thế nào nhé
Đầu tiên là file docker-compose. yml Các bạn tạo 1 thư mục đặt tên là sharding-by-docker-compose, side in 1 file docker-compose. yml with the content as after
version: '2'
services:
shard00:
image: mongo
command: mongod --replSet rs0 --shardsvr --dbpath /data/db --port 27017
volumes:
- ./data/shard00:/data/db
- /etc/localtime:/etc/localtime:ro
- ./scripts/replicaset_0:/scripts/replicaset_0
ports:
- 27022:27017
links:
- shard01
- shard02
shard01:
image: mongo
command: mongod --replSet rs0 --shardsvr --dbpath /data/db --port 27017
volumes:
- ./data/shard01:/data/db
- /etc/localtime:/etc/localtime:ro
ports:
- 27023:27017
shard02:
image: mongo
command: mongod --replSet rs0 --shardsvr --dbpath /data/db --port 27017
volumes:
- ./data/shard02:/data/db
- /etc/localtime:/etc/localtime:ro
ports:
- 27024:27017
Với file docker-compose as on, mình mới khởi tạo 3 node cho 1 replica set [replica set là gì các bạn đọc bài trước nhé], mình có map data ra bên ngoài [để sau này up lại thì data vẫn còn . Nội dung tập tin script ở bên dưới. Để xây dựng lên 9 nút, các bạn cứ lặp lại đoạn mã trên [trong khối dịch vụ thôi nhé] với các số chỉ là shard10, shard11, shard12, shard20,. lưu ý, nếu có port cổng ra ngoài cũng phải đổi đi nhé, ko nó xung đột đấy =]] OK, nhưng chỉ 9 node mongod ko thể tạo thành 1 cụm được, ta phải cố 3 node làm vai game config server, sau khi
configsvr0:
image: mongo
command: mongod --configsvr --replSet configRs --dbpath /data/db --port 27017
volumes:
- ./data/configsvr0:/data/db
- /etc/localtime:/etc/localtime:ro
- ./scripts/config_replicaset:/scripts/config_replicaset
ports:
- 27019:27017
links:
- shard00
- shard10
- shard20
configsvr1:
image: mongo
command: mongod --configsvr --replSet configRs --dbpath /data/db --port 27017
volumes:
- ./data/configsvr1:/data/db
- /etc/localtime:/etc/localtime:ro
ports:
- 27020:27017
links:
- configsvr0
configsvr2:
image: mongo
command: mongod --configsvr --replSet configRs --dbpath /data/db --port 27017
volumes:
- ./data/configsvr2:/data/db
- /etc/localtime:/etc/localtime:ro
ports:
- 27021:27017
links:
- configsvr1
Ok, mình đã có 3 node làm vai trò config, cũng mount dữ liệu và 1 file script bên ngoài, nội dung file script sẽ ở bên dưới nhé. Cuối cùng chúng ta cần 2 nút mongos đóng giao diện trò chơi, giao tiếp với người dùng, thêm đoạn mã sau
router0:
image: mongo
command: mongos --configdb configRs/configsvr0:27017,configsvr1:27017,configsvr2:27017 --port 27017
volumes:
- /etc/localtime:/etc/localtime:ro
- ./scripts/sharding_replicaset:/scripts/sharding_replicaset
ports:
- 27017:27017
links:
- configsvr2
router1:
image: mongo
command: mongos --configdb configRs/configsvr0:27017,configsvr1:27017,configsvr2:27017 --port 27017
volumes:
- /etc/localtime:/etc/localtime:ro
- ./scripts/sharding_replicaset:/scripts/sharding_replicaset
ports:
- 27018:27017
links:
- router0
Được rồi, để chạy thử coi nhé, các bạn chạy lệnh sau [nhớ vẫn là thao tác trong thư mục sharding-by-docker-compose]
docker-compose up
Kết quả là
Name Command State Ports
--------------------------------------------------------------------------------------------------------
shardingbydockercompose_configsvr0_1 /entrypoint.sh mongod --co .. Up 0.0.0.0:27019->27017/tcp
shardingbydockercompose_configsvr1_1 /entrypoint.sh mongod --co .. Up 0.0.0.0:27020->27017/tcp
shardingbydockercompose_configsvr2_1 /entrypoint.sh mongod --co .. Up 0.0.0.0:27021->27017/tcp
shardingbydockercompose_router0_1 /entrypoint.sh mongos --co .. Up 0.0.0.0:27017->27017/tcp
shardingbydockercompose_router1_1 /entrypoint.sh mongos --co .. Up 0.0.0.0:27018->27017/tcp
shardingbydockercompose_shard00_1 /entrypoint.sh mongod --re .. Up 0.0.0.0:27022->27017/tcp
shardingbydockercompose_shard01_1 /entrypoint.sh mongod --re .. Up 0.0.0.0:27023->27017/tcp
shardingbydockercompose_shard02_1 /entrypoint.sh mongod --re .. Up 0.0.0.0:27024->27017/tcp
shardingbydockercompose_shard10_1 /entrypoint.sh mongod --re .. Up 0.0.0.0:27025->27017/tcp
shardingbydockercompose_shard11_1 /entrypoint.sh mongod --re .. Up 0.0.0.0:27026->27017/tcp
shardingbydockercompose_shard12_1 /entrypoint.sh mongod --re .. Up 0.0.0.0:27027->27017/tcp
shardingbydockercompose_shard20_1 /entrypoint.sh mongod --re .. Up 0.0.0.0:27028->27017/tcp
shardingbydockercompose_shard21_1 /entrypoint.sh mongod --re .. Up 0.0.0.0:27029->27017/tcp
shardingbydockercompose_shard22_1 /entrypoint.sh mongod --re .. Up 0.0.0.0:27030->27017/tcp
Nhưng như vậy vẫn chỉ là các máy chủ mongo độc lập, bây giờ là lúc setup để chúng ta thành các replica set và sharding. Trong mỗi nút mongod ở trên mình có mount 1 script script ra bên ngoài như thế này
volumes:
- ./data/shard00:/data/db
- /etc/localtime:/etc/localtime:ro
- ./scripts/replicaset_0:/scripts/replicaset_0
bây giờ, trong thư mục sharding-by-docker-compose, các bạn tạo scrip thư mục, trong script tạo các thư mục replicaset_0, replicaset_1 tương ứng, trong mỗi thư mục replicaset_x đấy, tạo 1 tệp init_mongodb_replicaset. js with the content as after
var cfg = {
"_id": "rs1",
"members": [
{
"_id": 0,
"host": "shard10:27017"
},
{
"_id": 1,
"host": "shard11:27017"
},
{
"_id": 2,
"host": "shard12:27017"
}
]
};
var status = rs.initiate[cfg];
printjson[status];
OK, tập tin trên sẽ chạy với 3 nút shard10, 11 và 12, sau khi chạy đoạn mã trên, 3 nút shard10,11,12 sẽ chỉ được xác định vào 1 bản sao có id là rs1, làm tương tự với các nút shard00,01 . À, với những node đóng vai trò là config server, các bạn làm tương tự nhé, vì từ mongo 3. 0 trở đi, config server cũng là 1 replicaset rồi. Với router [mongos] thì sẽ hơi khác 1 chút, như trong file docker-compose, khởi tạo router0 mình có mount 1 thư mục script như sau
volumes:
- /etc/localtime:/etc/localtime:ro
- ./scripts/sharding_replicaset:/scripts/sharding_replicaset
Bây giờ các bạn chỉ cần tạo 1 thư mục sharding_replicaset rồi tạo tệp init_mongodb_sharding_replicaset. js with the content as after
________số 8Đoạn mã trên sau khi được chạy sẽ đưa tất cả các replicaset vào sharding, nghĩa là dữ liệu có thể được phân chia đều trên các replicaset này [sharding là gì các bạn cũng đọc bài trước nhé] OK, bây giờ sau khi có . after when access to command
docker exec -it docker exec -it shardingbydockercompose_shard00_1 bash
shardingbydockercompose_shard00_1 is name of container running shard00
you run command
configsvr0:
image: mongo
command: mongod --configsvr --replSet configRs --dbpath /data/db --port 27017
volumes:
- ./data/configsvr0:/data/db
- /etc/localtime:/etc/localtime:ro
- ./scripts/config_replicaset:/scripts/config_replicaset
ports:
- 27019:27017
links:
- shard00
- shard10
- shard20
configsvr1:
image: mongo
command: mongod --configsvr --replSet configRs --dbpath /data/db --port 27017
volumes:
- ./data/configsvr1:/data/db
- /etc/localtime:/etc/localtime:ro
ports:
- 27020:27017
links:
- configsvr0
configsvr2:
image: mongo
command: mongod --configsvr --replSet configRs --dbpath /data/db --port 27017
volumes:
- ./data/configsvr2:/data/db
- /etc/localtime:/etc/localtime:ro
ports:
- 27021:27017
links:
- configsvr1
0Đoạn lệnh trên để chạy đoạn script trong file config mình viết bên trên thôi mà. v Chạy xong kết quả sẽ như thế này
configsvr0:
image: mongo
command: mongod --configsvr --replSet configRs --dbpath /data/db --port 27017
volumes:
- ./data/configsvr0:/data/db
- /etc/localtime:/etc/localtime:ro
- ./scripts/config_replicaset:/scripts/config_replicaset
ports:
- 27019:27017
links:
- shard00
- shard10
- shard20
configsvr1:
image: mongo
command: mongod --configsvr --replSet configRs --dbpath /data/db --port 27017
volumes:
- ./data/configsvr1:/data/db
- /etc/localtime:/etc/localtime:ro
ports:
- 27020:27017
links:
- configsvr0
configsvr2:
image: mongo
command: mongod --configsvr --replSet configRs --dbpath /data/db --port 27017
volumes:
- ./data/configsvr2:/data/db
- /etc/localtime:/etc/localtime:ro
ports:
- 27021:27017
links:
- configsvr1
1Như vậy là 3 node shard00, 01,02 đã thành 1 replicaset rồi, các bạn chạy tương tự với các replicaset còn lại, kể cả config server và router. Riêng với router setup, các bạn sẽ nhận được kết quả như sau
configsvr0:
image: mongo
command: mongod --configsvr --replSet configRs --dbpath /data/db --port 27017
volumes:
- ./data/configsvr0:/data/db
- /etc/localtime:/etc/localtime:ro
- ./scripts/config_replicaset:/scripts/config_replicaset
ports:
- 27019:27017
links:
- shard00
- shard10
- shard20
configsvr1:
image: mongo
command: mongod --configsvr --replSet configRs --dbpath /data/db --port 27017
volumes:
- ./data/configsvr1:/data/db
- /etc/localtime:/etc/localtime:ro
ports:
- 27020:27017
links:
- configsvr0
configsvr2:
image: mongo
command: mongod --configsvr --replSet configRs --dbpath /data/db --port 27017
volumes:
- ./data/configsvr2:/data/db
- /etc/localtime:/etc/localtime:ro
ports:
- 27021:27017
links:
- configsvr1
2OK, như vậy là sharding trên 3 replicaset rs0,1,2 Kiểm tra phát nhé, các bạn truy cập vào 1 shard bất kỳ với lệnh
docker exec -it docker exec -it shardingbydockercompose_shard00_1 bash
in container you just access to, run try command
configsvr0:
image: mongo
command: mongod --configsvr --replSet configRs --dbpath /data/db --port 27017
volumes:
- ./data/configsvr0:/data/db
- /etc/localtime:/etc/localtime:ro
- ./scripts/config_replicaset:/scripts/config_replicaset
ports:
- 27019:27017
links:
- shard00
- shard10
- shard20
configsvr1:
image: mongo
command: mongod --configsvr --replSet configRs --dbpath /data/db --port 27017
volumes:
- ./data/configsvr1:/data/db
- /etc/localtime:/etc/localtime:ro
ports:
- 27020:27017
links:
- configsvr0
configsvr2:
image: mongo
command: mongod --configsvr --replSet configRs --dbpath /data/db --port 27017
volumes:
- ./data/configsvr2:/data/db
- /etc/localtime:/etc/localtime:ro
ports:
- 27021:27017
links:
- configsvr1
4after that run command
configsvr0:
image: mongo
command: mongod --configsvr --replSet configRs --dbpath /data/db --port 27017
volumes:
- ./data/configsvr0:/data/db
- /etc/localtime:/etc/localtime:ro
- ./scripts/config_replicaset:/scripts/config_replicaset
ports:
- 27019:27017
links:
- shard00
- shard10
- shard20
configsvr1:
image: mongo
command: mongod --configsvr --replSet configRs --dbpath /data/db --port 27017
volumes:
- ./data/configsvr1:/data/db
- /etc/localtime:/etc/localtime:ro
ports:
- 27020:27017
links:
- configsvr0
configsvr2:
image: mongo
command: mongod --configsvr --replSet configRs --dbpath /data/db --port 27017
volumes:
- ./data/configsvr2:/data/db
- /etc/localtime:/etc/localtime:ro
ports:
- 27021:27017
links:
- configsvr1
5thông tin của replicaset sẽ hiển thị
configsvr0:
image: mongo
command: mongod --configsvr --replSet configRs --dbpath /data/db --port 27017
volumes:
- ./data/configsvr0:/data/db
- /etc/localtime:/etc/localtime:ro
- ./scripts/config_replicaset:/scripts/config_replicaset
ports:
- 27019:27017
links:
- shard00
- shard10
- shard20
configsvr1:
image: mongo
command: mongod --configsvr --replSet configRs --dbpath /data/db --port 27017
volumes:
- ./data/configsvr1:/data/db
- /etc/localtime:/etc/localtime:ro
ports:
- 27020:27017
links:
- configsvr0
configsvr2:
image: mongo
command: mongod --configsvr --replSet configRs --dbpath /data/db --port 27017
volumes:
- ./data/configsvr2:/data/db
- /etc/localtime:/etc/localtime:ro
ports:
- 27021:27017
links:
- configsvr1
6Thì ra là replicaset rs0 có 3 thành viên, cái nào là chính, cái nào là phụ cũng đã rõ rồi. Kiểm tra vấn đề sharding phát nhỏ, truy cập vào bộ định tuyến 0
configsvr0:
image: mongo
command: mongod --configsvr --replSet configRs --dbpath /data/db --port 27017
volumes:
- ./data/configsvr0:/data/db
- /etc/localtime:/etc/localtime:ro
- ./scripts/config_replicaset:/scripts/config_replicaset
ports:
- 27019:27017
links:
- shard00
- shard10
- shard20
configsvr1:
image: mongo
command: mongod --configsvr --replSet configRs --dbpath /data/db --port 27017
volumes:
- ./data/configsvr1:/data/db
- /etc/localtime:/etc/localtime:ro
ports:
- 27020:27017
links:
- configsvr0
configsvr2:
image: mongo
command: mongod --configsvr --replSet configRs --dbpath /data/db --port 27017
volumes:
- ./data/configsvr2:/data/db
- /etc/localtime:/etc/localtime:ro
ports:
- 27021:27017
links:
- configsvr1
7the following command times
configsvr0:
image: mongo
command: mongod --configsvr --replSet configRs --dbpath /data/db --port 27017
volumes:
- ./data/configsvr0:/data/db
- /etc/localtime:/etc/localtime:ro
- ./scripts/config_replicaset:/scripts/config_replicaset
ports:
- 27019:27017
links:
- shard00
- shard10
- shard20
configsvr1:
image: mongo
command: mongod --configsvr --replSet configRs --dbpath /data/db --port 27017
volumes:
- ./data/configsvr1:/data/db
- /etc/localtime:/etc/localtime:ro
ports:
- 27020:27017
links:
- configsvr0
configsvr2:
image: mongo
command: mongod --configsvr --replSet configRs --dbpath /data/db --port 27017
volumes:
- ./data/configsvr2:/data/db
- /etc/localtime:/etc/localtime:ro
ports:
- 27021:27017
links:
- configsvr1
4configsvr0:
image: mongo
command: mongod --configsvr --replSet configRs --dbpath /data/db --port 27017
volumes:
- ./data/configsvr0:/data/db
- /etc/localtime:/etc/localtime:ro
- ./scripts/config_replicaset:/scripts/config_replicaset
ports:
- 27019:27017
links:
- shard00
- shard10
- shard20
configsvr1:
image: mongo
command: mongod --configsvr --replSet configRs --dbpath /data/db --port 27017
volumes:
- ./data/configsvr1:/data/db
- /etc/localtime:/etc/localtime:ro
ports:
- 27020:27017
links:
- configsvr0
configsvr2:
image: mongo
command: mongod --configsvr --replSet configRs --dbpath /data/db --port 27017
volumes:
- ./data/configsvr2:/data/db
- /etc/localtime:/etc/localtime:ro
ports:
- 27021:27017
links:
- configsvr1
9Thông tin về sharding sẽ hiện ra
router0:
image: mongo
command: mongos --configdb configRs/configsvr0:27017,configsvr1:27017,configsvr2:27017 --port 27017
volumes:
- /etc/localtime:/etc/localtime:ro
- ./scripts/sharding_replicaset:/scripts/sharding_replicaset
ports:
- 27017:27017
links:
- configsvr2
router1:
image: mongo
command: mongos --configdb configRs/configsvr0:27017,configsvr1:27017,configsvr2:27017 --port 27017
volumes:
- /etc/localtime:/etc/localtime:ro
- ./scripts/sharding_replicaset:/scripts/sharding_replicaset
ports:
- 27018:27017
links:
- router0
0mười, vậy là đã xong, các bạn thử chèn 100000 bản ghi vào mà xem, 100000 bản ghi đấy sẽ được bố phân rất đều, thôi bài sau mình sẽ hướng dẫn cách chạy test với ycsb nhé