Running mysql queries from django fabfile
How can I run SQL queries with my fab file below
def allow_webservers_for_db():
for ip in env.web_servers:
run('echo "GRANT ALL ON %s.* TO \'%s\'@\'%s\' IDENTIFIED BY \'%s\'; | mysql --user=%s --password=%s"' % (env.db_schema, env.db_web_user, ip, env.db_password, env.db_user, env.db_password), pty=True)
run('echo "UPDATE db SET host=\'%s\' where db=\'%s\'; | mysql --user=%s --password=%s --database=mysql"' % (ip, env.db_schema, env.db_web_user, env.db_password), pty=True)
run('echo "UPDATE user SET host=\'%s\' where user=\'%s\';| mysql --user=%s --password=%s --database=mysql"' % (ip, env.db_web_user, env.db_user, env.db_password), pty=True)
The code runs without error, but does not do what it is supposed to do. If I copy and paste the code generated with echo
in the mysql terminal mysql>
, the query is correct.
What am I missing here? Do I need to run mysql queries at all? I also don't want to load it from a text file.
source to share
You are just echoing the entire line. But you want to pipe the first part into a pipe in mysql. Delete the last one "
and place it between ;
and |
.
Example for the first line:
run('echo "GRANT ALL ON %s.* TO \'%s\'@\'%s\' IDENTIFIED BY \'%s\';" | mysql --user=%s --password=%s' ....
source to share