####环境准备
安装mysql(centos7 默认没有mysql源)
yum clearup
yum update
yum install vim libaio net-tools
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum install mysql-server python-mysqldb(Python-mysqldb未安装)
启动修改密码
update user set password=password(‘123456‘) where user=‘root‘;
###给slave赋权
grant replication slave on *.* to '用户名'@'192.168.1.153' identified by '密码';
###锁表,找到master当前的binlog点以及position位置
flush tables with read lock;
show master status; ###记录日志制文件以及position点
unlock tables;
###从库上操作,先停掉salve服务,然后配置连上master的配置
stop slave
####利用刚在主库上面创建的账号密码以及记录的二进制日志文件和position点,连接到主库的同步位置
change master to master_host='192.168.178.2',master_user='xldroot',master_password='123123',master_log_file='mysql-bin.000003', master_log_pos=790;
start slave
show slave status\G
#######一下两项为yes即OK,也可以自己去创建数据库,看下是否有同步
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
###上面是单方向主从配置,主主的话反着来一次即可(之前的从上创建连接的账号和密码以及记录节点位置,然后在之前的主连接到从),不过配置里面还是有区别
[client]
character-set-server = utf8 #####客户端默认连接字集集,若编译安装时已指定则不用填写
[mysqld]
datadir=/data/mysql/data
character-set-server = utf8
socket=/var/lib/mysql/mysql.sock
port = 3306
server_id = 1
log-bin= mysql-bin
binlog_format = mixed
bind = 0.0.0.0
read-only=0
#binlog-do-db=test
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
auto-increment-offset=1
auto-increment-increment=2
#主从复制
##replicate-do-db=test ###开启的话只同步这些库
replicate-ignore-db=mysql ###忽略同步这些库
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
relay_log=mysql-relay-bin
log-slave-updates=on
###binlog日志
log_bin = /data/log/mysql/mysql-bin.log
relay_log = /data/log/mysql/mysql-relay-bin
log_slave_updates = 1
expire_logs_days = 7
###慢查询
slow_query_log = ON
slow_query_log_file = /data/log/mysql/mysql-slow.log
#log_slow_queries = /data/log/mysql/mysql-slow.log
long_query_time = 2
key_buffer = 16M
max_allowed_packet = 16M
thread_stack = 192K
thread_cache_size = 50
[mysqld_safe]
log-error=/data/log/mysql/mariadb.log
pid-file=/data/log/mysql/mariadb.pid
[client]
character-set-server = utf8 #####客户端默认连接字集集,若编译安装时已指定则不用填写
[mysqld]
socket=/var/lib/mysql/mysql.sock
character-set-server = utf8
datadir =/data/mysql/data
port = 3306
server_id = 2
log-bin= mysql-bin
binlog_format = mixed
read-only=0
#replicate-do-db=test
replicate-ignore-db=mysql
replicate-ignore-db=information_schema
replicate-ignore-db=performance_schema
relay_log=mysql-relay-bin
log-slave-updates=on
#
##
##binlog-do-db=test
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
auto-increment-offset=2
auto-increment-increment=2
log_bin = /data/log/mysql/mysql-bin.log
relay_log = /data/log/mysql/mysql-relay-bin
log_slave_updates = 1
expire_logs_days = 7
slow_query_log = ON
slow_query_log_file = /data/log/mysql/mysql-slow.log
#log_slow_queries = /data/log/mysql/mysql-slow.log
long_query_time = 2
key_buffer = 16M
max_allowed_packet = 16M
thread_stack = 192K
thread_cache_size = 50
[mysqld_safe]
log-error=/data/log/mysql/mariadb.log
pid-file=/data/log/mysql/mariadb.pid
#####因为这个主从的跑配置没有在my.cnf里面,重新启动会加载数据目录下面的配置
/var/lib/mysql/master.info ###根据你自己的数据目录而定,记录了服务器地址,同步节点位置
####keepalived实现mysql主主自动切换
yum -y install keepalived
###加入开启启动
chkconfig keepalived on 2345
chkconfig mysqld on 2345
###查验
centos7
systemctl list-unit-files |egrep 'mysql|keepalived'
####keepalived配置
global_defs {
notification_email {
#failover@firewall.loc
root@localhost ###告警通知,现在作用不大,一般都可以通过其他监控软件来做
}
notification_email_from root@localhost ###发件人
smtp_server localhost ###SMTP服务器
smtp_connect_timeout 30
router_id LVS_DEVEL ###可一致,也可不一致
}
vrrp_instance VI_1 { ###相当于Cisco的vrrp协议,虚拟一个组,如果有多个时,定义多个组即可
state MASTER ###目前的主,只是一种称呼,实际由优先级决定,越高的是老大
interface ens4 ###绑定的接口
virtual_router_id 51 ###组id,必须一致,表示都加入同一个组
priority 100 ###优先级
advert_int 1 ###通告时间,也就是加入这个组的所有机器之前的通告
authentication { ###定义认证,内容必须一致
auth_type PASS
auth_pass baowang
}
virtual_ipaddress { ###这就是虚拟出来对外访问的地址
10.68.3.160
#192.168.200.17
}
}
virtual_server 10.68.3.160 3306 { ###细化,虚拟到哪些端口
delay_loop 6
lb_algo wrr ###带有权重的轮询
lb_kind NAT
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 10.68.3.145 3306 { ###绑定到的真实地址和端口
weight 5 ###权重下降值
notify_up /etc/keepalived/keepalived_start.sh ###检测整体服务器环境正常之后的操作
notify_down /etc/keepalived/keepalived_shutdown.sh ###定义脚本检测,down之后的操作
TCP_CHECK { ###定义一些基本的校验项
connect_timeout 10 ###超时时间
nb_get_retry 5 ###重连次数
delay_before_retry 3 ###重连间隔
connect_port 3306
}
}
}
}
###额外的,可以定义全脚本
vrrp_script chk_nginx {
script "/home/check_nginx.sh"
interval 2
weight 2
}
###局部模块,位置虚拟组vrrp_instance VI_1里面,用于调用全局的脚本
track_script {
chk_nginx #检测脚本
}
####定义日志输出:默认是在/var/log/message,这个日志里面混合太多,查看不太方便
1.修改etc/sysconfig/keepalived文件
把KEEPALIVED_OPTIONS="-D"
修改为KEEPALIVED_OPTIONS="-D -d -S 0"
2.设置syslog,修改/etc/syslog.conf,添加内容如下(centos7是/etc/rsyslog.conf)
# keepalived -S 0
local0.* /data/log/keepalive/keepalived.log
3.###重启日志服务和keepalive服务,在定义的路径下就可以看到需要的日志
systemctl restart rsyslog or systemctl restart syslog
service keepalived restart
4.####mysql得创建允许keepalive虚拟IP连接的账号