开发者

howto to create mysql database from fabric dynamically

开发者 https://www.devze.com 2022-12-14 05:45 出处:网络
Is it possible to create mysql database from fabric dynamically. This seems like it ge开发者_如何学运维ts stuck at the password prompt

Is it possible to create mysql database from fabric dynamically.

This seems like it ge开发者_如何学运维ts 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)


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

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


Try instead

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


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'";
0

精彩评论

暂无评论...
验证码 换一张
取 消