安裝軟件
vs_community
mysql-utilities-1.5.6-winx64.msi
python-3.4.0.amd64.msi
mysql-connector-python-2.1.4-py3.4-winx64.msi
mysql-installer-community-5.7.14.0.msihtml
Backing Store用於存儲整個HA集羣的服務器等相關配置,它須要一個MySQL實例來存儲這些信息,這個實例的版本須要跟其它在HA中的MySQL實例版本保持一致,並且必須是5.6.10及更高的版本。
localhost 3310 MySQL Fabric
D:\MySQL\MysqlFabric\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.java
[mysqld]python
# 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 = 128Mmysql
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_binsql
# These are commonly set, remove the # and set as required.
basedir = D:\MySQL\MysqlFabric
datadir = D:\MySQL\MysqlFabric\data
port = 3310
server_id = 10數據庫
# 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
--------------------------------------------------------------------------服務器
mysqld --initialize --datadir=D:\MySQL\MysqlFabric\data
D:\MySQL\MysqlFabric\data目錄下擴展名爲 .err的文件,查看初始化密碼 A temporary password is generated for root@
mysqld install mysqllFabric --defaults-file=D:\MySQL\MysqlFabric\my.ini
net start mysqllFabric
mysql -h localhost -P 3310 -u root -p
mysql>SET PASSWORD FOR 'root'@'localhost' = PASSWORD('root');
mysql>use mysql;
mysql>update user set host='%' where user='root'; 客服端受權訪問;
mysql>flush privileges; //刷新系統權限表ui
D:\MySQL\Utilities\etc\mysql\fabric.cfg
--------------------------------------------------------------------------
[DEFAULT]
prefix = D:\MySQL\Utilities
sysconfdir = D:\MySQL\Utilities\etc\mysql
logdir = D:\MySQL\Utilities\etc\mysqlthis
[storage]
address = localhost:3310
user = root
password = root
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 = file:///D:/MySQL/Utilities/etc/mysql/log/fabric.log
[sharding]
mysqldump_program = D:\MySQL\MysqlFabric\bin\mysqldump
mysqlclient_program = D:\MySQL\MysqlFabric\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
--------------------------------------------------------------------------
mysqlfabric manage setup --param=storage.user=root --param=storage.password=root
user/pwd: admin/admin
因爲fabric是基於GTID主從複製,因此這些實例中必需要啓用GTID
D:\MySQL\Mysql3311\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
gtid-mode=ON
enforce-gtid-consistency
log_slave_updates
# These are commonly set, remove the # and set as required.
basedir = D:\MySQL\Mysql3311
datadir = D:\MySQL\Mysql3311\data
port = 3311
server_id = 11
# 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
--------------------------------------------------------------------------
mysqld --initialize --datadir=D:\MySQL\Mysql3311\data
D:\MySQL\Mysql3311\data目錄下擴展名爲 .err的文件,查看初始化密碼 PSV9f6g3js%b
mysqld install mysql3311 --defaults-file=D:\MySQL\Mysql3311\my.ini
net start mysql3311
mysql -h localhost -P 3311 -u root -p
mysql>SET PASSWORD FOR 'root'@'localhost' = 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;
mysqld --initialize --datadir=D:\MySQL\Mysql3312\data
D:\MySQL\Mysql3312\data目錄下擴展名爲 .err的文件,查看初始化密碼 V-uq#tht+9s#
mysqld install mysql3312 --defaults-file=D:\MySQL\Mysql3312\my.ini
net start mysql3312
mysql -h localhost -P 3312 -u root -p
mysql>SET PASSWORD FOR 'root'@'localhost' = 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;
mysqld --initialize --datadir=D:\MySQL\Mysql3313\data
D:\MySQL\Mysql3313\data目錄下擴展名爲 .err的文件,查看初始化密碼 WlOG.qoV+3hD
mysqld install mysql3313 --defaults-file=D:\MySQL\Mysql3313\my.ini
net start mysql3313
mysql -h localhost -P 3313 -u root -p
mysql>SET PASSWORD FOR 'root'@'localhost' = 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;
mysqld --initialize --datadir=D:\MySQL\Mysql3314\data
D:\MySQL\Mysql3314\data目錄下擴展名爲 .err的文件,查看初始化密碼 =htkkogr?77I
mysqld install mysql3314 --defaults-file=D:\MySQL\Mysql3314\my.ini
net start mysql3314
mysql -h localhost -P 3314 -u root -p
mysql>SET PASSWORD FOR 'root'@'localhost' = 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;
--------------------------------------------------------------------------
mysqlfabric manage start
mysqlfabric group create my_group
mysqlfabric group add my_group localhost:3311
mysqlfabric group add my_group localhost:3312
mysqlfabric group add my_group localhost:3313
mysqlfabric group add my_group localhost:3314
查看一下my_group裏面的成員信息
mysqlfabric group lookup_servers my_group
都不是PRIMARY,說明剛剛搭建完的環境,系統是不會選舉出PRIMARY的。
自動挑選一臺服務器做爲主服務器
mysqlfabric group promote my_group
或手工指定
mysqlfabric group promote my_group --slave_id=localhost:3312 -- 指定localhost:3312提高爲master
Fabric在主服務器發生故障時,自動進行切換
mysqlfabric group activate my_group
Fabric也提供了phtyon和Java的API,能夠供軟件開發人員直接使用,之後的軟件開發人員,不是再直接鏈接到MySQL實例,而是鏈接到Fabric,由Fabric來統一分發請求,這有些象MySQL Proxy,但它的應用前景要比MySQL Proxy更寬更廣。
查看健康狀況
mysqlfabric group health my_group
當要對羣裏面的服務器進行停機維護的時候須要先將其從羣主裏面摘除,而後再進行維護,若是不這樣Fabric將會認爲該服務器已經壞掉,會自動切換。去除服務器能夠執行下面的命令:
mysqlfabric group demote my_group
mysqlfabric group remove my_group 服務器GUID
此時雖然原master已經起來 可是fabric並無把他自動恢復到主從裏面,原master狀態依舊爲FAULTY
一個標記位"faulty"狀態的服務器不能提高爲secondary或primary,須要先轉換成"spare"狀態。使用命令mysqlfabric server set_status <server-address> spare
支持的狀態值爲primary,secondary,spare,faulty。
改變到spare,避免寫和讀訪問,同時保證當故障切換時不會被選取。
表
checkpoints #存儲程序執行信息,在crash後,能安全的恢復執行程序
error_log #服務器錯誤報告信息
group_replication #定義複製,global groups和分片組,主要用於shard splitting,moving和global updates
group_view
groups #包含管理組信息
log |
permissions #包含權限信息,訪問到不一樣fabric子系統,當前僅僅定義了core子系統
proc_view |
role_permissions #表關聯的角色和權限
roles #包含用戶角色的信息
servers #包含fabric管理的全部服務器信息
shard_maps #包含名字和分片屬性的映射
shard_ranges #分片索引和使用映射分片key到分片
shard_tables #全部分片的表
shards #存儲每一個分片標識
user_roles
users #標識用戶有什麼權限訪問到不一樣子系統的功能
總結:
1. 程序必須經過Fabric-aware connectors來實現路由,目前只支持PHP,Python 和Java;
2. mysql實例進行恢復後,fabric不會自動改變實例的狀態,須要手工修改;
3. fabric state store和fabric note當前是單點,mysqlfabric進程自己不提供容錯,出現故障時須要重啓,當mysql fabric不可用時,fabric-aware鏈接器可以經過本地的cache繼續路由操做(配置ttl值);
4. 基於GTID的複製自己存在一些限制。
mysql-connector-java-5.1.40-bin.jar
jdbcUrl = jdbc:mysql:fabric://localhost:32274/jfinal_demo?fabricServerGroup=my_group&fabricUsername=admin&fabricPassword=admin
user = root
password = root
package com.demo.index;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.fabric.jdbc.FabricMySQLConnection;
import com.mysql.fabric.jdbc.FabricMySQLDataSource;
public class Fabric {
private static String USERNAME = "root";
private static String PWD = "root";
private static int MAX = 2000;
private static String SQL = "select * from blog where id=?";
private static String URL = "jdbc:mysql:fabric://localhost:32274/jfinal_demo?fabricServerGroup=my_group&fabricUsername=admin&fabricPassword=admin";
public static void main(String[] args) throws ClassNotFoundException, SQLException, UnsupportedEncodingException {
long start = System.currentTimeMillis();
testSelect();
}
private static Connection getConnection() throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.fabric.jdbc.FabricMySQLDriver");
FabricMySQLDataSource ds = new FabricMySQLDataSource();
ds.setServerName("localhost");
ds.setPort(Integer.valueOf("32274"));
ds.setDatabaseName("jfinal_demo");
ds.setFabricUsername("admin");
ds.setFabricPassword("admin");
ds.setDatabaseName("jfinal_demo"); // connect to the `mysql` database before creating our `employees` database
ds.setFabricServerGroup("my_group"); // connect to the global group
Connection con = ds.getConnection(USERNAME,PWD);
con.setReadOnly(true);//讀取備庫
return con;
}
private static void testSelect() throws ClassNotFoundException, SQLException {
Connection con = getConnection();
FabricMySQLConnection fcon=(FabricMySQLConnection) con;
PreparedStatement pt = fcon.prepareStatement(SQL);
pt.setInt(1, 9);
ResultSet rs = pt.executeQuery();
while(rs.next()){
String title = rs.getString("title");
System.out.println("the title is " + title);
}
rs.close();
pt.close();
con.close();
}
}
manage ping Check whether Fabric server is running or not.
D:\MySQL\Utilities>mysqlfabric manage ping
Fabric UUID: 5ca1ab1e-a007-feed-f00d-cab3fe13249e
Time-To-Live: 10000
若是直接鏈接從庫並修改數據,會形成PRIMARY主庫的修改沒法同步到從數據,須要把從數據庫衝突數據刪除,重啓衝突的從數據庫,數據會從新同步。
Fabric進程和storage單點故障
一、創建雙Fabric storage數據庫,經過otter同步,數據庫沒法鏈接時,變動fabric.cfg中[storage]的值,快速重啓Fabric進程mysqlfabric manage stop/start
二、創建雙Fabric進程,進程沒法訪問時啓動備用服務 mysqlfabric manage ping
D:\MySQL\Utilities>mysqlfabric manage ping
<urlopen error [Errno 10061] >
D:\MySQL\Utilities>mysqlfabric manage ping Fabric UUID: 5ca1ab1e-a007-feed-f00d-cab3fe13249e Time-To-Live: 10000 三、故障判斷代碼 com.mysql.fabric.jdbc.FabricMySQLConnectionProxy try { String url = this.fabricProtocol + "://" + this.host + ":" + this.port; this.fabricConnection = new FabricConnection(url, this.fabricUsername, this.fabricPassword); } catch (FabricCommunicationException ex) { throw SQLError.createSQLException("Unable to establish connection to the Fabric server", SQLError.SQL_STATE_CONNECTION_REJECTED, ex, getExceptionInterceptor(), this); } com.mysql.fabric.jdbc.FabricMySQLDriver if (com.mysql.jdbc.Util.isJdbc4()) { try { Constructor<?> jdbc4proxy = Class.forName("com.mysql.fabric.jdbc.JDBC4FabricMySQLConnectionProxy") .getConstructor(new Class[] { Properties.class }); return (Connection) com.mysql.jdbc.Util.handleNewInstance(jdbc4proxy, new Object[] { parsedProps }, null); } catch (Exception e) { throw (SQLException) new SQLException(e.getMessage()).initCause(e); } }