gpt4 book ai didi

docker - 在Docker-compose完成后填充cassandra

转载 作者:行者123 更新时间:2023-12-02 19:36:25 25 4
gpt4 key购买 nike

这是我的docker-compose.yaml:

version: '3'
services:
cassandra:
container_name: cassandra
image: cassandra:3.10
ports:
- 9142:9042
volumes:
- $PWD/src/database/migrations:/migrations
depends_on:
- mysql
mysql:
image: mysql:5.7
volumes:
- ./src/database/migrations:/docker-entrypoint-initdb.d
ports:
- 3307:3306
restart: always
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=leonardo
- MYSQL_ALLOW_EMPTY_PASSWORD=1

和一个shell脚本 bootstrap.sh开始:
#!/bin/bash
set -e
docker-compose -f $PWD/docker-compose.yaml down
docker-compose -f $PWD/docker-compose.yaml up -d

这正在工作并启动堆栈。
现在,我想向Cassandra添加一个键空间,但是,在此行下面抛出一个错误:
docker-compose -f $PWD/docker-compose.yaml exec cassandra /bin/sh -c $'cqlsh cassandra -e "CREATE KEYSPACE mykeyspace WITH replication = {\'class\': \'SimpleStrategy\', \'replication_factor\': \'1\'}  AND durable_writes = true;"'

Connection error: ('Unable to connect to any servers', {'172.18.0.2': error(111, "Tried connecting to [('172.18.0.2', 9042)]. Last error: Connection refused")})



Cassandra可能尚未初始化。
有什么好的方法呢?

最佳答案

如果在启动容器后立即尝试在bootstrap.sh脚本中的Cassandra中执行命令,则Cassandra服务器将没有时间启动。

一个简单的解决方案是使用until loop在脚本中等待几秒钟后再执行命令:

#!/bin/bash
set -e
docker-compose -f $PWD/docker-compose.yaml down
docker-compose -f $PWD/docker-compose.yaml up -d
until docker-compose -f $PWD/docker-compose.yaml exec cassandra /bin/sh -c "cqlsh cassandra -e 'show version'";
do
docker-compose -f $PWD/docker-compose.yaml exec cassandra /bin/sh -c $'cqlsh cassandra -e "CREATE KEYSPACE mykeyspace WITH replication = {\'class\': \'SimpleStrategy\', \'replication_factor\': \'1\'} AND durable_writes = true;"'
done

关于docker - 在Docker-compose完成后填充cassandra,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44646613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com