Home Database Mysql Tutorial 基于 GlusterFS 的高可用 MySQL 数据库系统测试

基于 GlusterFS 的高可用 MySQL 数据库系统测试

Jun 07, 2016 pm 03:46 PM
glusterfs mysql Available based on database High availability

MySQL 是使用最广泛的数据库之一,而 GlusterFS 是一个开源分布式文件系统。MySQL 的一个主要问题是存在单点故障,当MySQL节点发生故障时,整个业务系统将崩溃。MySQL replication,MySQL Cluster 和 MySQLGalera 都试图不同程度的解决这个问题,本文试图使

MySQL 是使用最广泛的数据库之一,而 GlusterFS 是一个开源分布式文件系统。MySQL 的一个主要问题是存在单点故障,当MySQL节点发生故障时,整个业务系统将崩溃。MySQL replication,MySQL Cluster 和 MySQL+Galera 都试图不同程度的解决这个问题,本文试图使用分布式文件系统 GlusterFS 来尝试解决 MySQL 的高可用性。

1. 安装 Ubuntu

首先我们搭建一个测试环境,在一台物理机(笔记本上)安装 2 个Ubuntu 13.10 虚拟机. 虚拟机软件可以使用VMWare的,也可以用VirtualBox。

虚拟机规范:
- 1 CPU
- 1G memory
- 2 Disks: 一块磁盘20G,用于安装系统,一块磁盘10G,留给 Gluster 使用。在安装的时候第二块磁盘可以留着,装完后用fdisk 分区并格式化。
- 其他设置: 保持2个VM配置一致,例如时区等。

安装完Ubuntu后可以做个 snapshot,系统崩溃后可以快速恢复。

2. 配置 Ubuntu

使用gluster和mysql前,先对ubuntu虚拟机做一些配置

2.1 network
安装之后虚拟机默认使用DHCP, 为了避免IP地址的变化,使用静态IP。

选择右上角的网络连接图标 -> 编辑连接

进入 IPv4 设置页面,选择 Manual 方法,并加入以下地址信息 (根据你自己的系统做适当的修改):
- Address: 192.168.53.218
- mask: 255.255.255.0
- Gateway: 192.168.53.2
- DNS: 192.168.53.2

如果不知道当前系统的 IP、网关和DNS,可以使用 nm-tool 显示系统的网络信息。

2.2 disk
在第二块磁盘上分区、创建文件系统,并mount

$ sudo fdisk /dev/sdb<br> n > w<br> $ sudo mkfs.ext4 /dev/sdb1<br> $ sudo mkdir -p /data/gv0/brick1<br> $ sudo vi /etc/fstab # 将下面这一行加入到 /etc/fstab 中。<br> /dev/sdb1 /data/gv0/brick1 ext4 defaults 1 2<br> $ sudo mount -a && mount

2.3 其他配置
2.3.1 sudoers
修改 sudoers 打开无密码访问。将下面一行加入到 /etc/sudoers, u1 是用户名,替换成你自己的用户名。

u1 ALL=(ALL:ALL) NOPASSWD: ALL<br> %sudo ALL=(ALL:ALL) NOPASSWD: ALL

2.3.2 ssh
$ sudo apt-get install ssh

3. 安装 gluster

首先安装 gluster 的依赖包, 如有需要,使用 apt-cache search 搜索包得名字。

$ sudo apt-get install -y flex bison openssl libssl-dev libreadline6 libreadline6-dev systemtap systemtap-sdt-dev

编译并安装最新的 gluster (目前最新版本是 3.4.2)

$ wget http://download.gluster.org/pub/gluster/glusterfs/3.4/3.4.2/glusterfs-3.4.2.tar.gz<br> $ tar zxf gluster-3.4.2.tar.gz<br> $ cd gluster-3.4.2<br> $ ./configure --enable-debug<br> $ make && sudo make install<br> $ sudo ldconfig<br> $ gluster --version
现在安装另一个虚拟机,可以做个snapshot,以免后面发生问题时可以快速恢复到可用状态。

安装之后,2台主机的主机名和IP地址信息:
- u1: 192.168.53.218
- u2: 192.168.53.221

4. 配置 Gluster replication (AFR) 并进行简单的测试

4.1 准备 bricks
bricks 是 gluster 的术语,是一个volume的基本组成部分,可以认为是主机上的一个目录。

在两个虚拟机上:

$ sudo mkdir -p /data/gv0/brick1/test

4.2 组建2个虚拟机组成的可信存储池
在 192.168.53.218 上执行下面命令

$ sudo gluster peer probe 192.168.53.221 # 替换为你的系统<br> $ sudo gluster peer status<br> Number of Peers: 1

Hostname: 192.168.53.221<br> Port: 24007<br> Uuid: e1de158a-1a81-4d6c-af55-b548d4c4c174<br> State: Peer in Cluster (Connected)

4.3 创建 replicated volume
在任意一台虚拟机上执行下面命令:

$ sudo gluster volume create testvol replica 2 192.168.53.218:/data/gv0/brick1/test 192.168.53.221:/data/gv0/brick1/test

// disable NFS (这一步是可选的)<br> $ sudo gluster volume set testvol nfs.disable on

$ sudo gluster volume start testvol<br> $ sudo gluster volume status

在一台虚拟机上(譬如 192.168.53.218 上)挂载创建的 gluster volume.

$ sudo mount -t glusterfs 192.168.53.218:/testvol /mnt/testvol

4.4 启动和停止 gluster 服务
- 启动: sudo /etc/init.d/glusterd start
- 停止: umount: sudo umount /path/to/dir
- 停止: volume: sudo gluster volume stop <volname></volname>
- 停止: glusterd: sudo /etc/init.d/glusterd stop

4.5 测试 Gluster 的 replication 功能
在前面挂载了 gluster volume 的虚拟机上创建一个文件:
$ echo "a\nb\n\c" > /mnt/testvol/test.txt

观察 brick 目录,test 文件应该出现在两个机器上的 /data/gv0/brick/test 目录下面。

$ cd /data/gv0/brick/test && rm test.txt
在一个brick目录内删除刚刚创建的文件,gluster 会自动恢复它。 (不是真正的全自动,只有在访问这个文件时才会恢复,否则gluster 不会恢复被删除的文件)

$ ls /data/gv0/brick/test/test.txt # gluster 会发现文件丢失了,并自动恢复。

4.5.1 由于 DNS 问题,文件不能自动恢复
在测试的过程中发现文件在一个brick上被删除后, gluster 不能自动恢复,并且log里面报告了大量的connection timeout、不能连接之类的错误。 阅读代码后返现 gluster 的自动恢复部分代码会使用 DNS, 尽管我们前面使用的都是 IP 地址。

而 Ubuntu 自带了一个小巧的 DNS server dnsmasq。它支持不在全局DNS内的局域网机器域名解析。 dnsmasq 用户场景是通过ADSL接入的家庭局域网。 也适合任何小型网络(小于1000个client)。 对这个测试而言最关键的是,它支持 /etc/hosts 配置文件。 Ubuntu 的network-manager 会自动启动 dnsmasq,然而默认它会忽略 /etc/hosts。 可以创建下面一个文件,使得这个文件生效:
$ cat /etc/NetworkManager/dnsmasq.d/hosts.conf<br> addn-hosts=/etc/hosts

$ sudo service networking restart<br> $ sudo /etc/init.d/dns-clean restart

修改后要求下面的几个命令输出相同的IP地址:

$ host u1<br> $ nslookup u1<br> $ getent ahosts u1

5. 安装 mysql

在所有虚拟机上上创建 gluster brick 目录,用来保存 mysql data。

$ mkdir -p /data/gv0/brick1/mysqldata
创建一个新的 volume:
$ sudo gluster volume create mysqldata replica 2 192.168.53.218:/data/gv0/brick1/mysqldata 192.168.53.221:/data/gv0/brick1/mysqldata

启动新创建的 volume:
$ sudo gluster volume start mysqldata

登录一台机器,mount gluster volume
$ sudo mount -t glusterfs 192.168.53.218:/mysqldata /mnt/mysqldata

安装 mysql 并使用新mount的volume:
$ 首先下载 MySQL 二进制包<br> $ sudo groupadd mysql<br> $ sudo useradd -r -g mysql mysql<br> $ cd /usr/local<br> $ sudo tar zxvf /path/to/mysql-VERSION-OS.tar.gz<br> $ sudo ln -s full-path-to-mysql-VERSION-OS mysql<br> $ cd /usr/local/mysql<br> $ sudo chown -R mysql:mysql .

$ sudo apt-get install -y libaio-dev # mysql 需要libaio.so
# 以上操作需要在所有 VM 上执行。

修改最大连接数, 这在后面的测试中会使用:
mysql> show variables like “max_connections”;
mysql> set global max_connections = 300; # 重启就失效了。
永久修改的办法是加入到配置文件中:
max_connections = 300
安装 mysql
$ sudo /usr/local/mysql/scripts/mysql_install_db --user=mysql<br> --basedir=/usr/local/mysql --datadir=/mnt/mysqldata<br> --defaults-file=/usr/local/mysql/my.cnf

启动mysql:
$ sudo /usr/local/mysql/support-files/mysql.server start<br> --datadir=/mnt/mysqldata --log-error=/usr/local/mysql/mysql.error

配置mysql:
$ /usr/local/mysql/bin/mysqladmin -u root password ''<br> $ echo 'bind-address: 0.0.0.0' >> /usr/local/mysql/my.cnf

$ /usr/local/mysql/bin/mysql -uroot # 执行下面的语句.

CREATE USER 'yydzero'@'localhost' IDENTIFIED BY 'goodluck';<br> GRANT ALL PRIVILEGES ON *.* TO 'yydzero'@'localhost' WITH GRANT OPTION;<br> CREATE USER 'yydzero'@'%' IDENTIFIED BY 'goodluck';<br> GRANT ALL PRIVILEGES ON *.* TO 'yydzero'@'%' WITH GRANT OPTION;

6. 测试 MySQL + GlusterFS

mysql> create database gluster<br> mysql> use gluster<br> mysql> CREATE TABLE IF NOT EXISTS test1 (i int, v varchar(1024), bb blob, INDEX USING BTREE (i))<br> mysql> INSERT INTO test1 (i, v) VALUES (1, 'x9byod')<br> mysql> SELECT * FROM test1;

这样我们就可以在 Gluster 分布式文件系统上运行 MySQL 了。后面我们将在这个系统上进行高并发和高故障率的测试,以验证Gluster+MySQL的稳定性。

Statement of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1252
29
C# Tutorial
1226
24
MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

Oracle's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL for Beginners: Getting Started with Database Management MySQL for Beginners: Getting Started with Database Management Apr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

See all articles