1安装CouchDB:docker pull klaemo/couchdb
创建文件夹:mkdir couchdb
启用一个CouchDB实例:docker run -p 5984:5984 -d --name my-couchdb -e COUCHDB_USER=admin -e COUCHDB_PASSWORD=password -v ~/couchdb:/opt/couchdb/data klaemo/couchdb
启动浏览器输入url:http://192.168.134.134:5984/_utils // 192.168.134.134是我自己的IP地址 控制台输入ifconfig可查看
浏览器里输入账号admin 密码 password 进入数据库
2删除原来的CouchDB容器:docker rm -f my-couchdb
3新建4个CouchDB数据库:
cd ~
mkdir couchdb0
mkdir couchdb1
mkdir couchdb2
mkdir couchdb3
docker run -p 5984:5984 -d --name couchdb0 -e COUCHDB_USER=admin -e COUCHDB_PASSWORD=password -v ~/couchdb0:/opt/couchdb/data klaemo/couchdb
docker run -p 6984:5984 -d --name couchdb1 -e COUCHDB_USER=admin -e COUCHDB_PASSWORD=password -v ~/couchdb1:/opt/couchdb/data klaemo/couchdb
docker run -p 7984:5984 -d --name couchdb2 -e COUCHDB_USER=admin -e COUCHDB_PASSWORD=password -v ~/couchdb2:/opt/couchdb/data klaemo/couchdb
docker run -p 8984:5984 -d --name couchdb3 -e COUCHDB_USER=admin -e COUCHDB_PASSWORD=password -v ~/couchdb3:/opt/couchdb/data klaemo/couchdb
4更改docker-compose-cli.yam 编辑4个peer节点:
peer0.org1.example.com:
container_name: peer0.org1.example.com
environment:
- CORE_LEDGER_STATE_STATEDATABASE=CouchDB
- CORE_LEDGER_STATE_COUCHDBCONFIG_COUCHDBADDRESS=192.168.134.134:5984
- CORE_LEDGER_STATE_COUCHDBCONFIG_USERNAME=admin
- CORE_LEDGER_STATE_COUCHDBCONFIG_PASSWORD=password
extends:
file: base/docker-compose-base.yaml
service: peer0.org1.example.com
5启用Fabric:./network_setup.sh up
查看数据库信息:curl http://192.168.134.134:5984/mychannel_mycc/_all_docs //提示没有权限查看的话url输入账号密码
: curl http://admin:password@192.168.134.134:5984/mychannel_mycc/_all_docs
只查看a的信息:curl http://admin:password@192.168.134.134:5984/mychannel_mycc/a
进入容器:docker exec -it cli bash
运行链码测试marbles02:peer chaincode install -n marbles02 -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/marbles02
peer chaincode instantiate -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/cacerts/ca.example.com-cert.pem -C mychannel -n marbles02 -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/marbles02 -c '{"Args":["init"]}' -P "OR ('Org1MSP.member','Org2MSP.member')"
peer chaincode invoke -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/cacerts/ca.example.com-cert.pem -C mychannel -n marbles02 -c '{"Args":["initMarble","marble2","red","50","tom"]}'
peer chaincode query -C mychannel -n marbles02 -c '{"Args":["readMarble","marble2"]}'
curl http://192.168.134.134:5984/mychannel/marbles02%00marble2 //查询数据库的信息
curl -X PUT http://192.168.100.129:5984/mychannel/marbles02%00marble2 -d '{"_id":"marbles02\u0000marble2","_rev":"1-a1844f47b9ed94294b430c9a9a6f543b","chaincodeid":"marbles02","data":{"docType":"marble","name":"marble2","color":"green","size":10,"owner":"tom"},"version":"6:0"}' //更改其中的颜色信息为green 四个数据库都更改
再次调用查询查看颜色信息:
curl http://192.168.134.134:5984/mychannel/marbles02%00marble2
转帐后在查看颜色信息:peer chaincode invoke -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/cacerts/ca.example.com-cert.pem -C mychannel -n marbles02 -c '{"Args":["transferMarble","marble2","jerry"]}'
peer chaincode query -C mychannel -n marbles02 -c '{"Args":["readMarble","marble2"]}' //显示颜色为green 表示成功篡改fabric中的数据