{"componentChunkName":"component---src-templates-tag-js","path":"/tags/database/","result":{"data":{"site":{"siteMetadata":{"title":"LoginRadius Blog"}},"allMarkdownRemark":{"totalCount":3,"edges":[{"node":{"fields":{"slug":"/engineering/is-your-database-secured-think-again/"},"html":"<p><strong>1. Secure your server</strong></p>\n<p>Many known attacks are possible only once physically accessing a machine. For this reason, it is best to have the application server and the database server on different machines. If this is not possible, greater care must be taken, Otherwise, by executing remote commands via an application server, an attacker may be able to harm your database even without permissions. For this reason, any service running on the same machine as the database should be granted the lowest possible permission that still allows the service to operate.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 494px; \"\n    >\n      <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 38.8663967611336%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAIAAAB2/0i6AAAACXBIWXMAAAsTAAALEwEAmpwYAAABPUlEQVQY031Q2U7DQAzc//8GfgOEBELiGXGofQlpQumRZnNtstnLewaXvlEJa2RZs56x12S5Chdi0chdr8pGHpnediqnIqdyP+h6grJV+MSNx07SzxrxR29cQFgf0ciHBC4acGCddw7AGrDBoziRshHbTi7/hzVnODhnD0vAQicjSXYc8mqYpEGnGBNm4/xq1z+uq009+YBcWqyiHJ6z5v2bHXrR4tJxSXIk+YHme9qOAru8DzjmqxW3b/v7VbUqjlxCQrHXn/V885TdvW4f1tVHcaJMLnomBeUlnZX1lwUVhAPTGyrwNsdB9sK6kBanqwleyj47TWVvdoNupU1yIsJ4CeeB6Vc8KXcaTcMNyjph2xkAVwQ1s5H1DDEzNnQDH3lSnFxfh2vHpEWgEd78QuIPI6NpaiOrEUkwJH8ANN/IbQHv70QAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"server-network-structure\"\n        title=\"server-network-structure\"\n        src=\"/static/db8fbab272d550b5b8073c8972f51a65/d72d4/server-network-structure.png\"\n        srcset=\"/static/db8fbab272d550b5b8073c8972f51a65/d72d4/server-network-structure.png 494w\"\n        sizes=\"(max-width: 494px) 100vw, 494px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n    </span><br>\nDo not forget to install the whole security package: Antivirus and Anti-spam, Firewall, and all of the security packages recommended by your operating system's vendor. Also, do not forget to spend 10 minutes thinking of your server's physical location - in the wrong location, your server can be stolen, flooded, harmed by wild animals or vagrants.</p>\n<p><strong>2. Localhost Security or Disable or restrict remote access</strong></p>\n<p>Consider whether MySQL will be retrieved from the system or directly accessed from its own server. On the off chance that remote access is utilized, guarantee that just characterized hosts can get to the server. This is commonly done through TCP wrappers, IP tables, or some other firewall programming or hardware accessibility tools.<br>\nTo confine MySQL from opening a network socket, the accompanying parameter ought to be included in the [mysqld] area of my.cnf or my.ini:</p>\n<p>skip-networking</p>\n<p>The document is situated in the <em>\"C:\\Program Files\\MySQL\\MySQL Server 5.1\"</em> catalog on the Windows operating system or <em>\"/etc/my.cnf\"</em> or <em>\"/etc/mysql/my.cnf\"</em> on Linux.<br>\nThis line cripples the start of systems administration in the middle of MySQL startup. It would be ideal if you bear in mind that a local connection can be used set up a connection to the MySQL server.</p>\n<p>Another possible solution is to force MySQL to listen only to the localhost by adding the following line in the <em>[mysqld]</em> section of <em>my.cnf</em>bind-address=127.0.0.1<br>\nYou may not be willing to incapacitate system access to your database server if clients in your organization interface with the server from their machines or the web server introduced on an alternate machine. In that case, the following restrictive grant syntax should be considered:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">mysql&gt; GRANT SELECT, INSERT ON mydb.\\* TO &#39;someuser&#39;@&#39;somehost&#39;;  </span></code></pre>\n<p><strong>3. Disable the use of LOCAL INFILE</strong></p>\n<p>The next change is to disable the use of the <em>\"LOAD DATA LOCAL INFILE\"</em> command, which will help to keep unapproved perusing from neighborhood records. This is particularly vital when new SQL Injection vulnerabilities in PHP applications are found.<br>\nIn addition, in certain cases, the <em>\"LOCAL INFILE\"</em> command can be used to gain access to other files on the operating system, for instance <em>\"/etc/passwd\"</em>, using the following command:  </p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">mysql&gt; LOAD DATA LOCAL INFILE &#39;/etc/passwd&#39; INTO TABLE table1</span></code></pre>\n<p>Or even significantly less difficult:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">mysql&gt; SELECT load\\_file(&quot;/etc/passwd&quot;)</span></code></pre>\n<p>To disable the usage of the <em>\"LOCAL INFILE\"</em> command, the following parameter should be added in the <em>[mysqld]</em> section of the MySQL configuration file.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">set-variable=local-infile=0</span></code></pre>\n<p><strong>4. Change root username and password, keep them strong.</strong></p>\n<p>The default administrator username on the MySQL server is <strong>\"root\"</strong>. Hackers often attempt to gain access to its permissions. To make this task harder, rename <strong>\"root\"</strong> to something else and provide it with a long, complex alphanumeric password.</p>\n<p>To rename the administrator’s username, use the rename command in the MySQL console:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"4\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">mysql&gt; RENAME USER root TO new\\_user;</span></code></pre>\n<p>The MySQL <em>\"RENAME USER\"</em> command first appeared in MySQL version 5.0.2. If you use an older version of MySQL, you can use other commands to rename a user:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"5\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">mysql&gt; use mysql;</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">mysql&gt; update user set user=&quot;new\\_user&quot; where user=&quot;root&quot;;</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">mysql&gt; flush privileges;</span></code></pre>\n<p>To change a user’s password, use the following command-line command:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"6\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">mysql&gt; SET PASSWORD FOR &#39;username&#39;@&#39;%hostname&#39; = PASSWORD(&#39;newpass&#39;);</span></code></pre>\n<p>It is also possible to change the password using the <em>\"mysqladmin\"</em> utility:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"7\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">shell&gt; mysqladmin -u username -p password newpass</span></code></pre>\n<p><strong>5. Remove the \"Test\" database</strong></p>\n<p>MySQL comes with a \"test\" database intended as a test space. It can be accessed by the anonymous user, and is therefore used by numerous attacks.<br>\nTo remove this database, use the drop command as follows:  </p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"8\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">mysql&gt; drop database test;  </span></code></pre>\n<p>Or use the <em>\"mysqladmin\"</em> command:  </p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"9\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">shell&gt; mysqladmin -u username -p drop test  </span></code></pre>\n<p><strong>6. Remove Anonymous and outdated accounts</strong></p>\n<p>The MySQL database comes with some anonymous users with blank passwords. As a result, anyone can connect to the database to check whether this is the case, do the following:  </p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"10\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">mysql&gt; select \\* from mysql.user where user=&quot;&quot;;  </span></code></pre>\n<p>In a secure system, no lines should be echoed back. Another way to do the same:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"11\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">mysql&gt; SHOW GRANTS FOR &#39;&#39;@&#39;localhost&#39;;</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">mysql&gt; SHOW GRANTS FOR &#39;&#39;@&#39;myhost&#39;;</span></code></pre>\n<p>If the grants exist, then anybody can access the database and at least use the default database<em>\"test\"</em>. Check this with:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"12\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">shell&gt; mysql -u blablabla</span></code></pre>\n<p>To remove the account, execute the following command:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"13\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">mysql&gt; DROP USER &quot;&quot;;</span></code></pre>\n<p>The MySQL <em>\"DROP USER\"</em> command is supported starting with MySQL version 5.0. If you use an older version of MySQL, you can remove the account as follows:</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"14\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">mysql&gt; use mysql;</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">mysql&gt; DELETE FROM user WHERE user=&quot;&quot;;</span>\n<span class=\"grvsc-line\"></span>\n<span class=\"grvsc-line\">mysql&gt; flush privileges;  </span></code></pre>\n<p><strong>7. Increase security with Role Based Access Control</strong></p>\n<p>A very common database security recommendation is to lower the permissions given to various parties. MySQL is no different. Typically, when developers work, they use the system's maximum permission and give less consideration to permission principles than we might expect. This practice can expose the database to significant risk.<br>\n* Any new MySQL 5.x installation already installed using the correct security measures.<br>\nTo protect your database, make sure that the file directory in which the MySQL database is actually stored is owned by the user \"mysql\" and the group \"mysql\".</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"15\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">shell&gt;ls -l /var/lib/mysql</span></code></pre>\n<p>In addition, ensure that only the user \"mysql\" and \"root\" have access to the directory <code>/var/lib/mysql</code>.<br>\nThe mysql binaries, which reside under the /usr/bin/ directory, should be owned by \"root\" or the specific system \"mysql\" user. Other users should not have write access to these files.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"16\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">shell&gt;ls -l /usr/bin/my\\*  </span></code></pre>\n<p><strong>8. Keep a check on database privileges</strong></p>\n<p>Operating system permissions were fixed in the preceding section. Now let’s talk about database permissions. In most cases, there is an administrator user (the renamed \"root\") and one or more actual users who coexist in the database. Usually, the \"root\" has nothing to do with the data in the database; instead, it is used to maintain the server and its tables, to give and revoke permissions, etc.<br>\nOn the other hand, some user ids are used to access the data, such as the user id assigned to the web server to execute \"select\\update\\insert\\delete\" queries and to execute stored procedures. In most cases, no other users are necessary; however, only you, as a system administrator can really know your application’s needs.</p>\n<p>Only administrator accounts needs to be granted the SUPER / PROCESS /FILE privileges and access to the mysql database. Usually, it is a good idea to lower the administrator’s permissions for accessing the data.</p>\n<p>Review the privileges of the rest of the users and ensure that these are set appropriately. This can be done using the following steps.  </p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"17\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">mysql&gt; use mysql;  </span></code></pre>\n<p>[Identify users]  </p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"18\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">mysql&gt; select \\* from users;  </span></code></pre>\n<p>[List grants of all users]  </p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"19\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">mysql&gt; show grants for ‘root’@’localhost’;</span></code></pre>\n<p>The above statement has to be executed for each user ! Note that only users who really need root privileges should be granted them.</p>\n<p>Another interesting privilege is \"SHOW DATABASES\". By default, the command can be used by everyone having access to the MySQL prompt. They can use it to gather information (e.g., getting database names) before attacking the database by, for instance, stealing the data. To prevent this, it is recommended that you follow the procedures described below.</p>\n<ul>\n<li>Add \" --skip-show-database\" to the startup script of MySQL or add it to the MySQL configuration file</li>\n<li>Grant the SHOW DATABASES privilege only to the users you want to use this command</li>\n</ul>\n<p>To disable the usage of the \"SHOW DATABASES\" command, the following parameter should be added in the [mysqld] section of the <code>/etc/my.cnf</code>:</p>\n<p>[mysqld]</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"20\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">skip-show-database  </span></code></pre>\n<p><strong>9. Enable Logging</strong></p>\n<p>If your database server does not execute many queries, it is recommended that you enable transaction logging, by adding the following line to [mysqld] section of the <code>/etc/my.cnf</code> file:</p>\n<p>[mysqld]</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"21\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">log =/var/log/mylogfile  </span></code></pre>\n<p>This is not recommended for heavy production MySQL servers because it causes high overhead on the server.<br>\nIn addition, verify that only the \"root\" and \"mysql\" ids have access to these logfiles (at least write access).</p>\n<p><strong>Error log</strong>Ensure only \"root\" and \"mysql\" have access to the log file \"hostname.err\". The file is stored in the mysql data directory. This file contains very sensitive information such as passwords, addresses, table names, stored procedure names and code parts. It can be used for information gathering, and in some cases, can provide the attacker with the information needed to exploit the database, the machine on which the database is installed, or the data inside it.</p>\n<p><strong>MySQL log</strong>Ensure only \"root\" and \"mysql\" have access to the logfile \"logfile XY\". The file is stored in the mysql data directory.</p>\n<p><strong>10. Change the root directory</strong> </p>\n<p>A chroot on UNIX {operating system} operating systems is an operation that changes the apparent disk root directory for the present running method and its children. A program that's re-rooted to a different directory cannot access or name files outside that directory, and therefore the directory is named a \"chroot jail\" or (less commonly) a \"chroot prison\".</p>\n<p>By using the chroot environment, the write access of the mySQL processes (and child processes) can be limited, increasing the security of the server.</p>\n<p>Ensure that a dedicated directory exists for the chrooted environment. This should be something like: <code>/chroot/mysql</code> In addition, to make the use of the database administrative tools convenient, the following parameter should be changed in the [client] section of MySQL configuration file:</p>\n<p>[client]</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"22\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">socket = /chroot/mysql/tmp/mysql.sock</span></code></pre>\n<p>Thanks to that line of code, there will be no need to supply the mysql, mysqladmin, mysqldump etc. commands with the <code>--socket=/chroot/mysql/tmp/mysql.sock</code> parameter every time these tools are run.</p>\n<p><strong>11. Delete old logs regularly</strong></p>\n<p>During the installation procedures, there's plenty of sensitive data which will assist unwelcome users to assault a database. This data is kept within the server’s history and might be terribly useful if one thing goes wrong during the installation. By analyzing the history files, administrators can figure out what has gone wrong and probably fix things up. However, these files are not needed after installation is complete.<br>\nWe should remove the content of the MySQL history file (~/.mysql_history), wherever all dead SQL commands are kept (especially passwords, that are kept as plain text):</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"23\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">cat /dev/null &gt; ~/.mysql\\_history</span></code></pre>\n<p>In conclusion,we should emphasize on database security. However it should be the first thing for any individual or a company.</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n</style>","frontmatter":{"date":"February 23, 2016","updated_date":null,"title":"Is Your Database Secured? Think Again","tags":["Database","Security"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1,"src":"/static/6a50b3ff2cea92449f2f6ab9856c8a0a/630fb/database_secure-300x300.png","srcSet":"/static/6a50b3ff2cea92449f2f6ab9856c8a0a/69585/database_secure-300x300.png 200w,\n/static/6a50b3ff2cea92449f2f6ab9856c8a0a/630fb/database_secure-300x300.png 300w","sizes":"(max-width: 300px) 100vw, 300px"}}},"author":{"id":"Kunal","github":"SuperKunal","avatar":null}}}},{"node":{"fields":{"slug":"/engineering/index-in-mongodb/"},"html":"<p> Index is a typical way to speed-up queries in normal database system. There is no difference between MongoDB and a document-based database system. This article gives insight about the index in MongoDB, for query optimization.</p>\n<h3 id=\"index-in-mongo\" style=\"position:relative;\"><a href=\"#index-in-mongo\" aria-label=\"index in mongo permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Index in Mongo:</h3>\n<h4 id=\"default\" style=\"position:relative;\"><a href=\"#default\" aria-label=\"default permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Default</h4>\n<p>_id is an ObjectId object, 12-byte BSON type that guarantees uniqueness within the collection. The ObjectId is generated based on timestamp, machine ID, process ID, and a process-local incremental counter.</p>\n<h4 id=\"single-field\" style=\"position:relative;\"><a href=\"#single-field\" aria-label=\"single field permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Single Field</h4>\n<p>For a single-field index and sort operations, the sort order (i.e. ascending or descending) of the index key does not matter because MongoDB can traverse the index in either direction. The value of index is the type of index. For example, 1 indicates ascending order and -1 specifies the descending order.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"0\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">db.friends.createIndex( { &quot;name&quot; : 1 } )</span></code></pre>\n<h4 id=\"\" style=\"position:relative;\"><a href=\"#\" aria-label=\" permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a></h4>\n<p>Compound Field</p>\n<p>The order of fields listed in a compound index has significance. For instance, if a compound index consists of { userid: 1, score: -1 }, the index sorts first by userid and then, within each userid value, sorts by score.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"1\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">db.products.createIndex( { &quot;item&quot;: 1, &quot;stock&quot;: 1 } )</span></code></pre>\n<h4 id=\"-1\" style=\"position:relative;\"><a href=\"#-1\" aria-label=\" 1 permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a></h4>\n<p>Multiple Key</p>\n<p>MongoDB uses multiple index to index the content in an array. MongoDB creates separate index entries for every element of the array. You do not need explicitly create multiple key.</p>\n<h4 id=\"text-index\" style=\"position:relative;\"><a href=\"#text-index\" aria-label=\"text index permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Text Index</h4>\n<p>A collection can have at most one text index.<br>\nPerformance cost for text index:<br>\ntext indexes can be large. They contain one index entry for each unique post-stemmed word in each indexed field for each document inserted.<br>\ntext indexes will impact insertion throughput because MongoDB must add an index entry for each unique post-stemmed word in each indexed field of each new source document.</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"2\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">db.reviews.createIndex( { comments: &quot;text&quot; } )</span></code></pre>\n<h4 id=\"hash-index\" style=\"position:relative;\"><a href=\"#hash-index\" aria-label=\"hash index permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Hash index</h4>\n<p>Query content by its hashed value. The hash is a function to computed by its value. The hashed value is designed to be distinct value. The one advantage is it is so quick, which take O(1) at most but by contract the normal binary search tree will take O(Log(N)). Hash will be theoretically quicker than normal binary search tree implementation. But the disadvantage is hash index performing range search will be extremely slowly than normal index.</p>\n<p>This an example in python to build a hash index</p>\n<pre class=\"grvsc-container dark-default-dark\" data-language=\"\" data-index=\"3\"><code class=\"grvsc-code\"><span class=\"grvsc-line\">db.active.createIndex( { a: &quot;hashed&quot; } )</span></code></pre>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n  .dark-default-dark {\n    background-color: #1E1E1E;\n    color: #D4D4D4;\n  }\n</style>","frontmatter":{"date":"September 01, 2015","updated_date":null,"title":"Index in MongoDB","tags":["MongoDB","Database"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1.5037593984962405,"src":"/static/37f07a89ba1a1b4fd56cbd7c0548d86a/ee604/mongo-db-index1.png","srcSet":"/static/37f07a89ba1a1b4fd56cbd7c0548d86a/69585/mongo-db-index1.png 200w,\n/static/37f07a89ba1a1b4fd56cbd7c0548d86a/497c6/mongo-db-index1.png 400w,\n/static/37f07a89ba1a1b4fd56cbd7c0548d86a/ee604/mongo-db-index1.png 800w,\n/static/37f07a89ba1a1b4fd56cbd7c0548d86a/f3583/mongo-db-index1.png 1200w,\n/static/37f07a89ba1a1b4fd56cbd7c0548d86a/196bc/mongo-db-index1.png 1278w","sizes":"(max-width: 800px) 100vw, 800px"}}},"author":{"id":"Mark Duan","github":null,"avatar":null}}}},{"node":{"fields":{"slug":"/engineering/relational-database-management-system-rdbms-vs-nosql/"},"html":"<p>Today in the market various type of Database options are available like RDBMS, NoSQL, Big Data, Database Appliance, etc. developers can get very confused with all the choice. They do not understand why they should consider a newer, alternative database when RDBMSs have been around for 25+ years. However, many big enterprises are already using alternative databases and are saving money, innovating more quickly, and completing projects.</p>\n<p><strong>Relational Database Management System (RDBMS)</strong></p>\n<p>RDBMS Database is a relational database. It is the standard language for relational database management systems.Data is stored in the form of rows and columns in RDBMS. The relations among tables are also stored in the form of the table SQL (Structured query Language) is a programming language used to perform tasks such as update data on a database, or to retrieve data from a database. Some common relational database management systems that use SQL are: Oracle, Sybase, Microsoft SQL Server, Access, etc.</p>\n<p><strong>Features Of RDBMS</strong></p>\n<ol>\n<li>SQL databases are table based databases</li>\n<li>Data store in rows and columns</li>\n<li>Each row contains a unique instance of data for the categories defined by the columns.</li>\n<li>Provide facility primary key, to uniquely identify the rows</li>\n</ol>\n<p><strong>Limitations for SQL database</strong></p>\n<p><strong>Scalability</strong>: Users have to scale relational database on powerful servers that are expensive and difficult to handle. To scale relational database it has to be distributed on to multiple servers. Handling tables across different servers is difficult .</p>\n<p><strong>Complexity</strong>: In SQL server’s data has to fit into tables anyhow. If your data doesn’t fit into tables, then you need to design your database structure that will be complex and again difficult to handle.</p>\n<p><strong>NoSQL</strong></p>\n<p>NoSQL commonly referred to as “Not Only SQL”. With NoSQL, unstructured ,schema less data can be stored in multiple collections and nodes and it does not require fixed table sachems, it supports limited join queries , and we scale it horizontally.</p>\n<p><strong>Benefits of NoSQL</strong></p>\n<p><strong>highly and easily scalable</strong></p>\n<p>Relational database or RDBMS databases are vertically Scalable When load increase on RDBMS database then we scale database by increasing server hardware power ,need to by expensive and bigger servers and NoSQL databases are designed to expand horizontally and in Horizontal scaling means that you scale by adding more machines into your pool of resources.</p>\n<p><strong>Maintaining NoSQL Servers is Less Expensive</strong></p>\n<p>Maintaining high-end RDBMS systems is expensive and need trained manpower for database management but NoSQL databases require less management. it support many Features like automatic repair, easier data distribution, and simpler data models make administration and tuning requirements lesser in NoSQL.</p>\n<p><strong>Lesser Server Cost and open-Source</strong></p>\n<p>NoSQL databases are cheap and open source. NoSql database implementation is easy and typically uses cheap servers to manage the exploding data and transaction while RDBMS databases are expensive and it uses big servers and storage systems. So the storing and processing data cost per gigabyte in the case of NoSQL can be many times lesser than the cost of RDBMS.</p>\n<p><strong>No Schema or Fixed Data model</strong></p>\n<p>NoSQL database is schema less so Data can be inserted in a NoSQL database without any predefined schema. So the format or data model can be changed any time, without application disruption.and change management is a big headache in SQL.</p>\n<p><strong>Support Integrated Caching</strong></p>\n<p>NoSQL database support caching in system memory so it increase data output performance and SQL database where this has to be done using separate infrastructure.</p>\n<p><strong>Limitations &#x26; disadvantage of NoSQL</strong></p>\n<ol>\n<li>NoSQL database is Open Source and Open Source at its greatest strength but at the same time its greatest weakness because there are not many defined standards for NoSQL databases, so no two NoSQL databases are equal</li>\n<li>No Stored Procedures in mongodb (NoSql database).</li>\n<li>GUI mode tools to access the database is not flexibly available in market</li>\n<li>too difficult for finding nosql experts because it is latest technology and NoSQL developer are in learning mode</li>\n</ol>\n<p><strong>Conclusion</strong></p>\n<p>RDBMS and NoSQL both dbs are great in data management and both are used to keep data storage and retrieval optimized and smooth. It’s hard to say which technology is better so developer take decision according requirement and situations</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n  }\n  \n  .grvsc-code {\n    display: inline-block;\n    min-width: 100%;\n  }\n  \n  .grvsc-line {\n    display: inline-block;\n    box-sizing: border-box;\n    width: 100%;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-line-highlighted {\n    background-color: var(--grvsc-line-highlighted-background-color, transparent);\n    box-shadow: inset var(--grvsc-line-highlighted-border-width, 4px) 0 0 0 var(--grvsc-line-highlighted-border-color, transparent);\n  }\n  \n</style>","frontmatter":{"date":"April 28, 2015","updated_date":null,"title":"RDBMS vs NoSQL","tags":["Database"],"coverImage":{"childImageSharp":{"fluid":{"aspectRatio":1,"src":"/static/27a4b3387143bba7cab9a1b54834454d/7d145/rdbms-vs-nosql.png","srcSet":"/static/27a4b3387143bba7cab9a1b54834454d/69585/rdbms-vs-nosql.png 200w,\n/static/27a4b3387143bba7cab9a1b54834454d/497c6/rdbms-vs-nosql.png 400w,\n/static/27a4b3387143bba7cab9a1b54834454d/7d145/rdbms-vs-nosql.png 610w","sizes":"(max-width: 610px) 100vw, 610px"}}},"author":{"id":"Team LoginRadius","github":"LoginRadius","avatar":null}}}}]}},"pageContext":{"tag":"Database"}},"staticQueryHashes":["1171199041","1384082988","2100481360","23180105","528864852"]}