首页 > 代码库 > Widnwos平台 Mysql5.7 + MyFabric1.5 数据库、群集HA 高可用性,安装部署
Widnwos平台 Mysql5.7 + MyFabric1.5 数据库、群集HA 高可用性,安装部署
一、基于Windows 安装部署
MySQL Fabric是Oracle推出的用来管理一“群”服务器的系统,可以非常简单地用来部署管理MySQL的数据分片(Shading)和高可用性(High Availability)功能。
二、准备的软件
使用MySQL的Fabric功能需要下载如下软件:
- MySQL server 5.7 版本,Fabric功能需要GTID 即全局事务ID(global transaction identifier)来实现。
- MySQL server 5.7 下载地址:http://dev.mysql.com/downloads/mysql/ 解压缩版。
- MySQ Fabric1.5 下载地址:http://dev.mysql.com/downloads/utilities/ 版本选择MySQL Utilities 1.5.6下载(包含MySQ Fabric1.5) 。
- Mysql-connector-net-6.9.9 下载地址:http://dev.mysql.com/downloads/connector/net/ 下载 Windows (x86, 32-bit), MSI Installer ,.net 测试使用。
- 需要使用Python2.6之后的版本,在Windows使用Python通常需要安装,地址:https://www.python.org/downloads/
三、软件安装
- HA 部署节点列表,一台机器安装5个MySQL 实例(模拟5台机器)
MySQL 实例地址 | MySQL 实例端口 | 备注说明 | 安装路径 | 数据库路径 | 配置文件路径 | MySQL服务名称 | 管理用户名称 | 管理用户密码 |
localhost | 3306 | MySQL Fabric 管理节点 | d:\test\Fabric | d:\test\Fabric\data | d:\test\Fabric\my.ini | MySQL3306 | fabric | root |
localhost | 3311 | 节点1 设置主从复制 | d:\test\MySQL3311\ | d:\test\MySQL3311\data | d:\test\MySQL3311\my.ini | MySQL3311 | fabric | root |
localhost | 3312 | 节点2 设置主从复制 | d:\test\MySQL3312\ | d:\test\MySQL3312\data | d:\test\MySQL3312\my.ini | MySQL3312 | fabric | root |
localhost | 3313 | 节点3 设置主从复制 | d:\test\MySQL3313\ | d:\test\MySQL3313\data | d:\test\MySQL3313\my.ini | MySQL3313 | fabric | root |
localhost | 3314 | 节点4 设置主从复制 | d:\test\MySQL3314\ | d:\test\MySQL3314\data | d:\test\MySQL3314\my.ini | MySQL3314 | fabric | root |
MySQL Server由于是压缩版的,各位可以把它解压在任何路径下(本实例d:\test\Fabric)。因为MySQL Fabric是用来管理服务器群的系统,在本例中我们需要使用5个MySQL Server实例来模拟5台服务器进行演示。
另外需要4个MySQL Server服务器用来构成一主三从的高可用构成,一主三从主要利用了MySQL的复制功能
系统环境设置 Path 添加 d:\test\Fabric\bin
1. Fabric管理节点(backing store)配置
1). MySQL 解压到d:\test\Fabric
2). 修改My.ini 配置文件
my.ini 配置如下:
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It‘s a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
basedir=d:\\test\\fabric
datadir=d:\\test\fabric\\data
port =3306
server_id =6
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
3). 数据库初始化
以系统管理管理身份运行 CMD, 执行命令:mysqld --initialize --datadir=d:\test\fabric\data 生成 data 目录及数据库文件;
查看初始化密码 打开 d:\test\fabric\data\ 扩展名为 .err的文件,如下:
初始化密码在最后一行:byy;ydXaW2dI(本例)
- 安装 MySQL3306 实例:
执行:mysqld install MySQL3306 --defaults-file=d:\test\fabric\my.ini
启动服务:net start MySQL3306
4). 修改 用户root 的密码
- 执行:mysql -h localhost -P 3306 -u root -p
- 输入密码:byy;ydXaW2dI
按顺序执行下列命令:
- mysql>set password=password(‘root‘);
- mysql>use mysql;
- mysql>update user set host=‘%‘ where user=‘root‘; 客服端授权访问;
- mysql>flush privileges; //刷新系统权限表
5). 创建用户并授权
- CREATE USER ‘fabric‘@‘localhost‘ IDENTIFIED BY ‘root‘;
- GRANT ALL ON fabric.* TO ‘fabric‘@‘localhost‘;
6). MySQL Utilities的安装路径为C:\MySQL Utilities(MysqlFabric1.5)
MySQL Fabric需要一个名为fabric.cfg的设置文件,该文件的路径 C:\MySQL Utilities\etc\mysql\fabric.cfg
下面是本例所使用的设置文件,[DEFAULT]里面是基本Fabric的基本信息,包括MySQLutility的安装位置,日志位置等等。[storage]里面是Fabric节点的相关信息,IP地址用户名和使用的fabric数据库名称。
[servers]下面的三种用户对应 上文提到的各个实例上的三个用户。 [protocol.xmlrpc]
按照默认的填写即可,需要给出一个管理用户名和密码。
fabric.cfg 配置文件路径如下:
[DEFAULT]
prefix = C:\MySQL Utilities
sysconfdir = C:\MySQL Utilities\etc\mysql
logdir = d:\test\Fabric
[storage]
address = localhost:3306
user = fabric
password = fabric
database = fabric
auth_plugin = mysql_native_password
connection_timeout = 6
connection_attempts = 6
connection_delay = 1
[servers]
user = fabric
password = fabric
backup_user = fabric
backup_password = fabric
restore_user = fabric
restore_password = fabric
unreachable_timeout = 5
[protocol.xmlrpc]
address = localhost:32274
threads = 5
user = admin
password =
disable_authentication = no
realm = MySQL Fabric
ssl_ca =
ssl_cert =
ssl_key =
[protocol.mysql]
address = localhost:32275
user = admin
password =
disable_authentication = no
ssl_ca =
ssl_cert =
ssl_key =
[executor]
executors = 5
[logging]
level = INFO
url = C:\MySQL Utilities\etc\mysql\fabric.log
[sharding]
mysqldump_program = d:\test\Fabric\bin\mysqldump
mysqlclient_program = d:\test\Fabric\bin\mysql
prune_limit = 10000
[statistics]
prune_time = 3600
[failure_tracking]
notifications = 300
notification_clients = 50
notification_interval = 60
failover_interval = 0
detections = 3
detection_interval = 6
detection_timeout = 1
prune_time = 3600
[connector]
ttl = 1
7). Fabric管理节点,初始化
- mysqlfabric manage setup --param=storage.user=fabric --param=storage.password=fabric
- 初始化成功,生成下列表
-
2. MySQL 各节点配置
- 节点 MySQL3311 my.ini 配置
所有的MySQL实例均需要配置文件my.ini,这个文件的位置可以由用户自行决定,只需在启动MySQL服务的时候指定该文件位置即可。本例使用d:\test\Mysql3311
配置文件路径: d:\test\Mysql3311\my.ini
my.ini 配置文件内容如下:
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It‘s a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
basedir= d:\\test\\Fabric
datadir= d:\\test\\MySQL3311\\data
port=3311
#每个实例必须设置不一样的 编号
server_id =11
log-bin
gtid-mode=ON
enforce-gtid-consistency
log_slave_updates
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
注意:
由于fabric是基于GTID主从复制,所以这些实例中必须要启用GTID,它们的配置文件要有这些参数:
log-bin
gtid-mode=ON
enforce-gtid-consistency
log_slave_updates
- 节点 MySQL3311 实例配置:
数据初始化:mysqld --initialize --datadir=d:\test\mysql3311\data
实例安装:mysqld install --defaults-file=d:\test\mysql3311\my.ini
启动实例:net start MySQL3311
- 修改MySQL3311实例默认root 的密码
执行:mysql -h localhost -P 3311 -u root -p
输入密码:初始化密码
按顺序执行下列命令:
mysql>set password=password(‘root‘);
mysql>use mysql;
mysql>update user set host=‘%‘ where user=‘root‘; 客服端授权访问;
mysql>flush privileges; //刷新系统权限表
- 创建用户及授权
- mysql>CREATE USER ‘fabric‘@‘localhost‘ IDENTIFIED BY ‘fabric‘;
mysql>GRANT ALL ON *.* TO ‘fabric‘@‘localhost‘;
- 数据同步复制设置
mysql>grant replication slave on *.* to ‘fabric‘@‘localhost‘;
mysql>reset slave;
mysql>reset master;
其他实例按照上面的步骤设置就行。
3、高可用性功能测试
启动所有的MySQL实例:
- net start MySQL3311
- net start MySQL3312
- net start MySQL3313
- net start MySQL3314
- 建立高可用群
C:\MySQL Utilities\mysqlfabric group create my_group //创建群 my_group
C:\MySQL Utilities\mysqlfabric group add my_group localhost:3311 //添加到群 my_group
C:\MySQL Utilities\mysqlfabric group add my_group localhost:3312
C:\MySQL Utilities\mysqlfabric group add my_group localhost:3313
C:\MySQL Utilities\mysqlfabric group add my_group localhost:3314
- 自动挑选一台服务器作为主服务器
C:\MySQL Utilities\mysqlfabric group promote my_group
- Fabric在主服务器发生故障时,自动进行切换
C:\MySQL Utilities\mysqlfabric group activate my_group
- 来查看该群的服务器状态
C:\MySQL Utilities\mysqlfabric group lookup_servers my_group
至此,一个高可用的可以自动切换的MySQL主从服务器群就设置好了,另外需要注意的一点是,
当要对群里面的服务器进行停机维护的时候需要先将其从群主里面摘除,然后再进行维护,如果不这样Fabric将会认为该服务器已经坏掉,会自动切换。去除服务器可以执行下面的命令:
C:\MySQL Utilities\mysqlfabric group demote my_group
C:\MySQL Utilities\mysqlfabric group remove my_group 服务器GUID
四、.net 连接该高可用群集测试
1. 打开 vs2015
2. 创建测试项目控制台程序名称为:MyFabric
添加引用:MySql.Data; MySql.Fabric.Plugin,文件在 Mysql-connector-net-6.9.9 的安装路径下。
3. App.config 配置文件内容
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="MySQL" type="MySql.Data.MySqlClient.MySqlConfiguration, MySql.Data,
Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/>
</configSections>
<MySQL>
<Replication>
<ServerGroups>
<Group name="Fabric" groupType="MySql.Fabric.FabricServerGroup, MySql.Fabric.Plugin">
<Servers>
<Server name="fabric" connectionstring="server=localhost;port=32275;uid=admin;password=root;"/>
</Servers>
</Group>
</ServerGroups>
</Replication>
</MySQL>
</configuration>
- 4. Program.cs 文件内容如下
using MySql.Data.MySqlClient;
using MySql.Fabric;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyFabric
{
class Program
{
public const string connectionString = "server=fabric;uid=root;password=root;";
static void Main(string[] args)
{
RunFabricTest();
}
static string AddEmployee(MySqlConnection conn)
{
conn.SetFabricProperties(groupId: "my_group", mode: FabricServerModeEnum.Read_Write);
MySqlCommand cmd = new MySqlCommand("USE employees", conn);
cmd.ExecuteNonQuery();
string sql = null;
int i1 = 0;
DateTime d1 = DateTime.Now;
DateTime d2;
for (int i = 0; i <= 100; i++)
{
sql = sql + "INSERT INTO employees VALUES("+i+", "+i+", "+i+"); ";
if (i1 == 50)
{
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
sql = null;
i1 = 0;
Console.WriteLine((DateTime.Now - d1).TotalSeconds);
d1 = DateTime.Now;
}
i1++;
}
cmd.CommandText = "SELECT @@global.gtid_executed";
cmd.Parameters.Clear();
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine("Transactions executed on the master " + reader.GetValue(0));
}
return reader.GetString(0);
}
}
static void FindEmployee(MySqlConnection conn, int emp_no, string gtid_executed)
{
conn.SetFabricProperties(groupId: "my_group", mode: FabricServerModeEnum.Read_only);
MySqlCommand cmd = new MySqlCommand("", conn);
cmd.CommandText = "SELECT WAIT_UNTIL_SQL_THREAD_AFTER_GTIDS(@gtid_executed, 0)";
cmd.Parameters.Add("gtid_executed", gtid_executed);
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine("Had to synchronize " + reader.GetValue(0) + " transactions.");
}
}
cmd.CommandText = "USE employees";
cmd.Parameters.Clear();
cmd.ExecuteNonQuery();
cmd.CommandText = "SELECT first_name, last_name FROM employees ";
cmd.CommandText += " WHERE emp_no = @emp_no";
cmd.Parameters.Clear();
cmd.Parameters.Add("emp_no", emp_no);
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
object[] values = new object[reader.FieldCount];
reader.GetValues(values);
Console.WriteLine("Retrieved {0}", string.Join(",", values));
}
}
}
static void RunFabricTest()
{
using (MySqlConnection conn = new MySqlConnection(connectionString))
{
string gtid_executed;
conn.SetFabricProperties(groupId: "my_group", mode: FabricServerModeEnum.Read_Write);
conn.Open();
MySqlCommand cmd = new MySqlCommand("", conn);
cmd.CommandText = "CREATE DATABASE IF NOT EXISTS employees;";
cmd.ExecuteNonQuery();
cmd.CommandText = "USE employees;";
cmd.ExecuteNonQuery();
cmd.CommandText = "DROP TABLE IF EXISTS employees;";
cmd.ExecuteNonQuery();
cmd.CommandText = "CREATE TABLE employees(";
cmd.CommandText += " emp_no INT, ";
cmd.CommandText += " first_name CHAR(40), ";
cmd.CommandText += " last_name CHAR(40),";
cmd.CommandText += " PRIMARY KEY(`emp_no`),";
cmd.CommandText += " UNIQUE KEY `id` (`emp_no`)";
cmd.CommandText += ") ENGINE = InnoDB DEFAULT CHARSET = latin1;";
cmd.ExecuteNonQuery();
gtid_executed = AddEmployee(conn);
FindEmployee(conn, 12, gtid_executed);
}
}
}
}
完成。
Widnwos平台 Mysql5.7 + MyFabric1.5 数据库、群集HA 高可用性,安装部署