MySQL Cluster集群的初级部署教程_MySQL
Mysql Cluster概述
MySql Cluster最显著的优点就是高可用性,高实时性,高冗余,扩展性强。
它允许在无共享的系统中部署"内存中"数据库的Cluster.通过无共享体系结构,系统能够使用廉价的硬件.此外,由于每个组件有自己的内存和磁盘,所以不存在单点故障.
它由一组计算机构成,每台计算机上均运行者多种进程,包括mysql服务器,NDB cluster的数据节点,管理服务启,以及专门的数据访问程序
所有的这些节点构成一个完整的mysql集群体系.数据保存在"NDB存储服务器"的存储引擎中,表(结构)则保存在"mysql服务器"中.应用程序通过"mysql服务器"访问这些数据表,集群管理服务器通过管理工具(ndb_mgmd)来管理"NDB存储服务器".
基本概念
"NDB"是一种"内存中"的存储引擎,它具有可用性高和数据一致性好的特点.下面介绍mysql cluster 节点时,它表示进程.在单台计算机上可以有任意数目的节点.
管理节点(MGM):这类节点的作用是管理mysql cluster内的其他节点,如配置文件和cluster 日志,启动并停止节点,运行备份等.cluster中的每个节点从管理服务器上检索配置数据,并请求管理服务器所在位置的方式.当数据节点内出现新的事件时,节点将关于这类事件的信息传输到管理服务器上,然后,又将这类信息写入cluster日志。由于这类节点负责管理其他节点的配置,所以应在启动其他节点之前首先启动这类节点.MGM节点是用命令"ndb_mgmd"来启动
数据节点(NDB):这类节点用于保存cluster的数据.数据节点的数目与副本的数目相关,是片段的倍数.假设有2个副本,每个副本有2个片段,那么就有4个数据节点.不过没有必要设置多个副本.数据节点是用命令"ndbd"来启动的.
SQL节点:这是用来访问cluster数据的节点.对于MYSQL cluster来说,客户端节点是使用NDB cluster存储引擎的传统Mysql服务器.通常,sql节点使用将"ndbcluster"添加到"my.cnf"后使用"mysqld" 启动
此外,可以有任意数目的cluster客户端进程或应该程序.它们分为两种类型,即标准mysql客户端和管理客户端.
标准mysql客户端:能够从php,perl,c,c++,java,python,ruby等编写的现有mysql应用程序上访问mysql cluster
管理客户端:这类客户端与管理服务器相连,并提供了启动和停止节点,启动和停止消息跟踪,显示节点版本和状态,启动和停止备份等命令.
以下是mysql cluster 架构示意图:
由于Mysql Cluster采用的是TCP/IP方式连接,并且节点之间的数据传输没有加密,最后使用单独的子网里.
下面来实施部署
为了方便 这里我把管理节点,数据节点,sql节点放在一台机器上.
管理节点1 10.1.6.205
数据节点1 10.1.6.203
数据节点2 10.1.6.205
sql节点1 10.1.6.203
sql节点2 10.1.6.205
1.安装(这里安装7.2.6版本)
下载mysql-cluster-gpl-7.2.6-linux2.6-x86_64.tar.gz 二进制包(里面包含ndb,mysql)
root@10.1.6.205:~# tar -C /usr/local -xzvf mysql-cluster-gpl-7.2.6-linux2.6-x86_64.tar.gz root@10.1.6.205:/usr/local# ln -s /usr/local/mysql-cluster-gpl-7.2.8-linux2.6-i686 /usr/local/mysql root@10.1.6.205:/usr/local# cd mysql root@10.1.6.205:/usr/local/mysql# scripts/mysql_install_db --user=mysql root@10.1.6.205:/usr/local/mysql# chown -R mysql:mysql /usr/local/mysql 同理10.1.6.203
2.配置SQL节点和存储NDB节点
root@10.1.6.205:/usr/local/mysql# vim /etc/my.cnf [mysqld] basedir=/usr/local/mysql/ datadir=/usr/local/mysql/data/ user=mysql port=3306 socket=/tmp/mysql.sock ndbcluster max_connect_errors=10000 ndb-connectstring=10.1.6.205 connect_timeout = 300 [mysql_cluster] ndb-connectstring=10.1.6.205
同理10.1.6.203
3.配置管理节点
root@10.1.6.205:/usr/local/mysql# vim /opt/cluster/config.ini [ndbd default] NoOfReplicas=2 DataMemory=80M #分配data storage使用的内存 每个ndb占用 IndexMemory=18M #分配index storage使用的内存 每个ndb占用 [tcp default] portnumber=2205 #ndb监听端口 #设置管理节点 [ndb_mgmd] NodeId=1 hostname=10.1.6.205 datadir=/opt/cluster #在MGM上保存日志的目录 #设置存储节点NDB1 [ndbd] NodeId=2 hostname=10.1.6.203 datadir=/usr/local/mysql/data #设置存储节点NDB2 [ndbd] NodeId=3 hostname=10.1.6.205 datadir=/usr/local/mysql/data #设置SQL节点1 [mysqld] NodeId=4 hostname=10.1.6.203 #设置SQL节点2 [mysqld] NodeId=5 hostname=10.1.6.205 [mysqld] #运行任意ip连接 [mysqld]
4.启动mysql cluster
1)先启动管理节点服务器.2)启动NDB存储节点服务器.3)启动SQL节点服务器.
1)执行启动MGM节点进程
root@10.1.6.205:/usr/local/mysql/bin# /usr/local/mysql/bin/ndb_mgmd -f /opt/cluster/config.ini MySQL Cluster Management Server mysql-5.5.22 ndb-7.2.6
必须用参数-f或--config-file告诉ndb_mgm配置文件config.ini文件所在的位置.
2)在2台存储节点服务器上,如果是第一次启动NDB进程的话,必须先执行以下命令:
root@10.1.6.205:/usr/local/mysql/bin# /usr/local/mysql/bin/ndbd --initial 2013-08-28 23:40:36 [ndbd] INFO -- Angel connected to '10.1.6.205:1186' 2013-08-28 23:40:36 [ndbd] INFO -- Angel allocated nodeid: 2
注意:仅在首次启动NDB时,或者在备份/恢复或配置文件发生变化且重启NDB时才使用-initial参数.因为该参数会使节点删除由早期NDB实例创建的,用于恢复的任何文件,包括用于恢复的日志文件.
如果不是第一次启动,用以下命令
root@10.1.6.205:/usr/local/mysql/bin# /usr/local/mysql/bin/ndbd
3)启动SQL节点服务器
root@10.1.6.203:/usr/local/mysql/bin# /usr/local/mysql/bin/mysqld_safe /etc/my.cnf &
5.查看各个节点情况
root@10.1.6.205:/usr/local/mysql# /usr/local/mysql/bin/ndb_mgm -- NDB Cluster -- Management Client -- ndb_mgm> show Cluster Configuration --------------------- [ndbd(NDB)] 2 node(s) id=2 @10.1.6.203 (mysql-5.5.22 ndb-7.2.6, Nodegroup: 0, Master) id=3 @10.1.6.205 (mysql-5.5.22 ndb-7.2.6, Nodegroup: 0) [ndb_mgmd(MGM)] 1 node(s) id=1 @10.1.6.205 (mysql-5.5.22 ndb-7.2.6) [mysqld(API)] 4 node(s) id=4 @10.1.6.203 (mysql-5.5.22 ndb-7.2.6) id=5 @10.1.6.205 (mysql-5.5.22 ndb-7.2.6) id=6 (not connected, accepting connect from any host) id=7 (not connected, accepting connect from any host) ndb_mgm>
6.测试
注意:与没有使用Cluster的Mysql相比,在mysql cluster内操作数据的方式没有太大的区别.操作时注意
1)表必须用engine=NDB或engine=NDBCLUSTER选项创建
2)每个NDB表必须有一个主键.如果在创建表时用户未定义主键,NDB Cluster存储引擎会自动生成隐含的主键.
该隐含键也将占用空间,就像任何其他的表索引一样.由于没有足够的内存来容纳这些自动创建的键,所以很容易出现问题.
在203 sql节点1上创建表
root@10.1.6.203:/usr/local/mysql/bin# /usr/local/mysql/bin/mysql -uroot -p mysql> use test; mysql> create table dave (num int(10)) engine=ndb; mysql> show create table dave\G; *************************** 1. row *************************** Table: dave Create Table: CREATE TABLE `dave` ( `num` int(10) DEFAULT NULL ) ENGINE=ndbcluster DEFAULT CHARSET=latin1 1 row in set (0.00 sec) mysql> insert into dave -> values -> (100); Query OK, 1 row affected (0.01 sec) mysql> select * from dave; +------+ | num | +------+ | 100 | +------+
然后在205 sql节点2上查看该表
root@10.1.6.205:/usr/local/mysql# /usr/local/mysql/bin/mysql -uroot -p mysql> use test mysql> select * from dave; +------+ | num | +------+ | 100 | +------+
测试OK
关注一下表
mysql> select * from ndbinfo.memoryusage; +---------+--------------+--------+------------+----------+-------------+ | node_id | memory_type | used | used_pages | total | total_pages | +---------+--------------+--------+------------+----------+-------------+ | 2 | Data memory | 851968 | 26 | 83886080 | 2560 | | 2 | Index memory | 212992 | 26 | 19136512 | 2336 | | 3 | Data memory | 851968 | 26 | 83886080 | 2560 | | 3 | Index memory | 212992 | 26 | 19136512 | 2336 |
注意:使用量写满会访问不了,这时需要调整配置DataMemory,IndexMemory参数.各配置文件都需调整重启生效.
7.关闭cluster
root@10.1.6.205:/usr/local/mysql/bin# /usr/local/mysql/bin/ndb_mgm -e shutdown Connected to Management Server at: 10.1.6.205:1186 3 NDB Cluster node(s) have shutdown. Disconnecting to allow management server to shutdown.
再关闭SQL节点mysqld服务
root@10.1.6.203:/usr/local/mysql/bin# /usr/local/mysql/bin/mysqladmin -uroot -p shutdown Enter password: 130829 02:19:57 mysqld_safe mysqld from pid file /usr/local/mysql/data//debian.pid ended [1]+ Done /usr/local/mysql/bin/mysqld_safe /etc/my.cnf
以上就是mysql cluster初步部署, 更多相关内容请关注PHP中文网(www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

Setting up a MySQL connection pool using PHP can improve performance and scalability. The steps include: 1. Install the MySQLi extension; 2. Create a connection pool class; 3. Set the connection pool configuration; 4. Create a connection pool instance; 5. Obtain and release connections. With connection pooling, applications can avoid creating a new database connection for each request, thereby improving performance.

PHP provides the following methods to delete data in MySQL tables: DELETE statement: used to delete rows matching conditions from the table. TRUNCATETABLE statement: used to clear all data in the table, including auto-incremented IDs. Practical case: You can delete users from the database using HTML forms and PHP code. The form submits the user ID, and the PHP code uses the DELETE statement to delete the record matching the ID from the users table.
