wget -qO- https://binaries.cockroachdb.com/cockroach-v19.2.2.linux-amd64.tgz | tar xvz
cp -i cockroach-v19.2.2.linux-amd64/cockroach /usr/local/bin/
--启动第一个节点
cockroach start --insecure --store=n1 --host=10.10.14.188 --http-port=8081 --background
启动第二个节点
cockroach start --insecure --store=n2 --host=10.10.14.187 --http-port=8081 --join=10.10.14.188:26257 --background
cockroach start --insecure --store=n3 --host=10.10.14.189 --http-port=8081 --join=10.10.14.188:26257 --background
--非安全模式链接数据库
cockroach sql --host=10.10.14.188 --insecure
--创建数据库:
create database testDb;
--创建 表
CREATE TABLE IF NOT EXISTS person (number INT PRIMARY KEY, name STRING,birthday DATE)
--创建用户
cockroach user set dsideal --host=10.10.14.188 --insecure
--为用户授权可以使用哪个库
cockroach sql --host=10.10.14.188 --insecure -e 'GRANT ALL ON DATABASE testdb TO dsideal'
--创建索引
CREATE INDEX ON student (type_id);
--JAVA代码
JAR包地址:链接: https://pan.baidu.com/s/1uiNTxetRYOj7TwJ394NkLQ 提取码: n2x7
package com.dsideal;
import java.sql.*;
import java.util.Properties;
public class test01 {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stubClass.forName("org.postgresql.Driver");
// Connect to the "bank" database.
Properties props = new Properties();
props.setProperty("user", "dsideal");
//非安全模式
props.setProperty("sslmode", "disable");
//安全模式
//props.setProperty("sslmode", "require");
//props.setProperty("sslrootcert", "certs/ca.crt");
//props.setProperty("sslkey", "certs/client.maxroach.pk8");
//props.setProperty("sslcert", "certs/client.maxroach.crt");
Connection db = DriverManager
.getConnection("jdbc:postgresql://10.10.14.188:26257/testdb", props);
try {
// Create the "accounts" table.
db.createStatement()
.execute("CREATE TABLE IF NOT EXISTS accounts (id INT PRIMARY KEY, balance INT)");
// Insert two rows into the "accounts" table.
db.createStatement()
.execute("INSERT INTO accounts (id, balance) VALUES (1, 1000), (2, 250)");
// Print out the balances.
System.out.println("Initial balances:");
ResultSet res = db.createStatement()
.executeQuery("SELECT id, balance FROM accounts");
while (res.next()) {
System.out.printf(" account %s: %s
",
res.getInt("id"),
res.getInt("balance"));
}
} finally {
// Close the database connection.
db.close();
}
}
}