具有负载均衡功能的MySQL服务器集群部署及实现
MySQL是一个高速度、高性能、多线程的关系型数据库管理系统,适用平台多,可扩展性强。
在实际生产环境中,部署和实现具有一定负载均衡功能的 MySQL服务器集群,对于提高用户数据库应用系统的性能、速度和稳定性具有明显的作用。本文简要介绍了在 FreeBSD 7.0-Release系统上部署实现MySQL服务器集群的方案,并对可能出现的问题提供了相应的解决方法。
1. 引言
MySQL是一个高速度、高性能、多线程、开放源代码,建立在客户/服务器(Client /Server)结构上的关系型数据库管理系统(RDBMS)。它始于1979年,最初是Michael Widenius为瑞典TcX公司创建的UNIREG数据库系统,当时的UNIREG没有SQL(Structured Query Language结构化查询语言)接口,限制了它的应用。1996年5月,Widenius开发出了MySQL的最初版本,开始在Internet上公开发行。MySQL的开发人员从一开始就一直关注它的性能,为此不惜特性集,直到今天,MySQL依然保持本色,以高速度高性能为首要原则。随着时间的推移,MySQL也加入了大型数据库产品的高级特性,如存储过程、视图、触发器等,使其在企业级数据库系统中开始被部署应用[1]。2008年10 月,SUN公司收购了MySQL AB公司,开始进入开源领域。随着重量级操作系统Solaris的开源,SUN MySQL在数据库市场占有的份额将会进一步提高。因此,在生产环境中部署具有负载均衡功能的MySQL服务器集群,对于提高企业数据库应用系统的速度、稳定性及可伸缩性具有很大的现实意义,也可以有效降低应用系统的投资成本。本文将以FreeBSD 7.0-Release操作系统为例,利用MySQL数据库的复制(Replication)特性,简要介绍部署MySQL服务器集群的实现方法和相关注意事项。
2. 系统模型
本集群的结构为一个主MySQL服务器(Master)服务器与多个从属 MySQL服务器(Slave)建立复制(replication)连接,主服务器与从属服务器实现一定程度上的数据同步,多个从属服务器存储相同的数据副本,实现数据冗余,提供容错功能。部署开发应用系统时,对数据库操作代码进行优化,将写操作(如UPDATE、INSERT)定向到主服务器,把大量的查询操作(SELECT)定向到从属服务器,实现集群的负载均衡功能。如果主服务器发生故障,从属服务器将转换角色成为主服务器,使应用系统为终端用户提供不间断的网络服务;主服务器恢复运行后,将其转换为从属服务器,存储数据库副本,继续对终端用户提供数据查询检索服务。
3. 部署实现
本文以一台主服务器带三台从属服务器为例,简要介绍MySQL服务器集群的实现方案和具体方法步骤。
3.1 系统部署
由于FreeBSD系统对机器硬件要求较低,出于降低系统部署成本考虑,主服务器和从属服务器操作系统均采用FreeBSD 7.0-Release,并采用最小化定制安装,完成以后系统占用磁盘空间仅为254M(不计swap分区所占空间,它随具体机器内存容量的变化而变化)。为充分发挥系统硬件性能,MySQL数据库采用源代码编译安装。
3.1.1 安装FreeBSD系统
在主服务器和从属服务器上安装FreeBSD 7.0-Release,具体安装方法步骤不是本文主要内容,在此略过,如有疑问可以参考FreeBSD系统手册。需要说明的是为方便用户系统运行期间的维护管理,要打开系统的SSH服务功能,系统安装配置期间允许root用户远程登录,正常运行以后要关闭root用户的远程登录功能。在/etc/ssh /sshd_config配置文件中对PermitRootLogin设置为yes为允许root用户远程登录到系统,设置为no即为不允许。修改保存文件后执行kill –HUP `cat /var/run/sshd.pid`命令即可重启sshd守护进程,使设置生效[2]。上述操作需要root用户权限。
3.1.2 编译安装MySQL
到MySQL的官方网站http://www.mysql.com下载MySQL数据库的安装源代码压缩包,本例下载的是mysql-5.1.30.tar.gz,以root用户身份登录到系统,开始安装MySQL数据库系统。
由于在对MySQL进行源代码编译安装时要求使用GNU的C编译器,而FreeBSD系统本身提供的不是该编译器,因此用户必须下载安装GNU C编译器[3]。把下载的gnumake-3.81.tar.gz解压安装,按如下步骤即可安装GNU C编译器:
[root@FreebsdMaster/softwares]# tar –zxvf gnumake-3.81.tar.gz
[root@FreebsdMaster /softwares]# cd make-3.81
[root@FreebsdMaster /softwares/make-3.81]# ./configure ; make; make install; make clean
GNU C编译器默认安装到/usr/local/bin,在安装MySQL时按绝对路径调用make命令即可,如# /usr/local/bin/make。
MySQL的编译安装比较复杂,具体操作及相关注意事项如下:
(1). 解压mysql-5.1.30.tar.gz,并进行配置
[root@FreebsdMaster /softwares]# tar –zxvf mysql-5.1.30.tar.gz
[root@FreebsdMaster /softwares]# cd mysql-5.1.30
[root@FreebsdMaster /softwares/mysql-5.1.30]# ./configure \
-–prefix=/app/mysql5 -–with-charset=gb2312
说明:MySQL默认的安装目录是/usr/local/mysql,为了增强系统部署的灵活性,紧贴用户应用系统实际情况,可以用—-prefix 参数定制安装目录。--with-charset参数是使MySQL数据库支持中文gb2312字符集,如果需要支持其它字符集,使用--with- extra-charset参数,格式为—-with-extra-charset=CHARSET1,CHARSET2, … 。
(2). 编译安装MySQL
[root@FreebsdMaster /softwares/mysql-5.1.30]# /usr/local/bin/make
[root@FreebsdMaster /softwares/mysql-5.1.30]# /usr/local/bin/make install
[root@FreebsdMaster /softwares/ mysql-5.1.30]# /usr/local/bin/make clean
注意一定要按绝对路径调用GNU C编译器,否则编译过程报错退出。
(3). 为系统添加mysql用户组和用户
[root@FreebsdMaster /]# pw group add mysql
[root@FreebsdMaster /]# adduser
FreeBSD系统没有groupadd命令,其添加用户组的命令是pw,添加用户命令adduser按系统提示操作即可完成。当然用 pw user add mysql命令也可以完成添加用户mysql的功能,但不如adduser命令功能完善。
(4). 更改/app/mysql5目录及文件属性
[root@FreebsdMaster /app]# chmod –R mysql mysql5
[root@FreebsdMaster /app]# chgrp –R mysql mysql5
说明:/app/mysql5为MySQL数据库系统所在目录,如果用户部署的应用系统数据量极大,可以将数据库系统目录设在大容量磁盘阵列上。磁盘阵列设置与具体机器硬件相关性很大,具体操作设置参考具体硬件系统说明和FreeBSD操作手册。不借助第三方软件,FreeBSD 7.0-Release系统可以配置RAID0、RAID1磁盘阵列。
(5). 安装MySQL系统初始数据库
[root@FreebsdMaster /softwares/ mysql-5.1.30]# ./scripts/mysql_install_db.sh --basedir=/app/mysql5 –-data-dir=/app/mysql5/data
注意:必须考虑Unix系统的文件权限特性,首先给mysql_install_db.sh脚本添加可执行属性,否则安装脚本无法执行。执行 chmod +x ./scripts/mysql_install_db.sh命令即可。参数--basedir=/app/mysql5和--datadir=/app /msyql5/data为必加参数,否则脚本运行报错退出,无法安装系统初始数据库mysql和测试数据库test,mysql数据库包含系统权限设置表,没有这些表数据库服务进程mysqld将无法启动。
(6). 再次更改/app/mysql5目录及其文件属性
[root@FreebsdMaster /app]# chmod –R root mysql5
[root@FreebsdMaster /app]# chmod –R mysql mysql5/data
这样做是为了保证系统安全,防止通过MySQL系统漏洞的入侵行为。MySQL以mysql用户身份运行,对/data目录具有完全访问权限,对其它目录则只有系统管理员根据需要设置的访问权限,确保FreeBSD系统的安全。这里需要特别提醒用户注意的是要确保mysqld文件(MySQL的守护进程,位于/app/mysql5/libexec目录下)没有设置setuid标志位,如果设置了该标志位,入侵者对其它文件就具有其不应有的权限。具体原因参考关于Unix系统安全的手册,本文对这个问题不再赘述。
(7). 启动MySQL服务器
上述操作全部完成以后,输入如下命令启动MySQL数据库服务守护进程:
[root@FreebsdMaster /]# /app/mysql5/bin/mysqld_safe –user=mysql &
--user参数表示以mysql用户身份运行MySQL数据库服务守护进程,“&”符号表示以后台方式运行。输入 netstat –an|grep 3306命令,如果看到下面的输出则表示MySQL5.1.30已在用户系统上编译安装成功。其中,3306是MySQL数据库系统默认的监听端口号。
[root@FreebsdMaster /]# netstat –an|grep 3306
tcp4 0 0 *.3306 *.* LISTEN
按照同样方法与步骤在其它三台机器上安装FreeBSD 7.0-Release与MySQL5.1.30,为下面的MySQL服务器集群配置准备好系统平台基础。
3.2 系统配置
所有系统安装完毕之后,需要对它们做一些必要的配置方可按照用户预先设计的结构模型实现具有负载均衡功能的MySQL服务器集群。
3.2.1 FreeBSD操作系统配置
(1). 调整内核参数,优化操作系统性能
因为是处于生产环境的服务器,投入正常运行之前一定要对内核参数进行必要的调整,提升系统性能,加强稳定性。简要说就是去掉不必要的硬件支持选项,使新内核占用更少的内存,加快启动速度。不同机器硬件系统差异很大,用户可根据具体系统的硬件情况不断调整试验,最终定制出最优化、最安全的FreeBSD 系统。一定要屏蔽掉组合键Ctrl+Alt+Del的热重启功能,防止用户误按Ctrl+Alt+Del重启系统,引起数据库系统服务意外中断。在内核配置文件如MYKERNEL中加入:
options SC_DISABLE_REBOOT
重新编译系统内核,重启后即可屏蔽Ctrl+Alt+Del组合键的热重启功能。如何定制新内核及重新编译FreeBSD系统,用于可参考FreeBSD系统手册,此不赘述。
(2). 进行其它设置,增强系统安全性
用户可根据以下一般安全准则设置自己的FreeBSD系统。
·选择一个好的密码并予以妥善保护。
·对于敏感文件采用适当安全等级的加密算法进行加密。对于敏感级别不是很高的文件采用系统自带的crypt工具加密;对于敏感度极高的文件则使用专用加密工具,如PGP、GPG进行不对称加密,提高加密文件的防攻击能力。
·仔细设置文件访问权限,达到保护文件目的。
·保护好自己的.profile文件。恰当设置.profile文件的访问权限,确保只有自己能访问。
·仔细维护已设置setuid或setgid标志位的任何程序。
·禁止不加防范地离开已注册终端。任何时候都必须按常规正常退出系统,先执行clear清屏命令,防止其它用户看到操作痕迹,再执行exit命令退出终端。
·防范特洛伊木马程序。正确设置PATH变量的检索顺序,确保系统目录位于当前目录的前面。
·防范病毒的入侵。
·监控最近一次注册时间,确保没有他人盗用自己的帐号。
特别提示:对于生产环境的服务器,一定要保证服务器的物理安全,安全措施再完善的系统,一旦被入侵者物理接触,所有的安全措施将形同虚设!
Unix系统安全是一个很大的课题,本文只是提及了一些最肤浅的常识,具体内容用户可以参考相关书籍或网上资源。为确保Unix系统的配置具有高度的安全性,可以使用Internet安全中心(Center for Internet Security,CIS)提供的安全测试工具Benchmarks或评估工具Scoring Tools,检验和监控系统配置的安全性 [4]。
3.2.2 MySQL数据库系统配置
为了能使上述系统实现一对多的主从复制(replication)和冗余机制,还需要对以上四个MySQL服务器进行一些必要的配置。
(1). 配置主机名和IP地址
本实现方案中把MySQL服务器Master主机名设为FreebsdMaster,IP地址配置为192.168.1.100,其它三台Slave 依次设为 FreebsdSlave1,192.168.1.101;FreebsdSlave2,192.168.1.102;FreebsdSlave3,IP 地址192.168.1.103。
(2). 设置root用户密码
MySQL的root用户初始密码为空,为保证系统安全必须为root用户设置密码:
[root@FreebsdMaster /]# /app/mysql5/bin/mysql –uroot –p
Enter password:
mysql>SET PASSWORD=PASSWORD(‘secret');
同Unix系统一样,输入密码时屏幕没有任何回显,进入系统后执行SET PASSWORD SQL语句设置或修改MySQL root用户密码。为使应用系统服务器远程连接到MySQL数据库服务器,除本地root用户以外,还要设置一个能从用户内部网络其它终端登录的root 用户。具体操作如下:
mysql>USE mysql;
mysql>UPDATE user SET Host='%' WHERE Host='hostname.yourdomain' AND User='root';
mysql>flush privileges;
完成上述操作后应用系统服务器就可以连接到MySQL数据库服务器进行各种操作了,这个远程root用户的初始密码为空,用户需要从远程终端登录,为其设置密码。
(3). 开启MySQL服务器的二进制更新日志功能
上述MySQL服务器安装完成后,安装程序没有生成my.cnf配置文件,为此用户需要手工添加/etc/my.cnf文件。MySQL服务是由 /app/myql5/libexec/mysqld提供的,其寻找my.cnf配置文件的默认顺序为/etc/my.cnf,~/.my.cnf, /usr/local/mysql/etc/my.cnf

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

In the field of modern computers, the TCP/IP protocol is the basis for network communication. As an open source operating system, Linux has become the preferred operating system used by many businesses and organizations. However, as network applications and services become more and more critical components of business, administrators often need to optimize network performance to ensure fast and reliable data transfer. This article will introduce how to improve the network transmission speed of Linux systems by optimizing TCP/IP performance and network performance of Linux systems. This article will discuss a

Introduction to the failover and recovery mechanism in the Nginx load balancing solution: For high-load websites, the use of load balancing is one of the important means to ensure high availability of the website and improve performance. As a powerful open source web server, Nginx's load balancing function has been widely used. In load balancing, how to implement failover and recovery mechanisms is an important issue that needs to be considered. This article will introduce the failover and recovery mechanism in Nginx load balancing and give specific code examples. 1. Failover mechanism

Dynamic failure detection and load weight adjustment strategies in the Nginx load balancing solution require specific code examples. Introduction In high-concurrency network environments, load balancing is a common solution that can effectively improve the availability and performance of the website. Nginx is an open source, high-performance web server that provides powerful load balancing capabilities. This article will introduce two important features in Nginx load balancing, dynamic failure detection and load weight adjustment strategy, and provide specific code examples. 1. Dynamic failure detection Dynamic failure detection

Building a high-availability load balancing system: Best practices for NginxProxyManager Introduction: In the development of Internet applications, the load balancing system is one of the essential components. It can achieve high concurrency and high availability services by distributing requests to multiple servers. NginxProxyManager is a commonly used load balancing software. This article will introduce how to use NginxProxyManager to build a high-availability load balancing system and provide

Use NginxProxyManager to implement reverse proxy load balancing strategy NginxProxyManager is an Nginx-based proxy management tool that can help us easily implement reverse proxy and load balancing. By configuring NginxProxyManager, we can distribute requests to multiple backend servers to achieve load balancing and improve system availability and performance. 1. Install and configure NginxProxyManager

High Availability and Disaster Recovery Solution of Nginx Load Balancing Solution With the rapid development of the Internet, the high availability of Web services has become a key requirement. In order to achieve high availability and disaster tolerance, Nginx has always been one of the most commonly used and reliable load balancers. In this article, we will introduce Nginx’s high availability and disaster recovery solutions and provide specific code examples. High availability of Nginx is mainly achieved through the use of multiple servers. As a load balancer, Nginx can distribute traffic to multiple backend servers to

Load balancing strategies are crucial in Java frameworks for efficient distribution of requests. Depending on the concurrency situation, different strategies have different performance: Polling method: stable performance under low concurrency. Weighted polling method: The performance is similar to the polling method under low concurrency. Least number of connections method: best performance under high concurrency. Random method: simple but poor performance. Consistent Hashing: Balancing server load. Combined with practical cases, this article explains how to choose appropriate strategies based on performance data to significantly improve application performance.

Backend server health check and dynamic adjustment in the Nginx load balancing solution require specific code examples Summary: In the Nginx load balancing solution, the health status of the backend server is an important consideration. This article will introduce how to use Nginx's health check module and dynamic adjustment module to implement health check and dynamic adjustment of the back-end server, and give specific code examples. Introduction In modern application architecture, load balancing is one of the commonly used solutions to improve application performance and reliability. Ngi
