1. 程式人生 > >測試MySQL Fabric用於replication的實驗

測試MySQL Fabric用於replication的實驗

        今天測試了MySQL Fabric用於replication的實驗,感覺MySQL的官方文件說的不是很清楚,特別是Fabric的配置檔案解釋的不清楚。習慣了Oracle的官方文件的條理清晰。測試過程如下:


== centos 6.3_x86 ==

node1  192.168.201.11    backing store (mysql fabric + mysql-server-5.6.19 + mysql-connector-python-1.2.2 )
node2  192.168.201.12     primary(mysql-server-5.6.19)
node4  192.168.201.14    secondary(mysql-server-5.6.19)
node5  192.168.201.15    secondary(mysql-server-5.6.19)

== node1 ==
[
[email protected]
~]# rpm -ivh http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm [[email protected] ~]# yum install mysql-community-server.i686 mysql-community-libs.i686 mysql-community-client.i686 mysql-utilities.noarch [[email protected] ~]# vim /etc/my.cnf [[email protected]
~]# mysql_install_db --defaults-file=/etc/my.cnf --user=mysql [[email protected] ~]# service mysqld start [[email protected] ~]# mysql_secure_installation [[email protected] ~]# mysql -u root -p -h 127.0.0.1 -P 63306 mysql> create user [email protected]'localhost' identified by 'fabric'; mysql> grant all on fabric.* to
[email protected]
'localhost'; mysql> flush privileges; == node2 == [[email protected] ~]# rpm -ivh http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm [[email protected] ~]# yum install mysql-community-server.i686 mysql-community-libs.i686 mysql-community-client.i686 [[email protected] ~]# vim /etc/my.cnf # For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html [mysqld] port=63307 character-set-server=utf8 # 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=on log_slave_updates=1 # 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 datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock server-id=20112 # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Recommended in standard MySQL setup sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid [[email protected] ~]# mysql_install_db --defaults-file=/etc/my.cnf --user=mysql [[email protected] ~]# service mysqld start [[email protected] ~]# mysql_secure_installation mysql> create user [email protected]'192.168.201.%' identified by 'fabric'; mysql> grant all on *.* to [email protected]'192.168.201.%'; mysql> flush privileges; [[email protected] ~]# mysqldump -u root -p -h 127.0.0.1 -P63307 < a.sql == node4 == [[email protected] ~]# rpm -ivh http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm [[email protected] ~]# yum install mysql-community-server.i686 mysql-community-libs.i686 mysql-community-client.i686 [[email protected] ~]# vim /etc/my.cnf # For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html [mysqld] port=63308 character-set-server=utf8 # 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. <span style="color:#FF0000;">log_bin gtid_mode=on enforce_gtid_consistency=on log_slave_updates=1</span> # 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 datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock server-id=20114 # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Recommended in standard MySQL setup sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid [[email protected] ~]# mysql_install_db --defaults-file=/etc/my.cnf --user=mysql [[email protected] ~]# service mysqld start [[email protected] ~]# mysql_secure_installation [[email protected] ~]# mysql -u root -p -h 127.0.0.1 -P63308 < a.sql [[email protected] ~]# mysql -u root -p -h 127.0.0.1 -P63308 -e "flush privileges" == node5 == [[email protected] ~]# rpm -ivh http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm [[email protected] ~]# yum install mysql-community-server.i686 mysql-community-libs.i686 mysql-community-client.i686 [[email protected] ~]# vim /etc/my.cnf # For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html [mysqld] port=63309 character-set-server=utf8 # 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. <span style="color:#FF0000;">log_bin gtid_mode=on enforce_gtid_consistency=on log_slave_updates=1</span> # 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 datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock server-id=20115 # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Recommended in standard MySQL setup sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid [[email protected] ~]# mysql_install_db --defaults-file=/etc/my.cnf --user=mysql [[email protected] ~]# service mysqld start [[email protected] ~]# mysql_secure_installation [[email protected] ~]# mysql -u root -p -h 127.0.0.1 -P63309 < a.sql [[email protected] ~]# mysql -u root -p -h 127.0.0.1 -P63309 -e "flush privileges" == node1 == [[email protected] ~]# vim /etc/mysql/fabric.cfg [DEFAULT] prefix = sysconfdir = /etc logdir = /var/log [statistics] prune_time = 3600 [logging] url = file:///var/log/fabric.log level = INFO [storage] auth_plugin = mysql_native_password <span style="color:#FF0000;">database = fabric user = fabric address = localhost:63306</span> connection_delay = 1 connection_timeout = 6 password = fabric connection_attempts = 6 [failure_tracking] notification_interval = 60 notification_clients = 50 detection_timeout = 1 detection_interval = 6 notifications = 300 detections = 3 <span style="color:#FF0000;">failover_interval = 0</span> prune_time = 3600 [servers] password = fabric user = fabric [connector] ttl = 1 [client] password = [protocol.xmlrpc] disable_authentication = no ssl_cert = realm = MySQL Fabric ssl_key = ssl_ca = threads = 5 <span style="color:#FF0000;">user = admin</span> <span style="color:#FF0000;">address = localhost:32274 password = admin</span> [executor] executors = 5 [sharding] mysqldump_program = /usr/bin/mysqldump mysqlclient_program = /usr/bin/mysql [[email protected] ~]# mysqlfabric help commands --------> 檢視所有命令幫助 [[email protected] ~]# mysqlfabric manage setup ------------>使用[email protected]'localhost'使用者連線node1上63306埠的mysql例項, 該例項儲存組及節點資訊 [[email protected] ~]# mysqlfabric manage start --daemonize --------> 後臺執行 [[email protected] ~]# mysqlfabric group create mycluster ------------>建立一個HA組, 組名mycluster [[email protected] ~]# mysqlfabric group add mycluster 192.168.201.12:63307 ------------>新增節點到組 [[email protected] ~]# mysqlfabric group add mycluster 192.168.201.14:63308 ------------>新增節點到組 [[email protected] ~]# mysqlfabric group add mycluster 192.168.201.15:63309 ------------>新增節點到組 [[email protected] ~]# mysqlfabric group promote mycluster ----------->自動提升一個節點為primary [[email protected] ~]# mysqlfabric group activate mycluster ---------------->啟用監控, 自動failover(必須failover_interval=1) [[email protected] ~]# mysqlfabric group health mycluster ----------->檢視組健康狀態 [[email protected] ~]# mysqlfabric group lookup_servers mycluster --------------->檢視組成員資訊 == node1 測試 node1做Fabric aware connector == [[email protected] ~]# cat test_fabric_query.py import mysql.connector from mysql.connector import fabric def add_employee(conn, emp_no, first_name, last_name): conn.set_property(group="<span style="color:#FF0000;">mycluster</span>", 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 need to keep track of what we have executed in order to, # at least, read our own updates from a slave. cur.execute("SELECT @@global.gtid_executed") for row in cur: print "Transactions executed on the master", row[0] return row[0] def find_employee(conn, emp_no, gtid_executed): conn.set_property(group="<span style="color:#FF0000;">mycluster</span>", mode=fabric.MODE_READONLY) cur = conn.cursor() # Guarantee that a slave has applied our own updates before # reading anything. cur.execute( "SELECT WAIT_UNTIL_SQL_THREAD_AFTER_GTIDS('%s', 0)" % (gtid_executed, ) ) for row in 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 row in cur: print "Retrieved", row # Address of the Fabric, not the host we are going to connect to. <span style="color:#FF0000;">conn = mysql.connector.connect( fabric={"host" : "localhost", "port" : 32274, "username": "admin", "password" : "admin" }, user="fabric", password="fabric", autocommit=True )</span> conn.set_property(group="<span style="color:#FF0000;">mycluster</span>", mode=fabric.MODE_READWRITE) cur = conn.cursor() cur.execute("CREATE DATABASE IF NOT EXISTS employees") cur.execute("USE employees") cur.execute("DROP TABLE IF EXISTS employees") cur.execute( "CREATE TABLE employees (" " emp_no INT, " " first_name CHAR(40), " " last_name CHAR(40)" ")" ) gtid_executed = add_employee(conn, 12, "John", "Doe") find_employee(conn, 12, gtid_executed) [[email protected] ~]# python ./test_fabric_query.py <span style="color:#000099;">Transactions executed on the master 2c1d171d-f962-11e3-9d0c-000c29ccf58f:1-5, 557be7db-63f1-11e3-8e92-000c29add0bd:1-11 Had to synchronize (2,) transactions. Retrieved (u'John', u'Doe')</span>