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.
Related
I am learning how to use mysql with Cloud9, I have a script used to create a default database and tables and loading sample data into the new database. How do I use the mysql-ctl tool to execute a script file?
It connects to a database just fine and I can execute ad-hoc queries without an issue.
You cannot use mysql-ctl to execute the script (you can see the source code running less $(which mysql-ctl) but you can use the usual mysql client command:
mysql -h 127.0.0.1 -u username < yourfile.sql
When you are in 'mysql-ctl' just run
source file_name.sql and your script will run.
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
This is a small question, but what is the name for a series of SQL/ DML / DDL commands stored in a file? Also, what is the syntax for running this file in DBMS?
The help command in mysql refers to it as a SQL script file. The syntax for running it in MySQL from the shell is:
mysql ..options.. < filename
e.g.
mysql -u username -p databasename < filename.sql
See the MySQL documentation for details of using the mysql command-line tool.
You can also run it from within the mysql command with:
mysql>source filename
or
mysql>. filename
I don't use phpMyAdmin, so I don't know if there's a way to do it from there.
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'
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.