kient88 发表于 2019-1-7 07:50:02

mysql热备+heartbeat集群



1、环境说明:
Master : 192.168.3.37
Slave: 192.168.3.38
V IP:192.168.3.55


2、mysql的安装(主备)
groupaddmysql
useradd -gmysql -s /sbin/nologin -M mysql


tar -zxvf mysql-5.0.67.tar.gz
cd mysql-5.0.67
./configure --prefix=/usr/local/mysql --with-charset=gbk--with-extra-charset=all --enable-hread-safe-client --enable-local-infile--with-low-memory
Make && make install


chmod +w /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql
cp support-files/my-medium.cnf /etc/my.cnf
  cp support-files/mysql.server /etc/init.d/mysqld
  

/usr/local/mysql/bin/mysql_install_db-–basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql


/usr/local/mysql/bin/mysqld_safe --user=mysql&


添加到系统服务
chkconfig --addmysqld
chkconfig--levels 2345 mysqld on


添加/usr/local/mysql/bin到环境变量PATH中
echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile


设置数据库密码
service mysqld start
mysqladmin password 123456


开启root远程访问权限
mysql –p123456
mysql> grant all on *.* to root@'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
mysql> quit


  2.1 主服务器上进行的操作
   启动mysql服务
       servicemysqld start
     通过命令行登录管理MySQL服务器
         mysql -uroot-p'new-password'授权给从数据库服务器192.168.3.38
       mysql> GRANT REPLICATION SLAVE ON *.* to 'rep1'@'192.168.3.38' identified by‘password’;
     查询主数据库状态
       Mysql>show master status;
       +------------------+----------+--------------+------------------+
      | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
       +------------------+----------+--------------+------------------+
       | mysql-bin.000005 | 261 | | |
      +------------------+----------+--------------+------------------+
     记录下 FILE 及 Position 的值,在后面进行从服务器操作的时候需要用到。

  2.2 配置从服务器
   修改从服务器的配置文件/etc/my.cnf
   将 server-id =1修改为server-id = 10,并确保这个ID没有被别的MySQL服务所使用。
     启动mysql服务
       service mysqld start
     通过命令行登录管理MySQL服务器
       mysql -uroot -p'new-password'
     执行同步SQL语句
       mysql> change master to
       master_host=’192.168.3.37’,
       master_user=’rep1’,
       master_password=’password’,
       master_log_file=’mysql-bin.000005’,
       master_log_pos=261;
     正确执行后启动Slave同步进程
      mysql> start slave;
     主从同步检查
      mysql> show slave status\G
   ==============================================
   * *************** 1. row *******************
Slave_IO_State:
Master_Host: 192.168.3.37
Master_User: rep1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 415
Relay_Log_File: localhost-relay-bin.000008
Relay_Log_Pos: 561
Relay_Master_Log_File: mysql-bin.000005
Slave_IO_Running: YES
Slave_SQL_Running: YES
Replicate_Do_DB:
……………省略若干……………
Master_Server_Id: 1
1 row in set (0.01 sec)
==============================================
其中Slave_IO_Running与 Slave_SQL_Running的值都必须为YES,才表明状态正常

如果主服务器已经存在应用数据,则在进行主从复制时,需要做以下处理:
(1)主数据库进行锁表操作,不让数据再进行写入动作
mysql> FLUSH TABLES WITH READ LOCK;
(2)查看主数据库状态
mysql> show master status;
(3)记录下FILE 及 Position 的值。
将主服务器的数据文件(整个/opt/mysql/data目录)复制到从服务器,建议通过tar归档压缩后再传到从服务器解压。

(4)取消主数据库锁定
mysql> UNLOCK TABLES;
3、heartbeat的安装
(主备机一样,唯独ha.cf里面ucast那条信息)
两台机器都要安装双机软件libnet.tar.gzheartbeat-2.1.3.tar.gz
Groupaddhaclient
useradd -g haclienthacluster
tar -zxvf heartbeat-2.1.3.tar.gz
cd heartbeat-2.1.3
./ConfigureMe configure
make && make install


heartbeat配置
总共有三个文件需要配置:
ha.cf 监控配置文件
haresources 资源管理文件
authkeys 心跳线连接加密文件
1、同步两台节点的时间

[*]  rm -rf /etc/localtime
[*]  \cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[*]  yum install -y ntp
[*]  ntpdate -d cn.pool.ntp.org
2、配置ha.cf

[*]  vi /etc/ha.d/ha.cf

[*]  debugfile /var/log/ha-debug                                 #打开错误日志报告
[*]  keepalive 2                                                   #两秒检测一次心跳线连接
[*]  deadtime 10                                                   #10 秒测试不到主服务器心跳线为有问题出现
[*]  warntime 6                                                      #警告时间(最好在 2 ~ 10 之间)
[*]  initdead 120                                                    #初始化启动时 120 秒无连接视为正常,或指定heartbeat
[*]                                                                  #在启动时,需要等待120秒才去启动任何资源。
[*]  

[*]  udpport 694                                                   #用 udp 的 694 端口连接
[*]  ucast eth0 192.168.3.38                                    #单播方式连接(主从都写对方的 ip 进行连接)
[*]  nodemaster                                                   #声明主服(是主机名uname -n不是域名)
[*]  node   slave                                                   #声明备服(是主机名uname -n不是域名)
[*]  auto_failback on                                                #自动切换(主服恢复后可自动切换回来)
[*]  ping_group group1 192.168.3.37 192.168.3.38 (ping对方和自己的,如果都ping不通则自己挂了,释放自己的资源).
[*]  respawn hacluster /usr/lib/heartbeat/ipfail               #监控ipfail进程是否挂掉,如果挂掉就重启它
3、配置authkeys
vi /etc/ha.d/authkeys
   写入:

[*]  auth 1
[*]  1 crc
存盘退出,然后
#chmod 600 authkeys
4、创建检测脚本(本处如果没有个监测脚本hearbeat 无法启动)
vi /etc/init.d/test(内容如下)
#!/bin/bash
echo "" $>/dev/null
chmod 777 /etc/init.d/test
#vi /etc/ha.d/haresources(配置资源文件)
各主机这部分应完全相同。
master 192.168.3.55 test
指定 master 调用预先写好的一个测试脚本,系统附加一个虚拟IP
192.168.3.55 给eth0:0
这里如果master 宕机后slave 可以新分配IP
192.168.10.200
(以下配置结合DRBD:
配置haresources

[*]  vi /etc/ha.d/haresources   
写入:

[*]  masterIPaddr::192.168.3.55/24/eth0   drbddisk::r0 Filesystem::/dev/drbd1::/data::ext3
node1:master主机名
IPaddr::192.168.79.135/24/eth0:设置虚拟IP
drbddisk::r0:管理资源r0
Filesystem::/dev/drbd1::/data::ext3:执行mount与unmout操作
node2配置基本相同,不同的是ha.cf中的192.168.3.38改为192.168.3.37)。
5、serviceheartbeat start
  




页: [1]
查看完整版本: mysql热备+heartbeat集群