项目进阶 之 集群环境搭建(二)MySQL集群 上次的博文中我们介绍了一下集群的相关概念,今天的博文我们介绍一下MySQL集群的相关内容。 1、MySQL集群简介 MySQL群集技术在分布式系统中为MySQL数据提供了冗余特性,增强了安全性,使得单个MySQL服务器故障不会对
项目进阶 之 集群环境搭建(二)MySQL集群上次的博文中我们介绍了一下集群的相关概念,今天的博文我们介绍一下MySQL集群的相关内容。
1、MySQL集群简介
MySQL群集技术在分布式系统中为MySQL数据提供了冗余特性,增强了安全性,使得单个MySQL服务器故障不会对系统产生巨大的负面效应,系统的稳定性得到保障。MySQL群集需要有一组计算机,每台计算机的角色可能是不一样的。MySQL群集中有三种节点:管理节点、数据节点和SQL节点。群集中的某计算机可能是某一种节点,也可能是两种或三种节点的集合。这三种节点只是在逻辑上的划分,所以它们不一定和物理计算机是一一对应的关系。
管理节点(也可以称管理服务器)主要负责管理数据节点和SQL节点,还有群集配置文件和群集日志文件。它监控其他节点的工作状态,能够启动、关闭或重启某个节点。其他节点从管理节点检索配置数据,当数据节点有新事件时就把事件信息发送给管理节点并写入群集日志。
数据节点用于存储数据。
SQL节点跟一般的MySQL服务器是一样的,我们可以通过它进行SQL操作。
下图中画出了三种群集节点及应用程序间的关系:
2、MySQL集群安装配置
下载MySQL集群:http://dev.mysql.com/downloads/cluster/
mysql-cluster-gpl-7.3.5-winx64.zip:
http://cdn.mysql.com/Downloads/MySQL-Cluster-7.3/mysql-cluster-gpl-7.3.5-winx64.zip
mysql-cluster-gpl-7.3.5-win32.zip:
http://cdn.mysql.com/Downloads/MySQL-Cluster-7.3/mysql-cluster-gpl-7.3.5-win32.zip
1)、MySQL简介
首先找三台电脑,或者是开三个虚拟机,管理节点部署在一台机子上,其他两台每台都部署一个数据节点和一个SQL节点。这里以两台机子举例,其中一台(IP为192.168.24.16)部署管理节点、数据节点和SQL节点,另一台(IP为192.168.24.43)部署数据节点和SQL节点。
实际应用中,不要将管理节点跟数据节点部署到一台机子上,因为如果数据节点宕机会导致管理节点不可用,同时整个MySQL群集也就都不可用了。所以一个MySQL群集理想情况下至少有三台服务器,将管理节点单独放到一台服务器上。这里以两台举例,只是为了说明三种节点的配置启动方法。
2)、配置管理节点
1、在IP为192.168.24.16的主机的C盘中新建文件夹mysql,然后在此文件夹下新建子目录bin和mysql-cluster,再将安装包中的bin目录下的ndb_mgm.exe和ndb_mgmd.exe拷贝到C:\mysql\bin下。在目录C:\mysql\bin下新建cluster-logs目录、config.ini文件和my.ini文件。2、config.ini文件内容如下:
[ndbd default] # Options affecting ndbd processes on all data nodes: # Number of replicas NoOfReplicas=2 DataDir=c:/mysqlcluster/datanode/mysql/bin/cluster-data # Directory for each data node's data files # Memory allocated to data storage DataMemory=80M # Memory allocated to index storage IndexMemory=18M # For DataMemory and IndexMemory, we have used the # default values. [ndb_mgmd] # Management process options: # Hostname or IP address of management node HostName=192.168.24.16 # Directory for management node log files DataDir=C:/mysql/bin/cluster-logs [ndbd] # Options for data node "A": # (one [ndbd] section per data node) # Hostname or IP address HostName=192.168.24.16 [ndbd] # Options for data node "B": # Hostname or IP address HostName=192.168.24.43 [mysqld] # SQL node options: # Hostname or IP address HostName=192.168.24.16 [mysqld] # SQL node options: # Hostname or IP address HostName=192.168.24.43
3、my.ini中的内容如下:
[mysql_cluster] # Options for management node process config-file=C:/mysql/bin/config.ini
3)、配置数据节点
1、在IP为192.168.24.16的主机中新建文件夹C:\mysqlcluster\datanode\mysql,然后在此文件夹中继续新建子目录bin和cluster-data,bin下再建一个子目录也叫cluster-data。
2、将安装包data目录下的所有文件拷贝到C:\mysqlcluster\datanode\mysql\cluster-data下
3、将安装包中bin目录下的ndbd.exe拷贝到C:\mysqlcluster\datanode\mysql\bin下,并在C:\mysqlcluster\datanode\mysql中新建my.ini文件,文件内容为:
[mysql_cluster] # Optionsfor data node process: #location of management server ndb-connectstring=192.168.24.16注:因为数据节点的配置是一样的,所以我们可以直接将192.168.24.16主机中的文件夹C:\mysqlcluster拷贝到192.168.24.43主机的C盘下。
4)、配置SQL节点
1、在192.168.24.16主机的C:\mysqlcluster下新建子目录sqlnode,在C:\mysqlcluster\sqlnode下新建子目录mysql,将安装包文件解压到mysql目录下,然后在C:\mysqlcluster\sqlnode\mysql下新建my.ini文件,文件内容为:[mysqld] # Options for mysqld process: # run NDB storage engine ndbcluster # location of management server ndb-connectstring=192.168.24.162、将my-default.ini 文件删除或更名为my-default.ini.bak
注:因为SQL节点的配置也是一样的,所以我们可以直接将192.168.24.16主机中的文件夹C:\mysqlcluster\sqlnode文件夹整个拷贝到192.168.24.43主机的C盘下。
3、启动MySQL Cluster
1)简介
在启动MySQL Cluster之前,首先要做的是保证每个SQL节点上的MySQL服务能够启动成功,并为每个SQL节点进行相关权限的分配以保证能够远程登录访问。然后依次启动三种节点。三种节点服务启动时,一定要按照先启动管理节点,后启动数据节点,再启动SQL节点的顺序进行。
2)、启动MySQL服务
1、进入C:\mysqlcluster\sqlnode\mysql\bin目录下,使用mysqld –install安装MySQL服务
2、 使用net start mysql命令启动MySQL服务
3)配置MySQL远程连接
1、 进入C:\mysqlcluster\sqlnode\mysql\bin目录下,使用mysql –uroot –p,然后输入密码,设置root用户密码。
2、 切换到mysql数据库,输入以下命令,为root用户分配远程连接的相关权限
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;

4)、启动管理节点
1、在192.168.24.16主机中打开命令行窗口,切到C:\mysql\bin目录,输入:
ndb_mgmd -f config.ini --configdir=C:\mysql\mysql-cluster回车,管理节点服务就启动了,命令行上可能没有任何提示信息,可以打开C:\mysql\bin\cluster-logs\ndb_1_cluster.log日志文件查看启动信息。
注意,此命令行窗口不能关闭,除非你想停止服务。
2、也可以将其做成服务,在命令行中输入:
ndb_mgmd --install=ndb_mgmd -f config.ini --configdir=C:\mysql\mysql-cluster
5)、启动数据节点
1、在192.168.24.16主机中打开新命令行窗口,切到C:\mysqlcluster\datanode\mysql\bin,输入:
ndbd
2、也可以将其做成服务,在命令行中输入:
ndbd --install=ndbd3、以相同的方法在其他数据节点,即192.168.24.43中启动数据节点服务。
4、在192.168.24.16主机中打开新命令行窗口,切到目录C:\mysql\bin,输入:
ndb_mgm回车,然后再输入:
ALL STATUS回车,就可以查看数据节点的连接信息了。
6)、启动SQL节点
1、在192.168.24.16主机中打开新命令行窗口,切到C:\mysqlcluster\sqlnode\mysql\bin,输入:
mysqld --console回车,SQL节点启动。
2、也可以将其做成服务,输入:
mysqld -install mysql3、以相同的方法在其他SQL节点,即192.168.24.43中启动SQL节点。
4、想要查看SQL节点的启动情况可以在192.168.1.10主机中同样打开新命令行,输入:
ndb_mgm回车,再输入:
SHOW回车,就可以看到SQL节点的连接情况了。
4、测试MySQL Cluster
1)、在任一SQL结点,即192.168.24.16主机上新创建一个数据库myDB,可以看到其他SQL结点,即192.168.24.43均创建了一个MyDB数据库2)、在任一SQL结点,即192.168.24.16主机上的myDB中使用
create table student (id int(2)) engine=ndbcluster;新建一个student表,可以看到其他SQL结点,即192.168.24.43均在myDB数据库中创建了一个student表
3)、在任一SQL结点,即192.168.24.16主机上的myDB中的student表插入几条数据之后,可以看到其他SQL结点,即192.168.24.43均作了相关的变化
注:与没有使用 Cluster的MySQL相比,在MySQL Cluster内操作数据的方式没有太大的区别。执行这类操作时应记住两点:
1、表必须用ENGINE=NDB或ENGINE=NDBCLUSTER选项创建,或用ALTER TABLE选项更改,以使用NDB Cluster存储引擎在 Cluster内复制它们。如果使用mysqldump的输出从已有数据库导入表,可在文本编辑器中打开SQL脚本,并将该选项添加到任何表创建语句,或用这类选项之一替换任何已有的ENGINE(或TYPE)选项。
2、另外还请记住,每个NDB表必须有一个主键。如果在创建表时用户未定义主键,NDB Cluster存储引擎将自动生成隐含的主键。(注释:该隐含 键也将占用空间,就像任何其他的表索引一样。由于没有足够的内存来容纳这些自动创建的键,出现问题并不罕见)。
到此为止关于MySQL集群的搭建就讲解完毕了,但是单纯的按照这样搭建还有一个缺陷,什么缺陷呢?咱们下次讲解,敬请期待!

Stored procedures are precompiled SQL statements in MySQL for improving performance and simplifying complex operations. 1. Improve performance: After the first compilation, subsequent calls do not need to be recompiled. 2. Improve security: Restrict data table access through permission control. 3. Simplify complex operations: combine multiple SQL statements to simplify application layer logic.

The working principle of MySQL query cache is to store the results of SELECT query, and when the same query is executed again, the cached results are directly returned. 1) Query cache improves database reading performance and finds cached results through hash values. 2) Simple configuration, set query_cache_type and query_cache_size in MySQL configuration file. 3) Use the SQL_NO_CACHE keyword to disable the cache of specific queries. 4) In high-frequency update environments, query cache may cause performance bottlenecks and needs to be optimized for use through monitoring and adjustment of parameters.

The reasons why MySQL is widely used in various projects include: 1. High performance and scalability, supporting multiple storage engines; 2. Easy to use and maintain, simple configuration and rich tools; 3. Rich ecosystem, attracting a large number of community and third-party tool support; 4. Cross-platform support, suitable for multiple operating systems.

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

MySQL backup policies include logical backup, physical backup, incremental backup, replication-based backup, and cloud backup. 1. Logical backup uses mysqldump to export database structure and data, which is suitable for small databases and version migrations. 2. Physical backups are fast and comprehensive by copying data files, but require database consistency. 3. Incremental backup uses binary logging to record changes, which is suitable for large databases. 4. Replication-based backup reduces the impact on the production system by backing up from the server. 5. Cloud backups such as AmazonRDS provide automation solutions, but costs and control need to be considered. When selecting a policy, database size, downtime tolerance, recovery time, and recovery point goals should be considered.

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

Optimizing database schema design in MySQL can improve performance through the following steps: 1. Index optimization: Create indexes on common query columns, balancing the overhead of query and inserting updates. 2. Table structure optimization: Reduce data redundancy through normalization or anti-normalization and improve access efficiency. 3. Data type selection: Use appropriate data types, such as INT instead of VARCHAR, to reduce storage space. 4. Partitioning and sub-table: For large data volumes, use partitioning and sub-table to disperse data to improve query and maintenance efficiency.

TooptimizeMySQLperformance,followthesesteps:1)Implementproperindexingtospeedupqueries,2)UseEXPLAINtoanalyzeandoptimizequeryperformance,3)Adjustserverconfigurationsettingslikeinnodb_buffer_pool_sizeandmax_connections,4)Usepartitioningforlargetablestoi


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools
