9

Is it possible to create mysql database from fabric dynamically.

This seems like it gets stuck at the password prompt

run('mysql -u %s -p %s -h %s  ' % (env.mysqluser, env.mysqlpassword, env.mysqlhost), pty=True)
run('CREATE DATABASE %s; ' % (dataname), pty=True)
run('exit', pty=True)

3 Answers 3

21

There's a better way of doing this using mysqladmin:

run('mysqladmin -u %s -p%s create %s' % (user, password, dbname))
1
  • Won't that save the password in the shell history log?
    – ioan
    Commented Mar 15, 2017 at 11:37
5

Try instead

run('echo "CREATE DATABASE %s;"|mysql --batch --user=%s --password=%s --host=%s' % (dataname, env.mysqluser, env.mysqlpassword, env.mysqlhost), pty=True)
5
  • 5
    Won't that save the password in the shell history log? Commented Dec 20, 2010 at 17:17
  • @Stavros Considering these are open tools which are freely available, you should probably go to the trouble of checking yourself rather than asking me. Commented Dec 21, 2010 at 12:44
  • 14
    Considering that my checking and not seeing it there will not prove that it doesn't get stored anywhere, the better solution is to ask you (as you might know more about this) rather than assume I checked every place such a critical security hole might manifest in, not to mention that not everyone who sees this might be inclined to go to the trouble of checking for themselves. Thanks for answering, though (and, for the record, I didn't see the password or the command in the shell log). Commented Dec 24, 2010 at 23:33
  • 1
    Even though the password is not stored in the shell history log, this does expose the password through such simple tools as "ps" (if you manage to time your attack correctly). So the security aware are well advised to steer away from this solution. Commented Jan 9, 2013 at 22:09
  • In fact, this method is mentioned at dev.mysql.com/doc/refman/5.5/en/password-security-user.html as "convenient, but insecure", together with alternatives that are better Commented Jan 9, 2013 at 22:13
0

I use the following one liner via command line

mysql -uroot -prootpassword -e "CREATE DATABASE dbname";

key is the -e switch.
if you like to have bash script with variables in db/user/pass and run it as ./myscript then

#!/bin/bash
DB="mydb"
USER="user1"
PASS="pass_bla"

mysql -uroot -prootpassword -e "CREATE DATABASE $DB CHARACTER SET utf8 COLLATE utf8_general_ci";
mysql -uroot -prootpassword -e "CREATE USER $USER@'127.0.0.1' IDENTIFIED BY '$PASS'";
mysql -uroot -prootpassword -e "GRANT SELECT, INSERT, UPDATE ON $DB.* TO '$USER'@'127.0.0.1'";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.