在Windows环境下配置MySQL集群_MySQL
前言
最近在项目中用到了MySQL集群,所以就和小伙伴们研究了两天。下面给大家分享一下成果。
小编始终觉得对新事物的学习,没有比看图这种方式更好地理解了。所以先来看一张mysql集群的架构图(摘自百度百科-MySQL Cluster):
上图一共分了四层:Applications、SQL、Storage、Management。
如果您的英语不是体育老师教的的话,那么您肯定已经猜出来每一层的职责了:
—–Applications主要是指需要连接数据库的应用程序;
—–SQL中每一个mysqld都是一个sql节点,Applications需要通过连接sql节点来存储数据,您可以把它看成应用程序与数据库集群进行数据交换的大门
—–Storage有‘仓库’的意思,所以数据都是存在数据节点(ndbd)中的,而且每个数据节点的数据都是一致的,都是一整套最新的数据
—–Management中就是管理节点,一个MySQL中只有一个管理节点,用来管理其他节点
综上所述,一个MySQL集群中包括三种节点(不包括Applications):管理节点、数据节点,sql节点。
一、下载集群版mysql
下载mysql-cluster-gpl-7.4.7-win32或mysql-cluster-gpl-7.4.7-winx64
下载地址:http://yunpan.cn/cd892RtysQ3Vk (提取码:45c8)
MySQL官网下载地址:http://dev.mysql.com/downloads/cluster/
二、配置MySQL集群
需要用三台机器(没有条件的朋友可以考虑用虚拟机),一台配置管理节点:另外两台每台配置一个数据节点和一个SQL节点(也可以用五台计算机,每台计算机配置一个节点):
管理节点:192.168.25.50
数据节点A:192.168.25.49
数据节点B:192.168.25.48
SQL节点A:192.168.25.49
SQL节点B:192.168.25.48
1、首先将下载压缩包解压到每台电脑的C:/mysql目录下:
当然放在其他盘的目录里也可以。
2、配置管理节点
在配置管理节点(192.168.25.50)的计算机上的C:\Mysql\Bin目录下建立cluster-logs和config两个文件夹。cluster-logs用来存储日志文件,在config文件夹中建立my.ini和config.ini两个配置文件:
my.ini
<code class="hljs ruby">[mysql_cluster] # Options for management node process config-file=C:/mysql/bin/config/config.ini</code>
config.ini
<code class="hljs ruby"><code class="hljs ruby">[ndbd default] # Options affecting ndbd processes on all data nodes: NoOfReplicas=2 # Number of replicas DataDir=C:/mysql/bin/cluster-data # Directory for each data node's data files # Forward slashes used in directory path, # rather than backslashes. This is correct; # see Important note in text DataMemory=80M # Memory allocated to data storage IndexMemory=18M # Memory allocated to index storage # For DataMemory and IndexMemory, we have used the # default values. Since the "world" database takes up # only about 500KB, this should be more than enough for # this example Cluster setup. [ndb_mgmd] # Management process options: HostName=192.168.25.50 # Hostname or IP address of management node DataDir=C:/mysql/bin/cluster-logs # Directory for management node log files [ndbd] # Options for data node "A": HostName=192.168.25.49 # Hostname or IP address [ndbd] # Options for data node "B": HostName=192.168.25.48 # Hostname or IP address [mysqld] # SQL node A options: HostName=192.168.25.49 # Hostname or IP address [mysqld] # SQL node B options: HostName=192.168.25.48 # Hostname or IP address</code></code>
<code class="hljs ruby">3、配置数据节点
<code class="hljs ruby">在配置数据节点(192.168.25.48、192.168.25.49)的计算机上的C:\Mysql\Bin目录下建立cluster-data文件夹,用来存放数据:
<code class="hljs ruby">SQL节点不用任何配置,至此,整个MySQL集群就搭建完成了。
<code class="hljs ruby">三、启动MySQL集群
<code class="hljs ruby">启动MySQL集群时,有一个启动顺序:先启动管理节点,再启动数据节点,再启动sql节点。
<code class="hljs ruby">1、启动管理节点:
<code class="hljs ruby">在cmd中运行如下命令
<code class="hljs ruby"><code class="hljs ruby"><code class="hljs tex">c:\mysql\bin\ndb_mgmd.exe --configdir=c:\mysql\bin\config --config-file=c:\mysql\bin\config\config.ini --ndb-nodeid=1 --reload –initial</code></code></code>
<code class="hljs ruby"><code class="hljs tex"><img src="/static/imghw/default1.png" data-src="http://img.bitscn.com/upimg/allimg/c150828/1440K00Q50-3M20.jpg" class="lazy" alt="这里写图片描述" title="\" />
<code class="hljs ruby"><code class="hljs tex">2、启动每个数据节点:
<code class="hljs ruby"><code class="hljs tex">在cmd中运行如下命令:
<code class="hljs ruby"><code class="hljs ruby"><code class="hljs tex"><code class="hljs tex">c:\mysql\bin\ndbd.exe --ndb-connectstring=192.168.25.50</code></code></code></code>
<code class="hljs ruby"><code class="hljs tex"><code class="hljs tex"><img src="/static/imghw/default1.png" data-src="http://img.bitscn.com/upimg/allimg/c150828/1440K00QF310-4B34.jpg" class="lazy" alt="这里写图片描述" title="\" />
<code class="hljs ruby"><code class="hljs tex"><code class="hljs tex">3、启动每个sql节点:
<code class="hljs ruby"><code class="hljs tex"><code class="hljs tex">在cmd中运行如下命令:
<code class="hljs ruby"><code class="hljs ruby"><code class="hljs tex"><code class="hljs tex"><code class="hljs brainfuck">c:\mysql\bin\mysqld.exe --ndbcluster --ndb-connectstring=192.168.25.50 --console</code></code></code></code></code>
<code class="hljs ruby"><code class="hljs tex"><code class="hljs tex"><code class="hljs brainfuck"><img src="/static/imghw/default1.png" data-src="http://img.bitscn.com/upimg/allimg/c150828/1440K00Q921Z-55413.jpg" class="lazy" alt="这里写图片描述" title="\" />
<code class="hljs ruby"><code class="hljs tex"><code class="hljs tex"><code class="hljs brainfuck">现在整个MySQL集群就已经启动了。
<code class="hljs ruby"><code class="hljs tex"><code class="hljs tex"><code class="hljs brainfuck">4、查看每个节点的状态:
<code class="hljs ruby"><code class="hljs tex"><code class="hljs tex"><code class="hljs brainfuck">在管理节点所在计算机上(192.168.25.50)打开ndb_mgm.exe,或者直接在cmd中运行
<code class="hljs ruby"><code class="hljs ruby"><code class="hljs tex"><code class="hljs tex"><code class="hljs brainfuck"><code class="hljs tex">c:\mysql\bin\ndb_mgm</code></code></code></code></code></code>
<code class="hljs ruby"><code class="hljs tex"><code class="hljs tex"><code class="hljs brainfuck"><code class="hljs tex">执行“show”命令,可以查看到每个节点的连接状态:
<code class="hljs ruby"><code class="hljs tex"><code class="hljs tex"><code class="hljs brainfuck"><code class="hljs tex"><img src="/static/imghw/default1.png" data-src="http://img.bitscn.com/upimg/allimg/c150828/1440K00R15620-A262.jpg" class="lazy" alt="这里写图片描述" title="\" />
<code class="hljs ruby"><code class="hljs tex"><code class="hljs tex"><code class="hljs brainfuck"><code class="hljs tex">这就表明每个节点均连接正常。下面测试数据。
<code class="hljs ruby"><code class="hljs tex"><code class="hljs tex"><code class="hljs brainfuck"><code class="hljs tex">三、测试MySQL集群
<code class="hljs ruby"><code class="hljs tex"><code class="hljs tex"><code class="hljs brainfuck"><code class="hljs tex">1、在sql节点A建立数据库并插入数据:
<code class="hljs ruby"><code class="hljs tex"><code class="hljs tex"><code class="hljs brainfuck"><code class="hljs tex">在sql节点A的计算机上(192.168.25.49)的cmd中运行C:\mysql\bin\mysql.exe -u root -p命令登录mysql,接下来需要输入密码时,密码默认为空(直接回车)。
<code class="hljs ruby"><code class="hljs tex"><code class="hljs tex"><code class="hljs brainfuck"><code class="hljs tex">创建数据库并插入数据:<br />
—–创建名为”MySQL_Cluster_Test”的数据库:
<code class="hljs ruby"><code class="hljs ruby"><code class="hljs tex"><code class="hljs tex"><code class="hljs brainfuck"><code class="hljs tex"><code class="hljs sql"> create database MySQL_Cluster_Test;</code></code></code></code></code></code></code>
<code class="hljs ruby"><code class="hljs tex"><code class="hljs tex"><code class="hljs brainfuck"><code class="hljs tex"><code class="hljs sql">—–创建表”T_User”:
<code class="hljs ruby"><code class="hljs ruby"><code class="hljs tex"><code class="hljs tex"><code class="hljs brainfuck"><code class="hljs tex"><code class="hljs sql"><code class="hljs sql">use MySQL_Cluster_Test; create table T_User(Name varchar(32),Age int) engine=ndbcluster;</code></code></code></code></code></code></code></code>
<code class="hljs ruby"><code class="hljs tex"><code class="hljs tex"><code class="hljs brainfuck"><code class="hljs tex"><code class="hljs sql"><code class="hljs sql">注意建表语句后面一定要加上 engine=ndbcluster
<code class="hljs ruby"><code class="hljs tex"><code class="hljs tex"><code class="hljs brainfuck"><code class="hljs tex"><code class="hljs sql"><code class="hljs sql">—–插入数据:
<code class="hljs ruby"><code class="hljs ruby"><code class="hljs tex"><code class="hljs tex"><code class="hljs brainfuck"><code class="hljs tex"><code class="hljs sql"><code class="hljs sql"><code class="hljs sql">insert into T_User values('DannyHoo',26);</code></code></code></code></code></code></code></code></code>
<code class="hljs ruby"><code class="hljs tex"><code class="hljs tex"><code class="hljs brainfuck"><code class="hljs tex"><code class="hljs sql"><code class="hljs sql"><code class="hljs sql">—–查询数据:
<code class="hljs ruby"><code class="hljs ruby"><code class="hljs tex"><code class="hljs tex"><code class="hljs brainfuck"><code class="hljs tex"><code class="hljs sql"><code class="hljs sql"><code class="hljs sql"><code class="hljs sql">select * from T_User;</code></code></code></code></code></code></code></code></code></code>
<code class="hljs ruby"><code class="hljs tex"><code class="hljs tex"><code class="hljs brainfuck"><code class="hljs tex"><code class="hljs sql"><code class="hljs sql"><code class="hljs sql"><code class="hljs sql"><img src="/static/imghw/default1.png" data-src="http://img.bitscn.com/upimg/allimg/c150828/1440K00R32Q0-L038.jpg" class="lazy" alt="这里写图片描述" title="\">
<code class="hljs ruby"><code class="hljs tex"><code class="hljs tex"><code class="hljs brainfuck"><code class="hljs tex"><code class="hljs sql"><code class="hljs sql"><code class="hljs sql"><code class="hljs sql">2、在sql节点B也可以查询到数据。
<code class="hljs ruby"><code class="hljs tex"><code class="hljs tex"><code class="hljs brainfuck"><code class="hljs tex"><code class="hljs sql"><code class="hljs sql"><code class="hljs sql"><code class="hljs sql">同样在sql节点B的计算机上(192.168.25.48)的cmd中运行C:\mysql\bin\mysql.exe -u root -p命令登录mysql。
<code class="hljs ruby"><code class="hljs tex"><code class="hljs tex"><code class="hljs brainfuck"><code class="hljs tex"><code class="hljs sql"><code class="hljs sql"><code class="hljs sql"><code class="hljs sql">执行 show databases; 命令可以查看到在sql节点A新建的数据库;<br>
执行use MySQL_Cluster_Test;<br>
select * from T_User;<br>
可以查询到在sql节点A插入的数据。
<code class="hljs ruby"><code class="hljs tex"><code class="hljs tex"><code class="hljs brainfuck"><code class="hljs tex"><code class="hljs sql"><code class="hljs sql"><code class="hljs sql"><code class="hljs sql"><img src="/static/imghw/default1.png" data-src="http://img.bitscn.com/upimg/allimg/c150828/1440K00R56250-Y330.jpg" class="lazy" alt="这里写图片描述" title="\">
<code class="hljs ruby"><code class="hljs tex"><code class="hljs tex"><code class="hljs brainfuck"><code class="hljs tex"><code class="hljs sql"><code class="hljs sql"><code class="hljs sql"><code class="hljs sql">到这里,整个集群的搭建和测试就完成了。假如一个数据节点宕机,并不会影响整个集群的运行,任何一个数据节点死掉甚至物理损坏都不用担心,因为每个数据节点保存的数据都是完整的一份数据(在你操作数据的时候,它早就自动为你把最新的数据备份到每一个数据节点上啦)。你可以测试一下,这时手动停止某个数据节点和sql节点,另外一个数据节点和sql节点还会正常运行。当你把停止的数据节点和sql节点重新启动时,会发现又重新连接到集群里了,而且每个数据节点的数据都是最新的。

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



Windows Recovery Environment (WinRE) is an environment used to repair Windows operating system errors. After entering WinRE, you can perform system restore, factory reset, uninstall updates, etc. If you are unable to boot into WinRE, this article will guide you through fixes to resolve the issue. Unable to boot into the Windows Recovery Environment If you cannot boot into the Windows Recovery Environment, use the fixes provided below: Check the status of the Windows Recovery Environment Use other methods to enter the Windows Recovery Environment Did you accidentally delete the Windows Recovery Partition? Perform an in-place upgrade or clean installation of Windows below, we have explained all these fixes in detail. 1] Check Wi

In this article, we will learn about the differences between Python and Anaconda. What is Python? Python is an open source language that places great emphasis on making the code easy to read and understand by indenting lines and providing whitespace. Python's flexibility and ease of use make it ideal for a variety of applications, including but not limited to scientific computing, artificial intelligence, and data science, as well as creating and developing online applications. When Python is tested, it is immediately translated into machine language because it is an interpreted language. Some languages, such as C++, require compilation to be understood. Proficiency in Python is an important advantage because it is very easy to understand, develop, execute and read. This makes Python

Scenario description for nodes to completely evacuate from ProxmoxVE and rejoin the cluster. When a node in the ProxmoxVE cluster is damaged and cannot be repaired quickly, the faulty node needs to be kicked out of the cluster cleanly and the residual information must be cleaned up. Otherwise, new nodes using the IP address used by the faulty node will not be able to join the cluster normally; similarly, after the faulty node that has separated from the cluster is repaired, although it has nothing to do with the cluster, it will not be able to access the web management of this single node. In the background, information about other nodes in the original ProxmoxVE cluster will appear, which is very annoying. Evict nodes from the cluster. If ProxmoxVE is a Ceph hyper-converged cluster, you need to log in to any node in the cluster (except the node you want to delete) on the host system Debian, and run the command

PHP database optimization method in high concurrency environment With the rapid development of the Internet, more and more websites and applications need to face high concurrency challenges. In this case, database performance optimization becomes particularly important, especially for systems that use PHP as the back-end development language. This article will introduce some database optimization methods in PHP high concurrency environment and give corresponding code examples. Using connection pooling In a high-concurrency environment, frequent creation and destruction of database connections may cause performance bottlenecks. Therefore, using connection pooling can

PHP integrated environment packages include: 1. PhpStorm, a powerful PHP integrated environment; 2. Eclipse, an open source integrated development environment; 3. Visual Studio Code, a lightweight open source code editor; 4. Sublime Text, a A popular text editor, widely used in various programming languages; 5. NetBeans, an integrated development environment developed by the Apache Software Foundation; 6. Zend Studio, an integrated development environment designed for PHP developers.

In today's cloud computing era, containerization technology has become one of the most popular technologies in the open source world. The emergence of Docker has made cloud computing more convenient and efficient, and has become an indispensable tool for developers and operation and maintenance personnel. The application of multi-node cluster technology is widely used based on Docker. Through multi-node cluster deployment, we can utilize resources more efficiently, improve reliability and scalability, and also be more flexible in deployment and management. Next, we will introduce how to use Docker to

Setting environment variables on Windows 11 can help you customize your system, run scripts, and configure applications. In this guide, we'll discuss three methods along with step-by-step instructions so you can configure your system to your liking. There are three types of environment variables System environment variables – Global variables are the lowest priority and are accessible to all users and applications on Windows and are typically used to define system-wide settings. User Environment Variables – Higher priority, these variables only apply to the current user and process running under that account, and are set by the user or application running under that account. Process environment variables – have the highest priority, they are temporary and apply to the current process and its sub-processes, providing the program

Common clusters in PHP include LAMP cluster, Nginx cluster, Memcached cluster, Redis cluster and Hadoop cluster. Detailed introduction: 1. LAMP cluster. LAMP refers to a combination of Linux, Apache, MySQL and PHP. It is a common PHP development environment. In a LAMP cluster, multiple servers run the same application and are balanced through a load balancer. Requests are distributed to different servers; 2. Nginx cluster, Nginx is a high-performance web server and so on.
