创建mysql需要的文件夹:
mkdir /usr/local/mysql/{etc,logs,data}
mkdir /usr/local/mysql/logs/{bin-log,relay-log,slow-log,error-log}
chown mysql:mysql /usr/local/mysql
创建一个mysql的程序运行用户:
sudo useradd –s /sbin/nologin mysql
编译安装:
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql/ -DMYSQL_DATADIR=/usr/local/mysql/data -DMYSQL_USER=mysql
make && make install
备注:如果编译出错,可以先find / -name CMakeCache.txt,执行删除,然后再根据报错排除错误,执行重新编译
赋予文件夹权限:
chown –R mysql:mysql /usr/local/mysql
配置文件的配置:
cp support-files/my-innodb-heavy-4G.cnf /etc/my.cnf
初始化mysql: /usr/local/mysql/scripts/mysql_install_db --user=mysql --defaults-file=/usr/local/mysql/etc/my.cnf --basedir=/usr/local/mysql/basedir --datadir=/usr/local/mysql/data
启动mysql: usr/local/mysql/basedir/bin/mysqld_safe --user=mysql
停止:/usr/local/mysql/basedir/bin/mysqladmin -uroot –p
(禁止通过kill的方式停止进程)
重写了配置文件后,启动报错:
查看错误日志排查错误/usr/local/mysql/logs/error-log/
查报错,百度经验需要重新初始化,初始化后解决问题
Mysql创建用户修改权限:
set password for root=password(‘12345678’)
grant all privileges on . to ‘sas’@’%’ identified by ‘12345678’;
delete from mysql.user where password=””;
主root:密码:12345678
从root:密码:12345678
创建一个可供主从复制的数据库用户:
grant replication slave on . to ‘slave‘@‘%‘ identified by ‘12345678‘;
查看二进制日志,做主主的复制:
show master status;
stop slave;
change master to master_host=‘192.168.102.26‘,master_user=‘slave‘,master_password= ‘12345678‘,master_log_file=‘binlog.000001‘,master_port=3306,master_log_pos=244;
start slave;
show slave status;
做成系统服务:
ln -s /usr/local/mysql/bin/ /usr/bin
ln -s /usr/local/mysql/lib/ /usr/lib
cp support-files/mysql.server /etc/init.d/mysqld -p
chmod 755 /etc/init.d/mysqld
chown mysql.mysql -R /usr/local/mysql
chkconfig mysqld on
安装keepalived:
tar –xf keepalived-1.2.7.tar.gz
./configure --prefix=/usr/local/keepalived
make
make install
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
mkdir -pv /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
修改配置文件:
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@linux.com
}
notification_email_from keepalived@localhost
smtp_server mail.163.com
smtp_connect_timeout 30
router_id MySQL-ha
}
vrrp_instance VI_1 {
state BACKUP #两台配置此处均是BACKUP
interface eth0 #网卡,可使用ifconfig查看
virtual_router_id 51
priority 100 #优先级,另一台改为90
advert_int 1
nopreempt #不抢占,只在优先级高的机器上设置即可,优先级低的机器不设置
authentication {
auth_type PASS
auth_pass keepalivedpass
}
virtual_ipaddress {
192.168.1.200 #虚拟ip
}
}
virtual_server 192.168.1.200 3306 {
delay_loop 2 #每个2秒检查一次real_server状态
lb_algo wrr #LVS算法
lb_kind DR #LVS模式
persistence_timeout 60 #会话保持时间
protocol TCP
real_server 192.168.1.104 3306 { #真实ip
weight 3
notify_down /etc/keepalived/check_keepalived #检测到服务down后执行的脚本
TCP_CHECK {
connect_timeout 10 #连接超时时间
nb_get_retry 3 #重连次数
delay_before_retry 3 #重连间隔时间
connect_port 3306 #健康检查端口
}
}
}
编辑服务down后执行的脚本:
Vim /etc/keepalived/check_keepalived
#!/bin/bash
MYSQL=/usr/local/mysql/bin/mysql
MYSQL_HOST=192.168.100.102 #根据实际填写
MYSQL_USER=root
MYSQL_PASSWORD=123456
CHECK_TIME=3
#mysql is working MYSQL_OK is 1 , mysql down MYSQL_OK is 0
MYSQL_OK=1
function check_mysql_helth (){
$MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p${MYSQL_PASSWORD} -e "show status;" >/dev/null 2>&1
if [ $? = 0 ];then
MYSQL_OK=1
echo mysql ok
else
MYSQL_OK=0
echo mysql no ok
fi
return $MYSQL_OK
}
while [ $CHECK_TIME -ne 0 ]
do
let "CHECK_TIME -= 1"
check_mysql_helth
if [ $MYSQL_OK = 1 ]; then
CHECK_TIME=0
exit 0
fi
if [ $MYSQL_OK -eq 0 ] && [ $CHECK_TIME -eq 0 ]
then
/etc/init.d/keepalived stop
exit 1
fi
sleep 1
done
启动keepalived:
service keepalived restart
查询VIP:
Ip addr
防火墙放开vrrp协议
防火墙开放
#iptables?-I?INPUT?-i?eth0?-d?224.0.0.0/8?-p?vrrp?-j?ACCEPT
#iptables?-I?OUTPUT?-o?eth0?–d 224.0.0.0/8?-p?vrrp?-j?ACCEPT
iptables -A INPUT -i eth0 -p vrrp -s 192.168.1.107 -j ACCEPT (对方ip地址)
或
iptables -A INPUT -p vrrp -j ACCEPT
验证:
1、在主库执行增删改操作,在从库观察状态是否同步
2、在从库执行增删改操作,在主库观察状态是否同步
3、停主的keepalived,查看从的ip,是否绑定成功
问题总结:
1、权限问题:
现象:初始化没有报成功,没有错误日志
分析:可能是由于无法写入而造成的异常
方案:检查用户权限、文件夹权限(包括一级目录的权限)
2、同步失败:
现象:查看从状态,显示的是ok
分析:可能是由于初始同步有问题,检查两库的状态
方案:重新同步主从数据库,若还是无法同步,可能是由于tar包损坏,查看MD5值,重新下载安装
3、5.5和5.6区别:
两个版本设置字符集不同,5.6版本增加了uuid的概念
4、keepalived绑定VIP失败:
方案:检查配置文件的问题
1)是否有多余的部分
2)检查防火墙的配置,放行vrrp协议
3)将检查健康状况脚本放在邮件配置下方
原文地址:http://blog.51cto.com/11962757/2084073