根据官方文档搭建MySQL Innodb Cluster

MySQL Innodb Cluster搭建

本次测试使用三台机器搭建mysql innodb cluster production mode,官方架构如下图所示:

机器列表:


1、创建数据库实例

首先在三台主机上创建3307数据库实例,数据库版本为mysql-8.0.13,配置文件如下:


[mysqld]

server-id = 1

port = 3307

socket = /data/mysql8/3306/mysql.sock

basedir = /data/mysql8/mysql8

datadir = /data/mysql8/3306/data

pid-file = /data/mysql8/3306/mysql.pid

log-error = /data/mysql8/3306/mysql-error.log

plugin-dir = /data/mysql8/mysql8/lib/plugin

#gernal setting

lower_case_table_names = 1

max_binlog_size = 1G

sync_binlog=1

innodb_flush_log_at_trx_commit = 1

#semi-sync

#rpl_semi_sync_master_enabled=1

#rpl_semi_sync_master_timeout=1000

#rpl_semi_sync_slave_enabled=1


#innodb

innodb_buffer_pool_size = 8G

innodb_buffer_pool_instances =8

#MGR

log-bin = /data/mysql8/3306/bin/mysql-bin

relay-log = /data/mysql8/3306/bin/relay-log

log-slave-updates

binlog-format=row

gtid-mode=ON

enforce-gtid-consistency=true

master-info-repository=table

relay-log-info-repository=table

transaction-write-set-extraction=XXHASH64


标红为必须设置的参数,其他GR配置前提:

必须使用innodb存储引擎,主要为了避免事务提交时冲突。

必须定义主键。

节点间网络延迟小。

使用IPV4协议。

每个节点上启动MySQL数据库实例,注意serverid配置:


./bin/mysqld --defaults-file=/data/mysql8/3306/3306.cfg --initialize-insecure

./bin/mysqld_safe --defaults-file=/data/mysql8/3306/3306.cfg --user=mysql &

mysql -uroot -p -S /data/mysql8/3306/mysql.sock

mysql> alter user root@’localhost’ identified by ‘xxxxxxx’;

mysql>flush privileges;



2、安装mysql shell

需要使用python2.7及以上。

1)安装python2.7



./configure

make && make install


2)解压mysql shell


tar zxvf  mysql-shell-8.0.13-linux-glibc2.12-x86-64bit.tar.gz



3)安装router

Router安装包被封装在mysql8.0.13压缩包中,直接解压使用即可。

3、部署innodb cluster

首先在10.191.143.24创建cluster


> mysqlsh --log-level=DEBUG3

JS> \connect mysql://root@localhost:3307?socket=(/data/mysql8/3306/mysql.sock)

JS> dba.verbose=2

JS > dba.configureInstance()


Mysqlsh自动检查当前实例配置是否满足创建cluster


Configuring local MySQL instance listening at port 3307 for use in an InnoDB cluster...

This instance reports its own address as OaasSvr

Clients and other cluster members will communicate with it through this address by default. If this is not correct, the report_host MySQL system variable should be changed.

WARNING: User 'root' can only connect from localhost.

If you need to manage this instance while connected from other hosts, new account(s) with the proper source address specification must be created.

1) Create remotely usable account for 'root' with same grants and password

2) Create a new admin account for InnoDB cluster with minimal required grants

3) Ignore and continue

4) Cancel

Please select an option [1]: 2


检测到root用户只限定本地登陆,需要重新创建用户


Account Name: cluster@10.191.143.%


继续检测


Some configuration options need to be fixed:

+-----------------+---------------+----------------+----------------------------+

| Variable        | Current Value | Required Value | Note                       |

+-----------------+---------------+----------------+----------------------------+

| binlog_checksum | CRC32         | NONE           | Update the server variable |

+-----------------+---------------+----------------+----------------------------+

Do you want to perform the required configuration changes? [y/n]: y


发现参数需要调整,自动调整。

创建用户后重新使用新用户验证后创建集群


JS > \connect mysql://cluster@10.191.143.24:3307

Please provide the password for 'cluster@10.191.143.24:3307': *********

Save password for 'cluster@10.191.143.24:3307'? [Y]es/[N]o/Ne[v]er (default No): YY

Fetching schema names for autocompletion... Press ^C to stop.

Your MySQL connection id is 22

Server version: 8.0.13 MySQL Community Server - GPL

No default schema selected; type \use <schema> to set one.

JS > dba.configureInstance()dba.configureInstance()

Configuring local MySQL instance listening at port 3307 for use in an InnoDB cluster...


This instance reports its own address as OaasSvr

Clients and other cluster members will communicate with it through this address by default. If this is not correct, the report_host MySQL system variable should be changed.


The instance '10.191.143.24:3307' is valid for InnoDB cluster usage.

JS > var cluster = dba.createCluster('testCluster')

Validating instance at 10.191.143.24:3307...

This instance reports its own address as OaasSvr

Instance configuration is suitable.

Creating InnoDB cluster 'testCluster' on 'cluster@10.191.143.24:3307'...

Adding Seed Instance...


Cluster successfully created. Use Cluster.addInstance() to add MySQL instances.

At least 3 instances are needed for the cluster to be able to withstand up to

one server failure.





 MySQL  10.191.143.24:3307 ssl  JS > cluster.status()cluster.status()

{

    "clusterName": "testCluster",

    "defaultReplicaSet": {

        "name": "default",

        "primary": "10.191.143.24:3307",

        "ssl": "REQUIRED",

        "status": "OK_NO_TOLERANCE",

        "statusText": "Cluster is NOT tolerant to any failures.",

        "topology": {

            "10.191.143.24:3307": {

                "address": "10.191.143.24:3307",

                "mode": "R/W",

                "readReplicas": {},

                "role": "HA",

                "status": "ONLINE"

            }

        }

    },

    "groupInformationSourceMember": "mysql://cluster@10.191.143.24:3307"

}


登陆2节点创建cluster用户


JS > \connect mysql://root@localhost:3307?socket=(/data/mysql8/3306/mysql.sock)

JS >dba.configureInstance('root@localhost:3307',{clusterAdmin:"'cluster'@'10.191.143.%'",clusterAdminPassword:'XXXXX'});

Configuring local MySQL instance listening at port 3307 for use in an InnoDB cluster...


This instance reports its own address as AaasSvr

Clients and other cluster members will communicate with it through this address by default. If this is not correct, the report_host MySQL system variable should be changed.


Some configuration options need to be fixed:

+-----------------+---------------+----------------+----------------------------+

| Variable        | Current Value | Required Value | Note                       |

+-----------------+---------------+----------------+----------------------------+

| binlog_checksum | CRC32         | NONE           | Update the server variable |

+-----------------+---------------+----------------+----------------------------+


Do you want to perform the required configuration changes? [y/n]: YY


Cluster admin user 'cluster'@'10.191.143.%' created.

Configuring instance...

The instance 'localhost:3307' was configured for use in an InnoDB cluster.


然后在1节点的mysqlshell中加入2节点


 MySQL  10.191.143.24:3307 ssl  JS > cluster.addInstance('cluster@10.191.143.25:3307')cluster.addInstance('cluster@10.191.143.25:3307')

A new instance will be added to the InnoDB cluster. Depending on the amount of

data on the cluster this might take from a few seconds to several hours.


Adding instance to the cluster ...


Validating instance at 10.191.143.25:3307...


This instance reports its own address as AaasSvr


Instance configuration is suitable.

Cluster.addInstance: WARNING: Not running locally on the server and can not access its error log.

ERROR:

Group Replication join failed.

ERROR: Error joining instance to cluster: '10.191.143.25:3307' - Query failed. MySQL Error (3092): ClassicSession.query: The server is not configured properly to be an active member of the group. Please see more details on error log.. Query: START group_replication: MySQL Error (3092): ClassicSession.query: The server is not configured properly to be an active member of the group. Please see more details on error log. (RuntimeError)


结果报错:Group Replication join failed.


在10.191.143.25上查看错误日志

2018-11-06T06:17:35.694946Z 0 [ERROR] [MY-011526] [Repl] Plugin group_replication reported: 'This member has more executed transactions than those present in the group. Local transactions: 9f50c222-e0e3-11e8-a687-0050569719f1:1-2 > Group transactions: 638b7777-e174-11e8-90f6-0050569770fd:1-6,

8a673b74-e0dd-11e8-818c-0050569770fd:1-21'

2018-11-06T06:17:35.695067Z 0 [ERROR] [MY-011522] [Repl] Plugin group_replication reported: 'The member contains transactions not present in the group. The member will now exit the group.'


因为新创建实例后我们修改了root用户密码,并创建了新的用户导致binlog日志gtid变化,启用GR时候报错。登陆2节点数据库,执行reset master将日志清除后重新执行


mysql> reset master;

Query OK, 0 rows affected (0.07 sec)

mysql> show master status\G

*************************** 1. row ***************************

             File: mysql-bin.000001

         Position: 151

     Binlog_Do_DB:

 Binlog_Ignore_DB:

Executed_Gtid_Set:

1 row in set (0.00 sec)

在1节点上重新加入集群实例:

 MySQL  10.191.143.24:3307 ssl  JS >  cluster.addInstance('cluster@10.191.143.25:3307') cluster.addInstance('cluster@10.191.143.25:3307')

A new instance will be added to the InnoDB cluster. Depending on the amount of

data on the cluster this might take from a few seconds to several hours.

Adding instance to the cluster ...

Validating instance at 10.191.143.25:3307...

This instance reports its own address as AaasSvr

Instance configuration is suitable.

The instance 'cluster@10.191.143.25:3307' was successfully added to the cluster.


成功,同样在3节点上执行一次。


 MySQL  10.191.143.24:3307 ssl  JS > cluster.status()cluster.status()

{

    "clusterName": "testCluster",

    "defaultReplicaSet": {

        "name": "default",

        "primary": "10.191.143.24:3307",

        "ssl": "REQUIRED",

        "status": "OK",

        "statusText": "Cluster is ONLINE and can tolerate up to ONE failure.",

        "topology": {

            "10.191.143.24:3307": {

                "address": "10.191.143.24:3307",

                "mode": "R/W",

                "readReplicas": {},

                "role": "HA",

                "status": "ONLINE"

            },

            "10.191.143.25:3307": {

                "address": "10.191.143.25:3307",

                "mode": "R/O",

                "readReplicas": {},

                "role": "HA",

                "status": "ONLINE"

            },

            "10.191.143.26:3307": {

                "address": "10.191.143.26:3307",

                "mode": "R/O",

                "readReplicas": {},

                "role": "HA",

                "status": "ONLINE"

            }

        }

    },

    "groupInformationSourceMember": "mysql://cluster@10.191.143.24:3307"

}


最终,innodb cluster创建成功。

测试集群,在10.191.143.24上创建test库并新建test表:

10.191.143.24:


create database test;

use test;

mysql> create table test(id int(11),primary key (id) );

Query OK, 0 rows affected (0.03 sec)

mysql> insert into test values (1);

Query OK, 1 row affected (0.05 sec)

mysql> insert into test values(2),(3);

Query OK, 2 rows affected (0.03 sec)

Records: 2  Duplicates: 0  Warnings: 0

mysql>


10.191.143.25上检查数据成功复制,但只能读不能写:

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,219评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,363评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,933评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,020评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,400评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,640评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,896评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,597评论 0 199
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,327评论 1 244
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,581评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,072评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,399评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,054评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,083评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,849评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,672评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,585评论 2 270

推荐阅读更多精彩内容