1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

couchdb

Posted at

實作平台 cloud9, OS Ubuntu 14

install couchDB

ref: http://wiki.apache.org/couchdb/Installing_on_Ubuntu

sudo apt-get install couchdb -y

Running CouchDB on a Cloud9 workspace

ref: https://docs.c9.io/v1.0/docs/setting-up-couchdb

sudo mkdir -p /var/run/couchdb
sudo chown couchdb:couchdb /var/run/couchdb 
sudo su couchdb -c /usr/bin/couchdb

couchDB web IDE on cloud9

ref: https://github.com/romannep/c9couch

set password

ref:

$ curl -X PUT http://127.0.0.1:5984/_config/admins/lighter -d '"1234"'

with php curl

ref: http://inchoo.net/dev-talk/couchdb-for-php-developers-crud/

check couchdb is run

echo '<h1>check db is run</h1>';
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1:5984/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
	'Content-type: application/json',
	'Accept: */*'
));

$response = curl_exec($ch);

var_dump($response);

curl_close($ch);

get database list

echo '<h1>get database list</h1>';
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1:5984/_all_dbs');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
	'Content-type: application/json',
	'Accept: */*'
));

$response = curl_exec($ch);

var_dump($response);
curl_close($ch);

create user

echo '<h1>create user</h1>';
$data = [
    "_id" => "org.couchdb.user:lighter",
    "type" => "user",
    "name" => "lighter",
    "roles" => [],
    "password_sha" => "b9e4dc549d020913569a0969cace3e131860ce38",
    "salt" => "1"
];

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1:5984/_users');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
	'Content-type: application/json',
	'Accept: */*'
));

$response = curl_exec($ch);
var_dump($response);

curl_close($ch);

create database

echo '<h1>create database</h1>';

$table = 'testtable';
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1:5984/'.$table);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, 'lighter:1234');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
	'Content-type: application/json',
	'Accept: */*'
));

$response = curl_exec($ch);
var_dump($response);

curl_close($ch);

get document

echo '<h1>get document</h1>';
$ch = curl_init();

$documentID = 'lighter';

curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1:5984/customers/'.$documentID);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
	'Content-type: application/json',
	'Accept: */*'
));

curl_setopt($ch, CURLOPT_USERPWD, 'lighter:1234');

$response = curl_exec($ch);
var_dump($response);

curl_close($ch);

update exist document

echo '<h1>update exist document</h1>';

$ch = curl_init();

$customer = array(
	'firstname' => 'FirstNameChange',
	'lastname' => 'LastNameChange',
	'username' => 'lighter',
	'email' => 'branko.ajzele@example.com',
	'pass' => md5('myPass123')
);

// use _rev to update 
$customer['_rev'] = '1-e4d11c20e5de3d100a82d4f14697bed3';

$payload = json_encode($customer);

curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1:5984/customers/'.$customer['username']);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); /* or PUT */
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
	'Content-type: application/json',
	'Accept: */*'
));

curl_setopt($ch, CURLOPT_USERPWD, 'lighter:1234');

$response = curl_exec($ch);
var_dump($response);
curl_close($ch);
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?