<?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>Chris Tate-Davies &#187; MySQL</title>
	<atom:link href="http://www.christatedavies.co.uk/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.christatedavies.co.uk</link>
	<description>An archive of helpful tit bits of information for development, and probably some stuff that is incomplete, wrong or boring...</description>
	<lastBuildDate>Tue, 31 Jan 2012 11:18:39 +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 Error in create table syntax &#8216;USING BTREE&#8217;</title>
		<link>http://www.christatedavies.co.uk/2011/09/27/mysql-error-in-create-table-syntax-using-btree/</link>
		<comments>http://www.christatedavies.co.uk/2011/09/27/mysql-error-in-create-table-syntax-using-btree/#comments</comments>
		<pubDate>Tue, 27 Sep 2011 08:54:13 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[ant]]></category>
		<category><![CDATA[dbdeploy]]></category>
		<category><![CDATA[terminal]]></category>

		<guid isPermaLink="false">http://www.christatedavies.co.uk/?p=240</guid>
		<description><![CDATA[We had an odd thing today, whilst performing a apply-db-changes we received the error: 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 'USING BTREE, KEY `index` (`fields`)' at line 1 It worked fine before, so why won&#8217;t ant [...]<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2011/09/27/mysql-error-in-create-table-syntax-using-btree/' addthis:title='MySQL Error in create table syntax &#8216;USING BTREE&#8217; ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>We had an odd thing today, whilst performing a apply-db-changes we received the error:</p>
<pre>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 'USING
BTREE, KEY `index` (`fields`)' at line 1</pre>
<p>It worked fine before, so why won&#8217;t ant create this table?</p>
<p>Well its down to the USING BTREE on MySQL Server version 5.0 using a InnoDB or MyISAM table. You can only have BTREE as an index and thus any reference to it on 5.0 seems to fail. May as well just take out the reference to the index type.</p>
<p>Annoying if you can&#8217;t work out the error, but now you know.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2011/09/27/mysql-error-in-create-table-syntax-using-btree/' addthis:title='MySQL Error in create table syntax &#8216;USING BTREE&#8217; ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.christatedavies.co.uk/2011/09/27/mysql-error-in-create-table-syntax-using-btree/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Setting up Class Table Inheritance with Doctrine 2.0</title>
		<link>http://www.christatedavies.co.uk/2011/09/13/setting-up-class-table-inheritance-with-doctrine-2-0/</link>
		<comments>http://www.christatedavies.co.uk/2011/09/13/setting-up-class-table-inheritance-with-doctrine-2-0/#comments</comments>
		<pubDate>Tue, 13 Sep 2011 10:51:58 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Doctrine]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[abstract]]></category>
		<category><![CDATA[classes]]></category>
		<category><![CDATA[doctrine]]></category>
		<category><![CDATA[inheritance]]></category>
		<category><![CDATA[Object-Relational Mapping]]></category>
		<category><![CDATA[ORM]]></category>
		<category><![CDATA[zend framework]]></category>

		<guid isPermaLink="false">http://www.christatedavies.co.uk/?p=234</guid>
		<description><![CDATA[Have had some serious problems getting this working, but after a 4 hour head bashing session, we&#8217;ve cracked it. CREATE TABLE inventory( inventory_id INT AUTO_INCREMENT, discriminator_column VARCHAR(20), category VARCHAR(50) NOT NULL, part_number VARCHAR(50) NOT NULL, PRIMARY KEY (inventory_id), KEY category_part_number (category, part_number), KEY discriminator_column (discriminator_column) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE inventory_room( inventory_id INT NOT [...]<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2011/09/13/setting-up-class-table-inheritance-with-doctrine-2-0/' addthis:title='Setting up Class Table Inheritance with Doctrine 2.0 ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Have had some serious problems getting this working, but after a 4 hour head bashing session, we&#8217;ve cracked it.</p>
<pre>CREATE TABLE inventory(
    inventory_id INT AUTO_INCREMENT,
    discriminator_column VARCHAR(20),
    category VARCHAR(50) NOT NULL,
    part_number VARCHAR(50) NOT NULL,
    PRIMARY KEY (inventory_id),
    KEY category_part_number (category, part_number),
    KEY discriminator_column (discriminator_column)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;</pre>
<pre>CREATE TABLE inventory_room(
    inventory_id INT NOT NULL,
    rack_limit INT(6),
    room_type VARCHAR(25),
    KEY inventory_id (inventory_id),
    CONSTRAINT inventory_room_key FOREIGN KEY (inventory_id) REFERENCES inventory (inventory_id)
        ON DELETE CASCADE
        ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;</pre>
<p>You have to have a discriminator field so that Doctrine can connect the tables nicely when its joining them.</p>
<pre>&lt;?php
namespace Net\Model\Inventory;
use Doctrine\ORM\Mapping as ORM;

/**
 * Inventory entity
 *
 * @ORM\Entity(repositoryClass="Net\Repository\Inventory")
 * @ORM\Table(name="inventory")
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="discriminator_column", type="string")
 * @ORM\DiscriminatorMap({"inventory" = "AbstractItem", "inventory_room" = "Room"})
 *
 * @category Net
 * @package  Model
 */
abstract class AbstractItem
{
    /**
     * @ORM\Id @ORM\Column(name="inventory_id") @ORM\GeneratedValue
     * @var int
     */
    protected $inventory_id;

    /**
     * @ORM\Column(type="string")
     * @var string
     */
    protected $category;    

    /**
     * @ORM\Column(type="string")
     * @var string
     */
    protected $part_number;
 }</pre>
<p>As you can see in the above class, we have a ORM\GeneratedValue field. This tells Doctrine that this field is an increment field. As this is a one-to-one relationship we do not need one in the inventory_room table as it will be joined on the inventory_id field.</p>
<p>In the next class, this is the child class that extends the abstract. So Doctrine will read the annotations at the top and see that its a joined table using the id field (inventory_id)</p>
<pre>&lt;?php
namespace Net\Model\Inventory;
use Doctrine\ORM\Mapping as ORM;

/**
 * Inventory Room entity
 * @ORM\Entity
 * @ORM\Table(name="inventory_room")
 *
 * @category Net
 * @package  Model
 */
class Room extends AbstractItem
{
    /**
     * @ORM\Column(type="string")
     * @var string
     */
    protected $room_type;

    /**
     * @ORM\Column(type="string")
     * @var string
     */
    protected $rack_limit;
}</pre>
<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2011/09/13/setting-up-class-table-inheritance-with-doctrine-2-0/' addthis:title='Setting up Class Table Inheritance with Doctrine 2.0 ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.christatedavies.co.uk/2011/09/13/setting-up-class-table-inheritance-with-doctrine-2-0/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Resetting forgotten MySQL root password</title>
		<link>http://www.christatedavies.co.uk/2011/05/27/resetting-forgotten-mysql-root-password/</link>
		<comments>http://www.christatedavies.co.uk/2011/05/27/resetting-forgotten-mysql-root-password/#comments</comments>
		<pubDate>Fri, 27 May 2011 11:47:31 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[admin]]></category>
		<category><![CDATA[forgotten password]]></category>
		<category><![CDATA[password]]></category>
		<category><![CDATA[root]]></category>
		<category><![CDATA[root password]]></category>

		<guid isPermaLink="false">http://www.christatedavies.co.uk/?p=177</guid>
		<description><![CDATA[If you have forgotten your root password for MySQL, then don&#8217;t panic. Although, if you don&#8217;t have root shell access, then do panic! Anyway, to reset a password, you need to stop MySQL and restart it with a special setting that allows you to login without passwords. Obviously this is a dangerous switch and you [...]<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2011/05/27/resetting-forgotten-mysql-root-password/' addthis:title='Resetting forgotten MySQL root password ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>If you have forgotten your root password for MySQL, then don&#8217;t panic. Although, if you don&#8217;t have root shell access, then do panic!</p>
<p>Anyway, to reset a password, you need to stop MySQL and restart it with a special setting that allows you to login without passwords. Obviously this is a dangerous switch and you have to remember to stop and start MySQL after you&#8217;ve finished!</p>
<h2>Linux:</h2>
<p>Login with superuser access (sudo su)</p>
<pre>&gt; /etc/init.d/mysql stop</pre>
<p>This will stop the MySQL service on the server, and all connections will be cut off.</p>
<p>Then we need to start MySQL with the secret switch:</p>
<pre>&gt; mysqld_safe --skip-grant-tables &amp;</pre>
<p>I don&#8217;t get any messages after this, but you may get one saying the mysqld_safe has started.</p>
<p>Then connect as root:</p>
<pre>&gt; mysql -u root</pre>
<p>You will be connected. And you can just assign the root user a new password:</p>
<pre>mysql&gt; USE mysql;
mysql&gt; UPDATE user SET password=PASSWORD(NEWPASSWORD) WHERE user = "root";
mysql&gt; FLUSH PRIVILEGES;
mysql&gt; exit;</pre>
<p>That&#8217;s it. So now, just stop and start mysql again (as superuser) and were done:</p>
<pre>&gt; /etc/init.d/mysql restart</pre>
<p>&nbsp;</p>
<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2011/05/27/resetting-forgotten-mysql-root-password/' addthis:title='Resetting forgotten MySQL root password ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.christatedavies.co.uk/2011/05/27/resetting-forgotten-mysql-root-password/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Changing your MySQL root password</title>
		<link>http://www.christatedavies.co.uk/2011/05/27/changing-your-mysql-root-password/</link>
		<comments>http://www.christatedavies.co.uk/2011/05/27/changing-your-mysql-root-password/#comments</comments>
		<pubDate>Fri, 27 May 2011 11:39:55 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[admin]]></category>
		<category><![CDATA[lost password]]></category>
		<category><![CDATA[privileges]]></category>
		<category><![CDATA[root]]></category>

		<guid isPermaLink="false">http://www.christatedavies.co.uk/?p=175</guid>
		<description><![CDATA[When you installed MySQL, you would have been asked for a root password. If you were testing or something similar, you may have used an empty password. This is all well and good until you need to put your server into production. So, how do you create one? &#62; mysqladmin -u root password NEWPASSWORD This [...]<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2011/05/27/changing-your-mysql-root-password/' addthis:title='Changing your MySQL root password ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>When you installed MySQL, you would have been asked for a root password. If you were testing or something similar, you may have used an empty password. This is all well and good until you need to put your server into production. So, how do you create one?</p>
<pre>&gt; mysqladmin -u root password NEWPASSWORD</pre>
<p>This will assign the root user, the password in NEWPASSWORD</p>
<p>But, what if you want to change the root&#8217;s password?</p>
<pre>&gt; mysqladmin -u root -pOLDPASSWORD password NEWPASSWORD</pre>
<p>i.e.</p>
<pre>&gt; mysqladmin -u root -pabcdefg password gfedcba</pre>
<p>The above example will change the root&#8217;s password (if it is right) from abcdefg to gfedcba (I know this isn&#8217;t a very strong password, but its only an example)</p>
<p>You can of course, log into MySQL to change the password for any user by following these commands:</p>
<pre>&gt; mysql -u root -pADMINPASSWORD</pre>
<pre>mysql&gt; USE mysql;
mysql&gt; UPDATE user SET password=PASSWORD('NEWPASSWORD') WHERE user = "root";
mysql&gt; FLUSH PRIVILEGES;</pre>
<p>Done.</p>
<p>If you forget your root password, then see my post on <a title="Resetting forgotten MySQL root password" href="http://www.christatedavies.co.uk/2011/05/resetting-forgotten-mysql-root-password/">forgotten MySQL root password</a>&#8230;</p>
<p>&nbsp;</p>
<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2011/05/27/changing-your-mysql-root-password/' addthis:title='Changing your MySQL root password ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.christatedavies.co.uk/2011/05/27/changing-your-mysql-root-password/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL Query Profiling</title>
		<link>http://www.christatedavies.co.uk/2011/04/27/mysql-query-profiling/</link>
		<comments>http://www.christatedavies.co.uk/2011/04/27/mysql-query-profiling/#comments</comments>
		<pubDate>Wed, 27 Apr 2011 09:57:18 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[diagnose slow query]]></category>
		<category><![CDATA[explain]]></category>
		<category><![CDATA[profiling]]></category>
		<category><![CDATA[queries]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[slow queries]]></category>

		<guid isPermaLink="false">http://www.christatedavies.co.uk/?p=153</guid>
		<description><![CDATA[This inbuilt part of MySQL allows you to look closely at queries run &#8211; per session. And you can use this information to find bottlenecks. To enable profiling, run this command in the MySQL client: SET profiling = 1; This will turn on the profiling. Then for each query you run, MySQL will log all the [...]<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2011/04/27/mysql-query-profiling/' addthis:title='MySQL Query Profiling ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>This inbuilt part of MySQL allows you to look closely at queries run &#8211; per session. And you can use this information to find bottlenecks. To enable profiling, run this command in the MySQL client:</p>
<pre>SET profiling = 1;</pre>
<p>This will turn on the profiling. Then for each query you run, MySQL will log all the queries and extra information about them.</p>
<p>Run yourself some queries, and then run this command:</p>
<pre>SHOW PROFILES;</pre>
<p>It will show you a standard MySQL recordset of the queries you have run, each with an individual ID, in the order that you ran them.</p>
<pre>+----------+-------------+--------------------------------------------------------+
| Query_ID | Duration    | Query                                                  |
+----------+-------------+--------------------------------------------------------+
|        1 |  0.00009260 | SELECT fieldname FROM table WHERE field = 'value'      |
+----------+-------------+--------------------------------------------------------+</pre>
<div id="_mcePaste">There will be as many rows as queries that you ran. And that&#8217;s not it, you can drill down even more, to see what the duration is made up with&#8230; To drill down on a query, make a note of the Query_ID and use the following statement:</div>
<div>
<pre>SHOW PROFILE FOR QUERY 1;</pre>
<div>The resultset will be a list of operations that MySQL ran in order to complete the query. For example:</div>
<pre>+--------------------------------+----------+
| Status                         | Duration |
+--------------------------------+----------+
| starting                       | 0.000033 |
| checking query cache for query | 0.000073 |
| Opening tables                 | 0.000013 |
| System lock                    | 0.000007 |
| Table lock                     | 0.000035 |
| init                           | 0.000032 |
| optimizing                     | 0.000014 |
| statistics                     | 0.000016 |
| preparing                      | 0.000014 |
| executing                      | 0.000009 |
| Sending data                   | 0.331296 |
| end                            | 0.000016 |
| end                            | 0.000003 |
| query end                      | 0.000005 |
| storing result in query cache  | 0.000105 |
| freeing items                  | 0.000012 |
| closing tables                 | 0.000007 |
| logging slow query             | 0.000003 |
| logging slow query             | 0.000048 |
| cleaning up                    | 0.000006 |
+--------------------------------+----------+
20 rows in set (0.00 sec)</pre>
<div>So you can see all the different operations performed by the server for that one query. This is invaluable in finding problems I would say in slow queries and bottlenecks in the database.</div>
</div>
<div>There is also a CPU profile you can use to see the CPU&#8217;s timings, and to do this, you just add on the CPU keyword:</div>
<pre>SHOW PROFILE CPU FOR QUERY 1;</pre>
<div>There are others you can use, MEMORY, BLOCK IO, SWAPS, etc. They are all available in the MySQL reference for the SHOW PROFILE syntax, <a href="http://dev.mysql.com/doc/refman/5.0/en/show-profiles.html">http://dev.mysql.com/doc/refman/5.0/en/show-profiles.html</a></div>
<div>Profiling was introduced in MySQL version 5.0.37 Community Server.</div>
<div></div>
<div></div>
<div></div>
<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2011/04/27/mysql-query-profiling/' addthis:title='MySQL Query Profiling ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.christatedavies.co.uk/2011/04/27/mysql-query-profiling/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to use mysqldump</title>
		<link>http://www.christatedavies.co.uk/2011/01/18/how-to-use-mysqldump/</link>
		<comments>http://www.christatedavies.co.uk/2011/01/18/how-to-use-mysqldump/#comments</comments>
		<pubDate>Tue, 18 Jan 2011 14:22:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[backups]]></category>
		<category><![CDATA[mysqldump]]></category>

		<guid isPermaLink="false">http://www.christatedavies.co.uk/?p=130</guid>
		<description><![CDATA[Using mysqldump to back up a database If you want to make a super quick backup of a MySQL database, the best tool for the job is the native mysqldump. Its also a command line utility, so you can use it from a telnet or SSH connection, etc&#8230; The basic command structure of mysqldump is: [...]<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2011/01/18/how-to-use-mysqldump/' addthis:title='How to use mysqldump ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<h3>Using mysqldump to back up a database</h3>
<p>If you want to make a super quick backup of a MySQL database, the best tool for the job is the native mysqldump. Its also a command line utility, so you can use it from a telnet or SSH connection, etc&#8230;</p>
<p>The basic command structure of mysqldump is:</p>
<pre>mysqldump -u {username} -p{password} {database} &gt; {backup-file}</pre>
<p>I.e. If you had root access, and you wanted to backup all the tables within the &#8220;customers&#8221; database you could run this command:</p>
<pre>mysqldump -u root -p customers &gt; mybackup.sql</pre>
<p>This would dump all the data to the mybackup.sql file. Note I didn&#8217;t enter a password. Mysql will ask you for your password in this instance. If you wanted to enter the password (where it could be seen, or saved into a BASH history) then you use the command -pABC123 where ABC123 is the password (not the lack of a space between -p and ABC123 &#8211; this is important!)</p>
<p>But if you are security conscious, omit the actual password from the command line, and merely use -p as this will prompt you for the password and therefore BASH or DOS will not store it.</p>
<p>If you wanted to only dump certain tables from the database, specify them after the database. For example, to back up the &#8220;address&#8221; and &#8220;billing&#8221; tables:</p>
<pre>mysqldump -u root -p customers address billing &gt; mybackup.sql</pre>
<h3>Restoring a mysqldump file</h3>
<p>If you want to restore a mysqldump&#8217;ed file into MySQL &#8211; you can simply run the following command:</p>
<pre>mysql -u {username} -p{password} {database} &lt; {backup-file}</pre>
<p>Note the change of direction for the &lt; symbol and the fact we&#8217;re calling the mysql command, and not the mysqldump command!!!</p>
<p>So, to squirt my backupfile back into the customers database</p>
<pre>mysql -u root -pABC123 customers &lt; mybackup.sql</pre>
<p>NB. This will overwrite your current database tables with whatever is in the backupfile!</p>
<h3>Using mysqldump to backup ALL databases</h3>
<p>If you have lots of databases, then you simply use the &#8211;all-databases command to back them all up into one file:</p>
<pre>mysqldump -u root -pABC123 --all-databases &gt; mytotalbackup.sql</pre>
<h3>Using mysqldump to backup multiple databases</h3>
<p>By using the &#8211;databases switch, we can state which databases we want backed up. Separate each database name with a space.</p>
<p>So to backup &#8220;customers&#8221;, &#8220;orders&#8221; and &#8220;misc&#8221; we use the following syntax:</p>
<pre>mysqldump -u root -pABC123 --databases customers orders misc &gt; multiple-databases.sql</pre>
<h3>Advanced techniques</h3>
<p><strong>Compressed backups</strong></p>
<p>More often than not, a database dump will be several hundred MB in size, if not more. MySQL can zip this for you automatically using the gzip command:</p>
<pre>mysqldump -u root -pABC123 customers | gzip &gt; compressed-backup.sql.gz</pre>
<p>The pipe before the gzip tells mysqldump to send the output to the next command, which is gzip and that, in turn, is asked to create &#8220;compressed-backup.sql.gz&#8221;</p>
<p><strong>Dumping the data definitions</strong></p>
<p>If you wanted a &#8220;skeleton&#8221; structure of the entire database, then mysqldump will allow this, by using the &#8211;no-data switch:</p>
<pre>mysql -u root -pABC123 --no-data customers &gt; customers-structure.sql</pre>
<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2011/01/18/how-to-use-mysqldump/' addthis:title='How to use mysqldump ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.christatedavies.co.uk/2011/01/18/how-to-use-mysqldump/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reading the MySQL binary log</title>
		<link>http://www.christatedavies.co.uk/2010/07/27/reading-the-mysql-binary-log/</link>
		<comments>http://www.christatedavies.co.uk/2010/07/27/reading-the-mysql-binary-log/#comments</comments>
		<pubDate>Tue, 27 Jul 2010 15:28:05 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[backups]]></category>
		<category><![CDATA[binary log]]></category>
		<category><![CDATA[mysqlbinlog]]></category>
		<category><![CDATA[replication]]></category>

		<guid isPermaLink="false">http://www.christatedavies.co.uk/?p=97</guid>
		<description><![CDATA[Having MySQL dumping out binary logs is a very good failsafe for backup redundancy. But, how do you get any of the information out of them? If you know a time span that you need to investigate, then you can easily use the mysqlbinlog utility: mysqlbinlog --start-datetime="2010-07-27 14:30:00" --stop-datetime="2010-07-27 15:00:00" &#62; backup.txt This will export [...]<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2010/07/27/reading-the-mysql-binary-log/' addthis:title='Reading the MySQL binary log ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Having MySQL dumping out binary logs is a very good failsafe for backup redundancy. But, how do you get any of the information out of them?</p>
<p>If you know a time span that you need to investigate, then you can easily use the mysqlbinlog utility:</p>
<pre>mysqlbinlog --start-datetime="2010-07-27 14:30:00" --stop-datetime="2010-07-27 15:00:00" &gt; backup.txt</pre>
<p>This will export all the recorded SQL Statements performed between 2:30pm and 3pm on the 27th July 2010 to the text file &#8220;backup.txt&#8221;. I can then browse at my convenience.</p>
<p>If you need to re-create any of the statements, you can copy and paste into your MySQL client of choice.</p>
<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2010/07/27/reading-the-mysql-binary-log/' addthis:title='Reading the MySQL binary log ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.christatedavies.co.uk/2010/07/27/reading-the-mysql-binary-log/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL &#8211; Order by certain value first</title>
		<link>http://www.christatedavies.co.uk/2010/03/23/mysql-order-by-certain-value-first/</link>
		<comments>http://www.christatedavies.co.uk/2010/03/23/mysql-order-by-certain-value-first/#comments</comments>
		<pubDate>Tue, 23 Mar 2010 21:59:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[order by]]></category>

		<guid isPermaLink="false">http://www.christatedavies.co.uk/?p=16</guid>
		<description><![CDATA[Ever wanted to sort a resultset of data, by value, but I wanted a couple of exceptions to appear at the top? SELECT country, population, CASE country WHEN 'United Kingdom' THEN 0 WHEN 'United States' THEN 1 WHEN 'New Zealand" THEN 2 ELSE country END AS countrySort FROM countryList  ORDER BY countrySort ASC; This will [...]<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2010/03/23/mysql-order-by-certain-value-first/' addthis:title='MySQL &#8211; Order by certain value first ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Ever wanted to sort a resultset of data, by value, but I wanted a couple of exceptions to appear at the top?</p>
<pre>SELECT country, population,
CASE country
WHEN 'United Kingdom' THEN 0
WHEN 'United States' THEN 1
WHEN 'New Zealand" THEN 2
ELSE country
END AS countrySort
FROM countryList 
ORDER BY countrySort ASC;</pre>
<p>This will sort the results by country, but with UK, US, NZ as the top three.</p>
<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2010/03/23/mysql-order-by-certain-value-first/' addthis:title='MySQL &#8211; Order by certain value first ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.christatedavies.co.uk/2010/03/23/mysql-order-by-certain-value-first/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MySQL Incorrect String value &#8220;x/80&#8243;</title>
		<link>http://www.christatedavies.co.uk/2010/03/18/mysql-incorrect-string-value-x-80/</link>
		<comments>http://www.christatedavies.co.uk/2010/03/18/mysql-incorrect-string-value-x-80/#comments</comments>
		<pubDate>Fri, 19 Mar 2010 01:45:05 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[replication]]></category>

		<guid isPermaLink="false">http://www.christatedavies.co.uk/?p=14</guid>
		<description><![CDATA[MySQL Incorrect String value replication error. We use ANT for releasing versions of our PHP applications. Its very smart and takes a lot of the problems related to releases out of the equation. However, everytime we performed a release, our replication server would break, and I&#8217;d have to skip a load of database delta error [...]<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2010/03/18/mysql-incorrect-string-value-x-80/' addthis:title='MySQL Incorrect String value &#8220;x/80&#8243; ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<h2>MySQL Incorrect String value replication error.</h2>
<p>We use ANT for releasing versions of our PHP applications. Its very smart and takes a lot of the problems related to releases out of the equation.</p>
<p>However, everytime we performed a release, our replication server would break, and I&#8217;d have to skip a load of database delta error messages with this command:</p>
<pre>SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1</pre>
<p>The error we received is:</p>
<pre>Error 'Incorrect string value: 'x9CxF37x12[k...' for
column 'applied_by' at row 1' on query. Default database:
'&lt;databasename&gt;'.</pre>
<pre>Query: 'INSERT INTO changelog (change_number, delta_set, complete_dt,
applied_by, description)</pre>
<pre>VALUES (35, 'Main', CURRENT_TIMESTAMP, USER(),
'&lt;database_delta_filename&gt;')'.</pre>
<p>I&#8217;d been getting these errors for a while.</p>
<p>Basically, the MySQL server was replicating the changelog table down to the slave, but for some reason it just would not accept the replicated query.</p>
<p>After a bit of scrummaging around, and changing field values/character sets, I discovered that in the string was &#8220;x80&#8243; which is a padding character as set out in http://en.wikipedia.org/wiki/ISO_8859-1 and shouldn&#8217;t be used in a string. I&#8217;m not really sure how it got there, or why, but I basically changed the &#8220;applied_by&#8221; field to be a BLOB &#8211; which is generically a VARCHAR with no character set and there fore ignores this.</p>
<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2010/03/18/mysql-incorrect-string-value-x-80/' addthis:title='MySQL Incorrect String value &#8220;x/80&#8243; ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.christatedavies.co.uk/2010/03/18/mysql-incorrect-string-value-x-80/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL REPLACE</title>
		<link>http://www.christatedavies.co.uk/2010/03/18/mysql-replace/</link>
		<comments>http://www.christatedavies.co.uk/2010/03/18/mysql-replace/#comments</comments>
		<pubDate>Fri, 19 Mar 2010 00:31:11 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.christatedavies.co.uk/?p=7</guid>
		<description><![CDATA[I&#8217;d not come across REPLACE as a MySQL function before. I&#8217;d always used a combination of concatination of LEFTs and RIGHTs and the occasional MID. Unbelievable that I&#8217;d not found this function before&#8230; SELECT REPLACE(fieldname, "look for", "replace with") FROM tablename; So simple&#8230;<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2010/03/18/mysql-replace/' addthis:title='MySQL REPLACE ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;d not come across REPLACE as a MySQL function before. I&#8217;d always used a combination of concatination of LEFTs and RIGHTs and the occasional MID.</p>
<p>Unbelievable that I&#8217;d not found this function before&#8230;</p>
<pre>SELECT REPLACE(fieldname, "look for", "replace with") FROM tablename;</pre>
<p>So simple&#8230;</p>
<div class="addthis_toolbox addthis_default_style addthis_" addthis:url='http://www.christatedavies.co.uk/2010/03/18/mysql-replace/' addthis:title='MySQL REPLACE ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.christatedavies.co.uk/2010/03/18/mysql-replace/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

