您的位置:首页 > 数据库

分布式数据库cockroachDB 初探

2018-01-07 00:00 3163 查看
摘要: ——之cockroachDB linux 安装

1.安装

官网下载安装软件
https://www.cockroachlabs.com/docs/stable/install-cockroachdb.html
解压:

tar -xvf  cockroach-v1.1.3.linux-amd64.tgz
cp cockroach-v1.1.3.linux-amd64/cockroach /usr/local/bin


2.启停数据库

启动数据库

[cockroach@vm1 ~]$ cockroach start --insecure \
> --port=26257 \
> --http-port=8081 \
> --host=192.168.0.107 \
> --store=path=/u1/cockroach/cr26257 \
> --background
*
* WARNING: RUNNING IN INSECURE MODE!
*
* - Your cluster is open for any client that can access 192.168.0.107.
* - Any user, even root, can log in without providing a password.
* - Any user, connecting as root, can read or write any data in your cluster.
* - There is no network encryption nor authentication, and thus no confidentiality.
*
* Check out how to secure your cluster: https://www.cockroachlabs.com/docs/stable/secure-a-cluster.html *
CockroachDB node starting at 2016-07-19 03:48:22.657874679 +0000 UTC (took 1.7s)
build:      CCL v1.1.3 @ 2017/11/27 13:59:10 (go1.8.3)
admin:      http://192.168.0.107:8081 sql:        postgresql://root@192.168.0.107:26257?application_name=cockroach&sslmode=disable
logs:       /u1/cockroach/cr26257/logs
store[0]:   path=/u1/cockroach/cr26257
status:     restarted pre-existing node
clusterID:  bf01cc59-dfca-459a-a768-6fbc6e18e180
nodeID:     1
[cockroach@vm1 ~]$

查看数据目录生成的文件

[cockroach@vm1 ~]$ cd /u1/cockroach/cr26257/
[cockroach@vm1 cr26257]$ ll -rth
total 2.5M
drwxr-xr-x. 2 cockroach cockroach 4.0K Jul 19 11:35 auxiliary
-rw-r--r--. 1 cockroach cockroach    0 Jul 19 11:35 LOCK
-rw-r--r--. 1 cockroach cockroach   37 Jul 19 11:35 IDENTITY
-rw-r--r--. 1 cockroach cockroach   13 Jul 19 11:35 COCKROACHDB_VERSION
-rw-r--r--. 1 cockroach cockroach 4.2K Jul 19 11:42 OPTIONS-000008
drwxr-xr-x. 2 cockroach cockroach 4.0K Jul 19 11:48 logs
drwxr-xr-x. 3 cockroach cockroach 4.0K Jul 19 11:48 local
-rw-r--r--. 1 cockroach cockroach   16 Jul 19 11:48 CURRENT
-rw-r--r--. 1 cockroach cockroach 4.2K Jul 19 11:48 OPTIONS-000011
-rw-r--r--. 1 cockroach cockroach   18 Jul 19 11:48 cockroach.http-addr
-rw-r--r--. 1 cockroach cockroach   19 Jul 19 11:48 cockroach.advertise-addr
-rw-r--r--. 1 cockroach cockroach   19 Jul 19 11:48 cockroach.listen-addr
-rw-r--r--. 1 cockroach cockroach 113K Jul 19 11:48 000012.sst
-rw-r--r--. 1 cockroach cockroach  298 Jul 19 11:48 MANIFEST-000008
-rw-r--r--. 1 cockroach cockroach 2.3M Jul 19 11:51 000009.log
[cockroach@vm1 cr26257]$


停止数据库

[cockroach@vm1 ~]$ cockroach quit --insecure --port=26257 --host=192.168.0.107
ok
[cockroach@vm1 ~]$


3.使用sql客户端登录与sql测试

SQL 测试

cockroach sql   --host 10.11.96.28   --port 26257  --insecure
# Welcome to the cockroach SQL interface.
# All statements must be terminated by a semicolon.
# To exit: CTRL + D.
#
# Server version: CockroachDB CCL v1.1.3 (linux amd64, built 2017/11/27 13:59:10, go1.8.3) (same version as client)
# Cluster ID: cf9d4e4c-aac0-4b91-9fdb-a8c921304106
#
# Enter \? for a brief introduction.
#
root@192.168.0.107:26257/>
root@192.168.0.107:26257/>
root@192.168.0.107:26257/> create database testdb
-> ;
CREATE DATABASE

Time: 79.629912ms

root@1
3ff0
92.168.0.107:26257/testdb> \h show
Command:     SHOW
Syntax:
SHOW SESSION, SHOW CLUSTER SETTING, SHOW DATABASES, SHOW TABLES, SHOW COLUMNS, SHOW INDEXES,
SHOW CONSTRAINTS, SHOW CREATE TABLE, SHOW CREATE VIEW, SHOW USERS, SHOW TRANSACTION, SHOW BACKUP,
SHOW JOBS, SHOW QUERIES, SHOW SESSIONS, SHOW TRACE

root@192.168.0.107:26257/testdb> show tables;
+---------+
|  Table  |
+---------+
| student |
+---------+
(1 row)

Time: 5.476509ms

root@192.168.0.107:26257/testdb>

root@192.168.0.107:26257/> show databases;
+--------------------+
|      Database      |
+--------------------+
| crdb_internal      |
| information_schema |
| pg_catalog         |
| system             |
| testdb             |
+--------------------+
(5 rows)

Time: 6.560387ms

root@192.168.0.107:26257/>
root@192.168.0.107:26257/testdb> \?
You are using 'cockroach sql', CockroachDB's lightweight SQL client.
Type:
\q to exit        (Ctrl+C/Ctrl+D also supported)
\! CMD            run an external command and print its results on standard output.
\| CMD            run an external command and run its output as SQL statements.
\set [NAME]       set a client-side flag or (without argument) print the current settings.
\unset NAME       unset a flag.
\show             during a multi-line statement or transaction, show the SQL entered so far.
\? or "help"      print this help.
\h [NAME]         help on syntax of SQL commands.
\hf [NAME]        help on SQL built-in functions.

More documentation about our SQL dialect and the CLI shell is available online: https://www.cockroachlabs.com/docs/stable/sql-statements.html https://www.cockroachlabs.com/docs/stable/use-the-built-in-sql-client.html

root@192.168.0.107:26257/testdb>

root@192.168.0.107:26257/testdb> create table student(id int,name varchar(20));
CREATE TABLE

Time: 39.821498ms

root@192.168.0.107:26257/testdb> insert into student values (1,'tom');
INSERT 1

Time: 162.412001ms

root@192.168.0.107:26257/testdb> commit;
pq: there is no transaction in progress
root@192.168.0.107:26257/testdb> insert into student values (1,'tom');^C
root@192.168.0.107:26257/testdb>
root@192.168.0.107:26257/testdb>
root@192.168.0.107:26257/testdb> select * from student;
+----+------+
| id | name |
+----+------+
|  1 | tom  |
+----+------+
(1 row)

Time: 87.176305ms

root@192.168.0.107:26257/testdb> insert into student values (2,'jimmy');
INSERT 1

Time: 20.773568ms

root@192.168.0.107:26257/testdb> SELECT * FROM student;
+----+-------+
| id | name  |
+----+-------+
|  1 | tom   |
|  2 | jimmy |
+----+-------+
(2 rows)

Time: 4.246073ms

root@192.168.0.107:26257/testdb>


4.登录web 管理界面

登录地址
http://192.168.0.107:8081/#/cluster/all/overview


5.小结

本文主要使用的命令如下:

启动

cockroach start --insecure \
--port=26257 \
--http-port=8081 \
--host=192.168.0.107 \
--store=path=/u1/cockroach/cr26257 \
--background

停库

cockroach quit --insecure \
--port=26257 \
--host=192.168.0.107

连接

cockroach sql --host  192.168.0.107  //报错需要使用以下非安全模式连接
cockroach sql --host  192.168.0.107 --insecure // 非安全模式连接

create database testdb;
CREATE TABLE testdb.accounts (id INT PRIMARY KEY, balance DECIMAL);


附录

profile 设置

export  CRPORT=26257
export  CRDATA=/u1/cockroach/data
export  LANG=en_US.utf8
export  CRHOME=/u1/cockroach/cockroach-v1.1.3
export  DATE=`date +"%Y%m%d%H%M"`
export  LD_LIBRARY_PATH=/u1/cockroach/cockroach-v1.1.3/bin:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/lib:$LD_LIBRARY_PATH
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Cockroach