Unable to make ScyllaDB keyspace and table while creating ScyllaDB container.

huangapple go评论65阅读模式
英文:

Unable to make scyllaDB keyspace and table while creating scylladb container

问题

I'm trying to make keyspace and table while creating scyllaDB container. For this I'm making a script file 'my_schema.sh':

CREATE KEYSPACE IF NOT EXISTS books_keyspace WITH replication = 
{'class':'SimpleStrategy','replication_factor':'1'};

CREATE TABLE IF NOT EXISTS books_keyspace.books
(
book_id int PRIMARY KEY, 
book_name text, 
genre text, 
price double, 
issue_date text, 
return_date text, 
fine double
)

Then I add this as entrypoint in docker-compose:

version: "3"

services:

  scyllaDB-node1:
    container_name: scyllaDB-node1
    image: scylladb/scylla:5.1.0
    restart: always
    command: --smp 1 --memory 750M --overprovisioned 1 --api-address 0.0.0.0
    entrypoint: ["/my_schema.sh"]
    volumes:
      - ./my_schema.sh:/my_schema.sh

But when I run this command:

docker compose -f script-docker-compose.yaml up -d

docker exec -it scyllaDB-node1 nodetool status

It gives the following error:

Error response from daemon: Container 44b7e04d61917444ceaaaec9135891c66355dffbf1b5f39c352f465db85ea08e is restarting, wait until the container is running

On checking logs:

docker logs scyllaDB-node1 

Creating keyspace and table...
/my_schema.sh: line 11: CREATE: command not found
/my_schema.sh: line 12: class:SimpleStrategy: command not found
/my_schema.sh: line 14: CREATE: command not found
/my_schema.sh: line 16: book_id: command not found
/my_schema.sh: line 17: book_name: command not found
/my_schema.sh: line 18: genre: command not found
/my_schema.sh: line 19: price: command not found
/my_schema.sh: line 20: issue_date: command not found
/my_schema.sh: line 21: return_date: command not found
/my_schema.sh: line 22: fine: command not found

Kindly help me, how can I make keyspace and table while creating container in docker-compose. Any help is appreciated.

英文:

I'm trying to make keyspace and table while creating scyllaDB container. For this I'm making a script file 'my_schema.sh':

CREATE KEYSPACE IF NOT EXISTS books_keyspace WITH replication = 
{'class':'SimpleStrategy','replication_factor':'1'};

CREATE TABLE IF NOT EXISTS books_keyspace.books
(
book_id int PRIMARY KEY, 
book_name text, 
genre text, 
price double, 
issue_date text, 
return_date text, 
fine double
)

Then I add this as entrypoint in docker-compose:

version: "3"

services:

  scyllaDB-node1:
    container_name: scyllaDB-node1
    image: scylladb/scylla:5.1.0
    restart: always
    command: --smp 1 --memory 750M --overprovisioned 1 --api-address 0.0.0.0
    entrypoint: ["/my_schema.sh"]
    volumes:
      - ./my_schema.sh:/my_schema.sh

But when I run this command:

docker compose -f script-docker-compose.yaml up -d

docker exec -it scyllaDB-node1 nodetool status

It gives the following error:

Error response from daemon: Container 44b7e04d61917444ceaaaec9135891c66355dffbf1b5f39c352f465db85ea08e is restarting, wait until the container is running

On checking logs:

docker logs scyllaDB-node1 

Creating keyspace and table...
/my_schema.sh: line 11: CREATE: command not found
/my_schema.sh: line 12: class:SimpleStrategy: command not found
/my_schema.sh: line 14: CREATE: command not found
/my_schema.sh: line 16: book_id: command not found
/my_schema.sh: line 17: book_name: command not found
/my_schema.sh: line 18: genre: command not found
/my_schema.sh: line 19: price: command not found
/my_schema.sh: line 20: issue_date: command not found
/my_schema.sh: line 21: return_date: command not found
/my_schema.sh: line 22: fine: command not found

Kindly help me, how can I make keyspace and table while creating container in docker-compose. Any help is appreciated.

答案1

得分: 1

以下是翻译好的部分:

首先,你的方法存在一些问题。

首先,您未提供一个脚本,而是CQL语句。您希望使用cqlsh-e-f标志来执行这些语句。

-e将接受常规的CQL语句;而
-f将接受一个文件以从中读取CQL语句。

你的方法的第二个问题是,你将问题中的“脚本”(正如我们在上面指出的那样,实际上并不是脚本)定义为容器的入口点。这意味着一旦你的脚本完成,容器就会终止,而且由于你正在使用docker-compose,它会尝试重新启动它。

一般来说,你不应该覆盖入口点。ScyllaDB容器默认使用supervisord作为入口点,这使得它很容易自定义。

因此,在定义容器启动时要执行的程序的正确方法是将supervisord服务定义放在/etc/supervisord.conf.d文件夹下,然后调用你想要的脚本或程序。

然而,这种方法带来了第三个问题:每次启动服务时都会执行您的服务。这通常可能不是问题,但在您的情况下可能是问题,特别是因为您正在运行一个分布式数据库。

因此,自动化架构创建的正确方法应该是从应用程序的角度来看,特别是要查看架构迁移工具(例如gocqlx migratecassandra-migrate),这些工具正是为此目的而制作的。

英文:

There are a couple of problems with your approach.

First, you are not providing a script, but rather CQL statements. You want to execute these statements using the cqlsh -e or -f flags.

-e will accept a regular CQL statement; whereas
-f will accept a file to read the CQL statements from

The second problem of your approach is that you are defining the script in question (which, as we saw in the above pointer is not really a script) as a container entrypoint. Which means that once your script finishes, the container will die, and as you are using docker-compose it will try to restart it.

In general, you do not want to override the entrypoint. The ScyllaDB container uses supervisord as the entrypoint by default, which makes it easily customizable.

Therefore, the right way to define a program to be executed when your container starts is by placing a supervisord service definition under the /etc/supervisord.conf.d folder, which then calls the script or program you want.

However, this approach brings a third problem: The fact that your service will now be executed everytime your service starts. This may not typically be a problem, but in your case it might be, specially as you are running a distributed database.

The right way to automate your schema creation, therefore, would be from an application perspective and, in particular, take a look at schema migration tools (such as gocqlx migrate, or cassandra-migrate), for example, which are made exactly for that purpose.

huangapple
  • 本文由 发表于 2023年4月19日 16:43:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052455.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定