Simply put, is executed on a server on another server sql statement also repeatedly performed again, so long as the initial state of the two databases are the same, then they could have been synchronized.
Of course, this mysql replication and duplication are automatically implemented, we should be configured.
Figure above two servers, demonstrate the process from a master server (master) to synchronize the data from the server (slave) a.
This is a master - slave replication of example. Master - master replication with each other just put the above example in turn do it again. So we introduce the example principles.
For a mysql server, generally there are two threads to be responsible for replication and replicated. When the open copy.
1. As a master server Master, will own every time changes are recorded in the binary log Binarylog. (To read the log from the server will be responsible for, and then execute it again in their midst.)
2. As from the server Slave, will be landing on the master account to the master, read Binarylog master, and writes his own relay log Relaylog, then your sql thread is responsible for reading the relay logs, and perform again. Change here on the primary server to synchronize from the server.
Main from 1: 203.18.17.129 (vmnet2)
From master 2: 203.18.17.130 (vmnet2)
VIP: 203.18.17.131
This monitoring arrangement monitor
vim /opt/chk_mysql.sh
#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]
then
/etc/init.d/keepalived stop
else
echo "running..." >> /opt/keepalived-running-info.log
sleep 5000
be
[root@localhost ~]# yum -y install keepalived
设置开机启动
[root@localhost ~]# chkconfig keepalived on
Keepalived配置文件
[root@localhost ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id MySQL-HA
}
vrrp_script chk_mysql_port { #检测mysql服务是否在运行。
script "/opt/chk_mysql.sh" #这里通过脚本监测
interval 2 #脚本执行间隔,每2s检测一次
weight -5 #脚本结果导致的优先级变更,检测失败(脚本返回非0)则优先级 -5
fall 2 #检测连续2次失败才算确定是真失败。会用weight减少优先级(1-255之间)
rise 1 #检测1次成功就算成功。但不修改优先级
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
203.18.17.131
}
track_script {
chk_mysql_port
}
}
}
启动keepalived服务
[root@localhost ~]# service keepalived start
正在启动 keepalived: [确定]
[root@localhost ~]# ip a
[root@localhost ~]# yum -y install ipvsadm
[root@localhost ~]# yum -y install mysql mysql-devel mysql-server
启动mysql服务
[root@localhost ~]# service mysqld start
[root@localhost ~]# cp /usr/share/doc/mysql-server-5.1.71/my-medium.cnf /etc/my.cnf
cp:是否覆盖"/etc/my.cnf"? y
[root@localhost ~]# vim /etc/my.cnf
[client]
default-character-set=utf8
[mysqld]
default-character-set=utf8
设置数据库密码
[root@localhost ~]# mysqladmin -uroot password '123123'
建立时间同步环境,在主服务器上安装配置NTP时间同步服务器
[root@localhost ~]# yum -y install ntp
备份文件
[root@localhost ~]# cp -p /etc/ntp.conf /etc/ntp.conf.origin
[root@localhost ~]# vim /etc/ntp.conf
手动添加此两行内容
56 server 127.127.1.0
57 fudge 127.127.1.0 startum 8
开启ntp服务
[root@localhost ~]# /etc/init.d/ntpd start
正在启动 ntpd: [确定]
开机启动ntp服务
[root@localhost ~]# chkconfig ntpd on
在从服务器上同步时间
[root@localhost ~]# yum -y install ntpdate
[root@localhost ~]# ntpdate 203.18.17.129
9 Aug 23:31:03 ntpdate[52482]: no server suitable for synchronization found
注意:主从mysql尽量保证版本相同
配置mysql 主从1服务器
[root@localhost ~]# vim /etc/my.cnf
56 log-bin=mysql-bin
57 log-slave-updates=true #手动添加,开启从日志
65 server-id =11
参数说明:
l log-bin 开启二进制日志
l server-id 设置server-id
l character-set-server 修改字符集为utf8
l binlog-do-db=mydatabase 只同步哪些数据库,除此之外,其他不同步
重启mysql服务
[root@localhost ~]# /etc/init.d/mysqld restart
停止 mysqld: [确定]
正在启动 mysqld: [确定]
创建数据库同步用户,并授予相应的权限
[root@localhost ~]# mysql -uroot -p123123
mysql> grant replication slave on *.* to 'myslave'@'203.18.17.130'identified by '123123';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
查看master状态
mysql> show master status;
执行同步SQL语句,参照MASTER配置:
mysql> change master to master_host='203.18.17.129',master_user='myslave',master_password='123123',master_log_file='mysql-bin.000004',master_log_pos=345;
Query OK, 0 rows affected (0.03 sec)
注意:主从2上已经创建数据库同步用户,并授予相应的权限在执行同步SQL语句。
启动slave同步进程
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
查看slave状态
mysql> show slave status \G;
监测监本的配置
vim /opt/chk_mysql.sh
#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]
then
/etc/init.d/keepalived stop
else
echo "running..." >> /opt/keepalived-running-info.log
sleep 5000
fi
[root@localhost ~]# yum -y install keepalived
设置开机启动
[root@localhost ~]# chkconfig keepalived on
Keepalived配置文件
[root@localhost ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id MySQL-HA
}
vrrp_script chk_mysql_port { #检测mysql服务是否在运行。
script "/opt/chk_mysql.sh" #这里通过脚本监测
interval 2 #脚本执行间隔,每2s检测一次
weight -5 #脚本结果导致的优先级变更,检测失败(脚本返回非0)则优先级 -5
fall 2 #检测连续2次失败才算确定是真失败。会用weight减少优先级(1-255之间)
rise 1 #检测1次成功就算成功。但不修改优先级
}
vrrp_instance VI_1 {
state MASTER
interface eth0
lvs_sync_daemon_inteface eth0
virtual_router_id 51
mcast_src_ip 203.18.17.130
priority 90
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
203.18.17.131
}
track_script {
chk_mysql_port
}
}
}
启动keepalived服务
[root@localhost ~]# service keepalived start
正在启动 keepalived: [确定]
[root@localhost ~]# ip a
[root@localhost ~]# yum -y install ipvsadm
[root@localhost ~]# yum -y install mysql mysql-devel mysql-sever
启动mysql服务
[root@localhost ~]# service mysqld start
正在启动 mysqld: [确定]
[root@localhost ~]# cp /usr/share/doc/mysql-server-5.1.71/my-medium.cnf /etc/my.cnf
cp:是否覆盖"/etc/my.cnf"? y
[root@localhost ~]# vim /etc/my.cnf
[client]
default-character-set=utf8
[mysqld]
default-character-set=utf8
设置数据库密码
[root@localhost ~]# mysqladmin -uroot password '123123'
配置服务器
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
server-id =12
log-bin=mysql-bin
relay-log=relay-bin
relay-log-index=slave-relay-bin.index
重启mysql服务
[root@localhost ~]# /etc/init.d/mysqld restart
停止 mysqld: [确定]
正在启动 mysqld: [确定]
[root@localhost ~]# mysql -uroot -p123123
创建数据库同步用户,并授予相应的权限
mysql> grant replication slave, replication client on *.* to 'repl'@'203.18.17.129' identified by '123123';
Query OK, 0 rows affected (0.00 sec)
刷新授权表信息
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
查看master状态
mysql> show master status;
执行同步SQL语句,参照MASTER配置:
mysql> change master to master_host='203.18.17.129',master_user='myslave',master_password='123123',master_log_file='mysql-bin.000004',master_log_pos=345;
Query OK, 0 rows affected (0.03 sec)
注意:主从1上已经创建数据库同步用户,并授予相应的权限在执行同步SQL语句。
启动slave同步进程
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
查看slave状态
mysql> show slave status \G;
Create a new database, create a new library auth test results have been backed up two
Create a new database dada 129 inside the database, which also appeared in the 130 database backup success explained.
203.18.17.129 server
Close keepalived service caused by malfunction
[root@localhost ~]# service keepalived stop
Look at 203.18.17.130 server VIP has not come
Success VIP address are automatically transferred
related articles:
Cluster LVS-DR + keepalived VIP address availability cluster bis
nginx + Keepalived hot standby HA cluster is automatically switched
nginx + keepalived hot standby HA cluster load balancing