search
HomeDatabaseMysql TutorialMySQL数据库服务器集群配置详解介绍

本文章介绍了关于MySQL数据库服务器集群配置,要做数据库服务器集群一般是用到大负载时才做,像我们一个二个几WIP是没有一点事情的。

一、介绍
这篇文档旨在介绍如何安装配置基于2台服务器的MySQL数据库集群。并且实现任意一台服务器出现问题或宕机时MySQL依然能够继续运行。
注意!
虽然这是基于2台服务器的MySQL集群,但也必须有额外的第三台服务器作为管理节点,但这台服务器可以在集群启动完成后关闭。同时需要注意的是 并不推荐在集群启动完成后关闭作为管理节点的服务器。尽管理论上可以建立基于只有2台服务器的MySQL集群,但是这样的架构,一旦一台服务器宕机之后集 群就无法继续正常工作了,这样也就失去了集群的意义了。出于这个原因,就需要有第三台服务器作为管理节点运行。
另外,可能很多朋友都没有3台服务器的实际环境,可以考虑在VMWare或其他虚拟机中进行实验。
下面假设这3台服务的情况:

 代码如下 复制代码
Server1: mysql1.vmtest.net 192.168.0.1   
Server2: mysql2.vmtest.net 192.168.0.2   
Server3: mysql3.vmtest.net 192.168.0.3  

Servers1和Server2作为实际配MySQL数据库集群的服务器。对于作为管理节点的Server3则要求较低,只需对Server3的系 统进行很小的调整并且无需安装MySQL,Server3可以使用一台配置较低的计算机并且可以在Server3同时运行其他服务。

二、在Server1和Server2上安装MySQL
注意:必须是max版本的MySQL,Standard版本不支持集群部署!
以下步骤需要在Server1和Server2上各做一次

 代码如下 复制代码


# mv mysql-max-4.1.9-pc-linux-gnu-i686.tar.gz /usr/local/   
 # cd /usr/local/   
 # groupadd mysql   
 # useradd -g mysql mysql   
 # tar -zxvf mysql-max-4.1.9-pc-linux-gnu-i686.tar.gz   
 # rm -f mysql-max-4.1.9-pc-linux-gnu-i686.tar.gz   
 # mv mysql-max-4.1.9-pc-linux-gnu-i686 mysql   
 # cd mysql   
 # scripts/mysql_install_db --user=mysql   
 # chown -R root .   
 # chown -R mysql data   
 # chgrp -R mysql .   
 # cp support-files/mysql.server /etc/rc.d/init.d/mysqld   
 # chmod +x /etc/rc.d/init.d/mysqld   
 # chkconfig --add mysqld

 

此时不要启动MySQL!


三、安装并配置管理节点服务器(Server3)
作为管理节点服务器,Server3需要ndb_mgm和ndb_mgmd两个文件:
从上下载mysql-max-4.1.9-pc-linux-gnu-i686.tar.gz

 代码如下 复制代码

# mkdir /usr/src/mysql-mgm   
 # cd /usr/src/mysql-mgm   
 # tar -zxvf mysql-max-4.1.9-pc-linux-gnu-i686.tar.gz   
 # rm mysql-max-4.1.9-pc-linux-gnu-i686.tar.gz   
 # cd mysql-max-4.1.9-pc-linux-gnu-i686   
 # mv bin/ndb_mgm .   
 # mv bin/ndb_mgmd .   
 # chmod +x ndb_mg*   
 # mv ndb_mg* /usr/bin/   
 # cd    # rm -rf /usr/src/mysql-mgm


 

在开始为这台管理节点服务器建立配置文件:

 

 代码如下 复制代码

# mkdir /var/lib/mysql-cluster   
 # cd /var/lib/mysql-cluster   
 # vi config.ini 在config.ini中添加如下内容:

[NDBD DEFAULT]

NoOfReplicas=2  

[MYSQLD DEFAULT]  

[NDB_MGMD DEFAULT]  

[TCP DEFAULT]   
# Managment Server  

[NDB_MGMD]     

HostName=192.168.0.3 #管理节点服务器Server3的IP地址
# Storage Engines  

[NDBD]  

HostName=192.168.0.1 #MySQL数据库集群Server1的IP地址   
DataDir= /var/lib/mysql-cluster  

[NDBD]  

HostName=192.168.0.2 #MySQL数据库集群Server2的IP地址   
DataDir=/var/lib/mysql-cluster    

  

以下2个[MYSQLD]可以填写Server1和Server2的主机名。
但为了能够更快的更换集群中的服务器,推荐留空,否则更换服务器后必须对这个配置进行更改。

[MYSQLD]  

[MYSQLD]  保存退出后,启动管理节点服务器Server3:

# ndb_mgmd     
启动管理节点后应该注意,这只是管理节点服务,并不是管理终端。因而你看不到任何关于启动后的输出信息。

四、配置集群服务器并启动MySQL
在Server1和Server2中都需要进行如下改动:

 代码如下 复制代码

# vi /etc/my.cnf  
 
 [mysqld]  
 
 ndbcluster  
 
 ndb-connectstring=192.168.0.3 #Server3的IP地址   
 [mysql_cluster]  
 
 ndb-connectstring=192.168.0.3 #Server3

的IP地址可以把/usr/local/mysql/bin/ndbd加到/etc/rc.local中实现开机启动。
注意:只有在第一次启动ndbd时或者对Server3的config.ini进行改动后才需要使用--initial参数!

五、检查工作状态
回到管理节点服务器Server3上,并启动管理终端:

 代码如下 复制代码
# /usr/bin/ndb_mgm     键入show命令查看当前工作状态:(下面是一个状态输出示例)
[root@mysql3 root]# /usr/bin/ndb_mgm    -- NDB Cluster -- Management Client --    ndb_mgm> show    Connected to Management Server at: localhost:1186    Cluster Configuration    [ndbd(NDB)] 2 node(s)    id=2 @192.168.0.1 (Version: 4.1.9, Nodegroup: 0, Master)    id=3 @192.168.0.2 (Version: 4.1.9, Nodegroup: 0)    [ndb_mgmd(MGM)] 1 node(s)    id=1 @192.168.0.3 (Version: 4.1.9)    [mysqld(API)] 2 node(s)    id=4 (Version: 4.1.9)    id=5 (Version: 4.1.9)    ndb_mgm>

如果上面没有问题,现在开始测试MySQL:
注意,这篇文档对于MySQL并没有设置root密码,推荐你自己设置Server1和Server2的MySQL root密码。
在Server1中:

 代码如下 复制代码
# /usr/local/mysql/bin/mysql -u root -p    > use test;    > CREATE TABLE ctest (i INT) ENGINE=NDBCLUSTER;    > INSERT INTO ctest () VALUES (1);    > SELECT * FROM ctest;  

  
应该可以看到1 row returned信息(返回数值1)。
如果上述正常,则换到Server2上重复上面的测试,观察效果。如果成功,则在Server2中执行INSERT再换回到Server1观察是否工作正常。
如果都没有问题,那么恭喜成功!

六、破坏性测试
将Server1或Server2的网线拔掉,观察另外一台MySQL数据库集群服务器工作是否正常(可以使用SELECT查询测试)。测试完毕后,重新插入网线即可。
如果你接触不到物理服务器,也就是说不能拔掉网线,那也可以这样测试:

在Server1或Server2上:

 代码如下 复制代码

# ps aux | grep ndbd     

将会看到所有ndbd进程信息:
root 5578 0.0 0.3 6220 1964 ? S 03:14 0:00 ndbd   
root 5579 0.0 20.4 492072 102828 ? R 03:14 0:04 ndbd   
root 23532 0.0 0.1 3680 684 pts/1 S 07:59 0:00 grep ndbd

     
然后杀掉一个ndbd进程以达到破坏MySQL集群服务器的目的:
# kill -9 5578 5579     
之后在另一台集群服务器上使用SELECT查询测试。并且在管理节点服务器的管理终端中执行show命令会看到被破坏的那台服务器的状态。
测试完成后,只需要重新启动被破坏服务器的ndbd进程即可:
# ndbd     注意!前面说过了,此时是不用加--inital参数的!
至此,MySQL数据库集群就配置完成了!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL's Place: Databases and ProgrammingMySQL's Place: Databases and ProgrammingApr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL: From Small Businesses to Large EnterprisesMySQL: From Small Businesses to Large EnterprisesApr 13, 2025 am 12:17 AM

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?Apr 13, 2025 am 12:16 AM

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.

MySQL: Not a Programming Language, But...MySQL: Not a Programming Language, But...Apr 13, 2025 am 12:03 AM

MySQL is not a programming language, but its query language SQL has the characteristics of a programming language: 1. SQL supports conditional judgment, loops and variable operations; 2. Through stored procedures, triggers and functions, users can perform complex logical operations in the database.

MySQL: An Introduction to the World's Most Popular DatabaseMySQL: An Introduction to the World's Most Popular DatabaseApr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

The Importance of MySQL: Data Storage and ManagementThe Importance of MySQL: Data Storage and ManagementApr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system suitable for data storage, management, query and security. 1. It supports a variety of operating systems and is widely used in Web applications and other fields. 2. Through the client-server architecture and different storage engines, MySQL processes data efficiently. 3. Basic usage includes creating databases and tables, inserting, querying and updating data. 4. Advanced usage involves complex queries and stored procedures. 5. Common errors can be debugged through the EXPLAIN statement. 6. Performance optimization includes the rational use of indexes and optimized query statements.

Why Use MySQL? Benefits and AdvantagesWhy Use MySQL? Benefits and AdvantagesApr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Describe InnoDB locking mechanisms (shared locks, exclusive locks, intention locks, record locks, gap locks, next-key locks).Describe InnoDB locking mechanisms (shared locks, exclusive locks, intention locks, record locks, gap locks, next-key locks).Apr 12, 2025 am 12:16 AM

InnoDB's lock mechanisms include shared locks, exclusive locks, intention locks, record locks, gap locks and next key locks. 1. Shared lock allows transactions to read data without preventing other transactions from reading. 2. Exclusive lock prevents other transactions from reading and modifying data. 3. Intention lock optimizes lock efficiency. 4. Record lock lock index record. 5. Gap lock locks index recording gap. 6. The next key lock is a combination of record lock and gap lock to ensure data consistency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use