高可用keepalived实例

发布时间:2020-06-23 22:09:34 作者:梁十八
来源:网络 阅读:327

高可用keepalived实例

主从数据库服务器的数据会最终一致(因为mysql复制是异步的,总会存在延迟),看似解决了单点问题,然而该方案并不完美。因为一旦主服务器宕机,必须手动把读写连接切换到原来的从服务器上,而这个过程需要时间,短则几分钟,长则数小时,这期间服务会出于瘫痪状态。


怎样在master宕机后,自动切换数据库连接呢?

        最简单的是使用虚拟ip:

                高可用keepalived实例

                高可用keepalived实例

                这里用另外一种方式keepalived:

                        高可用keepalived实例

                        高可用keepalived实例

                        (要注意同一时间只有一个主对外提供服务,另一个处于只读状态)

                        高可用keepalived实例

                        配置:

                                高可用keepalived实例

                                (指定id增长步长为2,自增id从1开始增长,于是主库上的id增长就是1、3、5、7、9...  注意,这参数修改是针对整个mysql服务器全局的,而不是针对某个表某个库的,也就是说,一旦修改后,该服务器下所有库表的值都是按照这种方式增加的

                                高可用keepalived实例

                                (指定id增长步长为2,自增id从2开始增长,于是主库上的id增长就是2、4、6、8、10...  注意,这参数修改是针对整个mysql服务器全局的,而不是针对某个表某个库的,也就是说,一旦修改后,该服务器下所有库表的值都是按照这种方式增加的

                                (经过上述设置,主备之间的主键id增长就不会产生冲突了)

                        (前面说过innodb的主键最好采用自增id的列,而在主主复制中为了避免两个组同时写所带来的主键冲突,我们需要修改自增主键的配置使两个自增主键按照不同的步长值来增长,当然这是为了以防万一才采取的配置,当我们使用主主复制时,最安全的方式还是要保障在任意时间均只有一个作为主提供服务,而另一个主只提供只读)

                        接下来还要安装keepalived组件(它可以将两个设备虚拟成一个设备,对外提供一个虚拟ip,也就是vip):

                                高可用keepalived实例

                                (注意:在互为主从的两台设备上都要安装keepalived)

                                高可用keepalived实例

                                

                                最好情况是把主从复制改为主主复制,先完成主主复制配置:

                                        先修改主服务器上的配置vi /etc/my.cnf:

                                                高可用keepalived实例

                                                (由于这俩是动态参数,不需要重启mysql)

                                                并且在mysql客户端设置:

                                                高可用keepalived实例

                                                退出当前mysql客户端,再进入查看:

                                                高可用keepalived实例

                                        修改从服务器上的配置vi /etc/my.cnf:

                                                高可用keepalived实例

                                                (由于这俩是动态参数,不需要重启mysql)

                                                并且在mysql客户端设置:

                                                高可用keepalived实例

                                                检查从服务器的账号:

                                                高可用keepalived实例

                                                (dba_repl账号在从服务器也是存在的)

                                                由于从服务器出于read_only状态,没有写,所以不用再重新初始化数据了,因为它的数据和主上是完全一致的,因为它没有任何写操作:

                                                高可用keepalived实例

                                                查看从服务器上的点:

                                                高可用keepalived实例

                                        在主上change master(主当从,从当主):

                                                高可用keepalived实例

                                                (将master的主服务器设置为原来的从服务器)

                                                启动复制链路(原来的主服务器,现在也是从服务器):

                                                高可用keepalived实例

                                                查看:

                                                高可用keepalived实例

                                

                                安装keepalived:

                                        在两台服务器上都进行安装:yum install keepalived -y

                                        查看两台config文件:

                                        高可用keepalived实例

                                        高可用keepalived实例

                                        高可用keepalived实例

                                        高可用keepalived实例

                                        (一定要保障check_mysql.sh脚本有执行权限,两台都是)

                                        启动两台服务器的keepalived:

                                        高可用keepalived实例

                                        查看虚拟ip:

                                        高可用keepalived实例

                                        (可能需要几秒钟才产生)

                                        关掉主服务器的mysql:

                                        高可用keepalived实例

                                        同时主的keepalived也会自动kill,已释放虚拟ip(vip),它的虚拟ip也会消失:

                                        高可用keepalived实例

                                        而从服务器上有:

                                        高可用keepalived实例

                                        (至此,宕机下的无缝切换完成)

推荐阅读:
  1. keepalived+haproxy高可用
  2. Memcached高可用集群——keepalived

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

高可用keepalived实例 alived

上一篇:ORACLE索引失效,更新统计信息

下一篇:web开发的编码规范

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》