Question

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)
Was it helpful?

Solution

Try instead

run('echo "CREATE DATABASE %s;"|mysql --batch --user=%s --password=%s --host=%s' % (dataname, env.mysqluser, env.mysqlpassword, env.mysqlhost), pty=True)

OTHER TIPS

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

run('mysqladmin -u %s -p%s create %s' % (user, password, dbname))

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'";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top