<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h3 class="title">
      <a name="mysql-cluster-install-example-data">
      </a>
      25.3.5 NDB Cluster Example with Tables and Data
     </h3>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045122186176">
  </a>
  <a class="indexterm" name="idm46045122184688">
  </a>
  <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
   <div class="admon-title">
    Note
   </div>
   <p>
    The information in this section applies to NDB Cluster running
        on both Unix and Windows platforms.
   </p>
  </div>
  <p>
   Working with database tables and data in NDB Cluster is not much
      different from doing so in standard MySQL. There are two key
      points to keep in mind:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      For a table to be replicated in the cluster, it must use the
      <a class="link" href="mysql-cluster.html" title="Chapter 25 MySQL NDB Cluster 8.4">
       <code class="literal">
        NDBCLUSTER
       </code>
      </a>
      storage engine. To
          specify this, use the
      <code class="literal">
       ENGINE=NDBCLUSTER
      </code>
      or
      <code class="literal">
       ENGINE=NDB
      </code>
      option when creating the table:
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa17073312"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> <em class="replaceable">tbl_name</em> <span class="token punctuation">(</span><em class="replaceable">col_name</em> <em class="replaceable">column_definitions</em><span class="token punctuation">)</span> <span class="token keyword">ENGINE</span><span class="token operator">=</span><span class="token keyword">NDBCLUSTER</span><span class="token punctuation">;</span></code></pre>
     </div>
     <p>
      Alternatively, for an existing table that uses a different
          storage engine, use
      <a class="link" href="alter-table.html" title="15.1.9 ALTER TABLE Statement">
       <code class="literal">
        ALTER TABLE
       </code>
      </a>
      to change the table to use
      <a class="link" href="mysql-cluster.html" title="Chapter 25 MySQL NDB Cluster 8.4">
       <code class="literal">
        NDBCLUSTER
       </code>
      </a>
      :
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa18539263"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">ALTER</span> <span class="token keyword">TABLE</span> <em class="replaceable">tbl_name</em> <span class="token keyword">ENGINE</span><span class="token operator">=</span><span class="token keyword">NDBCLUSTER</span><span class="token punctuation">;</span></code></pre>
     </div>
    </li>
    <li class="listitem">
     <p>
      Every
      <a class="link" href="mysql-cluster.html" title="Chapter 25 MySQL NDB Cluster 8.4">
       <code class="literal">
        NDBCLUSTER
       </code>
      </a>
      table has a
          primary key. If no primary key is defined by the user when a
          table is created, the
      <a class="link" href="mysql-cluster.html" title="Chapter 25 MySQL NDB Cluster 8.4">
       <code class="literal">
        NDBCLUSTER
       </code>
      </a>
      storage engine automatically generates a hidden one. Such a
          key takes up space just as does any other table index. (It is
          not uncommon to encounter problems due to insufficient memory
          for accommodating these automatically created indexes.)
     </p>
    </li>
   </ul>
  </div>
  <p>
   If you are importing tables from an existing database using the
      output of
   <a class="link" href="mysqldump.html" title="6.5.4 mysqldump — A Database Backup Program">
    <span class="command">
     <strong>
      mysqldump
     </strong>
    </span>
   </a>
   , you can open the SQL
      script in a text editor and add the
   <code class="literal">
    ENGINE
   </code>
   option to any table creation statements, or replace any existing
   <code class="literal">
    ENGINE
   </code>
   options. Suppose that you have the
   <code class="literal">
    world
   </code>
   sample database on another MySQL server
      that does not support NDB Cluster, and you want to export the
   <code class="literal">
    City
   </code>
   table:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-terminal"><div class="docs-select-all right" id="sa5597580"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-terminal"><span class="token prompt">$&gt; </span><span class="token command">mysqldump</span> <span class="token property">--add-drop-table</span> world City &gt; city_table<span class="token punctuation">.</span>sql</code></pre>
  </div>
  <p>
   The resulting
   <code class="filename">
    city_table.sql
   </code>
   file contains
      this table creation statement (and the
   <a class="link" href="insert.html" title="15.2.7 INSERT Statement">
    <code class="literal">
     INSERT
    </code>
   </a>
   statements necessary to
      import the table data):
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa15697349"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">DROP</span> <span class="token keyword">TABLE</span> <span class="token keyword">IF</span> <span class="token keyword">EXISTS</span> <span class="token punctuation">`</span>City<span class="token punctuation">`</span><span class="token punctuation">;</span>
<span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> <span class="token punctuation">`</span>City<span class="token punctuation">`</span> <span class="token punctuation">(</span>
  <span class="token punctuation">`</span>ID<span class="token punctuation">`</span> <span class="token datatype">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">auto_increment</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span><span class="token keyword">Name</span><span class="token punctuation">`</span> <span class="token datatype">char</span><span class="token punctuation">(</span><span class="token number">35</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">default</span> <span class="token string">''</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>CountryCode<span class="token punctuation">`</span> <span class="token datatype">char</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">default</span> <span class="token string">''</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>District<span class="token punctuation">`</span> <span class="token datatype">char</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">default</span> <span class="token string">''</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>Population<span class="token punctuation">`</span> <span class="token datatype">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">default</span> <span class="token string">'0'</span><span class="token punctuation">,</span>
  <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span>  <span class="token punctuation">(</span><span class="token punctuation">`</span>ID<span class="token punctuation">`</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span> <span class="token keyword">ENGINE</span><span class="token operator">=</span>MyISAM<span class="token punctuation">;</span>

<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> <span class="token punctuation">`</span>City<span class="token punctuation">`</span> <span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">'Kabul'</span><span class="token punctuation">,</span><span class="token string">'AFG'</span><span class="token punctuation">,</span><span class="token string">'Kabol'</span><span class="token punctuation">,</span><span class="token number">1780000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> <span class="token punctuation">`</span>City<span class="token punctuation">`</span> <span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token string">'Qandahar'</span><span class="token punctuation">,</span><span class="token string">'AFG'</span><span class="token punctuation">,</span><span class="token string">'Qandahar'</span><span class="token punctuation">,</span><span class="token number">237500</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> <span class="token punctuation">`</span>City<span class="token punctuation">`</span> <span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token string">'Herat'</span><span class="token punctuation">,</span><span class="token string">'AFG'</span><span class="token punctuation">,</span><span class="token string">'Herat'</span><span class="token punctuation">,</span><span class="token number">186800</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation"></span><em class="replaceable"><span class="token punctuation">(</span>remaining <span class="token keyword">INSERT</span> statements omitted<span class="token punctuation">)</span></em><span class="token punctuation"></span></code></pre>
  </div>
  <p>
   You need to make sure that MySQL uses the
   <a class="link" href="mysql-cluster.html" title="Chapter 25 MySQL NDB Cluster 8.4">
    <code class="literal">
     NDBCLUSTER
    </code>
   </a>
   storage engine for this
      table. There are two ways that this can be accomplished. One of
      these is to modify the table definition
   <span class="emphasis">
    <em>
     before
    </em>
   </span>
   importing it into the Cluster
      database. Using the
   <code class="literal">
    City
   </code>
   table as an example,
      modify the
   <code class="literal">
    ENGINE
   </code>
   option of the definition as
      follows:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa32089454"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token keyword">DROP</span> <span class="token keyword">TABLE</span> <span class="token keyword">IF</span> <span class="token keyword">EXISTS</span> <span class="token punctuation">`</span>City<span class="token punctuation">`</span><span class="token punctuation">;</span>
<span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> <span class="token punctuation">`</span>City<span class="token punctuation">`</span> <span class="token punctuation">(</span>
  <span class="token punctuation">`</span>ID<span class="token punctuation">`</span> <span class="token datatype">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">auto_increment</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span><span class="token keyword">Name</span><span class="token punctuation">`</span> <span class="token datatype">char</span><span class="token punctuation">(</span><span class="token number">35</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">default</span> <span class="token string">''</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>CountryCode<span class="token punctuation">`</span> <span class="token datatype">char</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">default</span> <span class="token string">''</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>District<span class="token punctuation">`</span> <span class="token datatype">char</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">default</span> <span class="token string">''</span><span class="token punctuation">,</span>
  <span class="token punctuation">`</span>Population<span class="token punctuation">`</span> <span class="token datatype">int</span><span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">default</span> <span class="token string">'0'</span><span class="token punctuation">,</span>
  <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span>  <span class="token punctuation">(</span><span class="token punctuation">`</span>ID<span class="token punctuation">`</span><span class="token punctuation">)</span>
<span class="token punctuation">)</span> <span class="token keyword">ENGINE</span><span class="token operator">=</span><span class="token keyword">NDBCLUSTER</span><span class="token punctuation">;</span>

<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> <span class="token punctuation">`</span>City<span class="token punctuation">`</span> <span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">'Kabul'</span><span class="token punctuation">,</span><span class="token string">'AFG'</span><span class="token punctuation">,</span><span class="token string">'Kabol'</span><span class="token punctuation">,</span><span class="token number">1780000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> <span class="token punctuation">`</span>City<span class="token punctuation">`</span> <span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token string">'Qandahar'</span><span class="token punctuation">,</span><span class="token string">'AFG'</span><span class="token punctuation">,</span><span class="token string">'Qandahar'</span><span class="token punctuation">,</span><span class="token number">237500</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> <span class="token punctuation">`</span>City<span class="token punctuation">`</span> <span class="token keyword">VALUES</span> <span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token string">'Herat'</span><span class="token punctuation">,</span><span class="token string">'AFG'</span><span class="token punctuation">,</span><span class="token string">'Herat'</span><span class="token punctuation">,</span><span class="token number">186800</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation"></span><em class="replaceable"><span class="token punctuation">(</span>remaining <span class="token keyword">INSERT</span> statements omitted<span class="token punctuation">)</span></em><span class="token punctuation"></span></code></pre>
  </div>
  <p>
   This must be done for the definition of each table that is to be
      part of the clustered database. The easiest way to accomplish this
      is to do a search-and-replace on the file that contains the
      definitions and replace all instances of
   <code class="literal">
    ENGINE=
    <em class="replaceable">
     <code>
      engine_name
     </code>
    </em>
   </code>
   with
   <code class="literal">
    ENGINE=NDBCLUSTER
   </code>
   . If you do not want to
      modify the file, you can use the unmodified file to create the
      tables, and then use
   <a class="link" href="alter-table.html" title="15.1.9 ALTER TABLE Statement">
    <code class="literal">
     ALTER TABLE
    </code>
   </a>
   to
      change their storage engine. The particulars are given later in
      this section.
  </p>
  <p>
   Assuming that you have already created a database named
   <code class="literal">
    world
   </code>
   on the SQL node of the cluster, you can
      then use the
   <a class="link" href="mysql.html" title="6.5.1 mysql — The MySQL Command-Line Client">
    <span class="command">
     <strong>
      mysql
     </strong>
    </span>
   </a>
   command-line client to read
   <code class="filename">
    city_table.sql
   </code>
   , and create and populate the
      corresponding table in the usual manner:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-terminal"><div class="docs-select-all right" id="sa22482387"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-terminal"><span class="token prompt">$&gt; </span><span class="token command">mysql</span> world &lt; city_table<span class="token punctuation">.</span>sql</code></pre>
  </div>
  <p>
   It is very important to keep in mind that the preceding command
      must be executed on the host where the SQL node is running (in
      this case, on the machine with the IP address
   <code class="literal">
    198.51.100.20
   </code>
   ).
  </p>
  <p>
   To create a copy of the entire
   <code class="literal">
    world
   </code>
   database
      on the SQL node, use
   <a class="link" href="mysqldump.html" title="6.5.4 mysqldump — A Database Backup Program">
    <span class="command">
     <strong>
      mysqldump
     </strong>
    </span>
   </a>
   on the
      noncluster server to export the database to a file named
   <code class="filename">
    world.sql
   </code>
   (for example, in the
   <code class="filename">
    /tmp
   </code>
   directory). Then modify the table
      definitions as just described and import the file into the SQL
      node of the cluster like this:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-terminal"><div class="docs-select-all right" id="sa72228990"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-terminal"><span class="token prompt">$&gt; </span><span class="token command">mysql</span> world &lt; /tmp/world<span class="token punctuation">.</span>sql</code></pre>
  </div>
  <p>
   If you save the file to a different location, adjust the preceding
      instructions accordingly.
  </p>
  <p>
   Running
   <a class="link" href="select.html" title="15.2.13 SELECT Statement">
    <code class="literal">
     SELECT
    </code>
   </a>
   queries on the SQL
      node is no different from running them on any other instance of a
      MySQL server. To run queries from the command line, you first need
      to log in to the MySQL Monitor in the usual way (specify the
   <code class="literal">
    root
   </code>
   password at the
   <code class="literal">
    Enter
      password:
   </code>
   prompt):
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa34357439"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql">$<span class="token operator">&gt;</span> mysql <span class="token operator">-</span>u root <span class="token operator">-</span>p
Enter <span class="token keyword">password</span>:
Welcome <span class="token keyword">to</span> the MySQL monitor<span class="token punctuation">.</span>  Commands <span class="token keyword">end</span> <span class="token keyword">with</span> <span class="token punctuation">;</span> <span class="token operator">or</span> \g<span class="token punctuation">.</span>
Your MySQL <span class="token keyword">connection</span> id <span class="token operator">is</span> <span class="token number">1</span> <span class="token keyword">to</span> <span class="token keyword">server</span> version: <span class="token number">8.4</span><span class="token punctuation">.</span><span class="token number">0</span><span class="token operator">-</span><span class="token keyword">ndb</span><span class="token operator">-</span><span class="token number">8.4</span><span class="token punctuation">.</span><span class="token number">0</span>

<span class="token keyword">Type</span> <span class="token string">'help;'</span> <span class="token operator">or</span> <span class="token string">'\h'</span> <span class="token keyword">for</span> <span class="token keyword">help</span><span class="token punctuation">.</span> <span class="token keyword">Type</span> <span class="token string">'\c'</span> <span class="token keyword">to</span> clear the buffer<span class="token punctuation">.</span>

<span class="token prompt">mysql&gt;</span></code></pre>
  </div>
  <p>
   We simply use the MySQL server's
   <code class="literal">
    root
   </code>
   account and assume that you have followed the standard security
      precautions for installing a MySQL server, including setting a
      strong
   <code class="literal">
    root
   </code>
   password. For more information, see
   <a class="xref" href="default-privileges.html" title="2.9.4 Securing the Initial MySQL Account">
    Section 2.9.4, “Securing the Initial MySQL Account”
   </a>
   .
  </p>
  <p>
   It is worth taking into account that NDB Cluster nodes do
   <span class="emphasis">
    <em>
     not
    </em>
   </span>
   make use of the MySQL privilege system
      when accessing one another. Setting or changing MySQL user
      accounts (including the
   <code class="literal">
    root
   </code>
   account) effects
      only applications that access the SQL node, not interaction
      between nodes. See
   <a class="xref" href="mysql-cluster-security-mysql-privileges.html" title="25.6.21.2 NDB Cluster and MySQL Privileges">
    Section 25.6.21.2, “NDB Cluster and MySQL Privileges”
   </a>
   , for
      more information.
  </p>
  <p>
   If you did not modify the
   <code class="literal">
    ENGINE
   </code>
   clauses in the
      table definitions prior to importing the SQL script, you should
      run the following statements at this point:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa3427803"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">USE</span> world<span class="token punctuation">;</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">ALTER</span> <span class="token keyword">TABLE</span> City <span class="token keyword">ENGINE</span><span class="token operator">=</span><span class="token keyword">NDBCLUSTER</span><span class="token punctuation">;</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">ALTER</span> <span class="token keyword">TABLE</span> Country <span class="token keyword">ENGINE</span><span class="token operator">=</span><span class="token keyword">NDBCLUSTER</span><span class="token punctuation">;</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">ALTER</span> <span class="token keyword">TABLE</span> CountryLanguage <span class="token keyword">ENGINE</span><span class="token operator">=</span><span class="token keyword">NDBCLUSTER</span><span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   Selecting a database and running a
   <span class="command">
    <strong>
     SELECT
    </strong>
   </span>
   query
      against a table in that database is also accomplished in the usual
      manner, as is exiting the MySQL Monitor:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa68041014"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">USE</span> world<span class="token punctuation">;</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> <span class="token keyword">Name</span><span class="token punctuation">,</span> Population <span class="token keyword">FROM</span> City <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> Population <span class="token keyword">DESC</span> <span class="token keyword">LIMIT</span> <span class="token number">5</span><span class="token punctuation">;</span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> Name      <span class="token punctuation">|</span> Population <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> Bombay    <span class="token punctuation">|</span>   10500000 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Seoul     <span class="token punctuation">|</span>    9981619 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> São Paulo <span class="token punctuation">|</span>    9968485 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Shanghai  <span class="token punctuation">|</span>    9696300 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> Jakarta   <span class="token punctuation">|</span>    9604900 <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output">5 rows in set (0.34 sec)</span>

<span class="token prompt">mysql&gt;</span> \q
Bye

$<span class="token operator">&gt;</span></code></pre>
  </div>
  <p>
   Applications that use MySQL can employ standard APIs to access
   <a class="link" href="mysql-cluster.html" title="Chapter 25 MySQL NDB Cluster 8.4">
    <code class="literal">
     NDB
    </code>
   </a>
   tables. It is important to
      remember that your application must access the SQL node, and not
      the management or data nodes. This brief example shows how we
      might execute the
   <a class="link" href="select.html" title="15.2.13 SELECT Statement">
    <code class="literal">
     SELECT
    </code>
   </a>
   statement
      just shown by using the PHP 5.X
   <code class="literal">
    mysqli
   </code>
   extension running on a Web server elsewhere on the network:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-php"><div class="docs-select-all right" id="sa83023363"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-php"><span class="token doctype">&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd"&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">http-equiv</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>Content-Type<span class="token punctuation">"</span></span>
           <span class="token attr-name">content</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>text/html; charset<span class="token punctuation">=</span>iso-8859-1<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>title</span><span class="token punctuation">&gt;</span></span>SIMPLE mysqli SELECT<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>title</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">&gt;</span></span>
<span class="token php language-php"><span class="token delimiter important">&lt;?php</span>
  <span class="token shell-comment comment"># connect to SQL node:</span>
  <span class="token variable">$link</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">mysqli</span><span class="token punctuation">(</span><span class="token string">'198.51.100.20'</span><span class="token punctuation">,</span> <span class="token string">'root'</span><span class="token punctuation">,</span> <span class="token string">'<em class="replaceable">root_password</em>'</span><span class="token punctuation">,</span> <span class="token string">'world'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token shell-comment comment"># parameters for mysqli constructor are:</span>
  <span class="token shell-comment comment">#   host, user, password, database</span>

  <span class="token keyword">if</span><span class="token punctuation">(</span> <span class="token function">mysqli_connect_errno</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">)</span>
    <span class="token keyword">die</span><span class="token punctuation">(</span><span class="token string">"Connect failed: "</span> <span class="token punctuation">.</span> <span class="token function">mysqli_connect_error</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token variable">$query</span> <span class="token operator">=</span> "<span class="token constant">SELECT</span> Name<span class="token punctuation">,</span> Population
            <span class="token constant">FROM</span> City
            <span class="token constant">ORDER</span> <span class="token constant">BY</span> Population <span class="token constant">DESC</span>
            <span class="token constant">LIMIT</span> <span class="token number">5</span>"<span class="token punctuation">;</span>

  <span class="token shell-comment comment"># if no errors...</span>
  <span class="token keyword">if</span><span class="token punctuation">(</span> <span class="token variable">$result</span> <span class="token operator">=</span> <span class="token variable">$link</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">query</span><span class="token punctuation">(</span><span class="token variable">$query</span><span class="token punctuation">)</span> <span class="token punctuation">)</span>
  <span class="token punctuation">{</span>
<span class="token delimiter important">?&gt;</span></span>
&lt;table border="1" width="40%" cellpadding="4" cellspacing ="1"&gt;
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>tbody</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>tr</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>th</span> <span class="token attr-name">width</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>10%<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span>City<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>th</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>th</span><span class="token punctuation">&gt;</span></span>Population<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>th</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>tr</span><span class="token punctuation">&gt;</span></span>
<span class="token php language-php"><span class="token delimiter important">&lt;?</span>
    <span class="token shell-comment comment"># then display the results...</span>
    <span class="token keyword">while</span><span class="token punctuation">(</span><span class="token variable">$row</span> <span class="token operator">=</span> <span class="token variable">$result</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">fetch_object</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
      <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"&lt;tr&gt;\n  &lt;td align=\"center\"&gt;%s&lt;/td&gt;&lt;td&gt;%d&lt;/td&gt;\n&lt;/tr&gt;\n"</span><span class="token punctuation">,</span>
              <span class="token variable">$row</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">Name</span><span class="token punctuation">,</span> <span class="token variable">$row</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">Population</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token delimiter important">?&gt;</span></span>
  &lt;/tbody
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>table</span><span class="token punctuation">&gt;</span></span>
<span class="token php language-php"><span class="token delimiter important">&lt;?</span>
  <span class="token shell-comment comment"># ...and verify the number of rows that were retrieved</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"&lt;p&gt;Affected rows: %d&lt;/p&gt;\n"</span><span class="token punctuation">,</span> <span class="token variable">$link</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token property">affected_rows</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  <span class="token keyword">else</span>
    <span class="token shell-comment comment"># otherwise, tell us what went wrong</span>
    <span class="token keyword">echo</span> <span class="token function">mysqli_error</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token shell-comment comment"># free the result set and the mysqli connection object</span>
  <span class="token variable">$result</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token variable">$link</span><span class="token operator">-</span><span class="token operator">&gt;</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token delimiter important">?&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">&gt;</span></span></code></pre>
  </div>
  <p>
   We assume that the process running on the Web server can reach the
      IP address of the SQL node.
  </p>
  <p>
   In a similar fashion, you can use the MySQL C API, Perl-DBI,
      Python-mysql, or MySQL Connectors to perform the tasks of data
      definition and manipulation just as you would normally with MySQL.
  </p>
 </div>
 <br/>
</div>
