MySQL Fabric安装使用杂记(未整理)

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

安装软件
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.msi

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.

[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:\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; //刷新系统权限表


D:\MySQL\Utilities\etc\mysql\fabric.cfg
--------------------------------------------------------------------------
[DEFAULT]
prefix = D:\MySQL\Utilities
sysconfdir = D:\MySQL\Utilities\etc\mysql
logdir = D:\MySQL\Utilities\etc\mysql

[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 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单点故障
1、建立双Fabric storage数据库,通过otter同步,数据库无法连接时,变更fabric.cfg中[storage]的值,快速重启Fabric进程mysqlfabric manage stop/start
2、建立双Fabric进程,进程无法访问时启动备用服务 mysqlfabric manage ping
D:\MySQL\Utilities>mysqlfabric manage ping

D:\MySQL\Utilities>mysqlfabric manage ping
Fabric UUID:  5ca1ab1e-a007-feed-f00d-cab3fe13249e
Time-To-Live: 10000
3、故障判断代码
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);
            }
        }

转载于:https://my.oschina.net/aixiaohua/blog/796530

你可能感兴趣的:(MySQL Fabric安装使用杂记(未整理))