5)此外,包括Fabric管理节点在内,所有的MySQL实例均需要配置文件my.ini,这个文件的位置可以由用户自行决定,只需在启动MySQL服务的时候指定该文件位置即可。本例使用与data文件夹平行的路径,
例如: C:\DEMO\Fabric\HA1\MySQL11\my.ini
启动MySQL实例的命令为:
>mysqld–defaults-file= C:\DEMO\Fabric\HA1\MySQL11\my.ini
各个实例的配置文件请参照下例,注意更改一下端口号和服务器ID和文件路径即可:
(basedir使用的均为管理节点的MySQL,datadir是上面各个实例的数据文件位置)
# For advice on how to change settings please see
#http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template whichwill be copied to the
# *** default location during install, and will bereplaced if you
# *** upgrade to a newer version of MySQL.
[mysqld]
# Remove leading # and set to the amount of RAM forthe most important data
# cache in MySQL. Start at 70% of total RAM fordedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important dataintegrity option: logging
# changes to the binary log between backups.
log_bin
gtid_mode=ON
log-slave-updates
enforce-gtid-consistency
# These are commonly set, remove the # and set asrequired.
basedir =C:\DEMO\Fabric\MGM
datadir =C:\DEMO\Fabric\HA1\MySQL11\data
port = 3311
server_id = 11
# Remove leading # to set options mainly useful forreporting servers.
# The server defaults are faster for transactionsand fast SELECTs.
# Adjust>
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
2、 建立高可用群
C:\MySQLUtilities\mysqlfabric group create my_group
C:\MySQLUtilities\mysqlfabric group add my_grouplocalhost:3311
C:\MySQLUtilities\mysqlfabric group add my_grouplocalhost:3312
C:\MySQLUtilities\mysqlfabric group add my_grouplocalhost:3313
这四条命令的意思为建立一个名为my_group的群,然后向此群中加入3个服务器,localhost:3311即为服务器的地址。
建立好组群之后需要让Fabric来挑选一台服务器作为主服务器:
C:\MySQLUtilities\mysqlfabric group promotemy_group
执行成功之后,可以选择执行下面的命令来让Fabric在主服务器发生故障时,自动进行切换
C:\MySQLUtilities\mysqlfabric group activatemy_group
可以通过mysqlfabricgroup lookup_servers my_group 来查看该群的服务器状态。
至此,一个高可用的可以自动切换的MySQL主从服务器群就设置好了,大家可以使用下面的Pyhson代码来进行测试一下了。另外需要注意的一点是,当要对群里面的服务器进行停机维护的时候需要先将其从群主里面摘除,然后再进行维护,如果不这样Fabric将会认为该服务器已经坏掉,会自动切换。去除服务器可以执行下面的命令:
C:\MySQLUtilities\mysqlfabric group demote my_group
C:\MySQLUtilities\mysqlfabric group remove my_group98d34488-c956-11e4-a915-f82fa8e5f8eb
后面的一长串是UUID各位在添加服务器的时候就会看到,删除服务器的时候选定对应的UUID即可。
import mysql.connector
from mysql.connector import fabric
def add_employee(conn, emp_no, first_name,last_name):
conn.set_property(group="my_group",mode=fabric.MODE_READWRITE)
cur =conn.cursor()
cur.execute("USE employees")
cur.execute(
"INSERT INTO employees VALUES (%s, %s, %s)",
(emp_no, first_name, last_name)
)
# We needto keep track of what we have executed in order to,
# atleast, read our own updates from a slave.
cur.execute("SELECT @@global.gtid_executed")
for rowin cur:
print("Transactions executed on the master", row[0])
return row[0]
def find_employee(conn, emp_no, gtid_executed):
conn.set_property(group="my_group", mode=fabric.MODE_READONLY)
cur =conn.cursor()
#Guarantee that a slave has applied our own updates before
# readinganything.
cur.execute(
"SELECT WAIT_UNTIL_SQL_THREAD_AFTER_GTIDS('%s', 0)" %
(gtid_executed, )
)
for rowin cur:
print("Had to synchronize", row, "transactions.")
cur.execute("USE employees")
cur.execute(
"SELECT first_name, last_name FROM employees "
"WHERE emp_no = %s", (emp_no, )
)
for rowin cur:
print("Retrieved", row)
# Address of the Fabric, not the host we are goingto connect to.
conn = mysql.connector.connect(
fabric={"host" : "localhost", "port" :32274,
"username": "admin", "password" :"123654"
},
user="root",
# password="123654",
autocommit=True
)
conn.set_property(group="my_group",mode=fabric.MODE_READWRITE)
cur = conn.cursor()
cur.execute("CREATE DATABASE IF NOT EXISTSemployees")
cur.execute("USE employees")
cur.execute("DROP TABLE IF EXISTSemployees")
cur.execute(
"CREATE TABLE employees ("
" emp_no INT, "
" first_name CHAR(40),"
" last_name CHAR(40)"
")"
)