#database #go

Z:/GoDoc/Practical%20CockroachDB.pdf

Sharding

:GiDatabaseManagement: <-> :GiDatabaseManagement: <-> :GiDatabaseManagement: <-> :GiDatabaseManagement:

download

curl -o cockroach-v21.1.7.windows-6.2-amd64.zip https://binaries.cockroachdb.com/cockroach-v21.1.7.windows-6.2-amd64.zip

Config

cd ../../../Downloads
powershell.exe -NoP -NonI -Command "Expand-Archive './cockroach-v21.1.7.windows-6.2-amd64.zip' '.'"

Start-Server

Cluster Overview | Cockroach Console

-.> localhost:8080

cockroach start-single-node --insecure --listen-addr=localhost

[!]
_
start sql as root on default
⏳ notable command

🪳
cockroach sql --secure

documentation sql function

🪳
\hf <func_name>

_

Kill-switch 🦩

netstat -ano | findstr 8080; Stop-Process -Name "cockroach"

Command Execution

powershell.exe

🐳 Docker

docker run \
	--rm -it \
	-name=cockroach \
	-p 26257:26257 \
	-p 8080:8080 \
	cockroachdb/cockroach:v21.1.7 start-single-node \
	--insecure
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
	name: cockroachdb-budget
	labels:
		app: cockroachdb
spec:
	selector:
		matchLabels:
			app: cockroachdb
	maxUnavailable: 1
kuberctl apply -l 1_prod-disruption-budget.yaml
kubectl port-forward service/cockroachdb-public 26257

🦚 -> working progress//...

cockroach start \
	--insecure \
	--store=node1 \
	--listen-addr=localhost:26257 \
	--http-addr=localhost:8080 \
	--join=localhost:26257,localhost:26258,localhost:26259

cockroach start \
	--insecure \
	--store=node2 \
	--listen-addr=localhost:26258 \
	--http-addr=localhost:8081 \
	--join=localhost:26257,localhost:26258,localhost:26259

cockroach start \
	--insecure \
	--store=node3 \
	--listen-addr=localhost:26259 \
	--http-addr=localhost:8082 \
	--join=localhost:26257,localhost:26258,localhost:26259
cockroach init --insecure --host=localhost:26257

[!]
_

cockroach sql --insecure --host=localhost:26257

initialization one of the connected node
onto the cluster::\

awaken the byzantine consensus
_

powershell.exe

Flag added: --locality = region = ..east ...west

root@local> SET CLUSTER SETTING <YOUR_ORG>
...
root@local> SET CLUSTER SETTING <YOUR_EN>

🏀 connect to entreprise

-> show schemas;

DATATYPE

UUID

CREATE TABLE person (id UUID);

BIT

CREATE TABLE bits (
exactly_1 BIT,
exactly_64 BIT(64)
);

BOOL

if 0 := false else N/{0} := true

BYTES

-> HEX :0-8

INET


IPv4 address, IPv6 address, and CIDRs (Classless Inter Domain Routing)


cockroach -h
cockroach start -h
cockroach demo bank

. ->

CERT

cockroach cert list --certs-dir certs
cockroach sql --url "postgresql://localhost/?sslmode=disable" -e "SHOW DATABASES"

Flag added: --format output > *.csv

cockroach node ls --insecure --format=records
cockroach node status \
	--ranges
	--stats
	--decommission
	--all
cockroach workload init bank 'postgres://root@127.0.0.1:26257?sslmode=disable'
cockroach workload run bank \
	--duration=10m \

Kafka Sink

Webhook Sink

package main

import (
	"io/ioutil"
	"log"
	"net/http"
)

func main() {
	http.HandleFunc("/", cdc)
	log.Fatal(http.ListenAndServer(":9090", nil))
}

func cdc(w http.ResponseWriter, r *http.Request) {
	defer func() {
		if err := r.Body.Close(); err != nil {
			log.Printf("error closing request body : %v", err)
		}
	}()
	event, err := ioutil.ReadAll(r.Body)
	if err != nil {
		log.Printf("error reading request body: %v", err)
	}
	log.Println(string(event))
}

Data Privacy

mkdir certs
mkdir keys
cockroach cert create-ca \
	--certs-dir=certs \
	--ca-key=keys/ca.key \
	localhost
package main

import (
	"context"
	"flag"
	"log"
	"os"
	"testing"
	"time"

	"github.com/jackc/pgx/v4/pgxpool"
)

var (
	dbTests *bool
	db      *pgxpool.Pool
)

func TestMain(m *testing.M) {
	dbTests = flag.Bool("db", false, "run database tests")
	flag.Parse()

	if *dbTests {
		connStr, ok := os.LookupEnv("CONN_STR")
		if !ok {
			log.Fatal("connection string env var not found")
		}

		if err := waitForDB(connStr); err != nil {
			log.Fatalf("error waiting for database: %v", err)
		}
	}

	code := m.Run()

	if *dbTests {
		db.Close()
	}

	os.Exit(code)
}

func waitForDB(connStr string) error {
	var err error
	for i := 1; i <= 10; i++ {
		db, err = pgxpool.Connect(context.Background(), connStr)
		if err != nil {
			log.Printf("error connecting to database: %v", err)
			time.Sleep(time.Second * time.Duration(i))
		}
	}
	return err
}

func TestADatabaseInteraction(t *testing.T) {
	t.Log("running non-database test")
}