CentOS6.5安装mysqlcluster7.3.7搭建mysql数据库集群环境(双管理中心)_MySQL
MySQL Cluster 是一种技术,该技术允许在无共享的系统中部署“内存中”数据库的 Cluster 。通过无共享体系结构,系统能够使用廉价的硬件,而且对软硬件无特殊要求。此外,由于每个组件有自己的内存和磁盘,不存在单点故障。 MySQL Cluster 由一组计算机构成,每台计算机上均运行着多种进程,包括MySQL服务器,NDB Cluster 的数据节点,管理服务器,以及(可能)专门的数据访问程序。
下文中没有特别指出的地方,都是两台服务器都需要执行的命令,并且由于是双管理中心配置,其实两台机器出了一些标识id和ip的配置不同外,其他参数配置都是基本一样的
1、下载mysql-cluster 7.3.7
http://dev.mysql.com/downloads/cluster/
2、环境清理及安装 1)清理CentOS6.5自带的mysql服务,其中第一个命令我不执行也可以,其他系统如果不可以,还是建议执行
# yum -y remove mysql # rpm -qa | grep mysql* # rpm -e --nodeps mysql-libs-5.1.71-1.el6.x86_64
2)环境准备
创建文件夹(分如下3个类来创建对应的文件夹)
存储节点:# mkdir /var/lib/mysql/data
管理节点:# mkdir /var/lib/mysql-cluster SQL节点:可不用 文件夹授权
进程DIR:# mkdir /var/run/mysqld
使用如下的命令来变更权限保证可写入:
# chmod -R 1777 /var/lib/mysql
# chmod -R 1777 /var/run/mysqld
# chmod -R 1777 /var/lib/mysql-cluster
3)安装mysql-cluster
首先解压MySQL-Cluster-gpl-7.4.2-1.el6.x86_64.rpm-bundle.tar
# tar xvf MySQL-Cluster-gpl-7.4.2-1.el6.x86_64.rpm-bundle.tar
然后执行如下命令安装
# rpm -ivh MySQL-Cluster-server-gpl-7.3.4-1.el6.x86_64.rpm
# rpm -ivh MySQL-Cluster-client-gpl-7.3.4-1.el6.x86_64.rpm
特别注意,当安装完毕server gpl包后,将出现如下提示信息,提醒我们整个cluster安装后的初次超级账户密码存在/root/.mysql_secret这个文件当中。
---------------------------------------------------------------------------------------------------------------------
A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER !
You will find that password in '/root/.mysql_secret'.
You must change that password on your first connect,
no other statement but 'SET PASSWORD' will be accepted.
See the manual for the semantics of the 'password expired' flag.
Also, the account for the anonymous user has been removed.
In addition, you can run:
/usr/bin/mysql_secure_installation
which will also give you the option of removing the test database.
This is strongly recommended for production servers.
-----------------------------------------------------------
2、配置管理节点
1)执行如下命令:
# cd /var/lib/mysql-cluster
# vi config.ini
2)配置config.ini文件,如附件100.218的配置文件:[computer]
Id=mgr-server-01
HostName=192.168.100.218
[mgm default]
datadir=/var/lib/mysql-cluster
[mgm]
HostName=192.168.100.218
NodeId=60
ExecuteOnComputer=mgr-server-01
PortNumber=1186
ArbitrationRank=2
[mgm]
HostName=192.168.100.217
NodeId=61
ExecuteOnComputer=mgr-server-01
PortNumber=1186
ArbitrationRank=2
[ndbd default]
NoOfReplicas=2
DataMemory=500M
IndexMemory=500M
[ndbd]
HostName=192.168.100.217
DataDir=/var/lib/mysql
NodeId=1
[ndbd]
HostName=192.168.100.218
DataDir=/var/lib/mysql
NodeId=2
[mysqld]
HostName=192.168.100.217
NodeId=81
[mysqld]
HostName=192.168.100.218
NodeId=82
100.217的配置文件:[computer]
Id=mgr-server-02
HostName=192.168.100.217
[mgm default]
datadir=/var/lib/mysql-cluster
[mgm]
HostName=192.168.100.218
NodeId=60
ExecuteOnComputer=mgr-server-01
PortNumber=1186
ArbitrationRank=2
[mgm]
HostName=192.168.100.217
NodeId=61
ExecuteOnComputer=mgr-server-01
PortNumber=1186
ArbitrationRank=2
[ndbd default]
NoOfReplicas=2
DataMemory=500M
IndexMemory=500M
[ndbd]
HostName=192.168.100.217
DataDir=/var/lib/mysql
NodeId=1
[ndbd]
HostName=192.168.100.218
DataDir=/var/lib/mysql
NodeId=2
[mysqld]
HostName=192.168.100.217
NodeId=81
[mysqld]
HostName=192.168.100.218
NodeId=82
其实两个文件差别不大,就在于computer配置中的名称和标识id
3、SQL节点和数据节点的配置
修改my.cnf文件,添加如下内容: [client] socket=/var/lib/mysql/mysql.sock [mysqld] max_connections=100 datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock ndbcluster ndb-connectstring=192.168.100.218,192.168.100.217 [mysqld_safe] log-error=/var/log/mysqld.log #pid-file=/var/run/mysqld/mysqld.pid pid-file=/var/lib/mysql/mysqld.pid
[mysql_cluster] ndb-connectstring=192.168.100.218,192.168.100.217
4、MySQL Cluster初次启动命令以及用户密码更改调整:(请严格按照次序启动)
1)启动mysql-cluster
执行初次启动前请先确认 将两台机器的防火墙关闭(service iptables stop 或者 设定 防火墙端口可通,两个端口即通讯端口1186、数据端口3306 )
初次启动mgt console命令:ndb_mgmd -f /var/lib/mysql-cluster/config.ini
启动均衡节点命令: ndbd --initial
启动数据节点命令: mysqld_safe --defaults-file=/usr/my.cnf --explicit_defaults_for_timestamp &
my.cnf在有些系统中是在etc目录下
注意在启动过程中需要监测整个控制台输出,发现有错误信息需要及时解决,根据错误日志内容来解决。
---------------------------------------------------------------------------------------------------------
如果一起正常,使用如下命令将Management console开启: ndb_mgm
执行# show
检查是否各个节点都已经完全启动,如下,每个节点都已经连接,如果有节点没连接,会看到
ndb_mgm> showConnected to Management Server at: 192.168.100.218:1186Cluster Configuration---------------------[ndbd(NDB)] 2 node(s)id=1 @192.168.100.217 (mysql-5.6.21 ndb-7.4.2, Nodegroup: 0)id=2 @192.168.100.218 (mysql-5.6.21 ndb-7.4.2, Nodegroup: 0, *)[ndb_mgmd(MGM)] 1 node(s)id=60 @192.168.100.218 (mysql-5.6.21 ndb-7.4.2)id=61 @192.168.100.217 (mysql-5.6.21 ndb-7.4.2)[mysqld(API)] 2 node(s)id=81 @192.168.100.217 (mysql-5.6.21 ndb-7.4.2)id=82 @192.168.100.218 (mysql-5.6.21 ndb-7.4.2)
如果有节点没连接,如下,id=81一行,表示没连接,检查问题故障点
ndb_mgm> showCluster Configuration---------------------[ndbd(NDB)] 2 node(s)id=1 @192.168.100.217 (mysql-5.6.21 ndb-7.4.2, Nodegroup: 0)id=2 @192.168.100.218 (mysql-5.6.21 ndb-7.4.2, Nodegroup: 0, *)[ndb_mgmd(MGM)] 1 node(s)id=60 @192.168.100.218 (mysql-5.6.21 ndb-7.4.2)id=61 @192.168.100.217 (mysql-5.6.21 ndb-7.4.2)[mysqld(API)] 2 node(s)id=81 (not connected, accepting connect from 192.168.100.217)id=82 @192.168.100.218 (mysql-5.6.21 ndb-7.4.2)
2)修改密码
当mysqld 启动完毕正常后(可以使用pgrep mysqld来获取进程ID),我们可以使用如下命令修改:
mysql -u root -p;
随机密码(具体请参见/root/.mysql_secret文件获取),进入后使用如下指令修改密码:
SET PASSWORD = PASSWORD('新密码');
但是这个新密码,必须是hash值,所以要使用如下方式获取密码字符串的hash值
select password('111111');
然而执行上面命令,会让你先设置密码,郁闷,又绕回来了,那就去其他地方的mysql上执行下吧 111111的hash值是“*FD571203974BA9AFE270FE62151AE967ECA5E0AA” SET PASSWORD = PASSWORD('*FD571203974BA9AFE270FE62151AE967ECA5E0AA'); 好郁闷啊,设置完以后,登陆还必须使用密文,那好吧,就用密文进去,进去后使用如下命令,强制使用明文进行登陆 use mysql; delete from user; grant all on *.* to root@'%' identified by "111111" with grant option; flush privileges; quit; 然后,可以使用明文密码登陆了
几台装有SQL数据节点的服务器皆需要执行一遍上述命令;
5、当上面步奏都操作完成以后,测试cluster是否正常工作
1)简单功能测试
在218上进入mysql后执行如下:
create database clustertest;
use clustertest;
CREATE TABLE testtable(Count INT) ENGINE=NDBCLUSTER; 注意:ENGINE=NDBCLUSTER; 引擎必须使用NDBCLUSTER才可以同步,否则不会进行同步
检查是否成功,在217上,进去mysql,查看是否自动创建了database和table,如果创建,则表示成功,如果失败,请检查环境配置
2)测试灾备情况下数据是否可以同步(一台数据库停掉了,再启动的时候,是否会将遗失的数据自动补充)
停掉217上的mysql服务,在218上执行
insert into testtable values (1);
启动217的mysql服务,使用show确认217的数据库节点已经连接上了,执行如下语句
select * from testtable;
如果发现有数据,则表示功能完备
6、关闭Cluster:(需要严格按照次序执行)
关闭数据节点:# mysqld stop (SQL节点可以用mysqladmin shutdown或别的方式关闭。)
在管理节点上执行:# ndb_mgm -e shutdown
将安全的关闭管理节点和数据节点。
关闭后使用如下的进程检测命令查看一下是否退出来了:
# pgrep mysqld
# ps aux | grep nbdb
如果没有,找到对应的pid进行kill 操作即可
kill -9 进程号
7、再次启动Cluster方案:
启动整个cluster的次序,注意,一些语句已经减去了初始化的内容:
ndb_mgmd -f /var/lib/mysql-cluster/config.inindbd
mysqld_safe --defaults-file=/etc/my.cnf --explicit_defaults_for_timestamp &
备注: 1、mysql安装后,默认会自动启动的,每次启动cluster的时候,都需要先把mysql的服务关闭,太麻烦,于是可以执行如下命令(我出手有点狠):
chkconfig --level 123456 mysql off
2、防火墙问题,偷个懒,我懒得去一一开放端口,干脆把防火墙直接关了(极不推荐的做法)
chkconfig --level 123456 iptables off

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

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



Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

PHP is a back-end programming language widely used in website development. It has powerful database operation functions and is often used to interact with databases such as MySQL. However, due to the complexity of Chinese character encoding, problems often arise when dealing with Chinese garbled characters in the database. This article will introduce the skills and practices of PHP in handling Chinese garbled characters in databases, including common causes of garbled characters, solutions and specific code examples. Common reasons for garbled characters are incorrect database character set settings: the correct character set needs to be selected when creating the database, such as utf8 or u

Through the Go standard library database/sql package, you can connect to remote databases such as MySQL, PostgreSQL or SQLite: create a connection string containing database connection information. Use the sql.Open() function to open a database connection. Perform database operations such as SQL queries and insert operations. Use defer to close the database connection to release resources.
