How to Truncate table from a .sh script? - mysql

I've got this .sh script to optimize table
#!/bin/sh
mysql -u'user' -p'pwd' -D'db' </var/www/vhosts/mydomain/httpdocs/optimize.sql
...and this sql in optimize.sql
OPTIMIZE TABLE `this_table`
This all works fine.
Now I'm doing the same thing to Truncate a table:
#!/bin/sh
mysql -u'user' -p'pwd' -D'db' </var/www/vhosts/mydomain/httpdocs/truncate.sql
...and this sql in truncate.sql
TRUNCATE TABLE `this_table`
...but it doesn't work! What is wrong?
P.S. I used a Crontab to run .sh every x minutes and this works only for optimization

Also you can use a file to store the mysql credentials, e.g:
nano .my.cnf
and add the following content:
[mysql]
user=your_user
password=the_super_secret_password
Then execute your SQL statement(s) in your shell script(s) like so:
#!/bin/bash
`mysql --defaults-file=/path/to/.my.cnf -e "truncate table this_table;"`
Why put the credentials in a separate file?
So that all of the shell scripts that you have that use these credentials don't have to be individually updated when the time comes to change the password.

Also you can
#!/bin/sh
echo "truncate table this_table" | mysql -u'user' -p'pwd' -D'db'

Related

Inserting data in mysql with shell script

I have a problem with a shell script:
I am trying to inject data from a source file containing MySQL queries into a data base. Here are the relevant lines of my shell script:
mysql -u root
source /usr/local/insert.sql;
quit;
For example I am running the file as ./insertfile and it is running smoothly but when it comes to data insertion in MySQL it is logging into MySQL using the mysql -u root command but the remaining operations (source /usr/local/insert.sql; and quit;) are not being executed. When I quit MySQL manually it tries to execute the rest of the command from my insert.sql file.
So please help me use the right shell script so that I can insert the queries from the source file.
One way to do that would be
mysql -u root --execute="source /usr/local/insert.sql; quit;"
It seems that your import hangs !
Check for lock on your database.
show processlist;
Run FLUSH TABLES to release any possible locks and then run your import command.
if source command hangs again :
Enter your myslq server
drop database insert;
create database insert
exit the mysql server and run:
mysqldump -u -p database-name < dump.sql
Thanks for your help. I have tried adding your line in my script and it was primarily giving some errors then I changed the command like below -
mysql -u root --execute="source /usr/local/insert.sql; \q"
Above line helped me to execute my command.
Thanks to all for being this much helpful.
Regards,
Shah9il

How do I execute a lot of SQL queries without problems?

I have an SQL file with 30,000 UPDATE lines. When I upload it through phpmyadmin it freezes at a certain point and doesn't update everything.
Is there a way to execute all 30,000 lines, without problems, all at once? or do I have to go through and manually execute 200 lines at a time?
Line Example:
UPDATE `table` SET `value1`='Some text', `value2`=0 `value3`=1 WHERE id=500;
^ I have 30,000 lines like that.
PHPMyAdmin's query parsing is slow. It's much better to log into server via SSH and execute the command using mysql client:
$ mysql -uUsername -pPassword DatabaseName < script.sql
If you don't have SSH access, you can upload the sql script (via FTP, for example) and write a small PHP script that calls the command using system, exec or similar PHP function:
<?php
system('mysql -uUsername -pPassword DatabaseName < script.sql');
Then invoke the script via browser.
Make sure you use full paths to mysql (/usr/bin/mysql usually) and your script file.
If you use non-system character set, make sure you add the default_character_set option as well.

Drop multiple databases using mysql command

I have many databases with different names.
I want to drop multiple databases, Is there any command since all names of db are different.
Eg: mysql db, Test db, live db.
As of I know, there is no specific command/query to delete multiple databases without having a specific pattern in their names. Even I was asked to do the favor several times. So I researched and found no specific solution. Then I tried the below hack. It worked without giving much trouble. May be it could help for you too.
Take all the databases using the below command.
SHOW DATABASES ;
Paste all of them in an excel/some other text file (I prefer NPP). Keep the only names which you want to delete from the list. Dont forget to remove your working db's from the list.
Add DROP DATABASE in front of those names.
That's it simple. Copy & Paste all of those in your workbench. You can execute all of them in one shot.
If you create a shell script this should remove all the databases. You will need to edit it to suit your needs.
DBUSER='user'
DBPASS='password'
SQLFILE='/path/to/file/databases.sql'
echo '* Dropping ALL databases'
DBS="$(mysql -u$DBUSER -p$DBPASS -Bse 'show databases' | grep -v Database | grep -v database | grep -v mysql | grep -v information_schema)"
for db in $DBS; do
echo "Deleting $db"
mysql -u$DBUSER -p$DBPASS -Bse "drop database $db; select sleep(0.1);"
done
First run this query to produce a list of drop commands:
select CONCAT('drop database `', schema_name,'`;') as database_name from information_schema.schemata where schema_name like '%DATABASES_TO_REMOVE%' order by schema_name;
Then copy the output rows of this query and paste into a query window
In my case I then needed to remove the single-quotes (') surrounding the resulting command queries which I did using a simple find + replace (often Ctrl + H, replace ' with < empty >)
And execute (highlighting all of the drop statements in my case)!
Unfortunetly, there is nothing like that, unless you create your own function.
simple bash script can be done this work
#!/bin/bash
cat /home/mshafee/file | while read line
do
mysql -u username -p****** -h 0.0.0.0 -e "drop database $line;"
done
here provide username, password and IP address.

Mysql restore to restore structure and no data from a given backup (schema.sql)

Hi I use mysql administrator and have restored backup files (backup.sql). I would like to use restore the structure without data and it is not giving me an option to do so. I understand phpadmin provides this. I can not use this however. Any one can tell me an easy way?
Dump database structure only:
cat backup.sql | grep -v ^INSERT | mysql -u $USER -p
This will execute everything in the backup.sql file except the INSERT lines that would have populated the tables. After running this you should have your full table structure along with any stored procedures / views / etc. that were in the original databse, but your tables will all be empty.
You can change the ENGINE to BLACKHOLE in the dump using sed
cat backup.sql | sed 's/ENGINE=(MYISAM|INNODB)/ENGINE=BLACKHOLE/g' > backup2.sql
This engine will just "swallow" the INSERT statements and the tables will remain empty. Of course you must change the ENGINE again using:
ALTER TABLE `mytable` ENGINE=MYISAM;
IIRC the backup.sql files (if created by mysqldump) are just SQL commands in a text file. Just copy-paste all the "create ..." statements from the beginning of the file, but not the "insert" statements in to another file and "mysql < newfile" you should have the empty database without any data in it.
there is no way to tell the mysql client to skip the INSERT commands. the least-hassle way to do this is run the script as-is and let it load the data, then just TRUNCATE all of the tables.
you can write a script to do the following:
1 : import the dump into a new database.
2 : truncate all the tables with a loop.
3 : export the db again.
4 : now u just have the structure
You can backup you MYSQL database structure with
mysqldump -u username –p -d database_name > backup.sql
(You should not supply password at command line as it leads to security risks.MYSQL will ask for password by default.)

Batch script to issue commands to mySQL db?

I am trying to create a batch script that would connect to a mySQL database and issue a delete command:
#echo off
echo Resetting all assessments...
mysql -hlocalhost -urdfdev -p%1 rdf_feedback
delete from competency_question_answer;
I will run this script providing the password as a command-line argument, but all this script does is, connects to the database, and the mysql> prompt will be shown. After I exit from mysql, the rest of the batch commands get to execute (and fail, no surprise).
How can I pass the SQL commands from the batch script to the mysql console? Is this even possible?
You need to use command line tools. I don't know if there exists any for MySQL but for SQL there is SQLCMD and for Oracle there is OSQL.
What you can also do is something like this.
mysql -uuser -ppass < foo.sql
Where foo.sql is the commands you want to execute.
You may need to connect multiple times:
#echo off
echo Resetting all assessments...
mysql -hlocalhost -urdfdev -p%1 rdf_feedback -e delete from competency_question_answer;
Alternatively, you should be able to put all your commands in a separate file such as input.sql and use:
mysql -hlocalhost -urdfdev -p%1 rdf_feedback <input.sql
echo "delete from competency_question_answer;" | mysql -hlocalhost -ur... etc.
Putting multiple sets of commands into .sql batch files works best, and you can execute multiples of these in the .bat file.