{"id":2843,"date":"2022-09-01T11:59:29","date_gmt":"2022-09-01T11:59:29","guid":{"rendered":"https:\/\/www.checkmateq.com\/blog\/?p=2843"},"modified":"2023-08-04T16:15:36","modified_gmt":"2023-08-04T16:15:36","slug":"cockroachdb-working","status":"publish","type":"post","link":"https:\/\/www.checkmateq.com\/blog\/cockroachdb-working","title":{"rendered":"Cockroach DB Working and Authorization"},"content":{"rendered":"<p>In this blog you can learn how data is stored in Cockroach DB and how to authorize users to create or manipulate data, if you are completely new to Cockroach DB, please refer this blog first for <a href=\"https:\/\/www.checkmateq.com\/blog\/installing-cockroachdb-cluster\/\">installing and creating a cockroach dB\u00a0<\/a><span style=\"color: #0000ee;\"><u>cluster.<\/u><\/span><\/p>\n<p>Above given Image is basic architecture of how data is written and stored in a Cockroach DB Node, Let&#8217;s understand what each thing is and how they work together<\/p>\n<p>Even though Cockroach DB uses its own <a href=\"https:\/\/www.checkmateq.com\/full-stack-development\">SQL<\/a> feature set, SQL Layer helps developers to run SQL queries, this allows them to use all the familiar concepts like schema, tables, and indexes.<\/p>\n<p><strong>Distributed Key-Value Store<\/strong>:\u00a0 when you store data in a table of Cockroach DB it stores that data as a key and a value, SQL layer communicates with the distributed key-value store which allows user to develop large tables and indexes.<\/p>\n<p><strong>Distributed\u00a0<\/strong><b>Transactions<\/b> are\u00a0the important part of this application, their implementation of this feature manages the transition from SQL to stores and ranges.<\/p>\n<p><b>Nodes<\/b> are the servers that store your data, a node can be either virtual or physical machines<\/p>\n<p>A <strong>Node<\/strong> may have one or more stores, and each store can hold many ranges, and these are managed by Rocks DB.<\/p>\n<p>As mentioned above you can treat Cockroach DB as the\u00a0 SQL database , that means you can connect your <strong>app or SQL client<\/strong> to Cockroach DB and do normal SQL stuff with it , The node that the app or client connects to is called the<strong> Gateway Node<\/strong> , and the machine that Cockroach DB\u00a0 node runs on will obviously have\u00a0 characteristics like CPU, memory and number of cores , and the layer that the APP\/SQL Client connects to is the <strong>SQL<\/strong><strong> Layer<\/strong> , this layer creates logical\u00a0 plans and\u00a0 sends them to the transaction and distribution layers , the <strong>distribution layer<\/strong> maps the SQL statements in to key-value pairs in the form of 64 MB chunks of data\u00a0 called ranges\u00a0 and writes it to the disk .<\/p>\n<h3 class=\"post-title-main\">Authorization in Cockroach DB<\/h3>\n<p>Authorization in Cockroach DB is done using <strong>Users and\u00a0<\/strong><b>Roles,<\/b> its controlling over <b>who <\/b>can\u00a0do <strong>what <\/strong>like read, write, update, delete, grant operations and on which resources like databases, tables, clusters, schemas, rows, users.\u00a0 Cockroach DB&#8217;s authorization is governed by the same policies in different scenarios like accessing the SQL shell or viewing data from the database console.<\/p>\n<h3 id=\"users-and-roles\" class=\"clickable-header top-level-header\">Users and Roles in Cockroach DB<\/h3>\n<p>There is no much technical differentiation between Cockroach DB &#8216;s role and user, A role\/user can be permitted to log in to the SQL shell, granted privileges to perform specific actions and database objects, A privilege assigned is referred as a &#8220;role&#8221; when it&#8217;s created for managing the privileges of the &#8220;users&#8221; and not for logging in directly, which is reserved for &#8220;users&#8221;.<\/p>\n<p>As mentioned above SQL statements <strong>CREATE USER<\/strong> and <strong>CREATE ROLE<\/strong> will almost create the same thing but\u00a0 with one difference CREATE<b> ROLE<\/b> will use NOLOGIN\u00a0option by default, restricting the user or role from being used to log in.<\/p>\n<h3>Creating a user for Cockroach DB<\/h3>\n<p>In one of your cockroach node terminals use the below given command to open the built in SQL shell<\/p>\n<pre>cockroach sql --certs-dir=certs --host=localhost:26257<\/pre>\n<p>give your certs directory name for <strong>&#8211;certs-dir\u00a0<\/strong>and node host name for &#8211;host.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-2896 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-cloud-solutions--300x72.jpeg\" alt=\"\" width=\"799\" height=\"192\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-cloud-solutions--300x72.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-cloud-solutions-.jpeg 1778w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Now create a user and give a password for authentication.<\/p>\n<pre>CREATE USER masteruser WITH PASSWORD 'Nalmas123';<\/pre>\n<p>here <strong>masteruser<\/strong> is a username and <strong>Nalmas123<\/strong> is password for the user<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-2897 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-cloud-Infrastructure-services--300x65.jpeg\" alt=\"\" width=\"808\" height=\"175\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-cloud-Infrastructure-services--300x65.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-cloud-Infrastructure-services--1024x223.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-cloud-Infrastructure-services--1200x262.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-cloud-Infrastructure-services-.jpeg 1384w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>Check user creation using the command &#8220;<strong>SHOW USERS;<\/strong>&#8221;<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-2920 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-server-support-300x70.jpeg\" alt=\"\" width=\"866\" height=\"202\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-server-support-300x70.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-server-support-1024x240.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-server-support-768x180.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-server-support-1536x359.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-server-support-1200x281.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-server-support.jpeg 1817w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<h3><strong>How to Grant Privileges to a User?<\/strong><\/h3>\n<pre>GRANT admin TO masteruser;<\/pre>\n<p>Above given command gives administrative access to user named <strong>masteruser\u00a0<\/strong>and <strong>admin<\/strong> is a pre predefined role here.<\/p>\n<p>GRANT is a statement that\u00a0you can use it to directly give privileges to a role or user, users granted a privilege\u00a0with <strong>GRANT<\/strong><b> <\/b>option can pass on that privilege to others, owner of an object will have the\u00a0 GRANT OPTION for all privileges by default.<\/p>\n<p>Cockroach DB checks the user&#8217;s and role&#8217;s permissions for each statement executed If the user does not have required permission to perform a particular operation\u00a0 \u00a0Cockroach DB gives an error.<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-2898 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Hire-Checkmate-DevOps-engineer-300x66.jpeg\" alt=\"\" width=\"868\" height=\"191\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Hire-Checkmate-DevOps-engineer-300x66.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Hire-Checkmate-DevOps-engineer-1024x226.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Hire-Checkmate-DevOps-engineer-768x170.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Hire-Checkmate-DevOps-engineer-1200x265.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Hire-Checkmate-DevOps-engineer.jpeg 1377w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<h3><\/h3>\n<h3><strong>Creating a New Role<\/strong><\/h3>\n<p>Now let&#8217;s create a new role that can create and rename databases.<\/p>\n<pre>CREATE ROLE can_create_database WITH CREATEDB;<\/pre>\n<p>SQL statement\u00a0 CREATE ROLE \u00a0creates\u00a0 roles, which are groups containing number of other users and roles as a member, you can assign privileges to roles and all members of the role even though if they are direct or indirect members.<\/p>\n<p>and<strong> can_create_database<\/strong> is the role that is being created with <strong>CREATEDB<\/strong> privileges, and use SHOW ROLES; to check the creation and to see already existing roles and users,<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-2922 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Screenshot-820-300x106.png\" alt=\"\" width=\"852\" height=\"301\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Screenshot-820-300x106.png 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Screenshot-820-1024x363.png 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Screenshot-820-1536x545.png 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Screenshot-820-1200x426.png 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Screenshot-820.png 1696w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p>&nbsp;<\/p>\n<h3>Granting New Privileges for <span style=\"font-size: 18.72px;\">an<\/span> Existing Role<\/h3>\n<pre>GRANT admin TO can_create_database;<\/pre>\n<p>can_create_database is a role that we have created, and admin is a privilege which give administrative access<\/p>\n<p><img loading=\"lazy\" class=\" wp-image-2924 aligncenter\" src=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-cloud-operation-management--300x140.jpeg\" alt=\"\" width=\"836\" height=\"390\" srcset=\"https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-cloud-operation-management--300x140.jpeg 300w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-cloud-operation-management--1024x478.jpeg 1024w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-cloud-operation-management--768x359.jpeg 768w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-cloud-operation-management--1536x718.jpeg 1536w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-cloud-operation-management--1200x561.jpeg 1200w, https:\/\/www.checkmateq.com\/blog\/wp-content\/uploads\/2022\/09\/Checkmate-cloud-operation-management-.jpeg 1567w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/p>\n<p>As you can see in above given snapshot role <strong>can_create_database <\/strong>is a member of <strong>admin <\/strong> now.<\/p>\n<p>&nbsp;<\/p>\n<p>To learn SQL statement refer this <a href=\"https:\/\/www.checkmateq.com\/blog\/mysql-commands\/\">blog<\/a><\/p>\n<p>Please <a href=\"https:\/\/www.checkmateq.com\/\">contact<\/a> our technical consultants if you have anything related to <a href=\"https:\/\/www.checkmateq.com\/cloud\">cloud<\/a> infrastructure to be discussed.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog you can learn how data is stored in Cockroach DB and how to authorize users to create or manipulate data, if you are completely new to Cockroach DB, please refer this blog first for installing and creating a cockroach dB\u00a0cluster. Above given Image is basic architecture of how data is written and &hellip; <a href=\"https:\/\/www.checkmateq.com\/blog\/cockroachdb-working\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Cockroach DB Working and Authorization&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":2910,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[3,79,2,71,70,69,68,59,11,78,63,23,16,24,80,8],"_links":{"self":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/2843"}],"collection":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/comments?post=2843"}],"version-history":[{"count":17,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/2843\/revisions"}],"predecessor-version":[{"id":4248,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/posts\/2843\/revisions\/4248"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media\/2910"}],"wp:attachment":[{"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/media?parent=2843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/categories?post=2843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.checkmateq.com\/blog\/wp-json\/wp\/v2\/tags?post=2843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}