如何在 Ubuntu 和其他 Linux 上安装 Apache Cassandra
Cassandra
![](/data/attachment/album/202112/04/151942ihpizylzk0i3pt0i.jpg)


[Apache Cassandra](https://cassandra.apache.org/_/index.html) 是一个自由开源的 NoSQL 数据库管理系统，用于在许多服务器上操作大量信息，提供无单点故障的高可用性。


我不打算讨论 NoSQL 数据库的细节。我将会告诉你如何在基于 Ubuntu 的 Linux 发行版上安装 Apache Cassandra。


请注意，这更多是为了实践。


### 在 Linux 上安装 Apache Cassandra


有多种方法可以在 Ubuntu 和其他 Linux 发行版上安装 Cassandra：


* 使用 Apache 的官方 deb 仓库安装：适合并推荐给基于 Debian 和 Ubuntu 的发行版。如果有更新的版本，会得到自动更新。
* 使用 Docker 安装：适用于所有 Linux 发行版。
* 从 tarball 安装：适用于所有 Linux，但不会自动更新到新版本。


这仅仅是为了练习和体验 Apache Cassandra。如果你要在一个项目中使用它和其他服务，你必须遵循该服务的完整配置和设置指南。


我将展示前两种方法。


#### 方法 1：使用官方仓库在 Ubuntu 和 Debian 上安装 Cassandra


在你安装和使用 Cassandra 之前，你需要在你的系统上安装 Python 和 Java。你可能需要 [在 Ubuntu 上安装 Java](https://itsfoss.com/install-java-ubuntu/)，但 Python 通常是预装的。


你可以用下面这行来检查先决条件：



```
java -version && python --version

```

所有先决条件都安装好了？那就好。让我们来安装 Cassandra。这里的方法与 [在 Ubuntu 中添加任意外部仓库](https://itsfoss.com/adding-external-repositories-ubuntu/) 相同。


首先，将 Apache Cassandra 仓库添加到你的源列表中。下面这个添加最新的主要版本（在写这篇文章的时候）4.0 系列。



```
echo "deb http://www.apache.org/dist/cassandra/debian 40x main" | sudo tee -a /etc/apt/sources.list.d/cassandra.sources.list

```

![Add Apache Cassandra repository](/data/attachment/album/202112/04/152044finw2ccwbw6k2cgw.png)


现在，下载并将 Apache Cassandra 仓库的密钥添加到服务器上的受信任密钥列表中。这样，你的系统就会信任来自你在上一步添加的仓库的软件包。


你应该确保 `apt` 可以通过 https 使用。



```
sudo apt install apt-transport-https

```

然后添加密钥：



```
wget https://www.apache.org/dist/cassandra/KEYS && sudo apt-key add KEYS

```

![Add Apache Cassandra repository key](/data/attachment/album/202112/04/152045vxulruzhrljzrigl.png)


你已经添加了仓库。更新本地缓存，使你的系统知道这个新仓库的存在。



```
sudo apt update

```

最后，用以下命令安装 Cassandra：



```
sudo apt install cassandra

```

![Installing Apache Cassandra on Ubuntu](/data/attachment/album/202112/04/152045ddlpwv9xpxzyiv5l.png)


安装完成后，Cassandra 服务会自动开始运行。如果你想的话，你仍然可以验证它：



```
sudo systemctl status cassandra.service

```

![Check if Cassandra is running](/data/attachment/album/202112/04/152046evhex7bux0ep0fbn.png)


你可以输入 `cqlsh` 连接到数据库。输入 `exit` 来退出这个 shell。


![Entering cqlsh](/data/attachment/album/202112/04/152046dztbntmrnz66a4tx.png)


这是非常基本和默认的设置。你可能需要根据你的需求来配置它。请查看 [官方文档中的配置部分](https://cassandra.apache.org/doc/latest/cassandra/getting_started/configuring.html)。


#### 方法 2：使用 Docker 安装 Apache Cassandra


这个方法适用于任何 Linux 发行版，只要你打算在 Docker 设置中使用它。


当然，你需要在你的系统上安装 Docker 来实现这个方法。这是这个方法的前提条件，我让你处理这件事情。


如果你有 Docker，使用下面的命令来拉取 Apache Cassandra 的 Docker 镜像：



```
sudo docker pull cassandra:latest

```

![Pulling Apache Cassandra docker image](/data/attachment/album/202112/04/152046swkwzc0zrczswxr0.png)


完成后，你可以用 [docker run 命令](https://linuxhandbook.com/docker-run-vs-start-vs-create/)来启动 Cassandra，像这样：



```
sudo docker run --name cass_cluster cassandra:latest

```

![Running Cassandra in a container](/data/attachment/album/202112/04/152047xnahi625am9vi6z6.png)


**注意：** `--name` 选项指的是创建的 Cassandra 集群的名称。


要与之前启动的 Cassandra 节点交互，你需要初始化 CQL shell，你可以用 Docker `exec` 命令这样做：



```
sudo docker exec -it cass_cluster cqlsh

```

![Access the cqlsh running in Docker.](/data/attachment/album/202112/04/152047b9w7977af7xsvahs.png)


**恭喜！** 现在你至少知道了在你的系统中安装 Apache Cassandra 的两种不同方法。


请记住，这篇文章只是一个介绍。如果你有兴趣了解更多关于 Apache Cassandra 的信息，请阅读 [文档](https://cassandra.apache.org/doc/latest/index.html)，在那里你可以找到更多关于这个神奇的 NoSQL 数据库管理系统的信息。如果这篇文章对你有帮助，请阅读并分享它。下一篇见。




---


via: <https://itsfoss.com/apache-cassandra-ubuntu/>


作者：[Marco Carmona](https://itsfoss.com/author/marco/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
[Apache Cassandra](https://cassandra.apache.org/_/index.html) is a free and open-source NoSQL database management system drawn to manipulate large amounts of information across many servers, providing high availability with no single point of failure.

I am not going into the details of NoSQL database. I am going to so you how you can install Apache Cassandra on Ubuntu based Linux distributions.

Please note that it is more for practicing

## Installing Apache Cassandra on Linux

There are multiple ways you can install Cassandra on Ubuntu and other Linux distributions:

- Install it using the official deb repositories from Apache: suitable and recommended for Debian and Ubuntu-based distributions. Gets automatic update if a newer version is available.
- Install it using Docker: works for all Linux distributions
- Install it from the tarball: works with all Linux but it won’t get updated to a new version automatically.

This is merely for practicing and experiencing Apache Cassandra. If you are going to use it in a project with other services, you’ll have to follow the complete configuration and setting guide of that service.

I’ll show the first two methods.

### Method 1: Install Cassandra on Ubuntu and Debian using the official repository

Before you could install and use Cassandra, you need to have Python and Java installed on your system. You may have to [install Java on Ubuntu](https://itsfoss.com/install-java-ubuntu/) however Python usually comes preinstalled.

You can check the prerequisite with the next line:

`java -version ; python --version`


All prerequisites well installed? That’s good. Let’s install Cassandra. The method here is the same as [adding any external repository in Ubuntu](https://itsfoss.com/adding-external-repositories-ubuntu/).

First, add the Apache Cassandra repository to your sources list. This one adds the latest major version (at the time of writing it) 4.0 series.

`echo "deb http://www.apache.org/dist/cassandra/debian 40x main" | sudo tee -a /etc/apt/sources.list.d/cassandra.sources.list`


![installing the debian packages step 1](https://itsfoss.com/content/images/wordpress/2021/11/Installing_the_Debian_packages_step_1-800x123.png)

Now, download and add the Apache Cassandra repository keys to the list of trusted keys on the server. This way, your system will trust the packages coming from the repository you added in the previous step.

You should make sure that apt can be used over https.

`sudo apt install apt-transport-https`


And then add the key:

`wget https://www.apache.org/dist/cassandra/KEYS && sudo apt-key add KEYS`


![installing the debian packages step 2](https://itsfoss.com/content/images/wordpress/2021/11/Installing_the_Debian_packages_step_2-800x490.png)

You have added the repository. Update the local cache so that your system is aware of the presence of this new repository.

`sudo apt update`


And lastly, install Cassandra with the following command:

`sudo apt install cassandra`


![installing the debian packages step 3](https://itsfoss.com/content/images/wordpress/2021/11/Installing_the_Debian_packages_step_3-800x490.png)

Once the installation finishes, Cassandra service starts running automatically. You could still verify it if you want:

`sudo systemctl status cassandra.service`


![verify cassandra](https://itsfoss.com/content/images/wordpress/2021/11/verify-cassandra-800x443.png)

You can connect to the database by typing cqlsh. Type exit to exit this shell.

![installing the debian packages step 4](https://itsfoss.com/content/images/wordpress/2021/11/Installing_the_Debian_packages_step_4-800x164.png)

This was the very basic and default set up. You’ll probably need to configure it based on your requirement. Please the [official document for the configuration part](https://cassandra.apache.org/doc/latest/cassandra/getting_started/configuring.html).

### Method 2: Install Apache Cassandra using Docker

This method will work with any Linux distribution as long as you are going to use it in a Docker setup.

Of course, you need to have Docker installed on your system for this method. That’s a prerequisite for this method and I let you handle that.

If you have Docker, use the command below to pull the docker image of Apache Cassandra.

`sudo docker pull cassandra:latest`


![using docker images step 2](https://itsfoss.com/content/images/wordpress/2021/11/Using_Docker_images_step_2-800x275.png)

Once that’s done, you can start Cassandra with the [docker run command](https://linuxhandbook.com/docker-run-vs-start-vs-create/) like this:

`sudo docker run --name cass_cluster cassandra:latest`


![using docker images step 3](https://itsfoss.com/content/images/wordpress/2021/11/Using_Docker_images_step_3-800x490.png)

**Note:** the `--name`

option will be the name of the Cassandra cluster created.

To interact with the Cassandra node started before, you need to initialize the CQL shell, and you can do it with the Docker exec command like this:

`sudo docker exec -it cass_cluster cqlsh`


![using docker images step 4](https://itsfoss.com/content/images/wordpress/2021/11/Using_Docker_images_step_4-800x225.png)

* Congrats! *Now you know at least two different ways to install Apache Cassandra in your system.

Please keep in mind that this post is only an introduction. If you are interested in knowing more about Apache Cassandra, read the [documentation](https://cassandra.apache.org/doc/latest/index.html) where you can find out more about this amazing NoSQL database management system. **Enjoy it and share this post if it was interesting and helpful for you! See you in the next one.**