I'm retrieving data from MYSQL databases with a .bat file. The bat file have the next structure
mysql.exe -b -r -u USER -pPASSWORD DATABASE -h IP -e "QUERY" > "PATH\example.txt"
My problem is with complicate queries. I have a query wit multiples joins, and another with asterisk, but doesn't work. I can run the queries perfectly in Navicat but in command line like the code above is impossible.
It is there a way to run this queries without changing the methodology of command line?.
The queries usually have inner join and left joins
Instead of providing the query through the -evalulation flag as a string, you could provide a SQL script file instead for reusability, then direct that in as standard-in to be executed by mysql, then direct the standard-out like you have been doing.
All-in-all, something like this
shell> mysql -b -s -r --username=USER --password=PASSWORD -h HOST db_name < script.sql > output.tab
Related
I can import a database like this:
C:\xampp\mysql\bin\mysql -u {username} -p {databasename} < file_name.sql
Or like this:
C:\xampp\mysql\bin\mysql -u {username} -p
use databasename;
source file_name.sql;
What is the difference between those 2 approaches? Is one preferred over the other? The first one seems to freze the cmd, the second is much more verbose in cmd, reporting every line giving a better feel for when it will finish. After all data was already imported cmd still reports performing some "tmp" table manipulations that I don't understand. Is there anything else to watch out?
I have a script to dump my databases, like this :
#!/usr/bin/env sh
PATH=/usr/bin:/bin
LOG="mybackup/log/backup_$(date +%Y-%m-%d_%H:%M:%S).log"
# some other command
# backup mysql database
echo "Backing up database" > ~/$LOG
mysqldump -u myusername -pmypassword --ignore-table={db1.table1,db1.table2,db1.table3,db1.table4} db1 -r ~/mybackup/db/db1_$(date +%Y-%m-%d_%H:%M:%S).sql >> ~/$LOG 2>&1
# some other command
When I run the command in terminal, it successfully dump my database without ignored tables. But when I run the command through the script, it dump all tables in the database.
I have tried to escape the curly braces :
mysqldump -u myusername -pmypassword --ignore-table=\{db1.table1,db1.table2,db1.table3,db1.table4\} db1 -r ~/mybackup/db/db1_$(date +%Y-%m-%d_%H:%M:%S).sql
but it still dump all table.
My question is how to escape braces/brackets properly in shell script?
I can't answer why it DOES work on command line, but the documentation says to use the option multiple times:
mysqldump --ignore-table documentation
man mylsqdump
--ignore-table=db_name.tbl_name
Do not dump the given table, which must be specified using both the
database and table names. To ignore multiple tables, use this option
multiple times. This option also can be used to ignore views.
I'm writing a bash script to do some db stuff. New to MySQL. I'm on Mac and have MySQL installed via homebrew.
Am using username "root" right now and there isn't a pw set. I included the pw syntax below just to help others out that may have a pw.
My goal is to have mysql commands be as "clean" as possible in my bash script
Not a hige deal, but would like to do this if possible.
Example
# If I can do it without logging in (*ideal)
mysql CREATE DATABASE dbname;
# Or by logging in with - mysql -u root -pPassword
CREATE DATABASE dbname;
# Instead of
mysql -u root -pPassword -e"CREATE DATABASE dbname";
Tried to simplify it. I have a handful of things I gotta do, so would rather keep my code cleaner if possible. I tried logging in with the bash script, but the script stopped once logged into MySQL and didn't run any commands.
Another option I was considering (but don't really like) would be just to keep username and pw string in a var and call it for every commmand like so
# Set the login string variable
login_details="-u root -p password -e"
# example command
mysql $login_details"CREATE DATABASE dbname";
So any ideas?
Write a new bash script file and run this file after putting all your commands into it. Don't forget to give right username and password in your bash script.
For bash script:
#!/bin/bash
mysql -u root -pSeCrEt << EOF
use mysql;
show tables;
EOF
If you want to run single mysql command.
mysql -u [user] -p[pass] -e "[mysql commands]"
Example:
mysql -h 192.168.1.10 -u root -pSeCrEt -e "show databases"
To execute multiple mysql commands:
mysql -u $user -p$passsword -Bse "command1;command2;....;commandn"
Note: -B is for batch, print results using tab as the column separator, with each row on a new line. With this option, mysql does not use the history file. Batch mode results in nontabular output format and escaping of special characters. -s is silent mode. Produce less output. -e is to execute the statement and quit
I need to run a monthly bash script via cron that is related to our company's billing system. This is done with two stored procedures. When I run them via the MySQL console and workbench, they work fine.
I've looked at this article and this is basically the way I do it.
I call via cron, a shell script that looks like this:
mysql -h 192.168.1.1 -u<username> -p<password> mydatabase < /path/to/billing_periods.sql
My text file that has the commands in it looks like this:
call sp_start_billing_period();
call sp_bill_clients();
What happens is that the first query runs, but the second one on the second line, doesn't.
I can make a stored procedure that wraps these two - but I just was hoping to learn why this was happening... Perhaps a mistake I made or a limit in the way you do this..
I also considered doing this (two calls to the MySQL shell):
mysql -h 192.168.1.1 -u<username> -p<password> mydatabase -e "call sp_start_billing_period();"
mysql -h 192.168.1.1 -u<username> -p<password> mydatabase -e "call sp_bill_clients();"
You could try separating each statement with a semicolon.
mysql -h 192.168.1.1 -u<username> -p<password> mydatabase -e "call sp_start_billing_period();call sp_bill_clients();"
If you have your statements in a file you can do:
while read LINE; do mysql -u<username> -p<password> mydatabase -e"$LINE";echo "-----------";done < statements.sql
I think you are only allowed to execute a single statement in your input .sql file, see the mysql documentation (manpage) for -e statement.
· --execute=statement, -e statement
Execute the statement and quit. The default output format is like that produced with --batch.
The -e is implicit. At least when I do different mysql queries I put them in their own script like you already suggested.
I'm running this command mysqldump -hlocalhost -uroot -d db_name -e --skip-add-drop-table --quick --skip-lock-tables >> /tmp/db1.sql
But when I do a load this file locally to my mysql server, I don't get any data in my folder. I'm not sure which of these flags is causing that to happen, because all they seem to be doing is skipping dropping the tables before hand, locking the tables, and retrieving rows from the table one row at a time. Thanks for the help!
The -d flag actually means no data, not specifying the database.