<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Kavoir &#187; SQL / MySQL Tips and Tutorials</title>
	<atom:link href="http://www.kavoir.com/category/programming/sql-and-mysql/feed" rel="self" type="application/rss+xml" />
	<link>http://www.kavoir.com</link>
	<description>Just another dumbass webmaster, goofing around...</description>
	<lastBuildDate>Thu, 09 Feb 2012 01:59:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>MySQL: LOAD DATA LOCAL INFILE only imports 1 or 2 rows?</title>
		<link>http://www.kavoir.com/2012/02/mysql-load-data-local-infile-only-imports-1-or-2-rows.html</link>
		<comments>http://www.kavoir.com/2012/02/mysql-load-data-local-infile-only-imports-1-or-2-rows.html#comments</comments>
		<pubDate>Tue, 07 Feb 2012 01:23:04 +0000</pubDate>
		<dc:creator>Yang Yang</dc:creator>
				<category><![CDATA[SQL / MySQL Tips and Tutorials]]></category>

		<guid isPermaLink="false">http://www.kavoir.com/?p=2739</guid>
		<description><![CDATA[It is common to import CSV files into MySQL database. You can do this with phpMyAdmin with small CSV files but with large ones, you would probably encounter the memory error and had to switch to MySQL command line “LOAD DATA LOCAL INFILE” to do the job. It looks like something like this: LOAD DATA [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>It is common to import CSV files into MySQL database. You can do this with phpMyAdmin with small CSV files but with large ones, you would probably encounter the memory error and had to switch to MySQL command line “LOAD DATA LOCAL INFILE” to do the job.</p>

<p>It looks like something like this:</p>
<pre><code>LOAD DATA LOCAL INFILE 'your.csv'
INTO TABLE `your_table`
FIELDS TERMINATED BY ','
ENCLOSED BY '&quot;'
LINES TERMINATED BY '\n'
(field1, field2, field3)</code></pre>
<p>And then you encounter another problem that it only imports the first 1 or 2 rows and then it stops. After some researching and trying, I was sure it’s something to do with the “<strong>LINES TERMINATED BY</strong>” directive. Depending on the platform that the CSV file is created on, the line delimiter may be</p>
<ol>
<li><strong>\n</strong> </li>
<li><strong>\r\n</strong> </li>
<li><strong>\r</strong> </li>
</ol>
<p>And you need to be correct on the line delimiter to properly parse the CSV file. </p>
<p>So the solution is to <strong>try them all</strong> one by one and see which one of them works. Chances are, one of them would make the whole command successfully import ALL rows.</p>
<p>Another simple approach is to deprive the whole command of the “<strong>LINES TERMINATED BY</strong>” directive and let MySQL do the call. It’ll probably detect things right but in my case, this doesn’t work but specifying ‘\r’.</p>
<h3>Related Posts:</h3>
<ul class="similar-posts">
<li><a href="http://www.kavoir.com/2010/11/mysql-export-table-to-csv-text-files-for-excel.html" rel="bookmark" title="November 18, 2010">MySQL: Export Table to CSV Text Files for Excel</a></li>
<li><a href="http://www.kavoir.com/2010/10/mysql-log-files-are-taking-a-lot-of-disk-space-how-to-disable-and-delete-them.html" rel="bookmark" title="October 25, 2010">MySQL log files are taking a lot of disk space &ndash; How to disable and delete them?</a></li>
<li><a href="http://www.kavoir.com/2009/03/sql-randomly-shuffle-rows-or-records-reorder-them-in-a-random-order.html" rel="bookmark" title="March 23, 2009">SQL: Randomly Shuffle Rows or Records &ndash; Reorder them in a random order</a></li>
<li><a href="http://www.kavoir.com/2011/02/mysql-how-to-export-a-database-table-to-xml.html" rel="bookmark" title="February 17, 2011">MySQL: How to export a database / table to XML?</a></li>
<li><a href="http://www.kavoir.com/2009/05/mysql-update-multiple-rows-with-one-single-query.html" rel="bookmark" title="May 17, 2009">MySQL: Update Multiple Rows or Records with One Single Query</a></li>
</ul>
<p><!-- Similar Posts took 2.498 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kavoir.com/2012/02/mysql-load-data-local-infile-only-imports-1-or-2-rows.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL Performance Tips</title>
		<link>http://www.kavoir.com/2011/03/mysql-performance-tips.html</link>
		<comments>http://www.kavoir.com/2011/03/mysql-performance-tips.html#comments</comments>
		<pubDate>Sat, 12 Mar 2011 03:49:50 +0000</pubDate>
		<dc:creator>Yang Yang</dc:creator>
				<category><![CDATA[SQL / MySQL Tips and Tutorials]]></category>

		<guid isPermaLink="false">http://www.kavoir.com/2011/03/mysql-performance-tips.html</guid>
		<description><![CDATA[I’m not an expert but I’ve had my own errors and trials along the way so here I am. These are all the tips I believe you should use when you are optimizing for MySQL database performance. Some stuff may be very handy, but they may not be good for performance. Design Normalize first, and [...]]]></description>
			<content:encoded><![CDATA[<p></p><p><img class="alignright size-full wp-image-2368" title="MySQL" src="http://www.kavoir.com/wp-content/uploads/2011/03/MySQL.png" alt="MySQL" width="110" height="57" />I’m not an expert but I’ve had my own errors and trials along the way so here I am. These are all the tips I believe you should use when you are optimizing for MySQL database performance. Some stuff may be very handy, but they may not be good for performance.<span id="more-2180"></span></p>
<h2>Design</h2>
<ol>
<li>Normalize first, and de-normalize where appropriate. Table joins are never lags in performance. Use them!</li>
<li>Choose the appropriate collation / charset. UTF16 is omnipotent but it requires 2 times the storage. UTF8 delivers more characters but it’s slower than latin1. Unless it’s necessary, use latin1.</li>
<li><strong>utf8_general_ci</strong> is slightly faster than utf8_unicode_ci, thus better in performance.</li>
<li>Use <strong>NOT NULL</strong> wherever possible.</li>
<li>Use indexes. Create indexes by analyzing the SELECTs you are doing on the table.</li>
<li><strong>Indexing</strong> increases performance in <strong>selecting</strong>, yet decreases performance in <strong>inserting</strong>. So don’t index everything but only those necessary. And don’t make duplicate indexes.</li>
</ol>
<h2>Querying</h2>
<ol>
<li>Use smallest data types possible.</li>
<li><strong>IN (…)</strong> is a total fail. Avoid it wherever possible.</li>
<li><strong>ORDER BY RAND()</strong> is a fail too.</li>
<li>When you need to SELECT … to make sure there’s no duplicate unique index values before inserting something, use <strong>INSERT … ON DUPLICATE KEY UPDATE …</strong> instead.</li>
<li>If your app writes a lot, use InnoDB; if it reads a lot, use MyISAM. Here’s a <a href="http://www.kavoir.com/2009/09/mysql-engines-innodb-vs-myisam-a-comparison-of-pros-and-cons.html">comparison</a>.</li>
<li>Never use <strong>LIMIT xxxx1, xxxx2</strong> with large tables especially when offset xxxx1 is a pretty large number. Instead, give a more strict <strong>WHERE …</strong> clause with <strong>LIMIT xxxx2</strong>.</li>
<li>If a feature is deprecated, stop using it.</li>
<li>Avoid wildcards % or _ at the beginning of LIKE queries, e.g. LIKE ‘%bloom’ – this should be avoided. For instance, you should store email addresses reversed (REVERSED()) so it’s faster to search by domain.</li>
<li>If you have a large set of data rows to be inserted at once, create a combined INSERT query string in PHP first – that is, to bulk insert them in batches rather than one by one. LOAD DATA is also a better option than individual INSERT.</li>
<li>Prefer <strong>GROUP BY</strong> over DISTINCT.</li>
<li>Frequently visit <a href="http://dev.mysql.com/doc/refman/5.0/en/slow-query-log.html">Slow Query Log</a>.</li>
<li>Avoid calculated values in WHERE … , LIMIT … , ORDER BY … , etc.</li>
<li>Use <strong>EXPLAIN …</strong> to get an idea of how MySQL is doing with your queries thus better optimize it. For example, it would suggest the indexes you should have created.</li>
<li>Don’t <strong>SELECT * …</strong> on everything, SELECT only what you need.</li>
<li>Be wary of lots of small queries that may be triggered in loops. Instead, combine them and use one large query wherever possible.</li>
</ol>
<h2>Storage Engines</h2>
<ol>
<li>Know the pros and cons of different storage engines and use them accordingly. But try them out in the test environment before making the decision.</li>
<li>Archive old data such as logs in ARCHIVE tables or MERGE tables.</li>
<li>BLACKHOLE engine is very fast for busy things like logs.</li>
</ol>
<h2>my.ini</h2>
<ol>
<li>Increase the default key buffer size: <strong>key_buffer = 128M</strong></li>
<li>Increase the default number of tables that can be kept open: <strong>table_cache = 128</strong></li>
<li>Increase the default sort buffer size:<br />
<strong>sort_buffer_size = 32M</strong><br />
<strong>myisam_sort_buffer_size = 32M</strong></li>
<li>Disable binary logging that is for data replication by commenting out the line like this:  <strong># log-bin=mysql-bin</strong></li>
</ol>
<h2>What to read from here:</h2>
<ol>
<li>Indexing: <a href="http://dev.mysql.com/doc/refman/5.0/en/mysql-indexes.html">http://dev.mysql.com/doc/refman/5.0/en/mysql-indexes.html</a></li>
<li>“LIMIT” Optimization: <a href="http://dev.mysql.com/doc/refman/5.0/en/limit-optimization.html">http://dev.mysql.com/doc/refman/5.0/en/limit-optimization.html</a></li>
<li>“ORDER BY” Optimization<a href="http://dev.mysql.com/doc/refman/5.0/en/order-by-optimization.html">http://dev.mysql.com/doc/refman/5.0/en/order-by-optimization.html</a></li>
<li>Efficient Paging: <a href="http://www.slideshare.net/suratbhati/efficient-pagination-using-mysql-6187107">http://www.slideshare.net/suratbhati/efficient-pagination-using-mysql-6187107</a></li>
<li>Configuration Tweaks: <a href="http://docs.cellblue.nl/2007/03/17/easy-mysql-performance-tweaks/">http://docs.cellblue.nl/2007/03/17/easy-mysql-performance-tweaks/</a></li>
</ol>
<h3>Related Posts:</h3>
<ul class="similar-posts">
<li><a href="http://www.kavoir.com/2009/05/mysql-huge-table-not-responding-after-adding-index.html" rel="bookmark" title="May 18, 2009">MySQL: Huge Table Not Responding after Adding Index</a></li>
<li><a href="http://www.kavoir.com/2009/04/mysql-id-between-start-and-end-instead-of-limit-start-step-for-better-database-performance.html" rel="bookmark" title="April 12, 2009">MySQL: id BETWEEN start AND end Instead of LIMIT start, step For Better Database Performance</a></li>
<li><a href="http://www.kavoir.com/2011/03/mysql-get-the-exact-size-of-a-database-by-query.html" rel="bookmark" title="March 5, 2011">MySQL: Get the exact size of a database by SQL query</a></li>
<li><a href="http://www.kavoir.com/2009/05/mysql-insert-if-doesnt-exist-otherwise-update-the-existing-row.html" rel="bookmark" title="May 2, 2009">MySQL: Insert if doesn&rsquo;t exist otherwise update the existing row</a></li>
<li><a href="http://www.kavoir.com/2009/05/mysql-insert-into-select-from.html" rel="bookmark" title="May 13, 2009">MySQL: INSERT INTO &hellip; SELECT &hellip; FROM &hellip; Selectively on Table Columns / Fields to Combine Multiple Tables into One</a></li>
</ul>
<p><!-- Similar Posts took 2.963 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kavoir.com/2011/03/mysql-performance-tips.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>MySQL: Get the exact size of a database by SQL query</title>
		<link>http://www.kavoir.com/2011/03/mysql-get-the-exact-size-of-a-database-by-query.html</link>
		<comments>http://www.kavoir.com/2011/03/mysql-get-the-exact-size-of-a-database-by-query.html#comments</comments>
		<pubDate>Sat, 05 Mar 2011 02:13:45 +0000</pubDate>
		<dc:creator>Yang Yang</dc:creator>
				<category><![CDATA[SQL / MySQL Tips and Tutorials]]></category>

		<guid isPermaLink="false">http://www.kavoir.com/2011/03/mysql-get-the-exact-size-of-a-database-by-query.html</guid>
		<description><![CDATA[phpMyAdmin doesn’t show the exact size in MB when the size of your database exceeds 1GB. It just shows something like 4.2GB, truncating everything out in the 100MB precision. So is it possible to get the database size in MB in exact numbers using MySQL query? Yes, it is: SELECT CONCAT(sum(ROUND(((DATA_LENGTH + INDEX_LENGTH - DATA_FREE) [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>phpMyAdmin doesn’t show the exact size in MB when the size of your database exceeds 1GB. It just shows something like 4.2GB, truncating everything out in the 100MB precision. So is it possible to get the database size in MB in exact numbers using MySQL query?</p>

<p>Yes, it is:</p>
<pre><code>SELECT CONCAT(sum(ROUND(((DATA_LENGTH + INDEX_LENGTH - DATA_FREE) / 1024 / 1024),2))," MB") AS Size FROM INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA like '<strong>YOUR_DATABASE_NAME_HERE</strong>%' ;</code></pre>
<p>Just change <strong>YOUR_DATABASE_NAME_HERE</strong> into the name of your database. And it would prints out something like this:</p>
<pre><code>+------------+
| Size       |
+------------+
| 4906.79 MB |
+------------+
1 row in set (0.05 sec)</code></pre>
<p>You can also get the size of a specific table. Read here: <a href="http://www.novell.com/communities/node/8706/check-mysql-database-size-using-sql-query">http://www.novell.com/communities/node/8706/check-mysql-database-size-using-sql-query</a><br />
<h3>Related Posts:</h3>
<ul class="similar-posts">
<li><a href="http://www.kavoir.com/2010/07/mysql-check-if-a-table-exists.html" rel="bookmark" title="July 30, 2010">MySQL: Check if a table exists</a></li>
<li><a href="http://www.kavoir.com/2009/09/mysql-select-and-show-all-mysql-users.html" rel="bookmark" title="September 3, 2009">MySQL: Select and Show all MySQL Users</a></li>
<li><a href="http://www.kavoir.com/2009/06/mysql-php-store-form-textarea-value-to-mysql-database-table.html" rel="bookmark" title="June 6, 2009">MySQL, PHP: Store form textarea value or data to MySQL database table</a></li>
<li><a href="http://www.kavoir.com/2009/06/mysql-php-display-mysql-table-fields-and-data.html" rel="bookmark" title="June 6, 2009">MySQL, PHP: Display MySQL table fields and data</a></li>
<li><a href="http://www.kavoir.com/2011/02/mysql-how-to-export-a-database-table-to-xml.html" rel="bookmark" title="February 17, 2011">MySQL: How to export a database / table to XML?</a></li>
</ul>
<p><!-- Similar Posts took 3.936 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kavoir.com/2011/03/mysql-get-the-exact-size-of-a-database-by-query.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL: Find non-ASCII characters in a table column</title>
		<link>http://www.kavoir.com/2011/03/mysql-find-non-ascii-characters.html</link>
		<comments>http://www.kavoir.com/2011/03/mysql-find-non-ascii-characters.html#comments</comments>
		<pubDate>Wed, 02 Mar 2011 15:54:28 +0000</pubDate>
		<dc:creator>Yang Yang</dc:creator>
				<category><![CDATA[SQL / MySQL Tips and Tutorials]]></category>

		<guid isPermaLink="false">http://www.kavoir.com/2011/03/mysql-find-non-ascii-characters.html</guid>
		<description><![CDATA[ASCII is the most fundamental character set that has been around since the early days of command line interfaces. There are 128 (0 &#8211; 127) most basic ASCII characters such as a-z, A-Z, 0-9, and all the printable punctuations you can type out by a single strike of your keyboard. As all ASCII characters have [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>ASCII is the most fundamental character set that has been around since the early days of command line interfaces. There are 128 (0 &#8211; 127) most basic ASCII characters such as a-z, A-Z, 0-9, and all the printable punctuations you can type out by a single strike of your keyboard.</p>

<p>As all ASCII characters have an internal decimal value of 0 – 127, which is 0&#215;00 – 0x0F in heximal values, you can find all the non-ASCII characters in <em>my_column</em> by query:</p>
<pre><code>SELECT * FROM my_table WHERE <strong>NOT HEX(my_column) REGEXP '^([0-7][0-9A-F])*$'</strong>;</code></pre>
<p>On the other hand, if you wish to find all records that a certain column (<em>my_column</em>) contains ASCII characters:</p>
<pre><code>SELECT * FROM my_table WHERE <strong>HEX(my_column) REGEXP '^([0-7][0-9A-F])*$'</strong>;</code></pre>
<h3>Related Posts:</h3>
<ul class="similar-posts">
<li><a href="http://www.kavoir.com/2009/05/mysql-deleting-duplicate-rows-or-records-selecting-unique-rows-into-a-new-table.html" rel="bookmark" title="May 16, 2009">MySQL: Deleting Duplicate Rows or Records | Selecting Unique Rows into A New Table</a></li>
<li><a href="http://www.kavoir.com/2009/05/mysql-find-duplicate-entry-rows-records.html" rel="bookmark" title="May 19, 2009">MySQL: Find Duplicate Entry / Rows / Records</a></li>
<li><a href="http://www.kavoir.com/2009/01/sql-insert-into-select-to-generate-or-combine-records-from-existing-ones-and-insert-into-an-existing-table.html" rel="bookmark" title="January 4, 2009">SQL: INSERT INTO … SELECT … to generate or combine records from existing ones and insert into an existing table</a></li>
<li><a href="http://www.kavoir.com/2009/05/mysql-update-multiple-rows-with-one-single-query.html" rel="bookmark" title="May 17, 2009">MySQL: Update Multiple Rows or Records with One Single Query</a></li>
<li><a href="http://www.kavoir.com/2009/01/sql-create-table-select-to-generate-new-table-from-existing-tables.html" rel="bookmark" title="January 4, 2009">SQL: CREATE TABLE &#8230; SELECT &#8230; to generate new table from existing tables</a></li>
</ul>
<p><!-- Similar Posts took 4.371 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kavoir.com/2011/03/mysql-find-non-ascii-characters.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL: How to export a database / table to XML?</title>
		<link>http://www.kavoir.com/2011/02/mysql-how-to-export-a-database-table-to-xml.html</link>
		<comments>http://www.kavoir.com/2011/02/mysql-how-to-export-a-database-table-to-xml.html#comments</comments>
		<pubDate>Thu, 17 Feb 2011 15:59:49 +0000</pubDate>
		<dc:creator>Yang Yang</dc:creator>
				<category><![CDATA[SQL / MySQL Tips and Tutorials]]></category>

		<guid isPermaLink="false">http://www.kavoir.com/2011/02/mysql-how-to-export-a-database-table-to-xml.html</guid>
		<description><![CDATA[You can export any MySQL database or table into an XML file by the exporting capabilities of phpMyAdmin, the web interface of MySQL. The problem with this approach, however, is that with large tables, you may have to manually export the table more than once by sections into several sequential XML files. A better approach [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>You can export any MySQL database or table into an XML file by the exporting capabilities of <a href="http://www.phpmyadmin.net">phpMyAdmin</a>, the web interface of MySQL. The problem with this approach, however, is that with large tables, you may have to manually export the table more than once by sections into several sequential XML files.</p>

<p>A better approach is by native MySQL command line that will create and store all entries of the table into one XML file:</p>
<pre><code>mysql -u <strong>db_user</strong> -p <strong>db_name</strong> --xml -e &quot;SELECT * FROM <strong>table_name</strong>&quot; &gt; <strong>table_name.xml</strong></code></pre>
<p>Wherein <strong>db_user</strong> is your MySQL server user, <strong>db_name</strong> is the name of the database and <strong>table_name</strong> is the name of the table that you would like exported to XML. The resulted XML will be stored in <strong>table_name.xml</strong>. </p>
<p>Note that this is different from <a href="http://www.kavoir.com/2009/09/mysql-how-to-backup-all-databases-at-once-as-root-with-mysqldump.html">mysqldump</a> in that it’s basically executing a query by the –e switch and output the results in XML (&#8211;xml). The results is then directed to the out file rather than displayed on terminal screen.</p>
<h3>Related Posts:</h3>
<ul class="similar-posts">
<li><a href="http://www.kavoir.com/2009/09/mysql-how-to-backup-all-databases-at-once-as-root-with-mysqldump.html" rel="bookmark" title="September 5, 2009">MySQL: How to backup ALL databases as root with mysqldump at once?</a></li>
<li><a href="http://www.kavoir.com/2008/12/backup-and-recover-a-mysql-database-under-command-line.html" rel="bookmark" title="December 14, 2008">Backup and recover a MySQL database under command line</a></li>
<li><a href="http://www.kavoir.com/2009/04/mysql-id-between-start-and-end-instead-of-limit-start-step-for-better-database-performance.html" rel="bookmark" title="April 12, 2009">MySQL: id BETWEEN start AND end Instead of LIMIT start, step For Better Database Performance</a></li>
<li><a href="http://www.kavoir.com/2010/11/mysql-export-table-to-csv-text-files-for-excel.html" rel="bookmark" title="November 18, 2010">MySQL: Export Table to CSV Text Files for Excel</a></li>
<li><a href="http://www.kavoir.com/2009/06/reset-mysql-root-password-after-you-forgot-or-lost-it.html" rel="bookmark" title="June 20, 2009">How to Recover or Reset MySQL root Password after You Forgot and Lost It</a></li>
</ul>
<p><!-- Similar Posts took 2.737 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kavoir.com/2011/02/mysql-how-to-export-a-database-table-to-xml.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL: Export Table to CSV Text Files for Excel</title>
		<link>http://www.kavoir.com/2010/11/mysql-export-table-to-csv-text-files-for-excel.html</link>
		<comments>http://www.kavoir.com/2010/11/mysql-export-table-to-csv-text-files-for-excel.html#comments</comments>
		<pubDate>Thu, 18 Nov 2010 13:57:14 +0000</pubDate>
		<dc:creator>Yang Yang</dc:creator>
				<category><![CDATA[SQL / MySQL Tips and Tutorials]]></category>

		<guid isPermaLink="false">http://www.kavoir.com/2010/11/mysql-export-table-to-csv-text-files-for-excel.html</guid>
		<description><![CDATA[MySQL tables can be exported to SQL dump file which is basically a text file of SQL queries that can be used to import the table back into database. To export MySQL tables into other formats such as CSV, phpMyAdmin proves to be very handy if you have changed the execution timeout in seconds to [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>MySQL tables can be exported to SQL dump file which is basically a text file of SQL queries that can be used to import the table back into database. To export MySQL tables into other formats such as CSV, phpMyAdmin proves to be very handy if you have changed the execution timeout in seconds to zero (so it never times out) – or it won&#8217;t work with large tables. However, there&#8217;s another way to do this in native SQL query and it works until the end of a very large table:<span id="more-2109"></span></p>
<pre><code>SELECT * FROM mytable INTO OUTFILE "c:/mytable.csv"
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"'
LINES TERMINATED BY "\n";</code></pre>
<p>The INTO OUTFILE command in this MySQL query will store all rows selected from the table mytable into the text file c:/mytable.csv in the form:</p>
<pre><code>"1","Anthony","24"
"2","Rachel","27"</code></pre>
<p>Now you can open and use the CSV file in another application such as Excel and manipulate it any way you like.</p>
<p>If you need another format, just change the query accordingly.</p>
<p>Add column / field headers at the beginning of the CSV file</p>
<p>To add column names at the beginning of the CSV file so each field is identified, use this SQL snippet:</p>
<pre><code><strong>SELECT 'id', 'name', 'age' UNION</strong>
SELECT * FROM mytable INTO OUTFILE "c:/mytable.csv"
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"'
LINES TERMINATED BY "\n";</code></pre>
<p>That is, adding &#8220;SELECT &#8216;id&#8217;, &#8216;title&#8217;, &#8216;slug&#8217; UNION&#8221;. And you will have:</p>
<pre><code><strong>"id","name","age"</strong>
"1","Anthony","24"
"2","Rachel","27"</code></pre>
<p>The awkward thing is you have to manually add the field names one by one. For now, I know no option other than this that can add the column names before all the records when you are dumping CSV files from MySQL, except with phpMyAdmin.<br />
<h3>Related Posts:</h3>
<ul class="similar-posts">
<li><a href="http://www.kavoir.com/2012/02/mysql-load-data-local-infile-only-imports-1-or-2-rows.html" rel="bookmark" title="February 7, 2012">MySQL: LOAD DATA LOCAL INFILE only imports 1 or 2 rows?</a></li>
<li><a href="http://www.kavoir.com/2011/02/mysql-how-to-export-a-database-table-to-xml.html" rel="bookmark" title="February 17, 2011">MySQL: How to export a database / table to XML?</a></li>
<li><a href="http://www.kavoir.com/2009/06/mysql-php-display-mysql-table-fields-and-data.html" rel="bookmark" title="June 6, 2009">MySQL, PHP: Display MySQL table fields and data</a></li>
<li><a href="http://www.kavoir.com/2009/01/sql-create-table-select-to-generate-new-table-from-existing-tables.html" rel="bookmark" title="January 4, 2009">SQL: CREATE TABLE &#8230; SELECT &#8230; to generate new table from existing tables</a></li>
<li><a href="http://www.kavoir.com/2009/05/mysql-replace-substring-with-another-string-the-mysql-string-replace-function.html" rel="bookmark" title="May 16, 2009">MySQL: Replace Substring with Another String &ndash; the MySQL String Replace Function</a></li>
</ul>
<p><!-- Similar Posts took 2.559 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kavoir.com/2010/11/mysql-export-table-to-csv-text-files-for-excel.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>MySQL log files are taking a lot of disk space &#8211; How to disable and delete them?</title>
		<link>http://www.kavoir.com/2010/10/mysql-log-files-are-taking-a-lot-of-disk-space-how-to-disable-and-delete-them.html</link>
		<comments>http://www.kavoir.com/2010/10/mysql-log-files-are-taking-a-lot-of-disk-space-how-to-disable-and-delete-them.html#comments</comments>
		<pubDate>Mon, 25 Oct 2010 15:10:18 +0000</pubDate>
		<dc:creator>Yang Yang</dc:creator>
				<category><![CDATA[SQL / MySQL Tips and Tutorials]]></category>

		<guid isPermaLink="false">http://www.kavoir.com/2010/10/mysql-log-files-are-taking-a-lot-of-disk-space-how-to-disable-and-delete-them.html</guid>
		<description><![CDATA[I have WAMP server installed on my local computer for PHP and MySQL development. After using it for a while, the MySQL installation folder has run up some seriously huge log files that are taking enormous amount of disk space. We are talking 10s of GB here. The file names are like ibdata1, mysql-bin.000017, etc.. [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>I have WAMP server installed on my local computer for PHP and MySQL development. After using it for a while, the MySQL installation folder has run up some seriously huge log files that are taking enormous amount of disk space. We are talking 10s of GB here. The file names are like ibdata1, mysql-bin.000017, etc.. It appears it’s because I frequently have <a href="http://www.scrapingweb.com/databases.html">large databases</a> in and out by the ‘mysql’ and ‘mysqldump’ command, building up large stacks of data importing and exporting logs for data rollback and recovery or something.</p>

<p>Simply deleting the log files may result in problems such as being unable to start MySQL again. Tried that and I had to reinstall it to make it work again.</p>
<p>After some searching, I’ve found this query to solve my problem. Just execute it after you have logged in MySQL via command line – of course you will need the root privileges:</p>
<pre><code>PURGE BINARY LOGS BEFORE '2036-12-12 12:12:12';</code></pre>
<p>Something like that would purge and delete all the huge logs to free the disk. Just make the date time to be as sci-fi as possible so that all log files are purged.<br />
<h3>Related Posts:</h3>
<ul class="similar-posts">
<li><a href="http://www.kavoir.com/2012/02/mysql-load-data-local-infile-only-imports-1-or-2-rows.html" rel="bookmark" title="February 7, 2012">MySQL: LOAD DATA LOCAL INFILE only imports 1 or 2 rows?</a></li>
<li><a href="http://www.kavoir.com/2009/05/1-tip-about-alter-table-xxx-add-index-yyy.html" rel="bookmark" title="May 14, 2009">1 tip about ALTER TABLE xxx ADD INDEX yyy</a></li>
<li><a href="http://www.kavoir.com/2009/07/mysql-command-line-character-set-option-for-importing-sql-files-encoded-in-utf8.html" rel="bookmark" title="July 16, 2009">mysql command line character set option for importing SQL files encoded in UTF8</a></li>
<li><a href="http://www.kavoir.com/2009/04/php-copying-renaming-and-moving-a-file.html" rel="bookmark" title="April 22, 2009">PHP: Copy, Rename or Move a File</a></li>
<li><a href="http://www.kavoir.com/2010/11/mysql-export-table-to-csv-text-files-for-excel.html" rel="bookmark" title="November 18, 2010">MySQL: Export Table to CSV Text Files for Excel</a></li>
</ul>
<p><!-- Similar Posts took 2.730 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kavoir.com/2010/10/mysql-log-files-are-taking-a-lot-of-disk-space-how-to-disable-and-delete-them.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL: Change Default Character Set or Default Collation in phpMyAdmin</title>
		<link>http://www.kavoir.com/2010/10/mysql-change-default-character-set-or-default-collation-in-phpmyadmin.html</link>
		<comments>http://www.kavoir.com/2010/10/mysql-change-default-character-set-or-default-collation-in-phpmyadmin.html#comments</comments>
		<pubDate>Sat, 16 Oct 2010 01:50:59 +0000</pubDate>
		<dc:creator>Yang Yang</dc:creator>
				<category><![CDATA[SQL / MySQL Tips and Tutorials]]></category>

		<guid isPermaLink="false">http://www.kavoir.com/2010/10/mysql-change-default-character-set-or-default-collation-in-phpmyadmin.html</guid>
		<description><![CDATA[It can be annoying when MySQL imports your UTF8 database (which contains exotic characters other than those in English) in the default character set of latin1_swedish_ci, jeopardizing the text content. It is also annoying when phpMyAdmin does the same and when you forgot to set the collation to utf8_general_ci for the new database which is [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>It can be annoying when MySQL imports your UTF8 database (which contains exotic characters other than those in English) in the default character set of latin1_swedish_ci, jeopardizing the text content. It is also annoying when phpMyAdmin does the same and when you forgot to set the collation to <strong>utf8_general_ci</strong> for the new database which is very probably going to store utf8 characters.</p>

<p>Let’s fix this once and for all.</p>
<p>Just locate and open the MySQL configuration file <strong>my.ini</strong> and find the section <strong>[mysqld]</strong>. Add the following directive:</p>
<pre><code>[mysqld]
default-character-set=utf8</code></pre>
<p>Save to my.ini and restart MySQL demon. Now MySQL will use utf8 as the default character set when importing databases or creating new databases. The default collation of phpMyAdmin has also changed to utf8_general_ci.<br />
<h3>Related Posts:</h3>
<ul class="similar-posts">
<li><a href="http://www.kavoir.com/2009/07/mysql-command-line-character-set-option-for-importing-sql-files-encoded-in-utf8.html" rel="bookmark" title="July 16, 2009">mysql command line character set option for importing SQL files encoded in UTF8</a></li>
<li><a href="http://www.kavoir.com/2012/02/mysql-load-data-local-infile-only-imports-1-or-2-rows.html" rel="bookmark" title="February 7, 2012">MySQL: LOAD DATA LOCAL INFILE only imports 1 or 2 rows?</a></li>
<li><a href="http://www.kavoir.com/2009/04/php-how-to-process-a-string-one-byte-character-at-a-time.html" rel="bookmark" title="April 23, 2009">PHP: How to process a string one byte (character) at a time</a></li>
<li><a href="http://www.kavoir.com/2011/03/mysql-find-non-ascii-characters.html" rel="bookmark" title="March 2, 2011">MySQL: Find non-ASCII characters in a table column</a></li>
<li><a href="http://www.kavoir.com/2009/09/mysql-how-to-backup-all-databases-at-once-as-root-with-mysqldump.html" rel="bookmark" title="September 5, 2009">MySQL: How to backup ALL databases as root with mysqldump at once?</a></li>
</ul>
<p><!-- Similar Posts took 2.734 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kavoir.com/2010/10/mysql-change-default-character-set-or-default-collation-in-phpmyadmin.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL: Check if a table exists</title>
		<link>http://www.kavoir.com/2010/07/mysql-check-if-a-table-exists.html</link>
		<comments>http://www.kavoir.com/2010/07/mysql-check-if-a-table-exists.html#comments</comments>
		<pubDate>Fri, 30 Jul 2010 02:48:15 +0000</pubDate>
		<dc:creator>Yang Yang</dc:creator>
				<category><![CDATA[SQL / MySQL Tips and Tutorials]]></category>

		<guid isPermaLink="false">http://www.kavoir.com/2010/07/mysql-check-if-a-table-exists.html</guid>
		<description><![CDATA[When I was creating an installation script, I needed to check if a table exists to make sure the installation had not been performed yet. How did I do that? I use this simple query to get whether a table exists in the specified database: SELECT table_name FROM information_schema.tables WHERE table_schema = 'db_name' AND table_name [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>When I was creating an installation script, I needed to check if a table exists to make sure the installation had not been performed yet. How did I do that?</p>

<p>I use this simple query to get whether a table exists in the specified database:</p>
<pre><code>SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'db_name'
AND table_name = 'table_name'</code></pre>
<p>Just fill in the ‘<strong>db_name</strong>’ as well as ‘<strong>table_name</strong>’. If this query returns 1 row, the table <strong>db_name.table_name</strong> does exist, otherwise it does not.</p>
<h3>Related Posts:</h3>
<ul class="similar-posts">
<li><a href="http://www.kavoir.com/2011/03/mysql-get-the-exact-size-of-a-database-by-query.html" rel="bookmark" title="March 5, 2011">MySQL: Get the exact size of a database by SQL query</a></li>
<li><a href="http://www.kavoir.com/2008/11/using-sql-to-find-records-existing-in-one-table-but-not-in-another.html" rel="bookmark" title="November 18, 2008">Using SQL to Find records existing in one table but not in another</a></li>
<li><a href="http://www.kavoir.com/2009/05/mysql-insert-into-select-from.html" rel="bookmark" title="May 13, 2009">MySQL: INSERT INTO &hellip; SELECT &hellip; FROM &hellip; Selectively on Table Columns / Fields to Combine Multiple Tables into One</a></li>
<li><a href="http://www.kavoir.com/2009/01/sql-create-table-select-to-generate-new-table-from-existing-tables.html" rel="bookmark" title="January 4, 2009">SQL: CREATE TABLE &#8230; SELECT &#8230; to generate new table from existing tables</a></li>
<li><a href="http://www.kavoir.com/2009/06/mysql-php-display-mysql-table-fields-and-data.html" rel="bookmark" title="June 6, 2009">MySQL, PHP: Display MySQL table fields and data</a></li>
</ul>
<p><!-- Similar Posts took 2.806 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kavoir.com/2010/07/mysql-check-if-a-table-exists.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MySQL logic operators: How to use AND, OR together in WHERE clauses in one query?</title>
		<link>http://www.kavoir.com/2010/07/mysql-logic-operators-how-to-use-and-or-together-in-where-clauses-in-one-query.html</link>
		<comments>http://www.kavoir.com/2010/07/mysql-logic-operators-how-to-use-and-or-together-in-where-clauses-in-one-query.html#comments</comments>
		<pubDate>Fri, 09 Jul 2010 02:25:07 +0000</pubDate>
		<dc:creator>Yang Yang</dc:creator>
				<category><![CDATA[SQL / MySQL Tips and Tutorials]]></category>

		<guid isPermaLink="false">http://www.kavoir.com/2010/07/mysql-logic-operators-how-to-use-and-or-together-in-where-clauses-in-one-query.html</guid>
		<description><![CDATA[AND and OR are common logic operators across all programming languages and in MySQL, they mean the same thing. A statement (a.k.a, a condition in any WHERE clauses such as id &#62; 100) can be either true or false. A group / combination of statements can be true or false, depending on their own values [...]]]></description>
			<content:encoded><![CDATA[<p></p><p><strong>AND</strong> and <strong>OR</strong> are common <em>logic operators</em> across all programming languages and in MySQL, they mean the same thing. A statement (a.k.a, a condition in any WHERE clauses such as <em>id &gt; 100</em>) can be either <strong>true</strong> or <strong>false</strong>. A group / combination of statements can be <strong>true</strong> or <strong>false</strong>, depending on their own values (true or false) and the logic operators that have joined them.</p>

<p><strong>AND</strong> and <strong>OR</strong> each can join 2 statements. </p>
<h3>AND – both statements must be true</h3>
<p>For example, in this MySQL query:</p>
<pre><code>SELECT * FROM student WHERE age &gt; 10 <strong>AND</strong> grade &lt; 4</code></pre>
<p>All students who are graded less than 4 and older than 10 will be selected. The logic operator <strong>AND</strong> commands that both of the conditional statements (<strong>age &gt; 10</strong> and <strong>grade &lt; 4</strong>) must be true for the whole statement (<strong>age &gt; 10 AND grade &lt; 4</strong>) to be true. You can add more conditions and ask them all to be true to narrow down the selection:</p>
<pre><code>SELECT * FROM student WHERE age &gt; 10 <strong>AND</strong> grade &lt; 4 <strong>AND</strong> sex = 'female'</code></pre>
<p>Which effectively selects all the female students who are older than 10 and have a grade of lower than 4 because the <strong>AND</strong> operators mean that all 3 statements must be true for the whole, joined statement (<strong>age &gt; 10 AND grade &lt; 4 AND sex = &#8216;female&#8217;</strong>) to be true.</p>
<h3>OR – at least one statement must be true</h3>
<p>Consider this example,</p>
<pre><code>SELECT * FROM student WHERE grade &gt; 4 <strong>OR</strong> grade &lt; 2</code></pre>
<p>This simply selects all the students who have a grade higher than 4 <strong>or</strong> lower than 2. All the students who satisfy one or more of these statements are selected. Students who satisfy none of these statements are neglected. You can as well use more than one OR in a series:</p>
<pre><code>SELECT * FROM student WHERE grade &gt; 4 <strong>OR</strong> age &lt; 9 <strong>OR</strong> surname = 'Bush'</code></pre>
<p>So that any student who has a grade higher than 4 or is less than 9 years old or has a surname of Bush will be selected.</p>
<h3>How to use AND and OR together?</h3>
<p>Consider the following example,</p>
<pre><code>SELECT * FROM student WHERE grade &gt; 2 <strong>AND</strong> grade &lt; 5 <strong>OR</strong> age &gt; 8 <strong>AND</strong> age &lt; 12</code></pre>
<p>As <strong>AND</strong> has a higher priority (effectively so in almost all programming languages) than <strong>OR</strong>, it will be evaluated before any OR statements. Therefore, this query will select all those students whose grade is higher than 2 <strong>and</strong> lower than 5 <strong>or</strong> those whose age is older than 8 <strong>and</strong> younger than 12.</p>
<p>You can add parenthesis but because <strong>AND</strong> has a higher priority in parsing the logic statement, it’s the same:</p>
<pre><code>SELECT * FROM student WHERE (grade &gt; 2 <strong>AND</strong> grade &lt; 5) <strong>OR</strong> (age &gt; 8 <strong>AND</strong> age &lt; 12)</code></pre>
<p>However, as parenthesis has the highest priority, if you add them in a different way such as this:</p>
<pre><code>SELECT * FROM student WHERE (grade &gt; 2 <strong>AND</strong> grade &lt; 5 <strong>OR</strong> age &gt; 8) <strong>AND</strong> age &lt; 12</code></pre>
<p>It would give completely different results. Firstly, <strong>grade &gt; 2 AND grade &lt; 5</strong> must be true or <strong>age &gt; 8</strong> must be true, secondly, <strong>age &lt; 12</strong> must be true. For example, these students will satisfy this WHERE clause:</p>
<ol>
<li>grade = 4, age = 7</li>
<li>age = 9</li>
<li>grade = 3, age = 11</li>
</ol>
<p>These students don’t satisfy this WHERE clause:</p>
<ol>
<li>age = 14</li>
<li>grade = 5, age = 7</li>
</ol>
<h3>Related Posts:</h3>
<ul class="similar-posts">
<li><a href="http://www.kavoir.com/2009/06/php-count-the-number-of-occurrences-of-each-unique-value-in-an-array.html" rel="bookmark" title="June 2, 2009">PHP: Count the Number of Occurrences of Each Unique Value in an Array</a></li>
<li><a href="http://www.kavoir.com/2009/02/css-difference-between-opacity0-visibilityhidden-and-displaynone.html" rel="bookmark" title="February 8, 2009">CSS: Difference between opacity:0, visibility:hidden and display:none</a></li>
<li><a href="http://www.kavoir.com/2009/04/jquery-get-the-text-and-value-of-the-selected-option-of-html-select-element.html" rel="bookmark" title="April 16, 2009">jQuery: Get the text and value of the selected option of HTML select element</a></li>
<li><a href="http://www.kavoir.com/2007/06/a-grand-new-redesign-magmania-wordpress-theme.html" rel="bookmark" title="June 22, 2007">A grand new redesign &#8211; MagMania wordpress theme</a></li>
<li><a href="http://www.kavoir.com/2009/01/sql-create-table-select-to-generate-new-table-from-existing-tables.html" rel="bookmark" title="January 4, 2009">SQL: CREATE TABLE &#8230; SELECT &#8230; to generate new table from existing tables</a></li>
</ul>
<p><!-- Similar Posts took 3.090 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kavoir.com/2010/07/mysql-logic-operators-how-to-use-and-or-together-in-where-clauses-in-one-query.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to bring down / optimize memory usage in your unmanaged Linux VPS box and avoid OOM (Out Of Memory) errors?</title>
		<link>http://www.kavoir.com/2010/07/how-to-bring-down-optimize-memory-usage-in-your-unmanaged-linux-vps-box-and-avoid-oom-out-of-memory-errors.html</link>
		<comments>http://www.kavoir.com/2010/07/how-to-bring-down-optimize-memory-usage-in-your-unmanaged-linux-vps-box-and-avoid-oom-out-of-memory-errors.html#comments</comments>
		<pubDate>Thu, 01 Jul 2010 02:00:07 +0000</pubDate>
		<dc:creator>Yang Yang</dc:creator>
				<category><![CDATA[Apache Web Server Tutorials & Tips]]></category>
		<category><![CDATA[PHP Tips & Tutorials]]></category>
		<category><![CDATA[SQL / MySQL Tips and Tutorials]]></category>

		<guid isPermaLink="false">http://www.kavoir.com/2010/07/how-to-bring-down-optimize-memory-usage-in-your-unmanaged-linux-vps-box-and-avoid-oom-out-of-memory-errors.html</guid>
		<description><![CDATA[The other day I was very upset about some extraordinary down times of my unmanaged VPS box at Linode. As it’s unmanaged, support staff at Linode are not responsible for the failures. I contacted them and they told me it’s OOM (Out Of Memory), pointing me to the right documentation to figure out how to [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>The other day I was very upset about some extraordinary down times of my unmanaged VPS box at <a href="http://www.kavoir.com/2009/09/moving-from-slicehost-to-linode-an-initial-vps-hosting-review.html">Linode</a>. As it’s unmanaged, support staff at Linode are not responsible for the failures. I contacted them and they told me it’s OOM (Out Of Memory), pointing me to the right documentation to figure out how to get the problem sorted out myself. After a few tweaks and observations for a week, so it seems that I have successfully optimized my VPS server to take on more traffic with less resources such as RAM.</p>

<p>The problem almost always lies in where the user is free to feed stuff to your website or program. Sometimes <a href="http://www.converthub.com">Convert Hub</a> spikes in memory usage and forces my box to use swap that relies on disk I/O to work. This happens when someone uploads an ultra large picture to be processed or converted. While I may restrict the size of the picture that is allowed to be uploaded, I may also do the following settings to optimize the entire LAMP environment so the other websites enjoy it as well.</p>
<h3>Apache 2 Low-Memory Optimization</h3>
<p>Use this command to identify the MPM you are using:</p>
<pre><code>apache2 -V | grep 'MPM' # for Debian-based systems</code></pre>
<pre><code>httpd -V | grep 'MPM' # for Fedora/CentOS systems</code></pre>
<p>Find and change these settings in your Apache 2 configuration file (usually found at /etc/apache2/apache2.conf):</p>
<pre><code>StartServers 1
MinSpareServers 3
MaxSpareServers 6
ServerLimit 24
MaxClients 24
MaxRequestsPerChild 3000</code></pre>
<p>Switch to <a href="http://www.lighttpd.net/">Lighttpd</a> or <a href="http://www.litespeedtech.com/">Litespeed</a> if possible.</p>
<h3>MySQL Low-Memory Optimization</h3>
<p>Same as above, find and change these settings of the MySQL configuration file (may be at /etc/mysql/my.cnf) accordingly:</p>
<pre><code>key_buffer = 16K
max_allowed_packet = 1M
thread_stack = 64K
table_cache = 4
sort_buffer = 64K
net_buffer_length = 2K</code></pre>
<h3>PHP Low-Memory Optimization</h3>
<p>Find your <a href="http://www.kavoir.com/2009/06/where-is-phpini-located.html">PHP configuration file (php.ini)</a> and modify the PHP script memory limit to 32M or less (default is 128M):</p>
<pre><code>memory_limit = 32M</code></pre>
<h3>Related Posts:</h3>
<ul class="similar-posts">
<li><a href="http://www.kavoir.com/2010/02/php-get-the-file-uploading-limit-max-file-size-allowed-to-upload.html" rel="bookmark" title="February 23, 2010">PHP: Get the File Uploading Limit &ndash; Max File Size Allowed to Upload</a></li>
<li><a href="http://www.kavoir.com/2010/09/check-how-much-memory-your-php-script-is-using-php-script-memory-usage.html" rel="bookmark" title="September 18, 2010">Check how much memory your PHP script is using. (PHP script memory usage)</a></li>
<li><a href="http://www.kavoir.com/2009/12/php-regular-expression-matching-input-subject-string-length-limit.html" rel="bookmark" title="December 12, 2009">PHP: Subject String Length Limit of Regular Expression Matching Functions</a></li>
<li><a href="http://www.kavoir.com/2011/03/mysql-performance-tips.html" rel="bookmark" title="March 12, 2011">MySQL Performance Tips</a></li>
<li><a href="http://www.kavoir.com/2011/01/linux-how-to-find-only-text-files.html" rel="bookmark" title="January 22, 2011">Linux: How to &#8216;find&#8217; and search ONLY text files?</a></li>
</ul>
<p><!-- Similar Posts took 5.119 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kavoir.com/2010/07/how-to-bring-down-optimize-memory-usage-in-your-unmanaged-linux-vps-box-and-avoid-oom-out-of-memory-errors.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A few database security tips &#8211; things to do to effectively protect MySQL databases</title>
		<link>http://www.kavoir.com/2010/02/a-few-database-security-tips-things-to-do-to-effectively-protect-mysql-databases.html</link>
		<comments>http://www.kavoir.com/2010/02/a-few-database-security-tips-things-to-do-to-effectively-protect-mysql-databases.html#comments</comments>
		<pubDate>Wed, 10 Feb 2010 08:18:00 +0000</pubDate>
		<dc:creator>Yang Yang</dc:creator>
				<category><![CDATA[Information Security]]></category>
		<category><![CDATA[SQL / MySQL Tips and Tutorials]]></category>

		<guid isPermaLink="false">http://www.kavoir.com/2010/02/a-few-database-security-tips-things-to-do-to-effectively-protect-mysql-databases.html</guid>
		<description><![CDATA[I’d like to share with you some tips about hardening the database part of your application. Here are a few things you can do in protecting the databases from being compromised in security: Create separate users with ONLY necessary privileges (as few as possible) to connect to the database for common daily tasks. Never use [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>I’d like to share with you some tips about hardening the <a href="http://www.usabledatabases.com/">database</a> part of your application. Here are a few things you can do in protecting the databases from being compromised in security:<span id="more-1625"></span></p>
<ol>
<li>Create separate users with <strong>ONLY</strong> necessary privileges (as few as possible) to connect to the database for common daily tasks. Never use the database owner / creator or even MySQL root user in your PHP scripts to perform routine tasks.</li>
<li>Protect against <a href="http://www.kavoir.com/2009/01/php-prevent-sql-injection-attacks.html">SQL injection attacks</a> by <a href="http://www.kavoir.com/2009/01/php-escape-string-for-sql-mysqlireal_escape_string-and-pdo.html">escaping</a> ALL incoming input after ensuring data types with a variety of PHP <a href="http://www.php.net/manual/en/ref.var.php">variable type</a> and <a href="http://www.php.net/manual/en/ref.ctype.php">character type</a> validation functions.</li>
<li>The <a href="http://php.net/manual/en/function.sprintf.php">sprintf</a>() function is both useful and secure in constructing SQL queries because of the built-in type checking. Better yet, use <a href="http://www.kavoir.com/2009/01/php-escape-string-for-sql-mysqlireal_escape_string-and-pdo.html">PDO</a>.</li>
<li>Turn off <strong>error messages</strong> MySQL or <a href="http://www.php.net/manual/en/security.errors.php">PHP</a> outputs when things go wrong so crackers know nothing about the technical details of your build such as database schema. As a matter of fact, a good rule of thumb in web application security is that the less people know about your application’s internal structure, the better.</li>
<li>For advanced SQL developers, extra abstraction layer in SQL in the form of <strong>stored procedures</strong> can benefit security because you implement yet another depth of defense and hide the schema of the database from the outside world.</li>
<li>For mission critical applications, it goes without saying that custom <strong>logging of database accesses</strong> can help a lot in identifying security risks.</li>
<li>If the database contains very sensitive data such as credit card information, you are strongly recommended to <strong>encrypt these tables or fields</strong>. Just use PHP cryptography extensions such as <a href="http://php.net/manual/en/book.mcrypt.php">Mcrypt</a> to encrypt any data that are to be stored and decrypt them when they are being retrieved.</li>
</ol>
<h3>Related Posts:</h3>
<ul class="similar-posts">
<li><a href="http://www.kavoir.com/2011/03/mysql-get-the-exact-size-of-a-database-by-query.html" rel="bookmark" title="March 5, 2011">MySQL: Get the exact size of a database by SQL query</a></li>
<li><a href="http://www.kavoir.com/2010/03/web-application-security-must-read-books-the-best-at-amazon.html" rel="bookmark" title="March 6, 2010">Web Application Security Books (PHP, MySQL, Apache), the Best at Amazon</a></li>
<li><a href="http://www.kavoir.com/2009/04/the-secure-way-to-store-passwords-with-php.html" rel="bookmark" title="April 20, 2009">The Secure Way to Store Passwords with PHP</a></li>
<li><a href="http://www.kavoir.com/2009/09/mysql-select-and-show-all-mysql-users.html" rel="bookmark" title="September 3, 2009">MySQL: Select and Show all MySQL Users</a></li>
<li><a href="http://www.kavoir.com/2009/06/best-mysql-books-to-learn-mysql-database-php-applications.html" rel="bookmark" title="June 17, 2009">Best MySQL Books to Learn MySQL Database Programming and Development (+ PHP Applications)</a></li>
</ul>
<p><!-- Similar Posts took 2.868 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kavoir.com/2010/02/a-few-database-security-tips-things-to-do-to-effectively-protect-mysql-databases.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL: How to backup ALL databases as root with mysqldump at once?</title>
		<link>http://www.kavoir.com/2009/09/mysql-how-to-backup-all-databases-at-once-as-root-with-mysqldump.html</link>
		<comments>http://www.kavoir.com/2009/09/mysql-how-to-backup-all-databases-at-once-as-root-with-mysqldump.html#comments</comments>
		<pubDate>Sat, 05 Sep 2009 06:19:41 +0000</pubDate>
		<dc:creator>Yang Yang</dc:creator>
				<category><![CDATA[Hosting Tips & Deals]]></category>
		<category><![CDATA[SQL / MySQL Tips and Tutorials]]></category>

		<guid isPermaLink="false">http://www.kavoir.com/2009/09/mysql-how-to-backup-all-databases-at-once-as-root-with-mysqldump.html</guid>
		<description><![CDATA[When you have a VPS or dedicated server to manage, typically you’d have a bunch of different mysql users granted the privileges of every particular database for the sake of security. While this works well in segregating the privileges and preventing hackers from gaining access to all databases upon the compromise of only one mysql [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>When you have a <a href="http://www.kavoir.com/2009/09/moving-from-slicehost-to-linode-an-initial-vps-hosting-review.html">VPS</a> or dedicated server to manage, typically you’d have a bunch of different mysql users granted the privileges of every particular database for the sake of security. While this works well in segregating the privileges and preventing hackers from gaining access to all databases upon the compromise of only one mysql user account, it means much more work when you are backing up all those databases on the server.</p>

<p>The solution is to use the <strong>mysqldump</strong> command as root. To backup all databases on the mysql server no matter which users they belong to, try the following via SSH:</p>
<pre><code>mysqldump -u root -p --<strong>all-databases</strong> &gt; all.sql</code></pre>
<p>It&#8217;d then prompt you for root password. After successful authentication, the mysql server will start dumping all databases into a text SQL file: all.sql. After it&#8217;s done, you can <a href="http://www.kavoir.com/2009/05/php-compress-files-into-tar-or-zip-make-a-zip-file-or-tar-file-with-php.html">compress</a> (“tar zcf all.tar.gz all.sql”) and put all.sql somewhere safe for backup.</p>
<p>To restore all.sql into the entire MySQL:</p>
<pre><code>mysql -u root -p &lt; all.sql</code></pre>
<p>Can&#8217;t be any simpler.<br />
<h3>Related Posts:</h3>
<ul class="similar-posts">
<li><a href="http://www.kavoir.com/2009/06/reset-mysql-root-password-after-you-forgot-or-lost-it.html" rel="bookmark" title="June 20, 2009">How to Recover or Reset MySQL root Password after You Forgot and Lost It</a></li>
<li><a href="http://www.kavoir.com/2008/12/backup-and-recover-a-mysql-database-under-command-line.html" rel="bookmark" title="December 14, 2008">Backup and recover a MySQL database under command line</a></li>
<li><a href="http://www.kavoir.com/2010/10/mysql-log-files-are-taking-a-lot-of-disk-space-how-to-disable-and-delete-them.html" rel="bookmark" title="October 25, 2010">MySQL log files are taking a lot of disk space &ndash; How to disable and delete them?</a></li>
<li><a href="http://www.kavoir.com/2009/09/mysql-select-and-show-all-mysql-users.html" rel="bookmark" title="September 3, 2009">MySQL: Select and Show all MySQL Users</a></li>
<li><a href="http://www.kavoir.com/2010/07/how-to-check-if-a-mysql-database-connection-is-successful-in-php.html" rel="bookmark" title="July 30, 2010">How to check if a MySQL database connection is successful in PHP?</a></li>
</ul>
<p><!-- Similar Posts took 2.916 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kavoir.com/2009/09/mysql-how-to-backup-all-databases-at-once-as-root-with-mysqldump.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL: Select and Show all MySQL Users</title>
		<link>http://www.kavoir.com/2009/09/mysql-select-and-show-all-mysql-users.html</link>
		<comments>http://www.kavoir.com/2009/09/mysql-select-and-show-all-mysql-users.html#comments</comments>
		<pubDate>Thu, 03 Sep 2009 11:26:48 +0000</pubDate>
		<dc:creator>Yang Yang</dc:creator>
				<category><![CDATA[SQL / MySQL Tips and Tutorials]]></category>

		<guid isPermaLink="false">http://www.kavoir.com/2009/09/mysql-select-and-show-all-mysql-users.html</guid>
		<description><![CDATA[To show all MySQL databases, you can simply type SHOW DATABASES; or SHOW TABLES; to show all tables of the current database. However, if you want to list and display all the MySQL users, SHOW USERS; won’t work. So how to display a list of the MySQL users? As all user information such as user [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>To show all MySQL databases, you can simply type <code>SHOW DATABASES;</code> or <code>SHOW TABLES;</code> to show all tables of the current database. However, if you want to list and display all the MySQL users, <code>SHOW USERS;</code> won’t work.</p>

<p>So how to display a list of the MySQL users?</p>
<p>As all user information such as user name and privilege data are stored in the table <strong>user</strong> of the database <strong>mysql</strong> that comes automatically created and populated after MySQL installation. You can show all the MySQL users of the current host by the following select query:</p>
<pre><code>SELECT user FROM mysql.user;</code></pre>
<p>Which would output a list of rows of all the active users, in the same format of what a <code>SHOW DATABASES;</code> would do.</p>
<h3>Related Posts:</h3>
<ul class="similar-posts">
<li><a href="http://www.kavoir.com/2009/06/mysql-php-display-mysql-table-fields-and-data.html" rel="bookmark" title="June 6, 2009">MySQL, PHP: Display MySQL table fields and data</a></li>
<li><a href="http://www.kavoir.com/2011/02/mysql-how-to-export-a-database-table-to-xml.html" rel="bookmark" title="February 17, 2011">MySQL: How to export a database / table to XML?</a></li>
<li><a href="http://www.kavoir.com/2009/09/mysql-how-to-backup-all-databases-at-once-as-root-with-mysqldump.html" rel="bookmark" title="September 5, 2009">MySQL: How to backup ALL databases as root with mysqldump at once?</a></li>
<li><a href="http://www.kavoir.com/2011/03/mysql-get-the-exact-size-of-a-database-by-query.html" rel="bookmark" title="March 5, 2011">MySQL: Get the exact size of a database by SQL query</a></li>
<li><a href="http://www.kavoir.com/2010/11/mysql-export-table-to-csv-text-files-for-excel.html" rel="bookmark" title="November 18, 2010">MySQL: Export Table to CSV Text Files for Excel</a></li>
</ul>
<p><!-- Similar Posts took 3.062 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kavoir.com/2009/09/mysql-select-and-show-all-mysql-users.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>MySQL: How to change or convert MyISAM to InnoDB or vice versa?</title>
		<link>http://www.kavoir.com/2009/09/mysql-how-to-change-or-convert-myisam-to-innodb-or-vice-versa.html</link>
		<comments>http://www.kavoir.com/2009/09/mysql-how-to-change-or-convert-myisam-to-innodb-or-vice-versa.html#comments</comments>
		<pubDate>Wed, 02 Sep 2009 02:08:18 +0000</pubDate>
		<dc:creator>Yang Yang</dc:creator>
				<category><![CDATA[SQL / MySQL Tips and Tutorials]]></category>

		<guid isPermaLink="false">http://www.kavoir.com/2009/09/mysql-how-to-change-or-convert-myisam-to-innodb-or-vice-versa.html</guid>
		<description><![CDATA[After we have discussed the advantages and disadvantages of MyISAM and InnoDB, you might want to convert them back and forth and see the performance differences yourself. For small website, the difference may be minor but for a large popular application, MyISAM tables yield better performance results in selecting data records InnoDB tables give better [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>After we have discussed the <a href="http://www.kavoir.com/2009/09/mysql-engines-innodb-vs-myisam-a-comparison-of-pros-and-cons.html">advantages and disadvantages of MyISAM and InnoDB</a>, you might want to convert them back and forth and see the performance differences yourself. For small website, the difference may be minor but for a large popular application, </p>

<ul>
<li>MyISAM tables yield better performance results in <strong>selecting</strong> data records </li>
<li>InnoDB tables give better performance results in <strong>inserting</strong> and <strong>updating</strong> data records </li>
</ul>
<p>To change a MyISAM table to an InnoDB table:</p>
<pre><code>ALTER TABLE table_name ENGINE = InnoDB;</code></pre>
<p>To convert an InnoDB table to a MyISAM table:</p>
<pre><code>ALTER TABLE table_name ENGINE = MyISAM;</code></pre>
<h3>Related Posts:</h3>
<ul class="similar-posts">
<li><a href="http://www.kavoir.com/2009/09/mysql-engines-innodb-vs-myisam-a-comparison-of-pros-and-cons.html" rel="bookmark" title="September 2, 2009">MySQL Engines: InnoDB vs. MyISAM &ndash; A Comparison of Pros and Cons</a></li>
<li><a href="http://www.kavoir.com/2011/03/mysql-performance-tips.html" rel="bookmark" title="March 12, 2011">MySQL Performance Tips</a></li>
<li><a href="http://www.kavoir.com/2008/11/using-sql-to-find-records-existing-in-one-table-but-not-in-another.html" rel="bookmark" title="November 18, 2008">Using SQL to Find records existing in one table but not in another</a></li>
<li><a href="http://www.kavoir.com/2009/04/mysql-id-between-start-and-end-instead-of-limit-start-step-for-better-database-performance.html" rel="bookmark" title="April 12, 2009">MySQL: id BETWEEN start AND end Instead of LIMIT start, step For Better Database Performance</a></li>
<li><a href="http://www.kavoir.com/2009/06/mysql-php-store-form-textarea-value-to-mysql-database-table.html" rel="bookmark" title="June 6, 2009">MySQL, PHP: Store form textarea value or data to MySQL database table</a></li>
</ul>
<p><!-- Similar Posts took 2.975 ms --></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kavoir.com/2009/09/mysql-how-to-change-or-convert-myisam-to-innodb-or-vice-versa.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

