• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
    問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
    當前位置: 首頁 - 科技 - 知識百科 - 正文

    SimpleMySQLMasterHAwithmysqlnd_ms_MySQL

    來源:懂視網 責編:小采 時間:2020-11-09 19:34:00
    文檔

    SimpleMySQLMasterHAwithmysqlnd_ms_MySQL

    SimpleMySQLMasterHAwithmysqlnd_ms_MySQL:I had the pleasure of presenting to the PHP Users Group Philippines a few days ago aboutmysqlnd_ms. The mysqlnd plugin, MySQL Master Slave, is a transparent layer on top of mysqlnd extension. This allows you to do read-write splitting and s
    推薦度:
    導讀SimpleMySQLMasterHAwithmysqlnd_ms_MySQL:I had the pleasure of presenting to the PHP Users Group Philippines a few days ago aboutmysqlnd_ms. The mysqlnd plugin, MySQL Master Slave, is a transparent layer on top of mysqlnd extension. This allows you to do read-write splitting and s

    I had the pleasure of presenting to the PHP Users Group Philippines a few days ago aboutmysqlnd_ms. The mysqlnd plugin, MySQL Master Slave, is a transparent layer on top of mysqlnd extension. This allows you to do read-write splitting and slave reads load balancing without needing to change anything from your application. But do you know you can also achieve a form of high availability with this plugin? I shared 2 forms on my presentation, using async MySQL replication either in master-slave configuration or master-master configuration, while the second form is having an all primary cluster where you can write to all nodes.

    This first part is to demonstrate how you can achieve a simple HA solution using the first form. First, all the sample code here can be found on myGitHub repository. So, to use the mysqlnd_ms plugin, it uses an additional external configuration file in JSON format. This configuration file, will define your master and slave nodes, failover properties and any filters (connection selection method) you want to dictate how the algorithm will provide you the connection.

    Let’s start with the mysqlnd_ms configuration I used,mysqlnd_ms_ms.ini :

    JavaScript
    {"primary": {"master": {"master_1": {"host": "127.0.0.1","port": "33001"}},"slave": {}},"standby": {"master": {"master_1": {"host": "127.0.0.1","port": "33002"}},"slave": {}}}

    {

    "primary":{

    "master":{

    "master_1":{

    "host":"127.0.0.1",

    "port":"33001"

    }

    },

    "slave":{

    }

    },

    "standby":{

    "master":{

    "master_1":{

    "host":"127.0.0.1",

    "port":"33002"

    }

    },

    "slave":{

    }

    }

    }

    Here, I have two applications defined, one called “primary” and another called “standby”, I have not defined any slaves for simplicity. The two MySQL instances running on port 33001 and 33002 are in master-master configuration.

    mysqlnd_ms.enable = 1mysqlnd_ms.disable_rw_split = 1mysqlnd_ms.force_config_usage = 1mysqlnd_ms.config_file = /home/revin/git/demo-me/phpugph201407/mysqlnd_ms_ms.ini

    mysqlnd_ms.enable=1

    mysqlnd_ms.disable_rw_split=1

    mysqlnd_ms.force_config_usage=1

    mysqlnd_ms.config_file=/home/revin/git/demo-me/phpugph201407/mysqlnd_ms_ms.ini

    This is the custom INI file I used for the tests,master-slave.ini . The first line simply enables the plugin for use. The second line, mysqlnd_ms.disable_rw_split instructs the plugin that I should only send all queries to the master because I only have masters for this test.

    As for the PHP script, the full copy can be foundhere, as it is a bit lengthy I will just explain the logic on what it does.

    1. To start the test, it bootstraps the test table via DROP and then CREATE queries.
    2. It then enters a for loop where it will execute an INSERT followed by a SELECT to validate the newly inserted row and additional information like the current active server id and the connection id.
    3. For every iteration of the loop, a new mysqli object is created to simulate non-persistent connections to the database server.
    4. To create the new connection, a call to the function connect_mysql is made which returns a mysqli object when successful. An important thing to remember here is that mysqlnd_ms uses lazy connections by default, this means that when the mysqli object is created, it is not really connected yet to the server. One has to issue a statement like 'SELECT 1' to start the connection manually or callmysqli::real_connect . Not even mysqli::ping does not work without the former, I’ve opened thisbug.
    5. After the mysqli object is returned, the INSERT statement will trigger mysqlnd_ms to actually establish the connection and then execute the statement. This is where the good part is, if the connection cannot be made, the query_write_mysql function will know and will re-request the connection from connect_mysql, this time within the connect_mysql function, connection to the primary will be retried at least 10 times if the type of error from the previous failure is something related to a connection like error numbers 2002 and2003 . If the connection cannot be established after 10 retries, the application creates a sentinel file as /tmp/PRIMARY_HAS_FAILED and will retry the connection to the secondary (slave or passive-master).

    Here is an example run, my primary has a server id or 101 while my standby is 102:

    [revin@forge phpugph201407]$ php -c master-slave.ini master-slave-ng.phpLast value 0001 from server id 101 thread id 7Last value 0003 from server id 101 thread id 837: [2002] Connection refusedConnection to host 'primary' failed: [0] Connection refused, retrying (1 of 10) in 3 secondsConnection to host 'primary' failed: [0] Connection refused, retrying (2 of 10) in 3 secondsConnection to host 'primary' failed: [0] Connection refused, retrying (3 of 10) in 3 secondsConnection to host 'primary' failed: [0] Connection refused, retrying (4 of 10) in 3 secondsConnection to host 'primary' failed: [0] Connection refused, retrying (5 of 10) in 3 secondsConnection to host 'primary' failed: [0] Connection refused, retrying (6 of 10) in 3 secondsConnection to host 'primary' failed: [0] Connection refused, retrying (7 of 10) in 3 secondsConnection to host 'primary' failed: [0] Connection refused, retrying (8 of 10) in 3 secondsConnection to host 'primary' failed: [0] Connection refused, retrying (9 of 10) in 3 secondsConnection to host 'primary' failed: [0] Connection refused, retrying (10 of 10) in 3 secondsThe primary host 'primary' has failed after 30 seconds, failing over to standby!52: [2002] Connection refusedLast value 0004 from server id 102 thread id 635Last value 0006 from server id 102 thread id 636Last value 0008 from server id 102 thread id 637[...]

    [revin@forgephpugph201407]$php-cmaster-slave.inimaster-slave-ng.php

    Lastvalue0001fromserverid101threadid7

    Lastvalue0003fromserverid101threadid8

    37:[2002]Connectionrefused

    Connectiontohost'primary'failed:[0]Connectionrefused,retrying(1of10)in3seconds

    Connectiontohost'primary'failed:[0]Connectionrefused,retrying(2of10)in3seconds

    Connectiontohost'primary'failed:[0]Connectionrefused,retrying(3of10)in3seconds

    Connectiontohost'primary'failed:[0]Connectionrefused,retrying(4of10)in3seconds

    Connectiontohost'primary'failed:[0]Connectionrefused,retrying(5of10)in3seconds

    Connectiontohost'primary'failed:[0]Connectionrefused,retrying(6of10)in3seconds

    Connectiontohost'primary'failed:[0]Connectionrefused,retrying(7of10)in3seconds

    Connectiontohost'primary'failed:[0]Connectionrefused,retrying(8of10)in3seconds

    Connectiontohost'primary'failed:[0]Connectionrefused,retrying(9of10)in3seconds

    Connectiontohost'primary'failed:[0]Connectionrefused,retrying(10of10)in3seconds

    Theprimaryhost'primary'hasfailedafter30seconds,failingovertostandby!

    52:[2002]Connectionrefused

    Lastvalue0004fromserverid102threadid635

    Lastvalue0006fromserverid102threadid636

    Lastvalue0008fromserverid102threadid637

    [...]

    This is not the perfect setup and there are a number of limitations, however it tells us that if you have a simple HA requirement like if you’re not running a very critical application but still do not want to be waken up at night but rather deal with issues in the morning, this might just fit. So here are some more notes:

  • If you have a master-slave configuration, you just basically shot your primary (master) in the foot during the failover. You may need to rebuild its data in the morning.
  • If instead you have master-master, you might just be able to bring the primary master back online, get it caught up in replication and then delete /tmp/PRIMARY_HAS_FAILED file to switch your application back to it.
  • The use of /tmp/PRIMARY_HAS_FAILED sentinel file is rudimentary, its not the only way. You should consider sending notifications to yourself when failover happens because this method requires human intervention to put back the primary master back in rotation.
  • The same effect can be achieved with a little more coding, but you can already take advantage of the plugin with less.

    I’ve also tested the plugin on the second form where you can write to multiple masters using Percona XtraDB Cluster. I’ve found a few interesting issues there so stay tuned.

    聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文檔

    SimpleMySQLMasterHAwithmysqlnd_ms_MySQL

    SimpleMySQLMasterHAwithmysqlnd_ms_MySQL:I had the pleasure of presenting to the PHP Users Group Philippines a few days ago aboutmysqlnd_ms. The mysqlnd plugin, MySQL Master Slave, is a transparent layer on top of mysqlnd extension. This allows you to do read-write splitting and s
    推薦度:
    標簽: mysql master with
    • 熱門焦點

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top
    主站蜘蛛池模板: 国产午夜精品一区二区三区| 久久精品中文騷妇女内射| 亚洲精品动漫免费二区| 亚洲国产精品自在线一区二区| 午夜精品久久久久久毛片| 国产精品最新国产精品第十页| 亚洲国产精品自产在线播放| 精品久久久久久久无码| 合区精品久久久中文字幕一区| 无码精品人妻一区二区三区漫画 | jizz国产精品网站| 国产精品成人小电影在线观看| 久久久久久亚洲Av无码精品专口| 国产成人精品日本亚洲专一区 | 国产午夜精品理论片久久影视 | 99精品全国免费观看视频..| 中文精品无码中文字幕无码专区| 国产精品视频久久久| 精品久久久久久中文字幕人妻最新| 国产成人精品综合久久久| 99久久久精品免费观看国产| 日韩精品真人荷官无码| 国产福利精品视频自拍 | 麻豆成人久久精品二区三区免费| 2022精品天堂在线视频| 青草国产精品久久久久久| 午夜精品久久影院蜜桃| 热RE99久久精品国产66热| 国产精品久久久福利| 亚洲无码精品浪潮| 综合在线视频精品专区| 亚洲精品国产日韩无码AV永久免费网| 国产叼嘿久久精品久久| 国产午夜精品无码| 精品一区二区三区东京热| 日韩精品无码一本二本三本| 亚洲处破女AV日韩精品| 亚洲AV成人无码久久精品老人| 成人一区二区三区精品| 国产精品热久久毛片| 国产精品素人搭讪在线播放|