高可用keepalived实例

高可用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实例

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


文章标题:高可用keepalived实例
本文地址:http://scyanting.com/article/gsddci.html