{"id":2009,"date":"2015-02-03T10:03:23","date_gmt":"2015-02-03T10:03:23","guid":{"rendered":"http:\/\/kedar.nitty-witty.com\/?p=2009"},"modified":"2015-02-26T13:57:12","modified_gmt":"2015-02-26T13:57:12","slug":"mysql-grant-syntax-dynamic-database-using-wildcards","status":"publish","type":"post","link":"https:\/\/kedar.nitty-witty.com\/blog\/mysql-grant-syntax-dynamic-database-using-wildcards","title":{"rendered":"MySQL grant syntax &#038; dynamic database using wildcards"},"content":{"rendered":"\n<p>I was recently looking at a MySQL grant script to create default system users after server setup.<br>One of the syntax failed with error and that drew my attention to the details.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''dbname%'.* to 'dbuser'@'%'' at line 1<\/em><\/pre>\n\n\n\n<p><br>The issue above was fixed by correcting the syntax. You shall read below to know more.<\/p>\n\n\n\n<p>A sample mysql grant syntax could be:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">GRANT ...PRIVILEGES ON DB.TABLE TO 'USER'@'HOST' IDENTIFIED BY 'PASSWORD';\n<\/pre>\n\n\n\n<p># There are varients and ways to create user but let&#8217;s consider this for simplicity.<\/p>\n\n\n\n<p>You may refer to the <a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.6\/en\/grant.html\" target=\"_blank\" rel=\"nofollow noopener\">documentation <\/a> for list of MySQL user privileges &amp; the grant syntax itself.<\/p>\n\n\n\n<p>We&#8217;re looking into dynamically specifying MySQL user privileges.<\/p>\n\n\n\n<h6 class=\"wp-block-heading\"><strong>GRANT &#8230;PRIVILEGES ON DB.TABLE TO <span style=\"color: #ff6600;\">&#8216;USER&#8217;@&#8217;HOST&#8217;<\/span> IDENTIFIED BY &#8216;PASSWORD&#8217;;<\/strong><\/h6>\n\n\n\n<p>A user in MySQL is combination of USERNAME and HOSTNAME.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><em><strong>Username@SPECIFIC-HOST<\/strong><\/em><br>&#8216;mysql-user&#8217;@&#8217;localhost&#8217;: User named &#8216;mysql-user&#8217; can connect from localhost<br>&#8216;mysql-user&#8217;@&#8217;192.168.1.1&#8217;: User named &#8216;mysql-user&#8217; can connect from 192.168.1.1<\/li>\n<\/ul>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">GRANT SELECT, INSERT ON mydb.mytbl TO 'someuser'@'somehost';<\/pre>\n\n\n\n<p>This will grant someuser to connect from somehost and the privileges of SELECT &amp; INSERT on mydb.mytbl.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><em><strong>Username@DYNAMIC-HOST<\/strong><\/em><br>&#8216;mysql-user&#8217;@&#8217;192.168.%&#8217;: User named &#8216;mysql-user&#8217; can connect from all IPs from the any IP of network &#8220;192.168&#8221;.<br>&#8216;mysql-user&#8217;@&#8217;%&#8217;: User named &#8216;mysql-user&#8217; can connect from any IP.<\/li>\n<\/ul>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">GRANT SELECT, INSERT ON mydb.mytbl TO 'someuser'@'%';<\/pre>\n\n\n\n<p>This will grant someuser to connect from any machine and the privileges of SELECT &amp; INSERT on mydb.mytbl.<\/p>\n\n\n\n<p>Let&#8217;s see what options we have while specifying MySQL Grants on database objects.<\/p>\n\n\n\n<h6 class=\"wp-block-heading\"><strong>GRANT &#8230;PRIVILEGES ON <span style=\"color: #ff6600;\">DB.TABLE <\/span>TO &#8216;USER&#8217;@&#8217;HOST&#8217; IDENTIFIED BY &#8216;PASSWORD&#8217;;<\/strong><\/h6>\n\n\n\n<ul class=\"wp-block-list\">\n<li><em><strong>ALL DATABASES, ALL TABLES (GRANT &#8230; ON *.*):<\/strong><\/em> The user can connect to server and have specified privileges on all databases, tables. Normally Global privileges are given using this.<\/li>\n\n\n\n<li><em><strong>ONE DATABASE, ALL TABLES (GRANT &#8230; ON DB.*):<\/strong><\/em> The user can enjoy the given privileges on only specified database &#8220;DB&#8221;<\/li>\n\n\n\n<li><em><strong>ONE DATABASE, ONE TABLE (GRANT &#8230; ON DB.TABLE):<\/strong><\/em> The user can only work with one table &#8220;TABLE&#8221; of database &#8220;DB&#8221;<\/li>\n\n\n\n<li><em><strong><span style=\"color: #ff6600;\">DYNAMIC DATABASE<\/span>, ALL TABLES:<\/strong><\/em> The user has the privileges assigned on all the databases that matches the pattern.<\/li>\n<\/ul>\n\n\n\n<p>The case I met was the GRANT where the SQL was written to provide privileges on a dynamic database name and had incorrect syntax.<\/p>\n\n\n\n<p>So yes it is fairly possible to specify a regular expression for a database name while granting privileges to a MySQL user.<\/p>\n\n\n\n<p>So we can grant SELECT permissions on all databases starting with WP to a user as follows:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; GRANT SELECT ON `WP%`.* to 'mysql-user'@'%';\nQuery OK, 0 rows affected (0.00 sec)\n<\/pre>\n\n\n\n<p>Above syntax grants SELECT command on all databases starting with &#8220;WP&#8221; (matching the regexp). <span style=\"text-decoration: underline;\">Note the backtick (`) around &#8220;%&#8221;. <\/span> The escape character (\\) is required for the literal usage of underscore (_) or percentage (%) in the syntax. I corrected those two to resolve said error earlier.<\/p>\n\n\n\n<p>MySQL documentation reads:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">The \"_\" and \"%\" wildcards are permitted when specifying database names in GRANT statements that grant privileges at the global or database levels.<\/pre>\n\n\n\n<p>In above line, it is clear about &#8220;usage of wildcard specifying database names at database level&#8221; but couldn&#8217;t quite understand what MySQL documentation meant about the usage of wildcards <span style=\"text-decoration: underline;\"><em>at global levels<\/em><\/span>!!!<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; grant replication slave on `%`.* to 'test'@localhost;\nERROR 1221 (HY000): Incorrect usage of DB GRANT and GLOBAL PRIVILEGES\n<\/pre>\n\n\n\n<p>Hope this helps.<\/p>\n","protected":false},"excerpt":{"rendered":"The MySQL grant syntax allows you to specify dynamic database names using the wildcard characters. This article explains the usecase of dynamic section of mysql grants.\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[8,377],"tags":[923,922,921,920],"class_list":{"0":"post-2009","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-mysql","7":"category-mysql-articles","8":"tag-dynamic-database-grants","9":"tag-mysql-dynamic-grants","10":"tag-mysql-grants","11":"tag-mysql-wildcards"},"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/2009","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/comments?post=2009"}],"version-history":[{"count":14,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/2009\/revisions"}],"predecessor-version":[{"id":2014,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/posts\/2009\/revisions\/2014"}],"wp:attachment":[{"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/media?parent=2009"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/categories?post=2009"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kedar.nitty-witty.com\/blog\/wp-json\/wp\/v2\/tags?post=2009"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}