Mysql Dump in XML format

To take dump in XML format you have to provide an extra option in default mysqldump command. This can be useful in cases where you want to migrate your MySQL database to some other database.

You can use below command to take MySQL Database dump in XML format.


mysqldump --xml -u[username] -p[password] [db_name] > filename.xml

or


mysqldump -X -u[username] -p[password] [db_name] > filename.xml

For example if you have a database test_database with an username as root and password as mypassword the command to take dump will be,


mysqldump --xml -u root -p mypassword test_database > filename.xml

MySQL General query logs

MySQL prints each query in General query logs. This can be helpful in application profiling if you want to see how many and which queries are being run in a transaction and when commit/rollback is executed.

You can enable MySQL general query logs by running below sql command from root user:

SET GLOBAL general_log = 'ON';

By default, logs will be stored at /var/log/query.log ,  If you are getting the “permission error” after running above command.

Edit your mysqld.conf file which is located under /etc/mysql/mysql.conf.d ( This location may be different for your installation )

vi /etc/mysql/mysql.conf.d/mysqld.conf

and change the value of

general_log_file = /var/log/query.log

to

general_log_file = /var/lib/mysql/your_log_filename.log

In my case default general query log file location was /var/log/query.log & MySQL didn’t had permission to write at that location.

Credits : Chirag Jain

MySQL query with the conditions as ‘NOT LIKE IN’

While working with MySQL database i needed to write a query which will exclude rows where a column contains one or more words from a set of words. i.e I want to fetch data from student from table where name doesn’t contain words shashank,john etc.

Initially i was trying to write a query like this  :

select * from TABLE
where
Column NOT Like IN
('%VALUE1%', '%VALUE2%', '%VALUE3%') 

but i was getting error as IN doesn’t work with Like.

There is two way you can write this query.

Method 1 :

Add multiple Like statement, each for every word to be excluded.

Select * from TABLE
where Column NOT Like '%VALUE1%'
AND Column NOT Like '%VALUE2%'
AND Column NOT Like '%VALUE3%' 

Method 2 :

If you have set of words which you want to include/exclude in search from a particular column. You may want to use regular expression function of mysql.

Exclude set of words from a column :

SELECT
*
FROM
TABLE
WHERE
Column NOT REGEXP 'VALUE1|VALUE2|VALUE3' 

Search set of words in a column :

SELECT
*
FROM
TABLE
WHERE
Column REGEXP 'VALUE1|VALUE2|VALUE3'