partial table dump using mysqldump

There are times when you only want to take partial dump of a mysql table. You can use mysqldump command with one extra option “–where”. You will have to provide your where clause (partial dump condition) in the “–” where option.

Below is the command to take a partial dump of a table.

mysqldump -u [username] -p  [databasename] [mytablename] --where  " mycolumn = 'somevalue' " > filename.sql

Related Posts:
1. MySQL dump in XML format.
2. MySQL general query logs.

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

Increase MySQL maximum connection limit

MySQL’s default configuration sets the maximum concurrent connections to 151. If you get a too many connections error when you are trying to connect to MySQL server, this means that all available connections are in use by other clients / Users.

The number of connections permitted is controlled by the max_connections system variable. Its default value is 151, although you can it can accept a max of 152 connection (1 Connection reserved for Super User).  If you need to support more connections, you can set a larger value for this .

MySQL 3.x:

# vi /etc/my.cnf
set-variable = max_connections = 170

MySQL 4.x and 5.x:

# vi /etc/my.cnf
max_connections = 170

Restart MySQL once you’ve made the changes.

 /etc/init.d/mysqld restart 

or

 service mysqld restart 

You can also change this variable after logging into mysql server as a root user.

max conn var change
change max connections limit from mysql terminal

Find the existing limit :

show variables like ‘max_connections’;

Set the updated maximum connection limit :

SET GLOBAL max_connections = 170;

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'