0

Is there anyway to do the same with prefix underscore with same username.

For example :-

#!/bin/bash
db_user=devdb
db_pass=`openssl rand -hex 16`
mysql -u root <<-EOF
CREATE USER '$db_user'@'localhost' IDENTIFIED BY '$db_pass';
GRANT ALL PRIVILEGES ON  `$db_user\_%` . * TO  '$db_user'@'localhost';
FLUSH PRIVILEGES;
EOF

In above script GRANT ALL PRIVILEGES giving error and i want solution for that.

If i am using below command without variables directory in mysql root then no error.

GRANT ALL PRIVILEGES ON  `devdb\_%` . * TO  'devdb'@'localhost';

Please reply if anyone have solution to this.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
voidead
  • 1
  • 4
  • What error is `GRANT ALL PRIVILEGES` giving ? – Zak Apr 23 '20 at 16:14
  • Also -- Does the database exist before you `GRANT ALL PRIVILEGES` ? – Zak Apr 23 '20 at 16:19
  • ERROR 1064 (42000) at line 3: 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 '* TO 'devdb'@'localhost'' at line 1 – voidead Apr 23 '20 at 16:20
  • That might be the `backslash` being interpreted by the shell, add one more to disable it's special feature. – Jetchisel Apr 23 '20 at 21:27

1 Answers1

0

Try using double quotes instead of single quotes for variables.

Refer: Difference between single and double quotes in Bash

Ankush
  • 573
  • 7
  • 11
  • Then what I guess is happening is that in your here-string you are directly passing the values to mysql shell one by one (In this case mysql will not expand the variable and will take them literally as $db_user). Try once without using the here-string to confirm. – Ankush Apr 23 '20 at 16:47