# Cassandra

## #安裝

[http://cassandra.apache.org/download/](http://cassandra.apache.org/download/)

Linux(Debian or Ubuntu)

```
echo "deb http://www.apache.org/dist/cassandra/debian 311x main" | sudo tee -a /etc/apt/sources.list.d/cassandra.sources.list;
curl https://www.apache.org/dist/cassandra/KEYS | sudo apt-key add -;
sudo apt-get update;
sudo apt-get install cassandra;
// start
sudo service cassandra start
//check
cqlsh
```

## 基礎操作

1.輸入cqlsh進入command line

> 如果用docker 輸入 docker ps 然後複製docker id 之後輸入 docker exec -it \<docker ID> /bin/bash

2.基本結構

![](https://2356031413-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0u4DAqfidGmTt67qrG%2F-M0u4EWtMiZmhb1fV2nz%2F-M0u4hmvXS4ArP3kP5oW%2Fanalogy.jpg?generation=1582596301740356\&alt=media)

## 使用

```
cqlsh

1.新增DB
CREATE KEYSPACE tutorialspoint
WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 3};

2.使用DB
 USE tutorialspoint;

3.新增Table
CREATE TABLE emp(
   emp_id int PRIMARY KEY,
   emp_name text,
   emp_city text,
   emp_sal varint,
   emp_phone varint
   );

4.確認Table
select * from emp;

5.新增資料

CREATE TABLE users(
user_id int PRIMARY KEY,
user_account text,
user_password text,
);

INSERT INTO users (user_id, user_account, user_password) VALUES(1,'test123', '12345');
```

## 預設cassandra只能讓Localhost連

要更改可參考[https://stackoverflow.com/questions/32258471/restricting-cassandra-to-localhost-only](https://stackoverflow.com/questions/32258471/restricting-cassandra-to-localhost-only)
