<!--#include virtual="/ssi/header.shtml" -->
      <h1>MySQL</h1>
      <div id="main">
        <p>
          My MySQL notes, both troubleshooting and SQL
          commands. 
        </p>
        <h2>Create database</h2>
<pre class="code">
  mysql&gt; create database db_name;
  mysql&gt; grant all on db_name.* to user@'%' identified by 'my_password';
  mysql&gt; grant all on db_name.* to user@'localhost' identified by 'my_password';
</pre>
        <h2>Read SQL from a file</h2>
        <p>
          There's two scenarios here, doing it from the standard
          command line and doing it from within the mysql shell. To do
          the latter, you simply use the <code>source</code> command: 
        </p>
        <pre class="code">mysql&gt; source create_my_db.sql</pre> and
        to run it from the shell, you'll simply do <pre
        class="code">$ mysql &lt; create_my_db.sql</pre> <h2>Different
        BLOB types and sizes</h2>
        <p>
          I was surprised to find that there are several BLOB types in
          MySQL, with the standard type called "BLOB" only capable of
          holding 65 kilobytes!
        </p>
        <p>
          To get BLOB columns to actually hold something useful, like
          a normal JPEG picture, you need to use either the MEDIUMBLOB
          or LONGBLOB type. On dev.mysql.com there's a
          <a href="http://dev.mysql.com/doc/refman/5.0/en/storage-requirements.html">
             complete overview of the different types and sizes
          </a>
        </p>
        <h2>Backing up the MySQL server</h2>
        <p>
          It's easy to backup the DB and compress it at the same time
          by issuing the following command:
        </p>
<pre class="code">
  $ mysqldump -u myuser -h localhost -pmypassword mydb\
    | gzip -9 > mydb-db.sql.gz
</pre>
        <p>
          Getting it back is then just a matter of:
        </p>
<pre class="code">
  $ zcat mydb-db.sql.gz | mysql -u myuser -h localhost\
    -pmypassword mydb -
</pre>
        <h2>Changing the length of a column</h2>
        <p>
          In my case, I needed to change a column from being a VARCHAR
          with 255 characters to allow 300 characters. What I needed
          to do was:
        </p>
<pre class="code">
  mysql&gt; alter table mytable modify mycolumn VARCHAR(300);
</pre>
        <h2>Not being able to login remotely</h2>
<pre class="code">
$ mysql -u myuser -pmypassword -h myhost mydb
ERROR 2003 (HY000): Can't connect to MySQL server on 'myhost' (111)
</pre>
        <p>
          This is because mysql on doesn't listen to other hosts than
          localhost, this is the default on Debian. To enable remote
          access, edit: <code>/etc/mysql/my.cnf</code> and the line:
        </p>
<pre class="code">
bind-address            = 127.0.0.1
</pre>
        <p>
          To allow only access through one IP/interface, set this
          variable to this IP, or comment it out to allow access to
          all hosts (less secure of course).
        </p>
        <h2>Starting a running counter on a given value</h2>
<pre class="code">
alter table my_table auto_increment = 200;
</pre>
      </div>
<!--#include virtual="/ssi/footer.shtml" -->
