When I run any select or update query in mysql workbench, it shows either
or number of rows returned.
In my script, I use mysql -u user -h ip db -se"select * from.."
I have tried redirecting mysql output:
./script.sh >> script.log 2>&1
but it shows message only for error not when successfully run.
It does not show 27 row(s) returned. So in that case, I could not check if any update statement, select or procedure run successfully.
How can I get output which runs successfully?
I found a solution, in the options of mysql.
Run it like this:
mysql -u USER -h HOST -p PORT --password -vv -se "YOUR QUERY" >output.txt 2>errors.txt
The addition of the -vv parameter will give you the number of affected rows.
-vvv will also tell you how much time it took to run the query.
Ex: I ran this:
mysql -u Nic3500-h localhost -P 3306 --password -vv -se "INSERT INTO stackoverflow.activity (activity_id, activity_name) VALUES ('10', 'testtest');" >output.txt 2>&1
And output.txt is:
--------------
INSERT INTO stackoverflow.activity (activity_id, activity_name) VALUES ('10', 'testtest')
--------------
Query OK, 1 row affected
Bye
You can do this with a query using INTO OUTFILE like shown in the mysql documentation
Related
I have two Linux (Debian) servers with MySQL (5.5) on them.
How can I run a query on one and import it directly into another one. I was thinking of something like the below, but can't figure out the last bit.
mysql -h1.2.3.4 -P3306 -uxxxx -pxxxx -e "SELECT id FROM db1.table1 limit 10" | mysql -h5.6.7.8 -P3306 -uxxxx -pxxxx -e "INSERT INTO db2.table2 (id) VALUES ????"
Is this actually possible, or do I need to find some other way of doing it?
maybe you can try to generate a db script of your existing DB then execute this on the target destination DB command line. Dont forget to copy the generated sql script into the destination if it is a seperate machine.
mysql -u <username> -p <databasename> < <scriptFilname.sql>
Trying to connect to an rds mysql server from an ec2 ubuntu server.
I use
mysql -h my_host_name -u admin_name -p database < data.sql
When the password prompts, I enter my password. However all this does it create a new blank line and does nothing else.
Any ideas?
When mysql is processing file input, it doesn't normally print informative messages, it only displays the results of SELECT queries. If you want to see messages from queries that modify the database, add the -v option to make it verbose.
mysql -v -h my_host_name -u admin_name -p database < data.sql
If you use -v -v it will produce even more details, and -v -v -v will be most informative.
This blank line probably means that your mysql is processing what is inside your "data.sql".
If you need to see what is been processed, you can first connect to mysql server with:
mysql -h my_host_name -u admin_name -p
Change to your database ( if you have one defined and your sql is not creating one... ):
mysql> change my_database;
Than you call your script execution with:
mysql> source data.sql;
{}'s
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 have a mysql table with large number of rows (10m)
From the mysql client, I want to run a query but not print results. This is because even though the query runs in 15 seconds, printing the results on to console takes many minutes.
How can I achieve this?
EDIT: My query is the following:
select user_id, count(*) as ct from user_geo_loc group by user_id, lat, lng;
EDIT 2: At the end of the execution, the mysql client prints the following
9950710 rows in set (9.31 sec)
I want to find out this time but not print the results (which takes 15 minutes)
When on Linux, you could redirect the output to /dev/null to prevent the output. Like this:
mysql -u username -p database -e "SELECT * FROM table" > /dev/null
On Windows the equivalent would be:
mysql -u username -p database -e "SELECT * FROM table" > NUL
Please note: The only thing printed on the console will be errors, to prevent this, you would have to redirect stderr to stdout by adding 2>&1 to the end (Linux)
In console, you may redirect output into the null device:
$ mysql -uUSER -pPASSWORD -e"select ..." DATABASE_NAME > /dev/null
or you may redirect into the file to look result later (this is much faster than print output into console):
$ mysql -uUSER -pPASSWORD -e"select ..." DATABASE_NAME > ./output.txt
It seems like you want a pager ?
run the following (in the MySQL console)
pager less
Which will use less and only show the first "screen" of info
my aim is to start a scheduled job on a windows server that deletes datasets on a local mysql server, also installed on a windows server.
But I would not just delete data, I want to create a logfile.
With this command:
mysql -v -h localhost -u user --password=password -P 3307 --database=smydatabase -B --silent --skip-named-commands < query.sql > logging.txt
an the following sql-file "query.sql
select count(*) from table;
I get the following logfile:
--------------
select count(*) from table
--------------
4420101
=======================
My first Question is: can I susspress the query and the both lines above and below.
The final sql-file will contain abount 20 Lines of sql-commands. My preffered goal is to create a formated logfile like this:
Job started at <date>
Deleting <4420101> datasets in table <table>
4420101 rows affected
Deleting <22013> datasets in table <persons>
etc.
So I have to create lines of logfile with select-statements and variables. other lines like delete-statements should not appear in the logfile. is this possible?
Include --skip-column-names:
mysql -v -h localhost -u user --password=password -P 3307 --database=smydatabase -B --silent --skip-named-commands --skip-column-names < query.sql > logging.txt
Hope this would help.