MySQL InnoDB Cluster

Mysql高可用环境的搭建比较麻烦,而且之前的那种mysql cluster使用的是内存式存储引擎,一旦断电就会灰飞烟灭,数据丢失。

自从mysql被oracle收购后,新版本发布频繁,推出了很多好用的功能和插件,其中就包括简化高可用环境的搭建难度。

整个过程包括:

  1. 基础环境的安装(mysql 5.7.15、mysql-shell、mysql-router)

  2. 部署多个实例

  3. 创建集群

  4. 部署 Mysql Router

  5. 故障测试

InnoDB Cluster 的搭建可以分为两种情况:

下面总结了多节点 InnoDB Cluster 搭建的详细过程,供有需要的朋友参考

1. 目标

准备4台服务器,node01、node02、node03 作为 cluster 节点,node04 作为管理节点,负责创建 cluster,并作为 cluster 的路由

最后,会搭建出一个高可用集群,通过 router 连接到这个cluster,MySQL客户端通过 router 与 cluster 进行沟通

2. 搭建思路

(1)安装基础环境

node 01、02、03 上安装好 mysql 与 mysql-shell

node04 上安装 mysql-shellmysql-router

(2)创建集群

在 node01 上创建集群,先配置好其 mysql 并启动,然后通过 node01 上的 shell 连接 node01 的 mysql,执行配置命令

dba.configureLocalInstance();

使其具备创建集群的条件

最后通过 node04 的 shell 连接 node01 的 mysql,执行创建集群的命令 

dba.createCluster()

(3)向集群中添加节点

集群创建起来后,接下来就是向其中添加节点

配置 node02、node03 的 mysql 并启动,然后使用各自的 mysql-shell 对其进行配置

最后通过 node04 的 mysql-shell 执行添加实例的命令 dba.addInstance() 把 node02,node03 添加到集群中

(4)使用 router 连接集群

集群搭建完成后,把 node04 的 router 启动起来,并连接到集群

client 就可以连接到 router,通过其操作集群了

3. 具体搭建过程

(1)环境安装

在各台服务器中配置好 hosts,如:

192.168.31.13 node03 
192.168.31.228 node02 
192.168.31.36 node01 

需要准备的软件:

  1. mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz

  2. mysql-shell-1.0.9-linux-glibc2.12-x86-64bit.tar.gz

  3. mysql-router-2.1.3-linux-glibc2.12-x86-64bit.tar.gz

根据上面的结构图,在各个服务器中安装好所需的软件

安装方法:

1. MySQL
# 解压
tar zxf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.7.17-linux-glibc2.5-x86_64 /usr/local/mysql-5.7
cd /usr/local

# 初始化数据库实例
mkdir data
mysql-5.7/bin/mysqld --initialize-insecure --basedir=$PWD/mysql-5.7 --datadir=$PWD/data

# 创建mysql用户
groupadd mysql5.7
useradd -g mysql5.7 mysql5.7
chown -R mysql5.7:mysql5.7 /usr/local/mysql-5.7
chown -R mysql5.7:mysql5.7 /usr/local/data
2. shell
# 直接解压即可
tar zxf mysql-shell-1.0.9-linux-glibc2.12-x86-64bit.tar.gz mysql-shell
3. router
# 直接解压即可
tar zxf mysql-router-2.1.3-linux-glibc2.12-x86-64bit.tar.gz mysql-router

(2)创建集群

  • 配置 node01 的 mysql 并启动

切换到mysql用户

su mysql5.7

编辑配置文件 vi /user/local/data/my.cnf,内容:

[mysqld]

# server configuration
datadir=/usr/local/data
basedir=/usr/local/mysql-5.7/

port=3306
socket=/usr/local/data/mysql.sock

server_id=1
gtid_mode=ON
enforce_gtid_consistency=ON
master_info_repository=TABLE
relay_log_info_repository=TABLE
binlog_checksum=NONE
log_slave_updates=ON
log_bin=binlog
binlog_format=ROW

transaction_write_set_extraction=XXHASH64

启动

nohup /usr/local/mysql-5.7/bin/mysqld --defaults-file=data/my.cnf >data/nohup.out 2>&1 &

# 退回root用户
exit

登录 MySQL

/usr/local/mysql-5.7/bin/mysql -uroot -h127.0.0.1 --skip-password

修改root默认密码

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'A123456';
  • 通过本机 mysql-shell 对 mysql 进行配置

进到 mysql-shell 的安装目录,登录 shell ,执行配置

bin/mysqlsh

连接到本机MySQL,执行配置命令

# 连接,需要输入密码(A123456)
mysql-js> shell.connect('root@localhost:3306');

# 执行配置命令,也需要密码
# 然后需要输入MySQL配置文件路径,本示例中的路径是 /usr/local/data/s1/s1.cnf
# 接下来需要创建供其他主机访问的用户,这里选择第1项,为root用户授权

mysql-js> dba.configureLocalInstance();
Please provide the password for 'root@localhost:3306':

Detecting the configuration file...
Default file not found at the standard locations.
Please specify the path to the MySQL configuration file: /usr/local/data/s1/s1.cnf
MySQL user 'root' cannot be verified to have access to other hosts in the network.

1) Create root@% with necessary grants
2) Create account with different name
3) Continue without creating account
4) Cancel
Please select an option [1]: 1
Password for new account:
Confirm password:
Validating instance...

The instance 'localhost:3306' is valid for Cluster usage
You can now use it in an InnoDB Cluster.

{
    "status": "ok"
}

status 为 ok 说明配置没问题了,可以用来创建cluster

  • 通过 node04 的 mysql-shell 连接 node01 创建 cluster

进入 node04 上的 mysql-shell 安装目录,登录 shell,连接 node01,创建 cluster

bin/mysqlsh 

# 连接01
mysql-js> shell.connect('root@node01:3306');

# 创建一个 cluster,命名为 'myCluster'
mysql-js> var cluster = dba.createCluster('myCluster');

# 创建成功后,查看cluster状态
mysql-js> cluster.status();

(3)添加实例节点 node02

  • 配置 node02 的 mysql 并启动

编辑配置文件 vi /usr/local/data/my.cnf,内容与 node01 上的一样,只有一行不同

server_id=2
  • 通过本机 mysql-shell 对 mysql 进行配置

登录 shell ,执行配置

bin/mysqlsh 

mysql-js> shell.connect('root@localhost:3306');
mysql-js> dba.configureLocalInstance();
  • 停掉 mysql,修改 my.cnf,添加配置项

vi data/my.cnf

# 在末尾添加
group_replication_allow_local_disjoint_gtids_join=ON

重启MySQL

  • 通过 node04 的 mysql-shell 添加 node02 到 cluster

# 添加实例
cluster.addInstance('root@node02:3306');

# 创建成功后,查看cluster状态
mysql-js> cluster.status();

(4)添加实例节点 node03

过程与 node02 完全相同,只需要注意 my.cnf 中的 'server_id' 值改为 3,和 addInstance 时改为 node03

(5)安装 router

进入 node04 中 mysql-router 安装目录,启动 router 

mysqlrouter --bootstrap root@ic-1:3306 --directory myrouter --name=myrouter
myrouter/start.sh

然后就可以使用MySQL客户端连接router了

可能有很多朋友,对与DBA不是很了解,那今天我们来看看如何成长为DBA

1. 概念了解阶段

《数据库系统实现》和《事务处理》 这两本书一定要好好看,算的是经典的书籍

2. 实践探索阶段

这个阶段需要多动手,多实践。看一百遍书都不如实际操作一次。从系统的安装,初始化,建库,建表到建索引,全都操作一遍。MySQL的手册非常详细,很容易上手。

这个阶段基本上可以掌握MySQL的常用功能。

这一阶段可以看下培训机构的视频比如动力学院的,比如尚官的,就不要看老男孩的了,不知道为啥对他的慢语速,不实操,只讲鸡汤特别反感。

3.进阶阶段

这个阶段要多实操,研究一些高可用架构,解决工作中遇到的各式各样的问题,最好可以去mysql社区解答问题,积累经验,比如表太大了如何分表,缓冲区如何设置才合理等等。

  最后祝大家在未来的DBA道路上越走越远,马上五一节就要来了,放假期间不分享视频,该出去旅游的就出去旅游把,五一回来,我们将有更多的视频资料。

 

  在这里需要强调下,如果没打算参加运维开发社群的,就不要加我了,时间不够,没有太多精力陪聊,谢谢。

   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值