做者:任仲禹
角色分爲 Primary 和 Secondaryhtml
# mysqlsh MySQL JS > dba.deploySandboxInstance(3306) MySQL JS > dba.deploySandboxInstance(3307) MySQL JS > dba.deploySandboxInstance(3308)
MySQL JS > dba.configureReplicaSetInstance('root@localhost:3306', {clusterAdmin: "'repl'@'%'", clusterAdminPassword: 'repl'});
MySQL JS > \connect root@localhost:3306 MySQL localhost:3306 ssl JS > var rs = dba.createReplicaSet("renzy")
MySQL localhost:3306 ssl JS > rs.status() { "replicaSet": { "name": "renzy", "primary": "127.0.0.1:3306", "status": "AVAILABLE", "statusText": "All instances available.", "topology": { "127.0.0.1:3306": { "address": "127.0.0.1:3306", "instanceRole": "PRIMARY", "mode": "R/W", "status": "ONLINE" } }, "type": "ASYNC" } }
MySQL localhost:3306 ssl JS > rs.addInstance('localhost:3307') MySQL localhost:3306 ssl JS > rs.addInstance('localhost:3308')
MySQL localhost:3306 ssl JS > rs.status() { "replicaSet": { "name": "renzy", "primary": "127.0.0.1:3306", "status": "AVAILABLE", "statusText": "All instances available.", "topology": { "127.0.0.1:3306": { "address": "127.0.0.1:3306", "instanceRole": "PRIMARY", "mode": "R/W", "status": "ONLINE" }, "127.0.0.1:3307": { "address": "127.0.0.1:3307", "instanceRole": "SECONDARY", "mode": "R/O", "replication": { "applierStatus": "APPLIED_ALL", "applierThreadState": "Slave has read all relay log; waiting for more updates", "receiverStatus": "ON", "receiverThreadState": "Waiting for master to send event", "replicationLag": null }, "status": "ONLINE" }, "127.0.0.1:3308": { "address": "127.0.0.1:3308", "instanceRole": "SECONDARY", "mode": "R/O", "replication": { "applierStatus": "APPLIED_ALL", "applierThreadState": "Slave has read all relay log; waiting for more updates", "receiverStatus": "ON", "receiverThreadState": "Waiting for master to send event", "replicationLag": null }, "status": "ONLINE" } }, "type": "ASYNC" } }
MySQL localhost:3306 ssl JS > rs.setPrimaryInstance('127.0.0.1:3308')
MySQL localhost:3306 ssl JS > rs.status() { "replicaSet": { "name": "renzy", "primary": "127.0.0.1:3308", "status": "AVAILABLE", "statusText": "All instances available.", "topology": { "127.0.0.1:3306": { "address": "127.0.0.1:3306", "instanceRole": "SECONDARY", "mode": "R/O", "replication": { "applierStatus": "APPLIED_ALL", "applierThreadState": "Slave has read all relay log; waiting for more updates", "receiverStatus": "ON", "receiverThreadState": "Waiting for master to send event", "replicationLag": null }, "status": "ONLINE" }, "127.0.0.1:3307": { "address": "127.0.0.1:3307", "instanceRole": "SECONDARY", "mode": "R/O", "replication": { "applierStatus": "APPLIED_ALL", "applierThreadState": "Slave has read all relay log; waiting for more updates", "receiverStatus": "ON", "receiverThreadState": "Waiting for master to send event", "replicationLag": null }, "status": "ONLINE" }, "127.0.0.1:3308": { "address": "127.0.0.1:3308", "instanceRole": "PRIMARY", "mode": "R/W", "status": "ONLINE" } }, "type": "ASYNC" } }
# ps -ef | grep 3308 root 18975 1 0 16:52 ? 00:01:23 /root/mysql-sandboxes/3308/bin/mysqld --defaults-file=/root/mysql-sandboxes/3308/my.cnf --user=root # kill -9 18975
MySQL localhost:3306 ssl JS > rs.forcePrimaryInstance("127.0.0.1:3306")
mysqlrouter --user=root --bootstrap root@localhost:3308
mysqlrouter -c /usr/local/mysql-router-8.0.19-linux-glibc2.12-x86_64/mysqlrouter.conf & // 經過 MySQL Router R/W 端口能夠自動識別並鏈接到 Primary # mysql -h10.186.63.158 -P6446 -uroot -proot -e 'select @@port;' mysql: [Warning] Using a password on the command line interface can be insecure. +--------+ | @@port | +--------+ | 3308 | +--------+ # mysql -h10.186.63.158 -P6446 -uroot -proot -e 'show slave hosts;' mysql: [Warning] Using a password on the command line interface can be insecure. +------------+-----------+------+------------+--------------------------------------+ | Server_id | Host | Port | Master_id | Slave_UUID | +------------+-----------+------+------------+--------------------------------------+ | 1587786506 | 127.0.0.1 | 3307 | 2457151498 | cb569215-3b29-11ea-80f4-02000aba3f9e | | 3616586416 | 127.0.0.1 | 3306 | 2457151498 | c23ee4be-3b29-11ea-815a-02000aba3f9e | +------------+-----------+------+------------+--------------------------------------+
總之,InnoDB 副本集雖存在諸多不足之處,但做爲 2020 年 Oracle 的開餐甜點其帶來的效果也讓衆多 MySQL DBA 眼前一亮,既然有了副本集,相信 Sharding 也是將來可期,你以爲呢?mysql