因项目要求需要使用Haproxy搭建MySQL主备高可用,主主文件复制和Tomcat负载均衡的系统,原理不在重复,网上很多案例。
一、环境说明
mysqlserver1:mysql主 172.25.10.129 5302
mysqlserver2:mysql从 172.25.10.130 5302
mysql版本:5.7.17
mysql数据存储路径:/Data/mysql
mysql VIP:172.25.10.131:15302
Mysql通过VIP地址访问:mysql -udev -h 172.25.10.131 -p -P 5302
Mysql用户:
root/asionMysql
rep/asionrep
dev/T_mobile2016
TomCat server1:172.25.10.129:18886
TomCat server2:172.25.10.130:18886
TomCat VIP: 172.25.10.131:18886
操作系统版本:Centos 6.8
http://blog.chinaunix.net/uid-25266990-id-3989321.html
二、mysql安装
1、环境和软件准备
下载mysql源码包
注意:官方现在提供mysql-5.7.17.tar.gz和mysql-boost-5.7.17.tar.gz两个包,建议下载后者,因为
该版本包含了mysql-5.7需要的boost库文件,不用再单独下载boots库。
[root@mysqlserver1 ~]# wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.17.tar.gz
2、安装MySQL,以下步骤包括安装cmake,MySQL,创建用户,修改密码等操作需要在db1和db2上执行
1>创建数据库用户
groupadd -g 330 mysql
useradd -u 330 -g mysql -M -s /sbin/nologin mysql #非登录用户
2>建立mysql安装目录及数据存放目录
mkdir /usr/local/mysql #在本机安装可不用创建该目录,make install时会自动创建
mkdir -p /Data/mysql
chown mysql:mysql -R /Data/mysql/
3>解决依赖关系
yum -y install gcc gcc-c++ ncurses ncurses-devel cmake bison git openssl-devel
注意:需要下载CMake 2.8.2上的版本,不然MySQL会编译出错,下载地址:http://distfiles.macports.org/cmake
tar -zxvf cmake-3.2.3.tar.gz
cd cmake-3.2.3
./bootstrap
gmake
make install
注意:如果报错:CMake Error: Could not find CMAKE_ROOT !!!,则是编译安装后参数设置有问题
在/etc/profile中设置 export CMAKE_ROOT=/usr/local/bin/
4>编译安装Mysql
tar -zxvf mysql-boost-5.7.17.tar.gz
# cd mysql-5.7.17
# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/Data/mysql \
-DMYSQL_TCP_PORT=5302 \
-DSYSCONFDIR=/etc \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DWITH_LIBWRAP=0 \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DWITH_SSL=system \
-DWITH_ZLIB=system \
-DWITH_BOOST=./boost/boost_1_59_0 \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=all \
-DENABLED_LOCAL_INFILE=1 \
-DENABLE_DTRACE=0 \
-DWITH_EMBEDDED_SERVER=1
make
make install
5>修改Mysql配置文件:
[root@mysqlserver1 ~]#vim /etc/my.cnf #创建配置文件
[client]
port = 5302
socket = /tmp/mysql.sock
default-character-set = utf8
[mysqld]
port = 5302
socket = /tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /Data/mysql
pid-file = /Data/mysql/mysql.pid
user = mysql
bind-address = 0.0.0.0
server-id = 1
log-bin=mysql-bin
relay-log=mysql-relay-bin
replicate-wild-ignore-table=mysql.%
replicate-wild-ignore-table=test.%
replicate-wild-ignore-table=information_schema.%
init-connect = 'SET NAMES utf8'
character-set-server = utf8
skip-name-resolve
#skip-networking
back_log = 300
max_connections = 1000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 4M
binlog_cache_size = 1M
max_heap_table_size = 8M
tmp_table_size = 16M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
sort_buffer_size = 8M
join_buffer_size = 8M
key_buffer_size = 4M
thread_cache_size = 8
query_cache_type = 0
query_cache_size = 0
#query_cache_limit = 2M
ft_min_word_len = 4
log_bin = mysql-bin
#binlog_format = mixed
expire_logs_days = 30
log_error = /Data/mysql/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /Data/mysql/mysql-slow.log
performance_schema = 0
explicit_defaults_for_timestamp
#lower_case_table_names = 1
skip-external-locking
default_storage_engine = InnoDB
#default-storage-engine = MyISAM
innodb_file_per_table = 1
innodb_open_files = 500
innodb_buffer_pool_size = 64M
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 2M
innodb_log_file_size = 32M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
bulk_insert_buffer_size = 8M
myisam_sort_buffer_size = 8M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
interactive_timeout = 28800
wait_timeout = 28800
[mysqldump]
quick
max_allowed_packet = 16M
[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M
6>数据库初始化
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/Data/mysql
注:
之前版本mysql_install_db是在mysql_basedir/script下,5.7放在了mysql_install_db/bin目录下,且已被废弃
“–initialize”会生成一个随机密码(~/.mysql_secret),而”–initialize-insecure”不会生成密码
–datadir目标目录下不能有数据文件
7>添加系统启动脚本
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
chmod +x /etc/init.d/mysql
chkconfig --add mysql
chkconfig mysql on
启动数据库:service mysql start
7>添加环境
echo 'PATH=/usr/local/mysql/bin:$PATH export PATH' >>/etc/profile
source /etc/profile
8>设置用户密码
# mysql
mysql> select User,Host,authentication_string from mysql.user;
+-----------+-----------+-------------------------------------------+
| User | Host | authentication_string |
+-----------+-----------+-------------------------------------------+
| root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
+-----------+-----------+-------------------------------------------+
mysql> set password for 'root'@'localhost'=password('asionMysql');
mysql> grant select,insert,delete,update,create,drop,alter,process,create view,show view,create temporary tables
on*.* to dev@'172.25.10.%' identified by 'T_mobile2016';
/*更改密码方法:
mysql> update mysql.user set authentication_string=password('123qwe') where user='root' and Host = 'localhost';
*特别提醒注意的一点是,新版的mysql数据库下的user表中已经没有Password字段了
而是将加密后的用户密码存储于authentication_string字段
mysql> alter user 'root'@'localhost' identified by '123';
这样也可以改密码:
mysql> set password for 'root'@'localhost'=password('123');
*/
mysql> flush privileges;
mysql> quit;
修改完毕。重启
[root@localhost ~]# service mysql restart
三、配置MySQL文件主主复制
1>创建主从同步用户:
在db1上执行:
grant replication slave on *.* to 'rep'@'172.25.10.%' identified by 'asionrep';
flush privileges;
2>在DB2的MySQL库中将DB1设为自己的主服务器
mysql> change master to
-> master_host='172.25.10.129',
-> master_user='rep',
-> master_password='asionrep',
-> master_log_file='mysql-bin.000005',
-> master_log_pos=1132;
注意:master_log_file和master_log_pos这两个选项的值要对应在DB1上通过SQL语句"show master status" 查询到的值。
3>在DB2上启动Slave服务
start slave;
4>在DB2上查看Slave服务状态
mysql> show slave status\G;
主要参数:Master_Host: 172.25.10.129,Slave_IO_Running: Yes,Slave_SQL_Running: Yes这几个指标
这样DB1到DB2的主从复制已经完成,接下来配置从DB2到DB1的主从复制,这个过程和前面完全一样。首先在DB2的MySQL库中创建复制用户
5>在db2上创建同步用户:
grant replication slave on *.* to 'rep'@'172.25.10.%' identified by 'asionrep';
flush privileges;
6>在DB1的MySQL库中将DB2设为自己的主服务器
mysql> change master to
-> master_host='172.25.10.130',
-> master_user='rep',
-> master_password='asionrep',
-> master_log_file='mysql-bin.000004',
-> master_log_pos=446;
7>在DB1上启动Slave服务
mysql> start slave;
mysql> show slave status\G;
停止slave:stop slave;
重新设置slave: reset slave;
注意:Slave_IO_Running:connecting,不为yes状态,需要检查防火墙是否关闭,网络是否通
至此MySQL的主主从复制配置完成!
四、haproxy安装配置
1、安装配置过程,(两台Haproxy的安装配置步骤一样)
haproxy下载地址:http://haproxy.1wt.eu/download/1.7/src/haproxy-1.7.3.tar.gz
# tar zxvf haproxy-1.7.3.tar.gz
# cd haproxy-1.7.3
# make TARGET=linux26 ARCH=x86_64
# TARGET是指定内核版本,ARCH指定CPU架构,我使用的是64bit系统
2、安装完毕后,创建配置文件和启动文件。
# cp /usr/local/sbin/haproxy /usr/sbin
# cp /root/soft/haproxy-1.7.3/examples/haproxy.init /etc/init.d/haproxy
# chmod 755 /etc/init.d/haproxy
# useradd -r haproxy
# mkdir /etc/haproxy
# cd /etc/haproxy/
3、编辑配置文件(两台Haproxy配置文件相同)
#vim /etc/haproxy/haproxy.cfg
global
#设置日志
log 127.0.0.1 local3 info
#chroot /usr/local/haproxy
#用户与用户组
user haproxy
group haproxy
#守护进程启动
daemon
#最大连接数
maxconn 4000
#默认配置
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000
listen stats
mode http
log global
option httplog #日志格式
bind 0.0.0.0:8881 #Haproxy本身监听的地址和套接字端口
stats enable #开启状态监控
stats refresh 300s #统计页面自动刷新时间
#stats hide-version
stats uri /stats #监听地址的访问目录
stats realm Haproxy\ Statistics
stats auth admin:admin #监控后台的账号
stats admin if TRUE
listen tomcat
bind *:18886
mode tcp
balance roundrobin
server app_1 172.25.10.129:8886 weight 1 check inter 3000ms rise 2 fall 3 maxconn 3000
server app_2 172.25.10.130:8886 weight 1 check inter 3000ms rise 2 fall 3 maxconn 3000
listen mysql
bind *:15302
mode tcp
balance roundrobin
server mysql_1 172.25.10.129:5302 weight 1 check inter 3000ms rise 2 fall 3 maxconn 3000
server mysql_2 172.25.10.130:5302 weight 1 check inter 3000ms rise 2 fall 3 maxconn 3000 backup
4、启动Haproxy服务,查看状态。
#service haproxy start
查看方法:http://ip/stats
五、安装配置keepalived(两台keepalived安装步骤一样)
#cd /root/software
#wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.33.tar.gz
#tar -zxvf pcre-8.33.tar.gz
#cd pcre-8.33
#./configure
#make && make install
#wget http://nginx.org/download/nginx-1.2.9.tar.gz
#tar -zxvf nginx-1.2.9.tar.gz
#cd nginx-1.2.9
#./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module --with-http_gzip_static_module
#make && make install
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived.example.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id Haproxy_HA
}
vrrp_instance HA_1 {
state BACKUP #DB1和DB2上均配置为BACKUP
interface eth0 #指定HA检测的网络接口
virtual_router_id 80 #虚拟路由标识,主备相同
priority 80 #定义优先级,slave设置90
unicast_src_ip 172.25.10.130##(本地IP地址),解决心跳广播被限制的情况
unicast_peer {
172.25.10.129##(对端IP地址)此地址一定不能忘记
}
advert_int 2 #设定master和slave之间同步检查的时间间隔
nopreempt #不抢占模式,只在优先级高的机器上设置即可,优先级低的机器可以不设置。
authentication {
auth_type PASS
auth_pass 23b14455cd
}
virtual_ipaddress {
172.25.10.131
}
}
virtual_server 172.25.10.131 8881 {
delay_loop 2
lb_algo wrr
lb_kind DR #LVS模式
persistence_timeout 60 #会话保持时间
protocol TCP #协议类型
real_server 172.25.10.130 8881{
weight 3
notify_down /usr/local/keepalived/Haproxy.sh
TCP_CHECK {
connect_timeout 10 #连接超时时间
nb_get_retry 3 #重连次数
delay_before_retry 3 #重连间隔时间
connect_port 8881 #健康检查端口
}
}
}
/usr/local/keepalived/Haproxy.sh
#!/bin/bash
pkill keepalived
if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then
pkill keepalived
fi
注意:因为keepalive使用心跳地址广播的方式获取对方状态信息,如果网络对广播消息做了限制应该使用:
unicast_src_ip 172.25.10.129##(本地IP地址)
unicast_peer {
172.25.10.130 ##(对端IP地址)此地址一定不能忘记
}