I want to make specific .sql file for each of my routines.
#!/bin/bash
routine_names=$(mysql mydb --execute="SELECT
*
FROM
information_schema.routines
WHERE
routine_type = 'PROCEDURE' OR routine_type ='FUNCTION'
AND routine_schema = 'mydb';"|cut -f1)
for routine in "$routine_names"
do
if [ -e "${routine}.sql" ]
then
echo "ok"
else
content_procedure=$(mysql mydb--execute="SHOW CREATE PROCEDURE $routine;")
echo "$content_procedure" >> masoud.txt
fi
done
my routine_names variable is a list of my procedures. like this:
SP_ONE
SP_TWO
SP_THREE
I want to loop of these result. but I think the result is not an array. because routine variable has all content.
wrap your mysql mydb ... command with ()
routine_names=($(mysql mydb --execute="SELECT
*
FROM
information_schema.routines
WHERE
routine_type = 'PROCEDURE' OR routine_type ='FUNCTION'
AND routine_schema = 'mydb';"|cut -f1))
I was curious and got the idea from here.
Just another case how I fix my problem
#!/bin/bash
mysql -u USER -p -h localhost -D database1 -e "SELECT ID FROM prodTable WHERE display=1 AND new=1 AND exDate<DATE_SUB(CURDATE(),INTERVAL 2 YEAR)" | while read ID;
do
echo "The following product has been moved: $ID"
done
Related
I want to update database from bash command:
#!/bin/bash
list= cat /home/wwwroot/list.txt;
mysql -u root -D dbname -e "UPDATE wp_postmeta SET meta_value = '$list' WHERE meta_key = 1";
cat /home/wwwroot/list.txt
text1
text2
text3
...
How to $list variable can be read in sql query?
Thank you.
Cat is an overkill here.
I suggest using an array if you need space delimited values.
list=($(</home/wwwroot/list.txt))
mysql -u root -D dbname -e "UPDATE wp_postmeta \
SET meta_value = '${list[#]}' WHERE meta_key = 1";
The advantage here is if you need comma separated values then you could employ bash [ parameter substitution ] to achieve your feet. Just change the query to
mysql -u root -D dbname -e "UPDATE wp_postmeta \
SET meta_value = '${list[#]/%/,}' WHERE meta_key = 1";
#Note ${list[#]/%/,} appends comma after each value in the text file
All good :-)
I have a simple bash script. I wish to get an exact count of the number of rows in each table of the database.
#!/bin/bash
TABLES_OLD=$( mysql -u user -ppassword MySchema --batch --skip-column-names -e"SHOW TABLES FROM MySchema" )
for table in "${TABLES_OLD[#]}"
do
QUERY="SELECT COUNT(*) FROM ${table}"
echo "${QUERY}"
done
The script prints:
SELECT COUNT(*) FROM Table 1
Table2
Table3
Table4
etc...
Clearly this is not what I want, and I don't even understand how what is happening is possible. What am I doing wrong?
Try this, put the tables into an array then loop thru the results
db_host='host'
db_user='user'
db_pass='password'
db='your_db'
read -ra var_id <<< $(mysql -h $db_host -u $db_user -p$db_pass $db -sse "show tables from $db")
for i in "${var_id[#]}";
do
results=$(mysql -h $db_host -u $db_user -p$db_pass $db -sse "select count(*)from $i")
echo "$i $results"
done
This should do it :
#/bin/bash
mysql -u user-ppassword -e "SELECT table_name, table_rows
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'your_data_base_name';"
Replace echo with eval
The code will be
#!/bin/bash
TABLES_OLD=$( mysql -u user -ppassword MySchema --batch --skip-column-names -e"SHOW TABLES FROM MySchema" )
for table in "${TABLES_OLD[#]}"
do
QUERY="SELECT COUNT(*) FROM ${table}"
eval "${QUERY}"
done
I am trying to check if a MYSQL user exists. This is as far as I have got. I fall down on capturing the answer from the output.
#!/bin/bash
echo -e "What is the MYSQL username called"
read DBUSER
if [ -z "$DBUSER" ]
then
exit
mysql -uUSER -pPASS -e "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = '$DBUSER')";
if
yes
do this
else
do this
this is the output I am getting
+-----------------------------------------------------+
| EXISTS(SELECT 1 FROM mysql.user WHERE user = 'bob') |
+-----------------------------------------------------+
| 1 |
+-----------------------------------------------------+
Can any one help please
Thanks very much for your help. Here is the final result working.
It needs the -sse
RESULT_VARIABLE="$(mysql -uUSER -pPASS -sse "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = '$DBUSER')")"
if [ "$RESULT_VARIABLE" = 1 ]; then
echo "TRUE"
else
echo "FALSE"
fi
Assigning the result to a variable can be done like this:
RESULT_VARIABLE="$(mysql -uUSER -pPASS -se "SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = '$DBUSER')")"
And you can also alias a column in MySQL, btw.
SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = '$DBUSER') AS does_it_exist
I have a MySQL update script I'd like to run from the command line, but I want to be able to pass a stage domain variable to the script.
I know this won't work, but it's the best way I can describe what I'm trying to do:
$ -uroot -hlocalhost mydatabase --execute "SET #domain = 'mydomain.dev' " < ./sql/update_domain.sql
Inside the script, I'm using the #domain variable, to update some configuration variables in a config table, using commands like this:
UPDATE my_cfg SET value = #domain WHERE name = 'DOMAIN';
Basically I want to prefix the SET #domain on the update_domain.sql file.
Any ideas how I can rectify my approach?
In your BATCH File :
mysql -e "set #domain=PARAMVALUE;source ./sql/update_domain.sql"
And in you SQL file :
UPDATE my_cfg SET value = #domain WHERE name = 'DOMAIN';
you can do that with sed like this:
echo "UPDATE my_cfg SET value = '#domain#' WHERE name = 'DOMAIN'" | sed 's/#domain#/mydomain.dev/' | mysql -uusername -ppassword dbname
or update.sql has UPDATE:
cat update.sql | sed 's/#domain#/mydomain.dev/' | mysql -uusername -ppassword dbname
This works for me:
system("(echo \"SET #domain = 'newstore.personera.abc';\"; cat sql/set_domain.sql) > /tmp/_tmp.sql")
system("mysql -uroot -hlocalhost newstore.personera.dev < /tmp/_tmp.sql")
system("rm /tmp/_tmp.sql")
...calling with system() from Capistrano.
I've found a better solution.
--init-command=name SQL Command to execute when connecting to MariaDB server.
mysql --init-command="SET #foo = 1; SET #bar = 2" -e "SELECT #foo, #bar, VERSION()"
Output:
+------+------+-------------------------------------+
| #foo | #bar | VERSION() |
+------+------+-------------------------------------+
| 1 | 2 | 10.6.3-MariaDB-1:10.6.3+maria~focal |
+------+------+-------------------------------------+
It also works with file redirection.
How to select all the tables from multiple databases in mySql..
I am doing the following steps but not able to achive the goal.
<?php
$a = "SHOW DATABASES";
$da = $wpdb->get_results($a);
foreach($da as $k){
echo '<pre>';
print_r ($k->Database);//prints all the available databases
echo '</pre>';
$nq = "USE $k->Database";//trying to select the individual database
$newda = $wpdb->get_results($nq);
$alld = "SELECT * FROM $k->Database";
$td = $wpdb->get_results($alld);
var_dump($td);//returns empty array
}
?>
Please Help me
Use the INFORMATION_SCHEMA:
select table_schema, table_name from information_schema.tables;
Even better:
Show all tables in all databases (except internal mysql databases) in one SQL statement.
SELECT table_schema, table_name FROM information_schema.tables WHERE table_schema NOT IN ( 'information_schema', 'performance_schema', 'mysql' )
You cannot do
SELECT * FROM database
but you can do
USE DATEBASE;
SHOW TABLES;
or even better:
SHOW TABLES IN database
I would go for this this:
mysql -e "select table_schema, table_name from information_schema.tables;" | \
grep -Pv '^(sys|performance_schema|TABLE_SCHEMA|mysql|information_schema)' | \
perl -pe 's/\s+/./' | \
sort -u
mysql -e'select table_schema, table_name from information_schema.tables;'
This depends on you having a file ~/.my.cnf in place with the following contents:
[client]
user=ADMINUSER ## set user, usually 'root'
password=PASSWORD ## set password