mysql+keepalived 主主高可用集群配置

 

Linux下MySQL+Keepalived高可用性配置

[日期:2012-12-13] 来源:Linux社区  作者:wgkgood [字体:大 中 小]

前言*在mysql高可用配置中,我们会看到有很多的方法,每种方法都有各自优缺点,那今天我们来看参看一下Mysql+Keepalived高可用配置。

一、安装环境:

系统版本:CentOS6.0 x86_64

Mysql版本:mysql-5.1.61

Mysqlserver_1: 192.9.117.140

Mysqlserver_2: 192.9.117.141

Keepalived-VIP:192.9.117.142

二、正式安装:

在两台服务器上分别安装mysql,这里直接采用yum安装,如下:

  1. yum install –y mysql mysql-devel mysql-server mysql-libs

安装完后,配置MySQL配置文件,mysql采用主主模式:

1) 192.9.117.140的配置文件如下:vi /etc/my.cnf

  1. [mysqld]
  2.  
  3. datadir=/data/mysql
  4.  
  5. socket=/var/lib/mysql/mysql.sock
  6.  
  7. user=mysql
  8.  
  9. # Disabling symbolic-links is recommended to prevent assorted security risks
  10.  
  11. symbolic-links=0
  12.  
  13. log-bin=mysql-bin
  14.  
  15. server-id = 1
  16. auto_increment_offset=1
  17.  
  18. auto_increment_increment=2
  19.  
  20. [mysqld_safe]
  21.  
  22. log-error=/var/log/mysqld.log
  23.  
  24. pid-file=/var/run/mysqld/mysqld.pid
  25.  
  26. master-host =192.9.117.141
  27.  
  28. master-user=tongbu
  29.  
  30. master-pass=123456
  31.  
  32. master-port =3306
  33.  
  34. master-connect-retry=60
  35.  
  36. replicate-do-db =map

2) 192.9.117.141的配置文件如下:vi /etc/my.cnf

  1. [mysqld]
  2.  
  3. datadir=/data/mysql
  4.  
  5. socket=/var/lib/mysql/mysql.sock
  6.  
  7. user=mysql
  8.  
  9. # Disabling symbolic-links is recommended to prevent assorted security risks
  10.  
  11. symbolic-links=0
  12.  
  13. log-bin=mysql-bin
  14.  
  15. server-id = 2
  • auto_increment_offset=2
  •  
  • auto_increment_increment=2
  1.  
  2. [mysqld_safe]
  3.  
  4. log-error=/var/log/mysqld.log
  5.  
  6. pid-file=/var/run/mysqld/mysqld.pid
  7.  
  8. master-host =192.9.117.140
  9.  
  10. master-user=tongbu
  11.  
  12. master-pass=123456
  13.  
  14. master-port =3306
  15.  
  16. master-connect-retry=60
  17.  
  18. replicate-do-db =map
  19.  
  20. 如上设置bin-log文件,并都设置对方为自己的主服务器,配置同步的数据库为map

三、配置MySQL:

1) 在两台mysql数据库服务器里面设置权限,分别执行如下命令:

  1. grant replication slave on *.* to 'tongbu'@'%' identified by '123456';

然后在141执行:

  1. show master status;
  2.  
  3. +------------------+----------+--------------+------------------+
  4.  
  5. | File
  6. |Position | Binlog_Do_DB | Binlog_Ignore_DB |
  7.  
  8. +------------------+----------+--------------+------------------+
  9.  
  10. | mysql-bin.000002 |
  11.  
  12. 106 |
  13. |
  14. |
  15.  
  16. +------------------+----------+--------------+------------------+
  17.  
  18. 1 row in set (0.00 sec)

2) 在192.9.117.140上将192.9.117.141设为自己的主服务器执行如下命令:

  1. change master to master_host='192.9.117.141',master_user='tongbu',master_password='123456',master_log_file='mysql-bin.000002',master_log_pos=106;
  2.  
  3. 然后启动start slave;
  4. 注意这里写的bin-log参数是在141查看到的,即是对方的参数。

3) 然后在140执行:

  1. show master status;
  2.  
  3. +------------------+----------+--------------+------------------+
  4.  
  5. | File
  6. |Position | Binlog_Do_DB | Binlog_Ignore_DB |
  7.  
  8. +------------------+----------+--------------+------------------+
  9.  
  10. | mysql-bin.000003 |
  11.  
  12. 445|
  13. |
  14. |
  15.  
  16. +------------------+----------+--------------+------------------+
  17.  
  18. 1 row in set (0.00 sec)
  19.  

4) 在192.9.117.141上将192.9.117.140设为自己的主服务器执行如下命令:

  1. change master to master_host='192.9.117.140',master_user='tongbu',master_password='123456',master_log_file='mysql-bin.000003',master_log_pos=445;
  2. 然后启动start slave;

5) MySQL同步测试配置完毕,我们会发现在任何一台mysql上更新同步的数据库里面的数据,都会同步到另一台mysql。

四、安装Keepalived:

  1. tar zxf keepalived-1.2.1.tar.gz
  2.  
  3. cd keepalived-1.2.1 &&./configure --with-kernel-dir=/usr/src/kernels/2.6.18-164.el5-i686 &&make && make install
  4.  
  5. DIR=/usr/local/ ;cp $DIR/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/ && cp $DIR/etc/sysconfig/keepalived /etc/sysconfig/ && mkdir -p /etc/keepalived && cp $DIR/sbin/keepalived /usr/sbin/
  6.  
  7. if
  8.  
  9. [ $? -eq 0 ];then
  10.  
  11. echo "Keepalived system server config success!"
  12.  
  13. else
  14. echo "Keepalived system server config failed ,please check keepalived!"
  15. exit 0
  16.  
  17. fi

五、配置keepalived:

1) 创建vi keepalived.conf文件,内容如下:

  1. ! Configuration File for keepalived
  2.  
  3. global_defs {
  4.  
  5. notification_email {
  6.  
  7. [email protected]
  8.  
  9. }
  10. notification_email_from [email protected]
  11.  
  12. smtp_server 127.0.0.1
  13.  
  14. smtp_connect_timeout 30
  15.  
  16. router_id LVS_DEVEL
  17. }
  18. # VIP1
  19.  
  20. vrrp_instance VI_1 {
  21.  
  22. state BACKUP
  23.  
  24. interface eth0
  25.  
  26. lvs_sync_daemon_inteface eth0
  27.  
  28. virtual_router_id 151
  29.  
  30. priority 90
  31.  
  32. advert_int 5
  33.  
  34. nopreempt
  35.  
  36. authentication {
  37.  
  38. auth_type PASS
  39.  
  40. auth_pass 2222
  41.  
  42. }
  43. virtual_ipaddress {
  44.  
  45. 192.9.117.142
  46. }
  47. }
  48.  
  49. virtual_server 192.9.117.142 3306 {
  50.  
  51. delay_loop 6
  52.  
  53. lb_algo wrr
  54.  
  55. lb_kind DR
  56.  
  57. persistence_timeout 60
  58.  
  59. protocol TCP
  60.  
  61. real_server 192.9.117.140 3306 {
  62.  
  63. weight 100
  64.  
  65. notify_down /data/sh/mysql.sh
  66.  
  67. TCP_CHECK {
  68.  
  69. connect_timeout 10
  70.  
  71. nb_get_retry 3
  72.  
  73. delay_before_retry 3
  74.  
  75. connect_port 3306
  76.  
  77. }
  78. }
  79. }

2) 141 keepalived同样如上配置,注意keepalived配置文件里面只添加一台mysql服务器ip地址,代表只使用这一台读写。

修改为Realserver192.9.117.141,并且设置优先级为90,都是BACKUP模式,并且nopreempt不抢占即可。

配置完后启动keepalived测试,可以先停止一台mysql,然后查看本地的keepalived是否停止,并且另外一台keepalived 已经变成了MASTER,如果是那就测试成功。

如上需要设置检查脚本/data/sh/mysql.sh,脚本内容为:

pkill keepalived

下图为keepalived.conf部分配置

 

MysqlCluster  
mysql+keepalived 主主高可用集群配置
Updated  Jul 30, 2012 by  [email protected]

Introduction

介绍如何配置mysql的双机热备,任何一台挂掉,都不会影响另外一台,当然,如果一台挂掉以后,没有重新起来,而另外一台也挂掉,将导致整个挂掉.

Details

详细介绍了,如何配置两台高可用mysql集群,测试环境如下:

  • 两台vm,系统都是centos6 64
  • 两台vm的 eth0 ip 地址分别是: (master1) 172.16.200.31 (master2)172.16.200.32
  • 两台vm均需要安装 keepalived ,虚拟的ip都是 172.16.200.30
  • 客户连接的ip地址为 172.16.200.30
  • 两台 mysql server 均充当 master 和 salve

 

详细步骤

  • 安装基本的软件,以下操作,两台机完全一模一样,都需要做,yum 安装 keepalived 需要 epel 的源
yum install keepalived mysql-server -y
  • 开始配置mysql,以下需要在两台mysql server里面配置

在/etc/my.cnf 文件中, [mysqld] 字段里面,添加如下

log-bin=mysql-bin
server
-id=1
slave
-skip-errors=all

slave-skip-errors 表示跳过错误,否则如果错误,则不能继续同步

  • 开始配置mysql master1 172.16.200.31
# 创建用来同步的账号
MySQL> grant replication slave on *.* to 'replication'@'%' identified by 'replication';  
# 查看状态
mysql
> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000006 |      106 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
  • 在master1 172.16.200.31 上,将 172.16.200.32 设为自己的主服务器
mysql> change master to master_host='172.16.200.32',master_user='replication',master_password='replication';
Query OK, 0 rows affected (0.05 sec)  
# 启动 slave
mysql
> start slave;  
Query OK, 0 rows affected (0.00 sec)  
# 查看 slave 状态
 show slave status
\G  
*************************** 1. row ***************************  
             
Slave_IO_State: Waiting for master to send event  
               
Master_Host: 192.168.1.201  
               
Master_User: replication  
               
Master_Port: 3306  
             
Connect_Retry: 60  
           
Master_Log_File: MySQL-bin.000003  
       
Read_Master_Log_Pos: 374  
             
Relay_Log_File: MySQL-master2-relay-bin.000002  
             
Relay_Log_Pos: 235  
     
Relay_Master_Log_File: MySQL-bin.000003  
           
Slave_IO_Running: Yes  
         
Slave_SQL_Running: Yes  
           
Replicate_Do_DB:  
       
Replicate_Ignore_DB:  
         
Replicate_Do_Table:  
     
Replicate_Ignore_Table:  
   
Replicate_Wild_Do_Table:  
Replicate_Wild_Ignore_Table:  
                 
Last_Errno: 0  
                 
Last_Error:  
               
Skip_Counter: 0  
       
Exec_Master_Log_Pos: 374  
           
Relay_Log_Space: 235  
           
Until_Condition: None  
             
Until_Log_File:  
             
Until_Log_Pos: 0  
         
Master_SSL_Allowed: No  
         
Master_SSL_CA_File:  
         
Master_SSL_CA_Path:  
           
Master_SSL_Cert:  
         
Master_SSL_Cipher:  
             
Master_SSL_Key:  
     
Seconds_Behind_Master: 0  
1 row in set (0.00 sec)
  • 将172.16.200.32 设为 172.16.200.31 的主服务器, 以下操作,针对 172.16.200.32
# 创建用户
MySQL> grant replication slave on *.* to 'replication'@'%' identified by 'replication';  
Query OK, 0 rows affected (0.00 sec)  
     
MySQL> show master status;  
+------------------+----------+--------------+------------------+  
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |  
+------------------+----------+--------------+------------------+  
| mysql-bin.000003 |      374 |              |                  |  
+------------------+----------+--------------+------------------+  
1 row in set (0.00 sec)
  • 在master 2, 172.16.200.32 上,将 172.16.200.31 设为自己的主服务器
mysql >change master to master_host='172.16.200.31',master_user='replication',master_password='replication';
Query OK, 0 rows affected (0.05 sec)  
 
MySQL> start slave;  
Query OK, 0 rows affected (0.00 sec)  
 
MySQL> show slave status\G  
*************************** 1. row ***************************  
             
Slave_IO_State: Waiting for master to send event  
               
Master_Host: 192.168.1.202  
               
Master_User: replication  
               
Master_Port: 3306  
             
Connect_Retry: 60  
           
Master_Log_File: MySQL-bin.000003  
       
Read_Master_Log_Pos: 374  
             
Relay_Log_File: MySQL-master1-relay-bin.000002  
             
Relay_Log_Pos: 235  
     
Relay_Master_Log_File: MySQL-bin.000003  
           
Slave_IO_Running: Yes  
         
Slave_SQL_Running: Yes  
           
Replicate_Do_DB:  
       
Replicate_Ignore_DB:  
         
Replicate_Do_Table:  
     
Replicate_Ignore_Table:  
   
Replicate_Wild_Do_Table:  
Replicate_Wild_Ignore_Table:  
                 
Last_Errno: 0  
                 
Last_Error:  
               
Skip_Counter: 0  
       
Exec_Master_Log_Pos: 374  
           
Relay_Log_Space: 235  
           
Until_Condition: None  
             
Until_Log_File:  
             
Until_Log_Pos: 0  
         
Master_SSL_Allowed: No
         
Master_SSL_CA_File:  
         
Master_SSL_CA_Path:  
           
Master_SSL_Cert:  
         
Master_SSL_Cipher:  
             
Master_SSL_Key:  
     
Seconds_Behind_Master: 0  
1 row in set (0.00 sec)
  • MySQL同步测试

如上述均正确配置,现在任何一台MySQL上更新数据都会同步到另一台MySQL, 测试的方法就是登陆任何一台mysql,然后创建数据库,接着登陆另外一台数据库,查看是否已经同步

配置 keepalived

配置 keepalived ,也是采用 rpm yum 安装的方式,不采用自己编译的方法.安装完成以后,默认的

 

 

 

你可能感兴趣的:(mysql,keepalived)