howto to create mysql database from fabric dynamically - mysql

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)

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

Related

Redirecting stdout to mysql in bash

I can execute mysql passing in a file as follows.
mysql -u username -p < some_file
In a bash script I have a function which echoes output which I want to pass into the same command in a bash script.
some_function() {
echo "Some SQL"
}
How can I pass the output into mysql using pipes/redirection?
I have tried the following, but it fails with no such file or directory. How can I use the output from the function here instead.
mysql -u username -p < some_function
No need to use a pipe or a redirection in this case, you can use directly -e options to execute some SQL commands:
mysql -u username -p -e "SQL/MySQL commands"
Exemple on a specific database:
mysql -u username -p -e "use database_name; SHOW tables"
mysql -u username -p -e "SHOW tables" database_name
And you can also catch the output of a command or function and passing it as argument like this:
sql_command="$(your_function)"
mysql -u username -p -e "${sql_command}" database_name;
If you really want to use a pipe or a redirection (but I think it make no sense in this case):
$ mysql -u root -p database_name < <(echo "SHOW TABLES") # redirection
$ mysql -u root -p database_name <<< "$(echo "SHOW TABLES")" # another way to use redirection
$ echo "SHOW TABLES"|mysql -u root -p database_name # pipe

mysql bash escaping my single quotes

I am trying to execute a mysql query from a shell script on Ubuntu 16. Let's call the variable $thing.
Shell script
thing=stuff.whatever
mysql -h host -u superuser -ppassword123 --database bestdbever -e "update table set column1=1 where column2='$thing';"
where gets sent is this....
mysql -h host -u superuser -ppassword123 --database bestdbever -e "update table set column1=1 where column2='\''stuff.whatever'\'';'
When escaping the single quote, it sends
mysql -h host -u superuser -ppassword123 --database bestdbever -e "update table set column1=1 where column2=\'\''stuff.whatever\'\'';'
and then I cry.

combining bash and SQL scripting to create wordpress sites

I am writing a script that will run on a Ubuntu 16.04.1 server. The purpose of the script is to install a wordpress site on the server. I took most of my scripting from this site.
I am trying to create and grant permissions on the database, however my script stops when I enter a mysql environment using mysql -u root -p. As well, variables created in the bash environment are not carried over to the mysql environment. Here is my script for this section.
echo "Please enter the Name of the Database(please make name relevant)"
read -p DBNAME
echo "Please enter the Name of the Database User(Please document username)"
read -p DBUSER
echo "Please enter a Database password(Please document password)"
read -p DBPASSWORD
a=\'
b=$a$DBPASSWORD$a
f='CREATE DATABASE '$DBNAME' CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci'
g=$a$f$a
c='GRANT ALL ON '$DBNAME.*'TO '$DBUSER'#localhost'' IDENTIFIED BY '$b
d=$a$c$a
mysql -u root -p -e $g
mysql -u root -p -e $c
mysql -u root -p -e "FLUSH PRIVILEGES"
Any help integrating the environment changes together would be great
When defining f and c use double quotes. Single quotes don't allow your variables to expand.
You log the user in as root to mysql, but don't supply a password, so it will prompt. Perhaps ask for the root password as well.
Perhaps something like the following would do the trick:
echo "Please enter the Name of the Database(please make name relevant)"
read -p DBNAME
echo "Please enter the Name of the Database User(Please document username)"
read -p DBUSER
echo "Please enter a Database password(Please document password)"
read -p DBPASSWORD
echo "Please enter your database's root password"
read -p DBROOTPASSWORD
a=\'
b=$a$DBPASSWORD$a
f="CREATE DATABASE '$DBNAME' CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"
g=$a$f$a
c="GRANT ALL ON $DBNAME.* TO $DBUSER#localhost IDENTIFIED BY "$b
d=$a$c$a
mysql -u root -p"$DBROOTPASSWORD" -e $g
mysql -u root -p"$DBROOTPASSWORD" -e $c
mysql -u root -p"$DBROOTPASSWORD" -e "FLUSH PRIVILEGES"

avoid asking second time mysql root password (bash)

In a simple script like this one:
set -x
# Check if db exists, if not we make it, make user, give privileges
if ! mysql -u root -p -e "use $db" 2>/dev/null; then
c1="CREATE DATABASE $db"
c2="GRANT ALL PRIVILEGES ON ${db}.* to '$username'#'localhost' IDENTIFIED BY '$password'"
c3="FLUSH PRIVILEGES"
mysql -u root -p -e "$c1; $c2; $c3"
else
echo 'DATABASE ExISTS, ABORTING'; exit $DB_EXISTS
fi
I am asked each time, bash sees mysql command, for my root credentials.
Is there a way to avoid that, so that once entered the root password, all
additional mysql commands execute seamlessly?
Try looking into adding password to ~/.my.cnf
[client]
user = root
password = XXXXXXXX
Check out :
How to execute a MySQL command from a shell script?
Specifying the --password argument
mysql -u root --password=my_mysql_pass db_name
Safer using a bash variable
mysql -u root --password=$MYSQL_PASS db_name

How to delete mysql database through shell command

I use the pylons and sqlalchemy. I constantly update the schema files and delete and recreate the database so that new schema can be made.
Every time I do this by opening the MySql Query Browser and login and delete the database/schema.
How do I delete the MySQL db/schema thorough linux shell commands in Ubuntu Linux?
Try the following command:
mysqladmin -h[hostname/localhost] -u[username] -p[password] drop [database]
In general, you can pass any query to mysql from shell with -e option.
mysql -u username -p -D dbname -e "DROP DATABASE dbname"
If you are tired of typing your password, create a (chmod 600) file ~/.my.cnf, and put in it:
[client]
user = "you"
password = "your-password"
For the sake of conversation:
echo 'DROP DATABASE foo;' | mysql
Another suitable way:
$ mysql -u you -p
<enter password>
>>> DROP DATABASE foo;
No need for mysqladmin:
just use mysql command line
mysql -u [username] -p[password] -e 'drop database db-name;'
This will send the drop command although I wouldn't pass the password this way as it'll be exposed to other users of the system via ps aux
MySQL has discontinued drop database command from mysql client shell. Need to use mysqladmin to drop a database.
You can remove database directly as:
$ mysqladmin -h [host] -u [user] -p drop [database_name]
[Enter Password]
Do you really want to drop the 'hairfree' database [y/N]: y
[root#host]# mysqladmin -u root -p drop [DB]
Enter password:******