RethinkDB is a NoSQL database that stores data as JSON documents. It has a super intuitive query language and has features commonly available in traditional RDBMS like “table joins” and “group by”. This tutorial will explain how to setup a RethinkDB cluster using 3 AKLWEB HOST VPS servers running Ubuntu 14.04.<\/p>\n
Create a new AKLWEB HOST startup script using the following content as a template.<\/p>\n
#!\/usr\/bin\/env bash\r\n\r\necho \">>> Installing RethinkDB<<<\"\r\n\r\n# Add RethinkDB Repo to list of Repos\r\nsource \/etc\/lsb-release && echo \"deb http:\/\/download.rethinkdb.com\/apt $DISTRIB_CODENAME main\" | sudo tee \/etc\/apt\/sources.list.d\/rethinkdb.list\r\nwget -qO- http:\/\/download.rethinkdb.com\/apt\/pubkey.gpg | sudo apt-key add -\r\n\r\n# Update Apt Repo & Install RethinkDB\r\nsudo apt-get update\r\nsudo apt-get install -qq rethinkdb\r\n<\/code><\/pre>\nSpin up a AKLWEB HOST VM instance by picking any\u00a0Server Type<\/em>,\u00a0Location<\/em>, and\u00a0Server Size<\/em>\u00a0of your choice. For the choice of\u00a0Operating System<\/em>, make sure to select\u00a0Ubuntu 14.04 x64<\/strong>\u00a0and check the\u00a0Enable Private Network<\/strong>\u00a0option. Also\u00a0MAKE SURE<\/strong>\u00a0to select the startup script created above before placing your order.<\/p>\nConnect To Your New VM Instance<\/h3>\n
Once your new VM instance is ready, connect to it from a terminal\/command-prompt via SSH.<\/p>\n
ssh root@[aklwebhost-ip-address]\r\n<\/code><\/pre>\nWhen prompted for your password, use the initial password under “server info” tab from AKLWEB HOST admin.<\/p>\n
You can update your password by typing\u00a0passwd<\/code>\u00a0and hitting the\u00a0ENTER<\/strong>\u00a0key. Ubuntu will prompt you for a new password (this way you can choose something that you easily remember).<\/p>\nAccessing Database<\/h3>\n
Start RethinkDB by running command below. Note that we’re “binding to all” so that we don’t run into issues when trying to access RethinkDB’s web admin interface.<\/p>\n
rethinkdb --bind all\r\n<\/code><\/pre>\nOnce RethinkDB is started, you access it either by command-line or a web interface.<\/p>\n
Web Access<\/h4>\n
To access via web, use port\u00a08080<\/strong>\u00a0by typing\u00a0http:\/\/[aklwebhost-ip-address]:8080<\/code>\u00a0in the address bar of your web browser. You should see RethinkDB’s awesome web administration tool.<\/p>\nCommand-Line Access<\/h4>\n
To access via command-line, start a new terminal\/command-prompt session and connect to the server via SSH as explained previously. Then, issue the command\u00a0rethinkdb admin join 127.0.0.1:29015<\/code>. Make sure to use port\u00a029015<\/strong>.<\/p>\nRemove Test Database<\/h3>\n
We will be using the web interface going forward to manipulate the database. Access it by browsing to\u00a0http:\/\/[aklwebhost-ip-address]:8080<\/code>\u00a0and click on the “Tables” link. You will see that RethinkDB already installed a “test” database. Click on the “Data Explorer” link and run below command to delete the “test” database.<\/p>\nr.dbDrop('test')\r\n<\/code><\/pre>\nPlaying Around in Database<\/h3>\n
While still on the “Data Explorer” section of RethinkDB’s admin, create a new database named “tweetDB” by running the code below.<\/p>\n
r.dbCreate('tweetDB')\r\n<\/code><\/pre>\nNext, create a “tweets” table.<\/p>\n
r.db('tweetDB').tableCreate('tweets')\r\n<\/code><\/pre>\nInsert some records into the “tweets” table.<\/p>\n
r.db('tweetDB').table('tweets').insert([\r\n {\r\n name: 'Lami',\r\n twitterHandle: 'mrLami',\r\n message: 'Best cloud hosting on the planet - aklwebhost'\r\n },\r\n {\r\n name: 'AKLWEB HOST',\r\n twitterHandle: '@aklwebhost',\r\n message: '50% off on new instances - coupon - tgif'\r\n }\r\n])\r\n<\/code><\/pre>\nQuery “tweets” table to see results.<\/p>\n
r.db('tweetDB').table('tweets')\r\n<\/code><\/pre>\nYou should see the following under tree view (with different id’s).<\/p>\n
[\r\n {\r\n \"id\": \"6afe436c-7db4-4c86-b4db-3279acb3265d\" ,\r\n \"message\": \"50% off on new instances - coupon - tgif\" ,\r\n \"name\": \"AKLWEB HOST\" ,\r\n \"twitterHandle\": \"@aklwebhost\"\r\n } ,\r\n {\r\n \"id\": \"fd328cd5-d9f4-40ee-8a32-880cd8cda15d\" ,\r\n \"message\": \"Best cloud hosting on the planet - aklwebhost\" ,\r\n \"name\": \"Lami\" ,\r\n \"twitterHandle\": \"mrLami\"\r\n }\r\n]\r\n<\/code><\/pre>\nSetting Up Cluster<\/h3>\n
Spin up a second and third machine using instructions from the\u00a0“Installation”<\/em>,\u00a0“Connect To Your New VM Instance”<\/em>\u00a0and\u00a0“Accessing Database”<\/em>\u00a0sections of this guide. Make sure to delete the “test” database on each of the new instances, and\u00a0DO NOT<\/strong>\u00a0add any new databases to them.<\/p>\nAfter deleting the “test” database from second and third instances, go back to their command prompts and stop RethinkDB by issuing\u00a0Ctrl + C<\/strong>\u00a0(or\u00a0Cmd + C<\/strong>\u00a0on Mac). Now start RethinkDB again on these instances by using the below command (we’re basically telling new second and third instances to join the first).<\/p>\n$ rethinkdb --join [ip-of-first-aklwebhost-vm]:29015 --bind all\r\n<\/code><\/pre>\nOn the second and third instances, access web admin interfaces, and go to the “Data Explorer” section. Run the following query to get a list of tweets.<\/p>\n
r.db('tweetDB').table('tweets')\r\n<\/code><\/pre>\nYou will see that they have already replicated from the master (first VM setup) instance.<\/p>\n
Conclusion<\/h3>\n
Although RethinkDB is still in its infancy at the time of this writing, it holds a lot of promise and has a powerful web-based administration interface that makes arduous tasks usually involved with scaling a database (sharding, replication) easily accomplished with just a few clicks.<\/p>\n
For further reading, visit the\u00a0RethinkDB Docs<\/span><\/a>. Also check out #rethinkdb IRC channel to learn more from the community.<\/p>\nOther Versions<\/h4>\n