Howto to create mysql database from fabric dynamically

Is it possible to create a dynamic mysql database from fabric.

It seems to be stuck in a password hint

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) 
+8
fabric
source share
3 answers

Try instead

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

There is a better way to do this with mysqladmin:

 run('mysqladmin -u %s -p%s create %s' % (user, password, dbname)) 
+21
source share

I use the following one liner through the command line

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

- the key is -e.
if you like a bash script with variables in db / user / pass and run it like. / 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'"; 
0
source share

Source: https://habr.com/ru/post/639904/


All Articles