search
HomeDatabaseMysql TutorialMySQL数据库集群配置_MySQL

Mysql集群

bitsCN.com

转:http://server.ccw.com.cn/yyjq/htm2010/20100624_870385.shtml 
收藏,备用。

此文章主要向大家讲述的是对MySQL数据库集群进行正确配置的实际操作步骤,以及对其概念的讲述,如果你对其相关的实际操作有兴趣了解的话,以下的文章将会给你提供相关的知识。   
一、介绍 
这篇文档旨在介绍如何安装配置基于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上各做一次 

 1 # mv mysql-max-4.1.9-pc-linux-gnu-i686.tar.gz /usr/local/     2 # cd /usr/local/     3 # groupadd mysql     4 # useradd -g mysql mysql     5 # tar -zxvf mysql-max-4.1.9-pc-linux-gnu-i686.tar.gz     6 # rm -f mysql-max-4.1.9-pc-linux-gnu-i686.tar.gz     7 # mv mysql-max-4.1.9-pc-linux-gnu-i686 mysql     8 # cd mysql     9 # scripts/mysql_install_db --user=mysql    10 # chown -R root .    11 # chown -R mysql data    12 # chgrp -R mysql .    13 # cp support-files/mysql.server /etc/rc.d/init.d/mysqld    14 # chmod +x /etc/rc.d/init.d/mysqld    15 # chkconfig --add mysqld  

此时不要启动MySQL! 


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

 1 # mkdir /usr/src/mysql-mgm     2 # cd /usr/src/mysql-mgm     3 # tar -zxvf mysql-max-4.1.9-pc-linux-gnu-i686.tar.gz     4 # rm mysql-max-4.1.9-pc-linux-gnu-i686.tar.gz     5 # cd mysql-max-4.1.9-pc-linux-gnu-i686     6 # mv bin/ndb_mgm .     7 # mv bin/ndb_mgmd .     8 # chmod +x ndb_mg*     9 # mv ndb_mg* /usr/bin/    10 # cd    # rm -rf /usr/src/mysql-mgm  

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

1 # mkdir /var/lib/mysql-cluster    2 # cd /var/lib/mysql-cluster    3 # 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中都需要进行如下改动: 

 1 # vi /etc/my.cnf    2  3 [mysqld]    4  5 ndbcluster    6  7 ndb-connectstring=192.168.0.3 #Server3的IP地址     8 [mysql_cluster]    9 10 ndb-connectstring=192.168.0.3 #Server3的IP地址 

保存退出后,建立数据目录并启动MySQL: 

1 # mkdir /var/lib/mysql-cluster    2 # cd /var/lib/mysql-cluster    3 # /usr/local/mysql/bin/ndbd --initial    4 # /etc/rc.d/init.d/mysqld start  

可以把/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数据库集群就配置完成了!

bitsCN.com
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
When should you use a composite index versus multiple single-column indexes?When should you use a composite index versus multiple single-column indexes?Apr 11, 2025 am 12:06 AM

In database optimization, indexing strategies should be selected according to query requirements: 1. When the query involves multiple columns and the order of conditions is fixed, use composite indexes; 2. When the query involves multiple columns but the order of conditions is not fixed, use multiple single-column indexes. Composite indexes are suitable for optimizing multi-column queries, while single-column indexes are suitable for single-column queries.

How to identify and optimize slow queries in MySQL? (slow query log, performance_schema)How to identify and optimize slow queries in MySQL? (slow query log, performance_schema)Apr 10, 2025 am 09:36 AM

To optimize MySQL slow query, slowquerylog and performance_schema need to be used: 1. Enable slowquerylog and set thresholds to record slow query; 2. Use performance_schema to analyze query execution details, find out performance bottlenecks and optimize.

MySQL and SQL: Essential Skills for DevelopersMySQL and SQL: Essential Skills for DevelopersApr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

Describe MySQL asynchronous master-slave replication process.Describe MySQL asynchronous master-slave replication process.Apr 10, 2025 am 09:30 AM

MySQL asynchronous master-slave replication enables data synchronization through binlog, improving read performance and high availability. 1) The master server record changes to binlog; 2) The slave server reads binlog through I/O threads; 3) The server SQL thread applies binlog to synchronize data.

MySQL: Simple Concepts for Easy LearningMySQL: Simple Concepts for Easy LearningApr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

MySQL: A User-Friendly Introduction to DatabasesMySQL: A User-Friendly Introduction to DatabasesApr 10, 2025 am 09:27 AM

The installation and basic operations of MySQL include: 1. Download and install MySQL, set the root user password; 2. Use SQL commands to create databases and tables, such as CREATEDATABASE and CREATETABLE; 3. Execute CRUD operations, use INSERT, SELECT, UPDATE, DELETE commands; 4. Create indexes and stored procedures to optimize performance and implement complex logic. With these steps, you can build and manage MySQL databases from scratch.

How does the InnoDB Buffer Pool work and why is it crucial for performance?How does the InnoDB Buffer Pool work and why is it crucial for performance?Apr 09, 2025 am 12:12 AM

InnoDBBufferPool improves the performance of MySQL databases by loading data and index pages into memory. 1) The data page is loaded into the BufferPool to reduce disk I/O. 2) Dirty pages are marked and refreshed to disk regularly. 3) LRU algorithm management data page elimination. 4) The read-out mechanism loads the possible data pages in advance.

MySQL: The Ease of Data Management for BeginnersMySQL: The Ease of Data Management for BeginnersApr 09, 2025 am 12:07 AM

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and 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
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

mPDF

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),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)