![Hyperledger Cookbook](https://wfqqreader-1252317822.image.myqcloud.com/cover/682/36698682/b_36698682.jpg)
上QQ阅读APP看书,第一时间看更新
How to do it...
To use CouchDB, follow these steps:
- Make sure network is not up. If it is up, shut down the network, as shown here:
$ cd fabric-samples/first-network
$ sudo ./byfn.sh down
- Start up the BYFN network using CouchDB:
Here we will start up the network by using the CouchDB database.
$ cd fabric-samples/first-network
$ sudo ./byfn.sh up -c mychannel -s couchdb
Following screenshot shows our network starting up:
![](https://epubservercos.yuewen.com/21D760/19470379601494606/epubprivate/OEBPS/Images/46026d29-66cc-469b-9f6c-096958faff8d.png?sign=1739313563-HkuIk3OIBc2ppoPnIXEVKRpOPTJy2PyR-0-94dfeda59dfefb1667895fa7f187124c)
- Install chaincode by navigating into the CLI container using the command-line interface:
$ sudo docker exec -it cli bash
$ peer chaincode install -n marbles -v 1.0
-p github.com/chaincode/marbles02/go
- Instantiate the chaincode:
$ export CHANNEL_NAME=mychannel
$ peer chaincode instantiate -o orderer.example.com:7050
--tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/
crypto/ordererOrganizations/example.com/orderers/
orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
-C $CHANNEL_NAME -n marbles -v 1.0 -c '{"Args":["init"]}' -P "OR
('Org0MSP.peer','Org1MSP.peer')"
- Invoke the chaincode. The following commands invoke chaincode to create marble.
$ peer chaincode invoke -o orderer.example.com:7050 --tls
--cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/
crypto/ordererOrganizations/example.com/orderers/
orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
-C $CHANNEL_NAME -n marbles -c
'{"Args":["initMarble","marble5","blue","35","tom"]}'
Following screenshot shows successful creation of chanincode:
![](https://epubservercos.yuewen.com/21D760/19470379601494606/epubprivate/OEBPS/Images/cd28a4e1-c072-4cea-a611-de01b5073b0c.png?sign=1739313563-46jjrW943iQIYHTrciF4L9L1TtFdDndv-0-350ff8167acb5365b788064b3aa30f84)
- Open the CouchDB UI by navigating to http://host-ip:5984/_utils/#/_all_dbs (in my case, my AWS public IP address is 3.91.245.92, so the URL is http://3.91.245.92:5984/_utils/#/_all_dbs):
In order to allow public access to CouchDB, we need open port 5984. Navigate to the AWS security group under the instance, launch the wizard, and choose Action | Edit Inbound Rules | Add Inbound Rule. This is shown as follows. After this, click Save. You can follow the below example to allow all IP address to access CouchDB:
![](https://epubservercos.yuewen.com/21D760/19470379601494606/epubprivate/OEBPS/Images/a3e4bbb9-11c5-428a-a483-31ac867e62d8.png?sign=1739313563-Ip7EGB0Yo317o44mkTvngHlJCm0Ilqmo-0-8b2970f75da88b7fe8cf15b93137b7e0)
- From mychannel_marbles, we can query and see the transaction ID with marble5:
![](https://epubservercos.yuewen.com/21D760/19470379601494606/epubprivate/OEBPS/Images/0486d607-d241-4a11-8946-2d8bbc1cf6f6.png?sign=1739313563-n2MqDXybeQPXtg5zXKn5vgUUicDU6g4D-0-66a5d09a1756f1720e00ae1384f0df24)
- Click marble5, and you will see the default marble5 files:
![](https://epubservercos.yuewen.com/21D760/19470379601494606/epubprivate/OEBPS/Images/676d1a18-1413-42ca-beaa-495e3c9dc1cb.png?sign=1739313563-piKk1fEGNLc5ShFbUFEIZZce2mYd5mmb-0-f89d62d0527771b5d83078cf1d6a19d2)
Here, we saw how to use CouchDB to view how transactions get created, and updated them on the Fabric network. We will write a smart contract and deploy it as an application in the next recipe.