I can't work out how to get the mysql client to return the number of rows deleted to the shell when running a delete. Does anyone know what option will enable this? Or ways around it?
Here's what i'm trying, but i get no output:
#!/bin/bash
deleted=`mysql mydb -e "delete from mytable where insertedtime < '2010-04-01 00:00:00'"|tail -n 1`
I was expecting something like this as the output from mysql:
deleted
999999
Which is why i have the tail -n 1 so i only pick up the count and not the column name.
When running the command by hand (mysql mydb -e "delete from mytable where insertedtime < '2010-04-01 00:00:00'") there is no output. When running the command interactively when running the mysql client i ge the following:
mysql>delete from mytable where insertedtime < '2010-04-01 00:00:00';
Query OK, 0 rows affected (0.00 sec)
I want to get the rows affected count into my shell variable.
Any help would be most appreciated.
This question was asked previously: How to get number of rows affected, while executing MySQL query from bash?
add "-vv"
mysql mydb -e "delete from mytable where insertedtime < '2010-04-01 00:00:00' -vv
https://dba.stackexchange.com/questions/23527/how-can-you-output-update-insert-query-results-when-using-mysql-e-execute
deleted=`mysql mydb -e "delete from mytable where insertedtime < '2010-04-01 00:00:00'"|tail -n 1`
int icount = mysql_CountRow(deleted);
it works for me try this.
Related
10 million rows. Want to backup data in SQL files with 100k rows (breaking the data into chunks). Is it possible to run a query like this:
SELECT * FROM `myTable` WHERE `counter` > 200000 and `counter` <= 300000 ---> Send to .sql file
I want to replace the psuedocode at the end of that statement with real code.
Using the "export" feature of PHPMyAdmin more than 100 times would take too long.
You should be able to use the mysqldump command:
mysqldump -u root -p [database_name] [tablename]
--where="'counter' > 200000 and 'counter' <= 300000" > [dumpfile.sql]
Additional info on the command here:
https://dev.mysql.com/doc/refman/8.0/en/mysqldump.html
You can try to use mysqldump command with script to backup your script.
for i in {1..100}
do
beginRow=$(( ($i - 1) * 100000 ))
endRow=$(( $i * 100000 ))
mysqldump -h <hostname> -u <username> -p <databasename> myTable --where="counter = > $beginRow and counter <= $endRow" --no-create-info > "./data-$i.sql"
done
I'm having a file mytest.sql file and it contains following statements:
use test;
tee output123.log;
select database() as 'Database';
select now() as 'Start Time';
set autocommit=0;
insert into table1 values(3,'k');
insert into table1 values(4,'kk');
insert into table1 values(5,'kkk');
commit;
select * from table1;
notee;
$mysql -h localhost -u root -p < mytest.sql
output of log file:
When i login to MySQL and execute the statements I'm getting the output in the below format :
output of log file :
I need the same above output when the statements are executed from shell script. how to do it .
What you're looking for is the --table option to mysql. https://dev.mysql.com/doc/mysql-shell/8.0/en/mysqlsh.html#option_mysqlsh_table notes "batch mode" specifically; mysql is probably inferring that --table should be on if the input device is a terminal and off otherwise, a common mechanism for such programs.
This appears to be simple, but I cannot figure out how to fix it.
Here is a simple sql script:
select c1 from t1 where c1='notPresent';
I execute it using interactive shell as in:
mysql -u somebody -psomePassword myDatabase
...
mysql> select c1 from t1 where c1='notPresent';
Empty set (0.00 sec)
Now I execute it as:
mysql -u somebody -psomePassword myDatabase < myFile.sql
The output is:
mysql: [Warning] Using a password on the command line interface can be insecure.
I know how to suppress this warning by saving the userId and password in my.cnf but I want to get the "Empty set" output too.
The same with SQL Updates. When script is from a file, it does not display status as in number of row updated.
This is part of a bigger application. I have narrowed the problem to the above situation.
You can use triple-verbose mode to get full output in batch mode:
echo "show tables" | mysql -v -v -v test
--------------
show tables
--------------
+--------------------+
| Tables_in_test |
+--------------------+
| ........ |
+--------------------+
2 rows in set (0.00 sec)
Bye
I spotted this option when I looked at the help:
mysql --help
...
-v, --verbose Write more. (-v -v -v gives the table output format).
I'm using some bash code which I got off another post on here
#!/bin/sh
users=$(mysql --user=user --password=password --database=$db -h _IP ADDRESS) -s -- execute="select$ users from db limit 1;"|cut -f1)
echo "$users"
The database is DB and the table is users basically I want to be able to get a user count from the table but when I run the script I get
ERROR 1049 (42000): Unknown database 'execute=select users from db limit 1;'
any idea what i'm doing wrong or a better way of doing it? if I do
select * form users;
on the mysql server itself it returns 12 rows in set (0.00 sec) 12 being the number of users, so I just want my script to query the user table on database DB and return the number of rows ie 12.
users_count=$(
mysql --user user_name --password=password -h x.x.x.x <<EOF | tail -n 1
select count(1) from mysql.user;
EOF
)
it seems that the order of your params is wrong...
MYSQL is telling you that 'execute=select users from db limit 1;' is in the position of the database parameter.
Try something like this:
mysql --user user_name --password=password -e 'select users from db_schema.table limit 1;'
Simply try this:
mysql -u username --password=password -h hostname database_name -e 'select count(1) from table limit 1;'
Is there a way to tell the last access time of a mysql table? By access I mean any type of operation in that table including update, alter or even select or any other operation.
Thanks.
You can get the last update time of a table.
SELECT update_time FROM information_schema.tables WHERE table_name='tablename'
You can use the OS level stat command.
Locate the ibd file for that particular table and run the below command
stat file_location
If the Table is being queried by SELECT, You can find the timestamp of when it was accessed with under the Access field.
I don't know how to get the exact time after-the-fact, but you can start dumping logs, do something, and then stop dumping logs. Whichever tables show up in the logs are the ones that were accessed during that time.
If you care to dig through the log, the queries are shown with timestamps.
Tell mysql where to put the log file
Add this line to my.cnf (on some systems it will be mysql.conf.d/mysqld.cnf).
general_log_file = /path/to/query.log
Enable the general log
mysql> SET global general_log = 1;
(don't forget to turn this off, it can grow very quickly)
Do the thing
All mysql queries will be added to /path/to/query.log
Disable the general log
mysql> SET global general_log = 0;
See which tables appeared
If it's short, you can just scroll through query.log. If not, then you can filter the log for known table names, like so:
query_words=$(cat mysql_general.log | tr -s [:space:] \\n | tr -c -d '[a-zA-Z0-9][:space:][_\-]' | egrep -v '[0-9]' | sort | uniq)
table_names=$(mysql -uroot -ptest -Dmeta -e"show tables;" | sort | uniq)
comm -12 <(echo $table_names) <(echo $query_words)
From there, you can grep the log file for whatever showed up in table_names. There you will find timestammped queries.
See also, this utility, which I made.
For a more detailed (db name and table name) plus the period (range), try this query:
select table_schema as DatabaseName,
table_name as TableName,
update_time as LastAccessTime
from information_schema.tables
where update_time < 'yyyy-mm-dd'
group by table_schema
order by update_time asc
Use information_schema database to find which table of respective database was updated:
SELECT UPDATE_TIME
FROM information_schema.tables
WHERE TABLE_SCHEMA = 'dbname'
AND TABLE_NAME = 'tablename'
order by UPDATE_TIME DESC