基于 GlusterFS 的高可用 MySQL 数据库系统测试
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的稳定性。

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

AI Hentai Generator
Generate AI Hentai for free.

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

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

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

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
