Skip to content

Instantly share code, notes, and snippets.

@webstrand
Last active May 18, 2024 23:42
Show Gist options
  • Save webstrand/0dc5ddac08d5f46f262eaf60dafd6190 to your computer and use it in GitHub Desktop.
Save webstrand/0dc5ddac08d5f46f262eaf60dafd6190 to your computer and use it in GitHub Desktop.
Create a mongodb replication-set for use with Prisma
mkdir -p dev.mongodb/{0,1,2}; rm -f dev.mongodb/{0,1,2}/log.*;
(
for i in {1..100}; do
socat -u OPEN:/dev/null UNIX-CONNECT:./dev.mongodb/0/.sock 2>/dev/null && break;
sleep 0.1;
done;
exec mongosh 'mongodb://.%2Fdev.mongodb%2F0%2F.sock' --eval "$(cat << EOF
const reinitiate = (() => {
try {
rs.status(); // throws if uninitiated
return (x) => rs.reconfig(x, { force: true });
}
catch(e) {
if(!(e instanceof Error) || e.name !== "MongoServerError" || e.code !== 94) throw e;
return (x) => rs.initiate(x);
}
})();
reinitiate({
_id: "rs0",
members: [
{ _id: 0, host: "./dev.mongodb/0/.sock" },
{ _id: 1, host: "./dev.mongodb/1/.sock" },
{ _id: 2, host: "./dev.mongodb/2/.sock" }
]
})
EOF
)";
)&
exec concurrently --kill-others -n dev.mongodb/0,dev.mongodb/1,dev.mongodb/2 \
"mongod --replSet rs0 --dbpath dev.mongodb/0 --logpath dev.mongodb/0/log --bind_ip dev.mongodb/0/.sock" \
"mongod --replSet rs0 --dbpath dev.mongodb/1 --logpath dev.mongodb/1/log --bind_ip dev.mongodb/1/.sock" \
"mongod --replSet rs0 --dbpath dev.mongodb/2 --logpath dev.mongodb/2/log --bind_ip dev.mongodb/2/.sock"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment