一、环境说明
mysqlserver1:mysql主10.65.1.174 5302
mysqlserver2:mysql从10.65.1.176 5302
mysql版本:5.7.17
mysql数据存储路径:/Data/mysql
mysql VIP:10.65.1.177:5302
Mysql通过VIP地址访问:mysql -udev -h 10.65.1.177 -p -P 5302
Mysql用户:
root/asionMysql
mysql/MysqlAsion
rep/asionrep
dev/T_mobile2016
TomCat server1:10.65.1.173:10086
TomCat server2:10.65.1.175:10086
TomCat VIP: 10.65.1.178:10086
NFS server:10.65.1.176 共享目录:/Data/ShareFile
操作系统版本:Centos 6.5
二、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.16
# 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 mysql@'10.65.1.%' identified by 'MysqlAsion';
/*更改密码方法:
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'@'10.65.1.%' identified by 'asionrep';
flush privileges;
2>在DB2的MySQL库中将DB1设为自己的主服务器
mysql> change master to
-> master_host='10.65.1.174',
-> 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: 10.65.1.174,Slave_IO_Running: Yes,Slave_SQL_Running: Yes这几个指标
这样DB1到DB2的主从复制已经完成,接下来配置从DB2到DB1的主从复制,这个过程和前面完全一样。首先在DB2的MySQL库中创建复制用户
5>在db2上创建同步用户:
grant replication slave on *.* to 'rep'@'10.65.1.%' identified by 'asionrep';
flush privileges;
6>在DB1的MySQL库中将DB2设为自己的主服务器
mysql> change master to
-> master_host='10.65.1.176',
-> 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 salve;
注意:Slave_IO_Running:connecting,不为yes状态,需要检查防火墙是否关闭,网络是否通
至此MySQL的主主复制配置完成!
四、配置MySQL高可用性:
1>安装keepalived:
yum -y install keeplived
chkconfig --add keepalived
chkconfig --level 35 keepalived on
2>修改DB1服务器keeplived配置文件:vim /etc/keepalived/keepalived.conf:
! 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 MySQL_HA
}
vrrp_instance HA_1 {
state BACKUP #DB1和DB2上均配置为BACKUP
interface eth0 #指定HA检测的网络接口
virtual_router_id 80 #虚拟路由标识,主备相同
priority 100 #定义优先级,slave设置90
advert_int 2 #设定master和slave之间同步检查的时间间隔
nopreempt #不抢占模式,只在优先级高的机器上设置即可,优先级低的机器可以不设置。
authentication {
auth_type PASS
auth_pass 23b14455cd
}
virtual_ipaddress {
10.65.1.177
}
}
virtual_server 10.65.1.177 5302 {
delay_loop 2
lb_algo wrr
lb_kind DR #LVS模式
persistence_timeout 60 #会话保持时间
protocol TCP #协议类型
real_server 10.65.1.174 5302{
weight 3
notify_down /usr/local/keepalived/mysql.sh
TCP_CHECK {
connect_timeout 10 #连接超时时间
nb_get_retry 3 #重连次数
delay_before_retry 3 #重连间隔时间
connect_port 5302 #健康检查端口
}
}
}
DB2服务器配置文件 /etc/keepalived/keepalived.conf
! 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 MySQL_HA
}
vrrp_instance HA_1 {
state BACKUP #DB1和DB2上均配置为BACKUP
interface eth0 #指定HA检测的网络接口
virtual_router_id 80 #虚拟路由标识,主备相同
priority 90 #定义优先级,slave设置90
advert_int 2 #设定master和slave之间同步检查的时间间隔
authentication {
auth_type PASS
auth_pass 23b14455cd
}
virtual_ipaddress {
10.65.1.177
}
}
virtual_server 10.65.1.177 5302 {
delay_loop 2
lb_algo wrr
lb_kind DR #LVS模式
persistence_timeout 60 #会话保持时间
protocol TCP #协议类型
real_server 10.65.1.176 5302{
weight 3
notify_down /usr/local/keepalived/mysql.sh
TCP_CHECK {
connect_timeout 10 #连接超时时间
nb_get_retry 3 #重连次数
delay_before_retry 3 #重连间隔时间
connect_port 5302 #健康检查端口
}
}
}
3>创建配置文件中需要使用的脚本:
/usr/local/keepalived/mysql.sh
#!/bin/bash
pkill keepalived
if [ `ps -C mysqld --no-header |wc -l` -eq 0 ];then
pkill keepalived
fi
fi
4>在DB1和DB2上分别运行VIP登录账号并赋权限:
grant all on *.* to 'dev'@'10.65.1.%' identified by 'T_mobile2016';
5>查看VIP地址对应的实际服务器:
在远程终端上连接VIP地址访问MySQL,然后在两台实际MySQL主机上执行show processlist;
1168 | mysql | 10.65.1.173:57447 | NULL | Sleep | 88 |有这种表示主机在此服务器上。
Mysql通过VIP地址访问:
连接方式:mysql -udev -h 10.65.1.177 -p -P 5302
至此 MySQL主主复制和MySQL高可用性完成。
五、keepalived 创建tomcat 高可用性
1>安装Java,关键设置环境变量:
export JAVA_HOME=/usr/java/jdk1.7.0_80
export JRE_HOME=/usr/java/jdk1.7.0_80
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$CLASSPATH
2>redis安装
tar -zxvf redis-3.2.8.tar.gz
cd redis-3.2.8
make
cd src
make install PREFIX=/usr/local/redis
cd /home/Asion/TmpFile/redis-3.2.8
mkdir -p /usr/local/redis/etc
mv redis.conf /usr/local/redis/etc/
cd /usr/local/redis/etc
3>编辑redis.conf
将daemonize属性改为yes(表明需要在后台运行) sed -n '/daemonize/p' redis.conf;
sed -i 's/daemonize no/daemonize yes/g' redis.conf
redis-server /usr/local/redis/etc/redis.conf
添加redis到服务中:
vim /etc/init.d/redis
#!/bin/sh
#
# redis Startup script for Redis Server
#
# chkconfig: - 80 12
# description: Redis is an open source, advanced key-value store.
#
# processname: redis-server
# config: /etc/redis.conf
# pidfile: /var/run/redis.pid
source /etc/init.d/functions
BIN="/usr/local/redis/bin"
CONFIG="/usr/local/redis/etc/redis.conf"
PIDFILE="/var/run/redis.pid"
### Read configuration
[ -r "$SYSCONFIG" ] && source "$SYSCONFIG"
RETVAL=0
prog="redis-server"
desc="Redis Server"
start() {
if [ -e $PIDFILE ];then
echo "$desc already running...."
exit 1
fi
echo -n $"Starting $desc: "
daemon $BIN/$prog $CONFIG
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
return $RETVAL
}
stop() {
echo -n $"Stop $desc: "
killproc $prog
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog $PIDFILE
return $RETVAL
}
restart() {
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
condrestart)
[ -e /var/lock/subsys/$prog ] && restart
RETVAL=$?
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $0 {start|stop|restart|condrestart|status}"
RETVAL=1
esac
exit $RETVAL
chmod +x /etc/init.d/redis
4>添加进程到服务:
service redis start
service redis stop
chkconfig --add redis
echo "export PATH="$PATH:/usr/local/redis/bin"" >> /etc/profile
配置下面的内核参数,否则Redis脚本在重启或停止redis时,将会报错,并且不能自动在
停止服务前同步数据到磁盘上/etc/sysctl.conf加上
#vim /etc/sysctl.conf
vm.overcommit_memory = 1
#sysctl -p
TomCat安装和配置略...
六、创建NFS文件共享服务:
以下步骤需要在两个节点上分别执行
1、NFS-Server服务器安装和配置:
1>安装nfs-utils rpcbind:yum install nfs-utils rpcbind
2>检查是否安装成功:rpm -qa|grep nfs
3>创建共享目录:mkdir -p /Data/ShareFile
4>修改配置文件:vi /etc/exports;添加一条记录:/Data/ShareFile *(rw,async,no_root_squash,no_subtree_check)
5>启动NFS服务(先启动rpcbind,再启动nfs):service rpcbind start;service nfs start
6>设置开机自动启动:chkconfig rpcbind on;chkconfig nfs on
2、NFS-Client 安装和配置:
1>查看远程文件共享是否正常:showmount -e 10.65.1.176
2>mount -t nfs 10.65.1.176:/Data/ShareFile /Data/SecurityBenchmark
如果需要开机启动请执行:vi /etc/fstab,添加:
10.65.1.176:/Data/ShareFile /Data/SecurityBenchmark nfs soft,intr,timeo=3,retry=30 0
至此NFS共享服务完成配置。
七、安装和配置Tomcat高可用
1、安装keepalived,过程如MySQL的keepalived安装和配置
2、配置高可用 /etc/keepalived/keepalived.conf:
Server1配置:
! 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 TomCat_HA
}
vrrp_instance Tom_2 {
state BACKUP
interface eth0 #指定HA检测的网络接口
virtual_router_id 85 #虚拟路由标识,主备相同
priority 90 #定义优先级,slave设置90
advert_int 3 #设定master和slave之间同步检查的时间间隔
nopreempt #不抢占模式,只在优先级高的机器上设置即可,优先级低的机器可以不设置。
authentication {
auth_type PASS
auth_pass 23b14455cd
}
virtual_ipaddress {
10.65.1.178
}
}
virtual_server 10.65.1.178 10086 {
delay_loop 2
lb_algo wrr
lb_kind DR #LVS模式
persistence_timeout 60 #会话保持时间
protocol TCP #协议类型
real_server 10.65.1.175 10086{
weight 3
notify_down /home/Asion/shell/TomCat.sh
TCP_CHECK {
connect_timeout 10 #连接超时时间
nb_get_retry 3 #重连次数
delay_before_retry 3 #重连间隔时间
connect_port 10086 #健康检查端口
}
}
}
监控脚本:/home/Asion/shell/TomCat.sh
#!/bin/bash
JAVA_PROCESS=`ps -C java --no-heading| wc -l`
if [ $JAVA_PROCESS -eq 0 ];then
echo "TomCat is stop"
sleep 2
if [ `ps -C java --no-heading| wc -l` -eq 0 ];then
/etc/init.d/keepalived stop
fi
fi
注意:如果存在多套集群,vrrp_instance virtual_router_id 这两个参数不能一样。
此次TomCat高可用性配置完成。