How do you execute a sql file using mysql-ctl - mysql

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.

Related

Export MySQL as CSV using Powershell

I am trying to extract a certain table as csv using powershell, since this task is scheduled. Since using it in Powershell is basically the same I am showing you the commandline commands I tried using:
mysql --user=root --password=root apps_monitor -e "select * from board into outfile 'C:\Temp\export.text'"
And then I get the error:
The MySQL server is running with the --secure-file-priv option so it cannot execute this statement.
My question is how I disable this option. I tried editing the my-default.ini in the MySQL Server directory and adding
secure_file_priv=NULL
And
secure_file_priv=""
But both did not do anything upon restarting the mysql service. Can someone please help me with that thing.

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.

How can I run multiple Stored Procedures Files & Triggers (.sql) From MySQL Workbench

I am trying to run a set of sql files with stored procedures and triggers in my windows XAMPP environment. Some suggested to me using a batch script but I do not know how to do this in windows.
Is it possible to run all these .sql files from within MySQL Workbench? How? If not, can anyone tell me how to run a batch file within windows?
Thank you.
It seems Workbench doesn't support the command "SOURCE" so the next best thing is is (at least in windows) is to run a batch job. Simply create a new .sql file and add the full path to each .sql file like so:
Create the batch file:
In windows, the batch file can be a .sql with the sql comman SOURCE which calls the other .sql files, like so:
create run.sql
SOURCE C:\xampp\htdocs\mysite\sql\procs\sp_article_delete.sql
SOURCE C:\xampp\htdocs\mysite\sql\procs\sp_article_insert.sql
SOURCE C:\xampp\htdocs\mysite\sql\procs\sp_article_load.sql
Open Command Line and CD to MySQL Folder
Open the command line, and cd to MySQL. If you are using XAMPP, the command/location should be something like:
cd C:\xampp\mysql\bin\
Execute the Batch File by pressing ENTER
Last, simply load mysql and run the batch file using the following command:
mysql -u root -h 127.0.0.1 my_database_name -vvv < C:\xampp\htdocs\mysite\sql\procs\run.sql
The execution above means the following:
mysql -u <username> -h <host> <database> -vvv < <batch_path_file_name>
-vvv shows all the queries being executed and the rows affected for debugging.
That's it. All .sql files mentioned in the run.sql file will be executed.

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.