MySQL dump ignores full DB views create statement - mysql

--> I've MySQL DB running v5.7.40
mysql> SHOW VARIABLES LIKE 'version';
+---------------+-----------------------------+
| Variable_name | Value |
+---------------+-----------------------------+
| version | 5.7.40-0ubuntu0.18.04.1-log |
+---------------+-----------------------------+
1 row in set (0.00 sec)
mysql>
--> Currently doing backup of one of the DBs using mysqldump
root#reporting:~# cat /var/scripts/backup.sh
#!/bin/bash
.
.
.
backup(){
DBASE="jiradbrepl"
mysqldump -u root $DBASE >/storage/backup/$DBASE-$MONTH_DAY.sql
}
root#reporting:~#
--> The backup completes without any errors or warnings but it doesn't include full create statement of DB views
root#reporting:~# bash /var/scripts/backup.sh
++ hostname -s
+ MY_HOSTNAME=reporting
++ date +%b-%d
+ MONTH_DAY=Jan-15
+ backup
+ DBASE=jiradbrepl
+ mysqldump -u root jiradbrepl
+ cleanup
+ find /storage/backup/ -type f -name '*.sql' -mtime +5 -exec rm '{}' ';'
root#reporting:~#
root#reporting:~# ls -lh /storage/backup/jiradbrepl-Jan-15.sql
-rw-r--r-- 1 root root 748M Jan 15 14:37 /storage/backup/jiradbrepl-Jan-15.sql
root#reporting:~#
--> For example this view stops at the middle of the creation statement
root#reporting-db:~# less /storage/backup/jiradbrepl-Jan-15.sql
.
.
.
DROP TABLE IF EXISTS `V_R_PROJECTS_BIRDEYE`;
/*!50001 DROP VIEW IF EXISTS `V_R_PROJECTS_BIRDEYE`*/;
SET #saved_cs_client = ##character_set_client;
SET character_set_client = utf8;
/*!50001 CREATE VIEW `V_R_PROJECTS_BIRDEYE` AS SELECT
1 AS `projectName`,
1 AS `CPID`,
1 AS `GEO`,
1 AS `region`,
1 AS `subRegion`,
1 AS `startDate`,
1 AS `endDate`,
1 AS `status`,
1 AS `LOE`,
1 AS `discountedLOE`,
1 AS `status_history`,
1 AS `prjPoCName`,
1 AS `ML_CQ`,
1 AS `BC_CQ`,
1 AS `commitedBudget`,
1 AS `URL`,
1 AS `prjActualDays`,
1 AS `prjPendingDays`,
1 AS `prjPlannedDays`,
1 AS `currentQuarterActualDays`,
1 AS `currentQuarterPendingDays`,
1 AS `remainingQuarterPlannedDays`,
1 AS `currentQuarterPlannedDays`,
1 AS `nextQuarterPlannedDays`,
1 AS `PSO_PM`,
1 AS `COE_PM`,
1 AS `techList`,
1 AS `budgetActuals`,
1 AS `budgetForecast`,
1 AS `budgetPlanned`*/;
SET character_set_client = #saved_cs_client;
root#reporting-db:~#
--> The backup runs with root user which has all privileges
mysql> show grants for root;
+-------------------------------------------------------------+
| Grants for root#% |
+-------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' WITH GRANT OPTION |
+-------------------------------------------------------------+
1 row in set (0.00 sec)
mysql>
root#reporting-db:~# mysql -ANe"SELECT USER(),CURRENT_USER()"
+----------------+----------------+
| root#localhost | root#localhost |
+----------------+----------------+
root#reporting-db:~#
--> I've tried running the backup using the command in Views not being dumped by mysqldump
--> Also using this command but that didn't help
mysqldump -u root --lock-tables --comments --dump-date --force --events --routines --add-drop-table --add-locks --log-error=/tmp/jiradbreplbackup.log jiradbrepl > /storage/backup/jiradbrepl-Jan-15_2.sql
root#reporting-db:~# cat /tmp/jiradbreplbackup.log
root#reporting-db:~#
Please let me know if you have any suggestion for this issue.

If you look further down in your dump file, you'll see the view is created a second time in a format that will look more complete. The name of the view is first created as a temporary table (that create statement is what you've shown above) in case other views depend on it (ref https://github.com/twitter-forks/mysql/blob/master/client/mysqldump.c#L2557). Then it is created as a view with the full create statement you expect. Here is an example.
Given this table:
create table mytable (id int, firstname varchar(50), lastname varchar(50));
And this view on that table:
create view myview as select id, firstname from mytable;
Do a dump of the database:
mysqldump -u user -ppassword mydatabase >/tmp/dump.sql
Resulting dump file:
cat /tmp/dump.sql
-- MySQL dump 10.13 Distrib 8.0.31, for macos13.0 (x86_64)
...
--
-- Table structure for table `mytable`
--
DROP TABLE IF EXISTS `mytable`;
...
CREATE TABLE `mytable` (
`id` int DEFAULT NULL,
`firstname` varchar(50) DEFAULT NULL,
`lastname` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
...
--
-- Dumping data for table `mytable`
--
...
--
-- Temporary view structure for view `myview`
--
DROP TABLE IF EXISTS `myview`;
/*!50001 DROP VIEW IF EXISTS `myview`*/;
...
/*!50001 CREATE VIEW `myview` AS SELECT
1 AS `id`,
1 AS `firstname`*/;
...
--
-- Final view structure for view `myview`
--
/*!50001 DROP VIEW IF EXISTS `myview`*/;
...
/*!50001 CREATE ALGORITHM=UNDEFINED */
/*!50013 DEFINER=`user`#`localhost` SQL SECURITY DEFINER */
/*!50001 VIEW `myview` AS select `mytable`.`id` AS `id`,`mytable`.`firstname` AS `firstname` from `mytable` */;
...
-- Dump completed on 2023-01-17 7:40:31

Related

How do I make creation of my MySQL view respect the same character encoding as the database's default?

I'm using MySQL 5.5.37. The default encoding for my database is utf8 ...
mysql> SELECT default_character_set_name FROM information_schema.SCHEMATA WHERE schema_name = "my_db";
+----------------------------+
| default_character_set_name |
+----------------------------+
| utf8 |
+----------------------------+
1 row in set (0.00 sec)
However, when I create a view using
CREATE OR REPLACE VIEW report_toc_item AS
SELECT ti.*, pti.type_id PARENT_TYPE_ID
FROM toc_item ti
JOIN toc_item pti ON pti.id = ti.parent_id
WHERE ti.type_id = 'sub_segment';
and then I show that view, the cndoing is showing as "latin1" instead of utf8 ...
mysql> show create view report_toc_item;
+--------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
| View | Create View | character_set_client | collation_connection |
+--------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
| report_toc_item | CREATE ALGORITHM=UNDEFINED DEFINER=`mytestuser`#`localhost` SQL SECURITY DEFINER VIEW `report_toc_item` AS select `ti`.`ID` AS `ID`,`ti`.`PARENT_ID` AS `PARENT_ID`,`ti`.`TOC_ID` AS `TOC_ID`,`ti`.`TITLE` AS `TITLE`,`ti`.`DESCRIPTION` AS `DESCRIPTION`,`ti`.`TYPE_ID` AS `TYPE_ID`,`ti`.`ORDER_NUM` AS `ORDER_NUM`,`ti`.`MY_OBJECT_SEGMENT_ID` AS `MY_OBJECT_SEGMENT_ID`,`ti`.`MY_OBJECT_SEGMENT_ORDER_NUM` AS `MY_OBJECT_SEGMENT_ORDER_NUM`,`ti`.`ELEMENT_ID` AS `ELEMENT_ID`,`ti`.`UNIT_TOC_ITEM_ID` AS `UNIT_TOC_ITEM_ID`,`ti`.`SHORT_NAME` AS `SHORT_NAME`,`ti`.`THIRD_PARTY_PROMPT_ID` AS `THIRD_PARTY_PROMPT_ID`,`pti`.`TYPE_ID` AS `PARENT_TYPE_ID` from (`toc_item` `ti` join `toc_item` `pti` on((`pti`.`ID` = `ti`.`PARENT_ID`))) where ((`ti`.`TYPE_ID` = 'sub_segment') and ((`pti`.`TYPE_ID` = 'lesson') or (`pti`.`TYPE_ID` = 'activity') or (`pti`.`TYPE_ID` = 'activity_practice') or (`pti`.`TYPE_ID` = 'unit_opener'))) | latin1 | latin1_swedish_ci |
+--------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+
1 row in set (0.00 sec)
How do I make my view respect the default encoding of the database? I would prefer not to hard-code the encoding into the creation of the view.
SHOW CREATE DATABASE mydb; -- to see what the database's charset is
SHOW VIEW x; -- to get the body
DROP VIEW x;
SET NAMES utf8mb4; -- or whatever you got from the first step
CREATE VIEW ... -- recreate the view.
Yeah, clumsy.

Importing to Couch-DB changes encoding

I have a database I am using that has support for different languages, the issue I am running into is, in the source SQL data, the format is correct.
MariaDB [stmtransit]> SELECT * FROM routes WHERE route_id = 181;
+----------+-----------+------------------+------------------+------------+------------+------------------------------------------+-------------+------------------+
| route_id | agency_id | route_short_name | route_long_name | route_desc | route_type | route_url | route_color | route_text_color |
+----------+-----------+------------------+------------------+------------+------------+------------------------------------------+-------------+------------------+
| 181 | 1 | 369 | Côte-des-Neiges | NULL | 3 | http://www.stm.info/fr/infos/reseaux/bus | 009EE0 | NULL |
+----------+-----------+------------------+------------------+------------+------------+------------------------------------------+-------------+------------------+
1 row in set (0.00 sec)
When I move do the query and move it into CouchDB, it changes accents and anything other than plain characters to
Côte-des-Neiges
My request is
function queryRouteTable(db, route_id) {
return db.query({
sql: "SELECT * FROM routes WHERE route_id = ?;",
values: [route_id],
})
.take(1);
}
Then my upload to couch uses rx and rx-couch with the code, and no matter where I view the document.route_long_name after the initial grab, its always formatted wrong.
What am I missing, why does it change after initial grab.
To display the current character encoding set for a particular database, type the following command at the mysql> prompt. Replace DBNAME with the database name:
SELECT default_character_set_name FROM information_schema.SCHEMATA S WHERE schema_name = "DBNAME";
If you have your encoding set per table use the following command. Replace DBNAME with the database name, and TABLENAME with the name of the table:
SELECT CCSA.character_set_name FROM information_schema.`TABLES` T,information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA WHERE CCSA.collation_name = T.table_collation AND T.table_schema = "DBNAME" AND T.table_name = "TABLENAME";
IMPORTANT: BACKUP YOUR DATABASE
If you have a working backup of your database you can convert it from your current encoding to UTF-8 by issuing the following commands:
mysql --database=DBNAME -B -N -e "SHOW TABLES" | awk '{print "SET foreign_key_checks = 0; ALTER TABLE", $1, "CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci; SET foreign_key_checks = 1; "}' | mysql --database=DBNAME
And in prompt:
ALTER DATABASE DBNAME CHARACTER SET utf8 COLLATE utf8_general_ci;
Now you should be able to export using UTF-8 and import into couch using UTF-8 encoding...
Hope that helps...
Turns out MariaDB has a bug which turns the database formating to latin1 intead of utf8
To correct for this you must go to /etc/my.cnf
remove all instances of
default-character-set=utf8
find title "mysqld" in my.cnf and put under it
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
and save.
Then restart mariadb.

SQL syntax error when creating a script stored procedure in MYSQL

I have an error when I create a stored procedure in mysql
delimiter //
CREATE PROCEDURE insertvaluefield(IN p_field INT)
BEGIN
DECLARE v_type VARCHAR(80) DEFAULT '';
SELECT type_field
FROM Field
WHERE id_field = p_field
LIMIT 1;
IF v_type IN ('text', 'textarea') THEN
DELETE FROM ValueField
WHERE field_valuefield = p_field;
END IF;
END;
//
delimiter ;
I do this to save this procedure in my database :
mysql -uXXXX -pXXXX DataBase < script.sql
This is a result error :
ERROR 1064 (42000) at line 2: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE PROCEDURE insertvaluefield(IN p_field INT) BEGIN DECLARE v_ty' at line 1
Correct Syntax to execute SQL script from Terminal is:
mysql -uXXXX -pXXXX -e "use your_db_name; source path_to_sql_file/script.sql;"
Check the output also, It is showing in test DB:
mysql> show procedure status;
+------+------------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+------- ---------------+--------------------+
| Db | Name | Type | Definer | Modified | Created | Security_type | Comment | character_set_client | collation_connection | Database Collation |
+------+------------------+-----------+----------------+-------------------- -+---------------------+---------------+---------+----------------------+----------------------+--------------------+
| test | insertvaluefield | PROCEDURE | root#localhost | 2015-11-19 21:18:34 | 2015-11-19 21:18:34 | DEFINER | | utf8 | utf8_general_ci | latin1_swedish_ci |
+------+------------------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+----------------------+--------------------+
I can't reproduce the problem. In my test case everything works as expected.
Remember to assign type_field to v_type:
...
-- SELECT `type_field`
SELECT `type_field` INTO `v_type`
...
File: /path/to/file/script.sql
USE `test`;
DELIMITER //
SELECT DATABASE()//
DROP TABLE IF EXISTS `Field`//
DROP PROCEDURE IF EXISTS `insertvaluefield`//
CREATE TABLE `Field` (
`id_field` INT,
`type_field` VARCHAR(80)
)//
CREATE PROCEDURE `insertvaluefield`(IN `p_field` INT)
BEGIN
DECLARE `v_type` VARCHAR(80) DEFAULT '';
-- SELECT `type_field`
SELECT `type_field` INTO `v_type`
FROM `Field`
WHERE `id_field` = `p_field`
LIMIT 1;
IF `v_type` IN ('text', 'textarea') THEN
/*
DELETE FROM ValueField
WHERE field_valuefield = p_field;
*/
/* TEST CASE */
SELECT CONCAT('
DELETE FROM `ValueField`
WHERE `field_valuefield` = ', `p_field`, ';
') `DELETE`;
END IF;
END//
INSERT INTO `Field`
(`id_field`, `type_field`)
VALUES
(1, 'text'),
(2, 'textarea')//
DELIMITER ;
CALL `insertvaluefield`(1)\G
CALL `insertvaluefield`(2)\G
Test:
$ mysql -u XXXX -p < /path/to/file/script.sql
Enter password:
DATABASE()
test
*************************** 1. row ***************************
DELETE:
DELETE FROM `ValueField`
WHERE `field_valuefield` = 1;
*************************** 1. row ***************************
DELETE:
DELETE FROM `ValueField`
WHERE `field_valuefield` = 2;

How to echo print statements while executing a sql script

We have a simple sql script which needs to be executed against a MySQL database and we would like print log statements on the progress of the script (e.g. Inserted 10 records into foo or Deleted 5 records from bar). How do we do this?
I would like to know the syntax to be used for insert/update/delete statements.
How do I know about the number of rows affected by my statement(s).
I would also like to control printing them using a ECHO off or on command at the top of the script.
The script should be portable across Windows / Linux OS.
This will give you are simple print within a sql script:
select 'This is a comment' AS '';
Alternatively, this will add some dynamic data to your status update if used directly after an update, delete, or insert command:
select concat ("Updated ", row_count(), " rows") as '';
I don't know if this helps:
suppose you want to run a sql script (test.sql) from the command line:
mysql < test.sql
and the contents of test.sql is something like:
SELECT * FROM information_schema.SCHEMATA;
\! echo "I like to party...";
The console will show something like:
CATALOG_NAME SCHEMA_NAME DEFAULT_CHARACTER_SET_NAME
def information_schema utf8
def mysql utf8
def performance_schema utf8
def sys utf8
I like to party...
So you can execute terminal commands inside an sql statement by just using \!, provided the script is run via a command line.
\! #terminal_commands
Just to make your script more readable, maybe use this proc:
DELIMITER ;;
DROP PROCEDURE IF EXISTS printf;
CREATE PROCEDURE printf(thetext TEXT)
BEGIN
select thetext as ``;
END;
;;
DELIMITER ;
Now you can just do:
call printf('Counting products that have missing short description');
What about using mysql -v to put mysql client in verbose mode ?
For mysql you can add \p to the commands to have them print out while they run in the script:
SELECT COUNT(*) FROM `mysql`.`user`
\p;
Run it in the MySQL client:
mysql> source example.sql
--------------
SELECT COUNT(*) FROM `mysql`.`user`
--------------
+----------+
| COUNT(*) |
+----------+
| 24 |
+----------+
1 row in set (0.00 sec)
You can use print -p -- in the script to do this example :
#!/bin/ksh
mysql -u username -ppassword -D dbname -ss -n -q |&
print -p -- "select count(*) from some_table;"
read -p get_row_count1
print -p -- "select count(*) from some_other_table;"
read -p get_row_count2
print -p exit ;
#
echo $get_row_count1
echo $get_row_count2
#
exit

How do I kill all the processes in Mysql "show processlist"?

Because I see a lot of processes there, and the "time" column shows big values for all of them.
Mass killing operation saves time. Do it in MySql itself:
Run these commands
mysql> select concat('KILL ',id,';') from information_schema.processlist
where user='root' and time > 200 into outfile '/tmp/a.txt';
mysql> source /tmp/a.txt;
Reference
---------edit------------
if you do not want to store in file, store in a variable
Just run in your command prompt
> out1=$(mysql -B test -uroot -proot --disable-column-names -e "select concat('KILL ',id,';') from information_schema.processlist where user='root' and time > 200;")
> out2= $(mysql -B test -uroot -proot --disable-column-names -e "$out1")
You need to kill them one by one, MySQL does not have any massive kill command. You can script it in any language, for example in PHP you can use something like:
$result = mysql_query("SHOW FULL PROCESSLIST");
while ($row=mysql_fetch_array($result)) {
$process_id=$row["Id"];
if ($row["Time"] > 200 ) {
$sql="KILL $process_id";
mysql_query($sql);
}
}
I have also searched how to parse through MySQL the command SHOW PROCESSLIST and ended with a one-liner in a Shell:
mysqladmin processlist -u <USERNAME> -p<PASSWORD> | \
awk '$2 ~ /^[0-9]/ {print "KILL "$2";"}' | \
mysql -u <USERNAME> -p<PASSWORD>
mysqladmin processlist will print a table with the thread ids;
awk will parse from the second column only the numbers (thread ids) and generate MySQL KILL commands;
and finally the last call to mysql will execute the passed commands.
You can run grep before the awk command to filter a particular database name.
Only for mariaDB
It doesn't get simpler then this, Just execute this in mysql prompt.
kill USER username;
It will kill all process under provided username. because most of the people use same user for all purpose, it works!
I have tested this on MariaDB not sure about mysql.
The following will create a simple stored procedure that uses a cursor to kill all processes one by one except for the process currently being used:
DROP PROCEDURE IF EXISTS kill_other_processes;
DELIMITER $$
CREATE PROCEDURE kill_other_processes()
BEGIN
DECLARE finished INT DEFAULT 0;
DECLARE proc_id INT;
DECLARE proc_id_cursor CURSOR FOR SELECT id FROM information_schema.processlist;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
OPEN proc_id_cursor;
proc_id_cursor_loop: LOOP
FETCH proc_id_cursor INTO proc_id;
IF finished = 1 THEN
LEAVE proc_id_cursor_loop;
END IF;
IF proc_id <> CONNECTION_ID() THEN
KILL proc_id;
END IF;
END LOOP proc_id_cursor_loop;
CLOSE proc_id_cursor;
END$$
DELIMITER ;
It can be run with SELECTs to show the processes before and after as follows:
SELECT * FROM information_schema.processlist;
CALL kill_other_processes();
SELECT * FROM information_schema.processlist;
Or... in shell...
service mysql restart
Yeah, I know, I'm lazy, but it can be handy too.
I recently needed to do this and I came up with this
-- GROUP_CONCAT turns all the rows into 1
-- #q:= stores all the kill commands to a variable
select #q:=GROUP_CONCAT(CONCAT('KILL ',ID) SEPARATOR ';')
FROM information_schema.processlist
-- If you don't need it, you can remove the WHERE command altogether
WHERE user = 'user';
-- Creates statement and execute it
PREPARE stmt FROM #q;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
That way, you don't need to store to file and run all queries with a single command.
KILL ALL SELECT QUERIES
select concat('KILL ',id,';')
from information_schema.processlist
where user='root'
and INFO like 'SELECT%' into outfile '/tmp/a.txt';
source /tmp/a.txt;
If you don't have information_schema:
mysql -e "show full processlist" | cut -f1 | sed -e 's/^/kill /' | sed -e 's/$/;/' ; > /tmp/kill.txt
mysql> . /tmp/kill.txt
This snipped worked for me (MySQL server 5.5) to kill all MySQL processes :
mysql -e "show full processlist;" -ss | awk '{print "KILL "$1";"}'| mysql
We can do it by MySQL Workbench. Just execute this:
kill id;
Example:
kill 13412
That will remove it.
I'd combine bash and mysql:
for i in $(mysql -Ne "select id from information_schema.processlist where user like 'foo%user' and time > 300;"); do
mysql -e "kill ${i}"
done
Here is a solution that you can execute without relying on the operating system:
STEP 1: Create a stored procedure.
DROP PROCEDURE IF EXISTS kill_user_processes$$
CREATE PROCEDURE `kill_user_processes`(
IN user_to_kill VARCHAR(255)
)
READS SQL DATA
BEGIN
DECLARE name_val VARCHAR(255);
DECLARE no_more_rows BOOLEAN;
DECLARE loop_cntr INT DEFAULT 0;
DECLARE num_rows INT DEFAULT 0;
DECLARE friends_cur CURSOR FOR
SELECT CONCAT('KILL ',id,';') FROM information_schema.processlist WHERE USER=user_to_kill;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET no_more_rows = TRUE;
OPEN friends_cur;
SELECT FOUND_ROWS() INTO num_rows;
the_loop: LOOP
FETCH friends_cur
INTO name_val;
IF no_more_rows THEN
CLOSE friends_cur;
LEAVE the_loop;
END IF;
SET #s = name_val;
PREPARE stmt FROM #s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SELECT name_val;
SET loop_cntr = loop_cntr + 1;
END LOOP the_loop;
SELECT num_rows, loop_cntr;
END $$
DELIMITER ;
STEP 2: Call the stored procedure giving it the name of a database user whose processes you want to kill. You could rewrite the stored procedure to filter on some other criteria if you like.
CALL kill_user_processes('devdba');
mysqladmin pr -u 'USERNAME' -p'PASSWORD' | awk '$2~/^[0-9]+/{print $2}' | xargs -i mysqladmin -u 'USERNAME' -p'PASSWORD' kill {}
login to Mysql as admin:
mysql -uroot -ppassword;
And than run command:
mysql> show processlist;
You will get something like below :
+----+-------------+--------------------+----------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-------------+--------------------+----------+---------+------+-------+------------------+
| 49 | application | 192.168.44.1:51718 | XXXXXXXX | Sleep | 183 | | NULL ||
| 55 | application | 192.168.44.1:51769 | XXXXXXXX | Sleep | 148 | | NULL |
| 56 | application | 192.168.44.1:51770 | XXXXXXXX | Sleep | 148 | | NULL |
| 57 | application | 192.168.44.1:51771 | XXXXXXXX | Sleep | 148 | | NULL |
| 58 | application | 192.168.44.1:51968 | XXXXXXXX | Sleep | 11 | | NULL |
| 59 | root | localhost | NULL | Query | 0 | NULL | show processlist |
+----+-------------+--------------------+----------+---------+------+-------+------------------+
You will see complete details of different connections. Now you can kill the sleeping connection as below:
mysql> kill 52;
Query OK, 0 rows affected (0.00 sec)
The following worked great for me:
echo "show processlist" | mysql | grep -v ^Id | awk '{print $1}' | xargs -i echo "KILL {}; | mysql"
for python language, you can do like this
import pymysql
connection = pymysql.connect(host='localhost',
user='root',
db='mysql',
cursorclass=pymysql.cursors.DictCursor)
with connection.cursor() as cursor:
cursor.execute('SHOW PROCESSLIST')
for item in cursor.fetchall():
if item.get('Time') > 200:
_id = item.get('Id')
print('kill %s' % item)
cursor.execute('kill %s', _id)
connection.close()
An easy way would be to restart the mysql server..
Open "services.msc" in windows Run, select Mysql from the list. Right click and stop the service. Then Start again and all the processes would have been killed except the one (the default reserved connection)
#! /bin/bash
if [ $# != "1" ];then
echo "Not enough arguments.";
echo "Usage: killQueryByDB.sh <db_name>";
exit;
fi;
DB=${1};
for i in `mysql -u <user> -h localhost ${DB} -p<password> -e "show processlist" | sed 's/\(^[0-9]*\).*/\1/'`; do
echo "Killing query ${i}";
mysql -u <user> -h localhost ${DB} -p<password> -e "kill query ${i}";
done;
Sometimes I have some zombies mysql processes that can't be killed (using MAMP Pro).
First get a list of all mysql processes:
ps -ax | grep mysql
Next kill every one of them with (replace processId with the first column in previous command result):
kill -9 processId
I used the command flush tables to kill all inactive connections which where actually the mass problem.
If you are using laravel then this is for you:
$query = "SHOW FULL PROCESSLIST";
$results = DB::select(DB::raw($query));
foreach($results as $result){
if($result->Command == "Sleep"){
$sql="KILL ". $result->Id;
DB::select(DB::raw($sql));
}
}
Of-course, you should use this use Illuminate\Support\Facades\DB; after your namespace.
Kill does not work if the running process is not yours. I merged some of these solutions and extended them to create a more simple one (at least to me).
m=mysql -p $password -h $host -u$user #you can also inline it of course
for i in `$m -e "show processlist" | awk '/$anySearchString/ {print $1}'`; do $m -e "call mysql.rds_kill($i);"; done
Query 1
select concat('KILL ',id,';') from information_schema.processlist where user='username' into outfile '/tmp/a.txt';
Query 2
source a.txt
This will enable you to kill all the queries in show processlist by specific user.