mancha 发表于 2018-12-31 09:16:13

keepalived+lvs+mysql实现MySQL

一、      安装两台mysql服务器

在MySQL1上安装MySQL-5.6.15
1.   安装数据库软件(MySQL-5.6.15)
# tar -xv -f MySQL-5.6.15-1.el6.x86_64.rpm-bundle.tar -C /tmp/
# rpm -Uvh MySQL-*.rpm
2.   拷贝主配置文件,启动服务,初始化数据库。
# cp/usr/share/mysql/my-default.cnf /etc/my.cnf
# /etc/init.d/mysqlstart
# cat/root/.mysql_secret//MySQL-5.6.15不允许空密码登录,安装时自动设置密码,初次登录密码。
# mysql -u root -p
Enter password:    //输入安装时自动生成的密码。
3.   修改root用户从本地登录数据库的密码,然后以新密码登录测试。
mysql> set password for root@'localhost'=password('xiaoke');//修改root密码
mysql> exit   //退出数据库
# mysql -u root –p
Enter password:   //输入更改后的密码:xiaoke
测试登录成功。
4.   设置MySQL服务开机自启动
# chkconfig mysql on
# chkconfig --list mysql
mysql       0:关闭1:关闭   2:启用   3:启用   4:启用   5:启用   6:关闭
在MySQL2上安装MySQL-5.6.15,安装过程同MySQL1。
二、      两台MySQL互为主从搭建
将MySQL1作为MySQL2的主数据服务器
1.   在MySQL1上做用户授权,使MySQL2可以登录MySQL1服务器。
mysql> grant replication slave on *.*to slaveuser@'%' identified by 'slaveuser';
2.   编辑MySQL1数据库的主配置文件

……
log_bin = MySQL1-bin   //开启并设置binlog名称
server_id = 5   //两台服务器的id号不能相同,另一台设置为6。
……
3.   重新启动MySQL1数据库服务器,登录并查看主数据服务器的状态。
# /etc/init.d/mysqlrestart
# mysql -u root –pxiaoke
mysql> show master status\G
*************************** 1. row***************************
File: MySQL1-bin.000001
Position: 120
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
4.   登录MySQL2数据库服务器,并将其配置为MySQL1的从数据库服务器
# mysql -u root –pxiaoke
mysql> change master to
   -> master_host='100.1.1.5',
   -> master_user='slaveuser',
   -> master_password='slaveuser',
   -> master_log_file='MySQL1-bin.000001',
   -> master_log_pos=120;
mysql> start slave;
mysql> show slave status\G    //查看从服务器的状态
*************************** 1. row***************************
Slave_IO_State: Waiting for master to sendevent
Master_Host: 100.1.1.5
Master_User: slaveuser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: MySQL1-bin.000001
Read_Master_Log_Pos: 120
Relay_Log_File: MySQL2-relay-bin.000002
Relay_Log_Pos: 284
Relay_Master_Log_File: MySQL1-bin.000001
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: 120
Relay_Log_Space: 458
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
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 5
Master_UUID:28fc3e49-d548-11e3-b1a2-000c29f5f6bc
Master_Info_File:/var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has readall relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
将MySQL2作为MySQL1的主数据服务器
1.   在MySQL2上做用户授权,使MySQL1可以登录MySQL2服务器。
mysql> grant replication slave on *.*to slaveuser@'%' identified by 'slaveuser';
2.   编辑MySQL2数据库的主配置文件

……
log_bin = MySQL2-bin   //开启并设置binlog名称
server_id = 6   // 由于不能和MySQL1的一致,所以将其设置为6
……
3.   重新启动MySQL2数据库服务器,登录并查看主数据服务器的状态。
# /etc/init.d/mysqlrestart
# mysql -u root –pxiaoke
mysql> show master status\G
*************************** 1. row***************************
File: MySQL2-bin.000001
Position: 120
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
4.   登录MySQL1数据库服务器,并将其配置为MySQL2的从数据库服务器
# mysql -u root –pxiaoke
mysql> change master to
-> master_host='100.1.1.5',
-> master_user='slaveuser',
-> master_password='slaveuser',
->master_log_file='MySQL1-bin.000001',
-> master_log_pos=120;
mysql> start slave;
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 100.1.1.6
Master_User: slaveuser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: MySQL2-bin.000001
Read_Master_Log_Pos: 120
Relay_Log_File: MySQL1-relay-bin.000002
Relay_Log_Pos: 284
Relay_Master_Log_File: MySQL2-bin.000001
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: 120
Relay_Log_Space: 458
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
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 6
Master_UUID: 620bc0fe-d54d-11e3-b1c4-000c29149913
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting forthe slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
两台MySQL数据库服务器互为主从搭建成功!
三、      安装keepalived,实现高可用(HA)数据库集群
在MySQL1上安装配置keepalived
1.   安装软件(keepalived-1.2.7)
# yum -y installgcc gcc-C++ kernel-devel openssl-develpopt-devel
# tar -zxv -fkeepalived-1.2.7.tar.gz -C /usr/src/
# cd/usr/src/keepalived-1.2.7/
# ./configure--sysconf=/etc --with-kernel-dir=/usr/src/kernels/2.6.32-358.el6.x86_64/&& make && make install
# ln -s/usr/local/sbin/keepalived /sbin/
2.   在MySQL1上配置keepalived
# vim/etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
notification_email {   //设置报警邮件
   handetian@126.com   //报警收件人邮件地址
   }
   notification_email_fromxiaoke@localhost    //发送报警邮件的地址
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id MySQL_ha
}

vrrp_instance VI_1 {
state BACKUP   //两台此处均为BACKUP
    interface eth0
    virtual_router_id 51
priority 100//优先级,另一台改为90
    advert_int 1
nopreempt//不抢占,只在优先级高的机器上设置即可,优先级低的机器不设置
    authentication {
      auth_type PASS
       auth_pass han-mysql   //两台密码一致,均为:han-mysql
    }
    virtual_ipaddress {
      100.1.1.200   
    }
}

virtual_server 100.1.1.200 3306 {
    delay_loop 2//每隔2秒检查一次real_server状态
    lb_algo wrr   //LVS算法
    lb_kind DR   //LVS模式
    persistence_timeout 60    //会话保持时间
    protocol TCP
    real_server 100.1.1.5 3306{
      weight 3
      notify_down/usr/local/shell/bin/MySQL.sh//检测到服务down后执行的脚本
      TCP_CHECK {
            connect_timeout 10    //连接超时时间
            nb_get_retry 3    //重试次数
            delay_before_retry3    //重连间隔时间
            connect_port 3306   //健康检查端口
      }
    }
}
3.   编写检测到服务down之后所需要执行的脚本
# mkdir -p/usr/local/shell/bin/
# vim /usr/local/shell/bin/MySQL.sh
#!/bin/bash
pkill -9 keepalived
4.   启动keepalived的服务
# /etc/init.d/keepalivedstart
在MySQL2上安装配置keepalived,步骤同上,不同之处,以在上述配置过程中标明。
四、调整网卡和内核参数
1.   调整网卡参数(两台数据库修改一致)
# cd/etc/sysconfig/network-scripts/
# cpifcfg-lo{,:0}
# cat ifcfg-lo:0
DEVICE=lo:0
IPADDR=100.1.1.200    //虚拟数据库IP地址
NETMASK=255.255.255.255
BROADCAST=100.1.1.200
ONBOOT=yes
NAME=loopback
2.   调整内核参数(两台数据库修改一致)
# tail /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
五、 测试
MySQL远程登录测试
在CT-client客户端上做登录测试,看登录虚拟数据库服务器(VIP)是否成功,在登录之前两台MySQL都要做授权允许从远程登录。
1.   授权远程登录
# mysql -u root –pxiaoke
mysql> grant all privileges on *.* todiscuz@'%' identified by 'discuz';
# mysql -u root –pxiaoke
mysql> grant all privileges on *.* todiscuz@'%' identified by 'discuz';
2.   客户端登录测试
# mysql -h 100.1.1.200-udiscuz –pdiscuz
登录成功!!!




页: [1]
查看完整版本: keepalived+lvs+mysql实现MySQL