本节中的信息适用于在 Unix 和 Windows 平台上运行的 NDB 集群。The information in this section applies to NDB Cluster running on both Unix and Windows platforms.
在 NDB 集群中处理表和数据与在标准 MySQL 中处理并无太大不同。需要记住两个关键点: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:
要在集群中复制表,该表必须使用
NDBCLUSTER存储引擎。为此,可以在创建表时使用ENGINE=NDBCLUSTER或ENGINE=NDB选项:For a table to be replicated in the cluster, it must use theNDBCLUSTERstorage engine. To specify this, use theENGINE=NDBCLUSTERorENGINE=NDBoption when creating the table:CREATE TABLE
tbl_name(col_namecolumn_definitions) ENGINE=NDBCLUSTER;或者,对于使用不同存储引擎的现有表,可以使用
ALTER TABLE将表更改为使用NDBCLUSTER:Alternatively, for an existing table that uses a different storage engine, useALTER TABLEto change the table to useNDBCLUSTER:ALTER TABLE
tbl_nameENGINE=NDBCLUSTER;每个
NDBCLUSTER表都有一个主键。如果用户在创建表时没有定义主键,NDBCLUSTER存储引擎会自动生成一个隐藏主键。这样的主键会占用空间,就像其他表索引一样。(由于自动创建的索引可能导致内存不足,从而引发问题,这种情况并不少见。)EveryNDBCLUSTERtable has a primary key. If no primary key is defined by the user when a table is created, theNDBCLUSTERstorage 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.)
如果您正在使用mysqldump的输出从现有数据库导入表,您可以在文本编辑器中打开 SQL 脚本并向任何表创建语句添加ENGINE选项,或者替换任何现有的ENGINE选项。假设您在另一台不支持 NDB Cluster 的 MySQL 服务器上有world示例数据库,并且您想要导出City表:If you are importing tables from an existing database using the
output of mysqldump, you can open the SQL
script in a text editor and add the ENGINE
option to any table creation statements, or replace any existing
ENGINE options. Suppose that you have the
world sample database on another MySQL server
that does not support NDB Cluster, and you want to export the
City table:
$> mysqldump --add-drop-table world City > city_table.sql
生成的city_table.sql文件包含此表的创建语句(以及导入表数据所需的INSERT语句):The resulting city_table.sql file contains
this table creation statement (and the
INSERT statements necessary to
import the table data):
DROP TABLE IF EXISTS `City`;
CREATE TABLE `City` (
`ID` int(11) NOT NULL auto_increment,
`Name` char(35) NOT NULL default '',
`CountryCode` char(3) NOT NULL default '',
`District` char(20) NOT NULL default '',
`Population` int(11) NOT NULL default '0',
PRIMARY KEY (`ID`)
) ENGINE=MyISAM;
INSERT INTO `City` VALUES (1,'Kabul','AFG','Kabol',1780000);
INSERT INTO `City` VALUES (2,'Qandahar','AFG','Qandahar',237500);
INSERT INTO `City` VALUES (3,'Herat','AFG','Herat',186800);
(remaining INSERT statements omitted)
您需要确保 MySQL 为此表使用 NDBCLUSTER 存储引擎。有两种方法可以实现这一点。其中一种是在将表导入 Cluster 数据库之前 修改 表定义。以 City 表为例,修改定义中的 ENGINE 选项如下:You need to make sure that MySQL uses the
NDBCLUSTER storage engine for this
table. There are two ways that this can be accomplished. One of
these is to modify the table definition
before importing it into the Cluster
database. Using the City table as an example,
modify the ENGINE option of the definition as
follows:
DROP TABLE IF EXISTS `City`;
CREATE TABLE `City` (
`ID` int(11) NOT NULL auto_increment,
`Name` char(35) NOT NULL default '',
`CountryCode` char(3) NOT NULL default '',
`District` char(20) NOT NULL default '',
`Population` int(11) NOT NULL default '0',
PRIMARY KEY (`ID`)
) ENGINE=NDBCLUSTER;
INSERT INTO `City` VALUES (1,'Kabul','AFG','Kabol',1780000);
INSERT INTO `City` VALUES (2,'Qandahar','AFG','Qandahar',237500);
INSERT INTO `City` VALUES (3,'Herat','AFG','Herat',186800);
(remaining INSERT statements omitted)
这必须针对要成为集群数据库一部分的每个表的定义进行。实现这一点的最简单方法是在包含定义的文件中进行搜索和替换,将所有 ENGINE= 的实例替换为 engine_nameENGINE=NDBCLUSTER。如果您不想修改文件,也可以使用未修改的文件来创建表,然后使用 ALTER TABLE 来更改它们的存储引擎。具体内容在本节后面给出。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
ENGINE=
with engine_nameENGINE=NDBCLUSTER. If you do not want to
modify the file, you can use the unmodified file to create the
tables, and then use ALTER TABLE to
change their storage engine. The particulars are given later in
this section.
假设您已经在集群的 SQL 节点上创建了一个名为 world 的数据库,那么您可以使用 mysql 命令行客户端读取 city_table.sql,并以常规方式创建和填充相应的表:Assuming that you have already created a database named
world on the SQL node of the cluster, you can
then use the mysql command-line client to read
city_table.sql, and create and populate the
corresponding table in the usual manner:
$> mysql world < city_table.sql
非常重要的一点是,必须在运行 SQL 节点的主机上执行前述命令(在这种情况下,即在 IP 地址为 198.51.100.20 的机器上)。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
198.51.100.20).
要在 SQL 节点上创建整个 world 数据库的副本,请在非集群服务器上使用 mysqldump 将数据库导出到名为 world.sql 的文件(例如,在 /tmp 目录中)。然后按照刚才描述的方法修改表定义,并将文件导入集群的 SQL 节点,如下所示:To create a copy of the entire world database
on the SQL node, use mysqldump on the
noncluster server to export the database to a file named
world.sql (for example, in the
/tmp directory). Then modify the table
definitions as just described and import the file into the SQL
node of the cluster like this:
$> mysql world < /tmp/world.sql
如果将文件保存到其他位置,请相应调整前述指令。If you save the file to a different location, adjust the preceding instructions accordingly.
在 SQL 节点上运行 SELECT 查询与在任何其他 MySQL 服务器实例上运行它们没有区别。要从命令行运行查询,首先需要以通常的方式登录 MySQL 监视器(在 root 提示符下指定 Enter
password: 密码):Running SELECT 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
root password at the Enter
password: prompt):
$> mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1 to server version: 8.4.7-ndb-8.4.7
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
我们只需使用 MySQL 服务器的 root 账户,并假设您已按照安装 MySQL 服务器的标准安全措施操作,包括设置一个强密码的 root 密码。有关更多信息,请参阅 第 2.9.4 节,“保护初始 MySQL 账户”。We simply use the MySQL server's root
account and assume that you have followed the standard security
precautions for installing a MySQL server, including setting a
strong root password. For more information, see
Section 2.9.4, “Securing the Initial MySQL Account”.
值得注意的是,NDB Cluster 节点在相互访问时不使用 MySQL 权限系统。设置或更改 MySQL 用户帐户(包括 root 帐户)只会影响访问 SQL 节点的应用程序,而不会影响节点之间的交互。更多信息请参见 第 25.6.19.2 节,“NDB Cluster 和 MySQL 权限”。It is worth taking into account that NDB Cluster nodes do
not make use of the MySQL privilege system
when accessing one another. Setting or changing MySQL user
accounts (including the root account) effects
only applications that access the SQL node, not interaction
between nodes. See
Section 25.6.19.2, “NDB Cluster and MySQL Privileges”, for
more information.
如果在导入 SQL 脚本之前未修改表定义中的 ENGINE 子句,则应此时运行以下语句:If you did not modify the ENGINE clauses in the
table definitions prior to importing the SQL script, you should
run the following statements at this point:
mysql>USE world;mysql>ALTER TABLE City ENGINE=NDBCLUSTER;mysql>ALTER TABLE Country ENGINE=NDBCLUSTER;mysql>ALTER TABLE CountryLanguage ENGINE=NDBCLUSTER;
选择数据库并对该数据库中的表运行 SELECT 查询的方法与通常方式相同,退出 MySQL 监视器也是如此:Selecting a database and running a SELECT query against a table in that database is also accomplished in the usual manner, as is exiting the MySQL Monitor:
mysql>USE world;mysql>SELECT Name, Population FROM City ORDER BY Population DESC LIMIT 5;+-----------+------------+ | Name | Population | +-----------+------------+ | Bombay | 10500000 | | Seoul | 9981619 | | São Paulo | 9968485 | | Shanghai | 9696300 | | Jakarta | 9604900 | +-----------+------------+ 5 rows in set (0.34 sec) mysql>\qBye $>
使用 MySQL 的应用程序可以使用标准 API 访问 NDB 表。重要的是要记住,您的应用程序必须访问 SQL 节点,而不是管理节点或数据节点。这个简短的示例展示了如何使用运行在网络其他地方的 Web 服务器上的 PHP 5.X mysqli 扩展来执行刚才显示的 SELECT 语句:Applications that use MySQL can employ standard APIs to access
NDB 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 SELECT statement
just shown by using the PHP 5.X mysqli
extension running on a Web server elsewhere on the network:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<title>SIMPLE mysqli SELECT</title>
</head>
<body>
<?php
# connect to SQL node:
$link = new mysqli('198.51.100.20', 'root', 'root_password', 'world');
# parameters for mysqli constructor are:
# host, user, password, database
if( mysqli_connect_errno() )
die("Connect failed: " . mysqli_connect_error());
$query = "SELECT Name, Population
FROM City
ORDER BY Population DESC
LIMIT 5";
# if no errors...
if( $result = $link->query($query) )
{
?>
<table border="1" width="40%" cellpadding="4" cellspacing ="1">
<tbody>
<tr>
<th width="10%">City</th>
<th>Population</th>
</tr>
<?
# then display the results...
while($row = $result->fetch_object())
printf("<tr>\n <td align=\"center\">%s</td><td>%d</td>\n</tr>\n",
$row->Name, $row->Population);
?>
</tbody
</table>
<?
# ...and verify the number of rows that were retrieved
printf("<p>Affected rows: %d</p>\n", $link->affected_rows);
}
else
# otherwise, tell us what went wrong
echo mysqli_error();
# free the result set and the mysqli connection object
$result->close();
$link->close();
?>
</body>
</html>
我们假设运行在 Web 服务器上的进程可以访问 SQL 节点的 IP 地址。We assume that the process running on the Web server can reach the IP address of the SQL node.
以类似的方式,您可以使用 MySQL C API、Perl-DBI、Python-mysql 或 MySQL 连接器来执行数据定义和操作任务,就像通常使用 MySQL 一样。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.