cockroachdb

Oct 3, 04:49 AM

NOTE: pay particular attention to the “—store” name – maybe better not to rely on —store defaults??

NOTE: probably best to only run one node – it appears that starting up two (or more) nodes then trying to start just one does not work.

installation and initial setup:

###########sudo su; ## this line should NOT be cut-pasted, since what follows it will be ignored….

set -o vi;
sudo yum —assumeyes update ;
sudo yum —assumeyes group install “Development Tools” ;
sudo yum —assumeyes install golang ; ## cockroachdb requires golang
#

mkdir ~/go ;
export GOPATH=~/go ;
export PATH=$PATH:$GOPATH/bin ;
go version ;

##mkdir ~/cockroachdb ;

sudo curl https://binaries.cockroachdb.com/cockroach-latest.linux-amd64.tgz > ~/cockroach.tgz;
sudo gzip -d ~/cockroach.tgz ;
sudo tar -xvf ~/cockroach.tar ;
sudo rm -v -f ~/cockroach.tar;
###cd cockroach-latest.linux-amd64 ;
sudo mv —interactive —verbose ~/cockroach-latest.linux-amd64/cockroach /usr/local/bin/ ;
sudo rmdir —verbose ~/cockroach-latest.linux-amd64/ ;

  1. export PATH=$PATH:/root/cockroachdb/cockroach-latest.linux-amd64 ; ???
  2. cd ; ???
    cockroach version ;

##

  1. NOTE: sudo cockroach DOES NOT WORK – root is not allowed to start cockroach!

cockroach start —background —insecure ; ## skip the CA certificate stuff (for now)
cockroach start —background —insecure —logtostderr ; ## note optional logging errors!

build: beta-20160929 2016/09/29 17:51:34 (go1.7.1) admin: http://localhost:8080 sql: postgresql://rootlocalhost:26257?sslmode=disable logs: cockroach-data/logs store0: path=cockroach-data

(http-host comes from the google-console in this case)

cockroach start —store=node2 —insecure —port=26258 —http-port=8081 —join=localhost:26257 —background ;

  1. this one did not work for some reason:

cockroach start —store=node2 \ —insecure \ —port=26258 \ —http-host=10.138.0.2 \ —http-port=8081 \ —join=localhost:26257 \ —background ;

build: beta-20160929 2016/09/29 17:51:34 (go1.7.1) admin: http://10.138.0.2:8081 sql: postgresql://rootcockroach-db:26258?sslmode=disable logs: node2/logs store0: path=node2 join0: localhost:26257

cockroach node status —insecure ; ## shows status of all nodes (2017-10-27 added “—insecure)
cockroach quit —port=26257 —insecure ; ## stop node

========================================================
all examples are —insecure – how to secure:

(notice use of internal and external IP numbers for google VPS instance)

cockroach cert create-ca \ —certs-dir=./path-to-certs-directory \ —ca-key=./path-to-ca-key/ca-key ;

cockroach cert create-node localhost 35.196.129.11 \ —certs-dir=./path-to-certs-directory \ —ca-key=./path-to-ca-key/ca-key ;

cockroach cert create-client markedwards \ —certs-dir=./path-to-certs-directory \ —ca-key=./path-to-ca-key/ca-key ;

cockroach user set markedwards \ —certs-dir=./path-to-certs-directory \ —host=35.196.129.11 \ —password ;

cockroach start —certs-dir=./path-to-certs-directory \ —host=10.142.0.16 \ —http-host=localhost \ —background ;

cockroach node ls —certs-dir=./path-to-certs-directory \ —host=35.196.129.11 ;

cockroach node status —certs-dir=./path-to-certs-directory \ —host=35.196.129.11 ;

cockroach quit —host=35.196.129.11 \ —certs-dir=./path-to-certs-directory ;

cockroach sql -e ‘GRANT ALL ON DATABASE bank TO markedwards’ \ —certs-dir=./path-to-certs-directory \ —host=35.196.129.11 ;

cockroach sql -e ‘GRANT SELECT, INSERT, UPDATE ON bank.users TO markedwards’ \ —certs-dir=./path-to-certs-directory \ —host=35.196.129.11 ;

==
feathers file edits:

./src/sequelize.js: dialect: ‘postgres’, dialectOptions: { ssl: true }, /* added dialectOptions */ ./config/default.json: “postgres”: “postgres://markedwards:aaaaaa@35.196.129.11:26257/bank”

======================================================

  1. create a database “bank” , create a user “maxroach” and give maxroach priveleges

cockroach sql -e ‘CREATE DATABASE bank;’ —insecure ;
cockroach user set maxroach —insecure ; ## create user maxroach
cockroach sql -e ‘GRANT ALL ON DATABASE bank TO maxroach;’ —insecure ;

cockroach sql —user=maxroach —insecure; ## (2017-10-27 added “—insecure)

###############create database bank; SHOW DATABASES; /* notice comment has closed star-slash */ SET DATABASE=bank ; /* comments has to be closed like this */ CREATE TABLE accounts ( id INT PRIMARY KEY , balance DECIMAL , createdat TIMESTAMP , updatedat TIMESTAMP ) ; SHOW COLUMNS FROM accounts; INSERT INTO accounts VALUES ( unique_rowid(), 10000.50 ) ; /* notice unique_id() ! */ SELECT * FROM accounts ;

========================
—insecure – seems to be the only way thus far to allow for anything other than “localhost”
https://www.cockroachlabs.com/docs/secure-a-cluster.html

============== N O D E ===

NODE installation:

sudo firewall-cmd —zone=dmz —add-port=26257/tcp —permanent ; sudo firewall-cmd —reload ;

sudo curl —silent —location https://rpm.nodesource.com/setup_8.x | sudo bash – ;

sudo yum —assumeyes install nodejs ;

node —version ; ## look for v8.8.1 (at the time of this writing)

== posgres example:

https://www.cockroachlabs.com/docs/stable/build-a-nodejs-app-with-cockroachdb.html – node.js app (2017-10-27 !!)
mkdir ./node-test; cd ./node-test;
npm install async pg —save ;
curl https://raw.githubusercontent.com/cockroachdb/docs/master/_includes/app/basic-sample.js > basic-sample.js;
node basic-sample.js ; ## worked 2017-10-27

== sequelize example
https://www.cockroachlabs.com/docs/stable/build-a-nodejs-app-with-cockroachdb-sequelize.html

mkdir ~/sequelize-example; cd ~/sequelize-example;
curl https://raw.githubusercontent.com/cockroachdb/docs/master/_includes/app/sequelize-basic-sample.js > sequelize-basic-sample.js ;
npm install sequelize sequelize-cockroachdb —save ;
node sequelize-basic-sample.js ;

==============

https://www.cockroachlabs.com/docs/constraints.html

??????
update -what about this?
https://www.cockroachlabs.com/docs/serial.html
CREATE TABLE serial (a SERIAL PRIMARY KEY, b STRING, c BOOL);
???????

parent/child:
CREATE TABLE grandParentTable ( id INT PRIMARY KEY , grandParentTable_name VARCHAR NOT NULL ); CREATE TABLE parentTable ( id INT PRIMARY KEY , grandParentTable INT NOT NULL REFERENCES grandParentTable(id) , parentTable_name VARCHAR NOT NULL , INDEX (grandParentTable) );
CREATE TABLE childTable ( id INT PRIMARY KEY , parentTable INT NOT NULL REFERENCES parentTable(id) , childTable_name VARCHAR NOT NULL , INDEX (parentTable) );
CREATE TABLE newkey ( id INT PRIMARY KEY , newkey_value VARCHAR NOT NULL );
CREATE TABLE childTable ( id INT PRIMARY KEY , parentTable INT NOT NULL REFERENCES parentTable (id) , newkey INT NOT NULL REFERENCES newkey (id) , childTable_name VARCHAR NOT NULL , INDEX (parentTable ) , index (newkey) );

GRANT SELECT, INSERT ON transanlys.childtable TO maxroach;
GRANT SELECT, INSERT ON transanlys.parenttable TO maxroach;
GRANT SELECT, INSERT ON transanlys.grandparenttable TO maxroach;
SHOW GRANTS ON transanlys.parenttable FOR maxroach;
——————-+—————+———————- | Table | User | Privileges |
——————-+—————+———————- | parentTable | maxroach | INSERT,SELECT |
——————-+—————+———————-

INSERT INTO grandparenttable ( id , grandParentTable_name ) VALUES ( unique_rowid() , ‘grandParentTable-1’ ) ; INSERT INTO parenttable ( id , grandParentTable , parentTable_name ) SELECT unique_rowid() , id , ‘parentTable-1’ FROM grandparenttable WHERE 1 AND grandParentTable_name = ‘grandParentTable-1’ ; INSERT INTO childtable ( id , parentTable , childTable_name ) SELECT unique_rowid() , id , ‘childTable-1’ FROM parenttable WHERE 1 AND parentTable_name = ‘parentTable-1’ ; SHOW INDEXES from childtable; SHOW CONSTRAINTS from childtable;

======================

SET database = ‘transanlys’;
DROP TABLE childtable;

CREATE TABLE childTable (id INT PRIMARY KEY, parentTable INT NOT NULL REFERENCES parentTable (id), newkey INT NOT NULL REFERENCES newkey (id), childTable_name VARCHAR NOT NULL, INDEX (parentTable ), index (newkey) );

DELETE FROM childtable; DELETE FROM parenttable; DELETE FROM grandparenttable; delete from newkey ;

GRANT SELECT, INSERT ON transanlys.childtable TO maxroach;

(run node script)

SELECT count(*) FROM parenttable
, newkey
, childtable
WHERE (newkey.id = childtable.newkey)
AND (parenttable.id = childtable.parenttable)
AND (newkey.id = 5);

===================================================
2017-04-07:
to install/run cockroachdb from docker (to get around the stupid -msse4.2 error)
curl -fsSL https://get.docker.com/ | sh ;
systemctl start docker;
systemctl status docker;
docker run hello-world ; ## test!
git clone https://github.com/cockroachdb/cockroach.git ;
git submodule update —init ;
(edit rocksdb/cgo_flags.go file and remove the -msse4.2 on line eight)
export GOPATH=~/go;
export PATH=$PATH/$GOPATH/bin ;
build/builder.sh make build ;
./cockroach version ;

============================================
2017-04-12 – create a deepstream cockroach table:

create table sequelizeTable
( sequelizeId SMALLSERIAL PRIMARY KEY
, teacherEmail VARCHAR
, sequelizeName VARCHAR
, sequelizeAddress VARCHAR
, transactionID VARCHAR
, createdAt TIMESTAMP
, updatedAt TIMESTAMP
) ;
============================================
FIREWALL ISSUE: to test:

yum —assumeyes install httpd ;
systemctl start httpd.service ;
echo ‘test apache test’ >/var/www/html/index.html ;

(see if page will load)

https://superuser.com/questions/794104/how-to-permanently-disable-firewall-in-red-hat-linux
service iptables save
service iptables stop
chkconfig iptables off

to enable default port 26257:
firewall-cmd —zone=public —add-port=26257/tcp —permanent ;
firewall-cmd —reload;
#########################################################

yum —assumeyes install jemalloc-devel ; ## ????? (in case you have to build) ???

======= LOAD-STRESS TEST ===========================

CREATE TABLE alternateParent
( id INT PRIMARY KEY
, alternateParent_value VARCHAR NOT NULL
);

CREATE TABLE grandParentTable
( id INT PRIMARY KEY
, grandParentTable_name VARCHAR NOT NULL
);

CREATE TABLE parentTable
( id INT PRIMARY KEY
, grandParentTable INT NOT NULL REFERENCES grandParentTable(id) ON DELETE CASCADE ON UPDATE CASCADE
, parentTable_name VARCHAR NOT NULL
, INDEX (grandParentTable)
);

CREATE TABLE childTable
( id INT PRIMARY KEY
, parentTable INT NOT NULL REFERENCES parentTable (id) ON DELETE CASCADE ON UPDATE CASCADE
, alternateParent INT NOT NULL REFERENCES alternateParent (id) ON DELETE CASCADE ON UPDATE CASCADE
, childTable_name VARCHAR NOT NULL
, INDEX (parentTable )
, index (alternateParent)
);

cat loadGrandParent.js

let maxGrandParent = 100;
let grandParentKey= 0;
for ( grandparentIncr=1; grandparentIncr <= maxGrandParent; grandparentIncr++ ) { grandParentKey++; var grandparentData = ‘Grandparent Value: ‘ + grandParentKey;
console.log(`INSERT INTO “grandparenttable” (“id”,“grandparenttable_name”) VALUES (${grandParentKey}, ‘${grandparentData}’); `);
}

cat loadParent.js

let maxParent = 50;
let parentKey = 0;
for ( var grandParentIncr = 1; grandParentIncr <= 100; grandParentIncr++ ) { grandParentData = ‘Grandparent Value: ‘ + grandParentIncr; for ( var parentIncr = 1; parentIncr <= maxParent; parentIncr++ ) { parentKey++; var parentData = grandParentData + ‘ Parent Data: ‘ + parentKey;
console.log(`INSERT INTO “parenttable” (“id”,“grandparenttable”,“parenttable_name”) VALUES (${parentKey}, ${grandParentIncr}, ‘${parentData}’); `); }

}

cat loadAlternateParent.js let maxAlternateParent = 10000; let alternateParentKey= 0; for ( alternateparentIncr=1; alternateparentIncr <= maxAlternateParent; alternateparentIncr++ ) { alternateParentKey++; var saveKey = alternateParentKey * 100; var altData = ‘Alternative Parent! ‘ + saveKey; console.log(`INSERT INTO “alternateparent” (“id”,“alternateparent_value”) VALUES (${saveKey}, ‘${altData}’); `); }

cat loadChild.js
let maxChild = 20;
let childKey = 0;
for ( var parentIncr = 1; parentIncr <= 5000; parentIncr++ ) { for ( var childIncr = 1; childIncr <= maxChild; childIncr++ ) { childKey++; var childData = ‘Parent data: ‘ + parentIncr + ‘ Child Data! ‘ ; var alternateParentKey = (Math.floor(childKey / 10)+1) * 100 ;
console.log(`INSERT INTO “childtable” (“id”,“parenttable”,“alternateparent”,“childtable_name”) VALUES (${childKey}, ${parentIncr}, ${alternateParentKey}, ‘${childData}’); `); }
}

WITH SEQUELIZE CALLS:
cat loadChild.js
const sleep = require(‘sleep’);

const Sequelize = require(‘sequelize’);
const sequelize = new Sequelize(‘bank’, ‘feathersuser’, ‘aaaaaa’, { host: ’159.203.111.167’, port: 26257, dialect: ‘postgres’, dialectOptions: { ssl: true }, operatorsAliases: false, pool: { max: 5, min: 0, acquire: 30000, idle: 10000 }
});

sequelize .authenticate() .then(() => { console.log(‘Connection has been established successfully.’); }) .catch(err => { console.error(‘Unable to connect to the database:’, err); });

let maxChild = 10;
let childKey = 0;

sequelize.query(“SELECT id, parentTable_name FROM parentTable WHERE 1=1”).spread( (results, metadata) => { results.forEach((element, index, array) => { console.log(element.id); console.log(element.parenttable_name); for ( var childIncr = 1; childIncr <= maxChild; childIncr++ ) { childKey++; var childData = element.parenttable_name + ‘ Child Data: ‘ + childKey; sequelize.query(`INSERT INTO childTable (id, parentTable, alternateParent, childTable_name) VALUES (${childKey}, ${element.id}, 12345, ‘${childData}’); `); } })
})

================================================

Mark Edwards

,

---

deepstream from scratch

Aug 9, 05:15 AM

2016-08-09

UPDATE: 2017-02-03 – firewall:
firewall-cmd —zone=public —add-port=6020/tcp —permanent ;
firewall-cmd —reload

ran on amazon ec2-instance

be SURE to get the IP numbers before starting – notice that amazon has an internal and external number which may be different

1) open ports via amazon “security groups” under “network and security” and open ports (in this example, ports 9020 and 9021)

2) PING – create a new “inbound rule” by picking the “ALL ICMP” dropdown and the “Anywhere” dropdown.

sudo -i ; ## go into super-user mode (sort of) on amazon or wherever sudo is required

  1. install & start deepstream
    wget https://bintray.com/deepstreamio/rpm/rpm -O /etc/yum.repos.d/bintray-deepstreamio-rpm.repo ;
    yum —assumeyes install deepstream.io ;
    deepstream start &

  1. install node:
    curl —silent —location https://rpm.nodesource.com/setup_6.x | bash – ;
    yum —assumeyes install nodejs ;

  1. run a deepstream server
    npm init; ### create a package.json file
    npm install deepstream.io —save ; ## might take a little while
    npm install deepstream.io-client-js —save ; ## dont forget

// OPTIONAL – create a little script to talk to our server. this is really
// a client, but it just so happens to be running on the server
// however, it behaves just like a client would!
// the only difference is the internal IP and the different port number

cat < client-on-server.js ;
var deepstream = require(‘deepstream.io-client-js’);
ds = deepstream( ’172.30.0.111:9021’ ).login(); // must match the host IP number in the server // port has to match tcpPort number in the server
record = ds.record.getRecord( ‘clientRecord’ );
record.set(‘clientField’, ‘test from the node client running on the server! ‘);
record.subscribe (‘clientField’ , function (value) { console.log(‘subscribed value: ‘ + value);
});
END

// lastly, a browser script !


optionally: ln -s ./node_modules/deepstream.io-client-js/dist/deepstream.min.js ; ## create a pointer to our deepstream.min.js file and use:

####cat < server.js ; ## NOT NEEDED !
####// deepstream server:
####var DeepstreamServer = require(‘deepstream.io’);
####var server = new DeepstreamServer();
####server.set(‘host’, ’172.30.0.111’); // use the I-N-T-E-R-N-A-L IP number !
####server.set (‘port’, 9020 );
####server.set (‘tcpPort’, 9021 );
####// this is the default, but its a nice reminder
####server.start();
####END
####
####node server.js & ## our new little deepstream server is now running in the background

  1. NO LONGER NEEDED !

==============================================
==============================================
success !

DS client-server listener (2016-08-11)

// newServer.js
var deepstream = require(‘deepstream.io-client-js’);
var util = require(‘util’);
var dateFormat = require(‘dateformat’);

ds = deepstream( ’172.30.0.114:9021’ ).login();
ds.record.listen(’^stock/.*’, function (recordName, isSubscribed) { console.log(‘recordName: ‘ + recordName) ; console.log(‘isSubscribed?: ‘ + isSubscribed) ; console.log(‘get client record:’); ds.record.getRecord( recordName ).get (‘stockField’ ); ds.record.getRecord( recordName ).set (‘stockField’ , ‘newServer setting this value ! ! !’ ); ds.record.getRecord( recordName ).subscribe (function (value) { console.log(’ newServer.js — subscribed value: ‘ + util.inspect(value)); var now = new Date(); ds.record.getRecord( recordName).set (‘result’ , ‘newServer is responding to you at ‘ + dateFormat(now, “dddd, mmmm dS, yyyy, h:MM:ss TT”) ); });
});

//newClient.js ( client test but runs on server AND MAY BE TOTALLY UNNECESSARY)
var deepstream = require(‘deepstream.io-client-js’);
var util = require(‘util’);

var recordName = ‘stock/mark’ ;

ds = deepstream( ’172.30.0.114:9021’ ).login();
ds.record.getRecord( recordName ).set(‘stockField’, ‘newClient is setting this value !! ‘);
ds.record.getRecord( recordName ).get(‘stockField’ );
ds.record.getRecord( recordName ).subscribe (function (value) { console.log(‘stock/mark — subscribed value: ‘ + util.inspect(value)); ds.record.getRecord( recordName ).get( );
});

// webpage one:



newtest.html

Mark Edwards

,

---

stripe

Jun 29, 08:57 AM

1) install composer locally:

php -r “copy(‘https://getcomposer.org/installer’, ‘composer-setup.php’);”

php -r “if (hash_file(‘SHA384’, ‘composer-setup.php’) === ‘e115a8dc7871f15d853148a7fbac7da27d6c0030b848d9b3dc09e2a0388afed865e6a3d6b3c0fad45c48e2b5fc1196ae’) { echo ‘Installer verified’; } else { echo ‘Installer corrupt’; unlink(‘composer-setup.php’); } echo PHP_EOL;”

php composer-setup.php ;
php -r “unlink(‘composer-setup.php’);”

2) create the composer.json file:
cat > composer.json ;

{ “require”: { “monolog/monolog”: “1.0.*” }
}

3) ?php composer.phar install;

4) create config.php, charge.php and index.php

cat > config.php ;

<?php
require_once(‘vendor/autoload.php’);

$stripe = array( “secret_key” => “sk_test_jgji8VZWf90mcw4BBaO1YiMp”, “publishable_key” => “pk_test_Dmx9iBVuUizlpXIS1DsUtYNn”
);

\Stripe\Stripe::setApiKey($stripe[‘secret_key’]);
?>

5) cat > charge.php ;

<?php require_once(’./config.php’);

$token = $_POST[‘stripeToken’]; $customer = \Stripe\Customer::create(array( ‘email’ => ‘customer@example.com’, ‘source’ => $token )); $charge = \Stripe\Charge::create(array( ‘customer’ => $customer->id, ‘amount’ => 5000, ‘currency’ => ‘usd’ )); echo ‘

Successfully charged $50.00!

‘; ?>

6) cat > index.php ;