###############
yum install httpd mysql mysql-server
vim /etc/my.cnf
[mysqld]
server_id = 1
vim /etc/my.cnf
[mysqld]
server_id = 2 //大于1就可以
vim /etc/my.cnf
[mysqld]
server_id = 1
log-bin=binlog
log-bin-index=binlog.index //指定索引文件名字,默认就是,可以不写
vim /etc/my.cnf
[mysqld]
server_id = 2
relay_log = /var/lib/mysql/mysql-relay-bin
relay_log_index=/var/lib/mysql/mysql-relay-bin.index
mysql> GRANT replication slave ON *.* TO 'kyo'@'%' identified by '123';
Query OK, 0 rows affected (0.05 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>unlock tables; //此时可以解锁,两边数据可以不一致,因为知道了二进制日志写到的位置,salve向master要数据时,只要从不一致的位置起索要就可以了。
#mysql -u kyo -p123 -h 192.168.18.42 //这个用户只要数据同步权限
>exit
mysql> change master to master_host='192.168.18.42', master_port=3306, master_user='kyo', master_password='123', master_log_file='binlog.000003',master_log_pos=295; //bin-log与上边查看的要一致
mysql> start slave;
mysql> show slave status \G
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Master_log_File:binlog.000016 //当前读取的bin-log
Read_Master_log_Pos:880 //已经读取的位置。
Exec_Master_Log_Pos:880 //执行到的位置
(Read_Master_log_Pos:880 Exec_Master_Log_Pos:880如果不同就是延迟 问题)
假如master42数据库停止,
将Master数据库锁表
mysql> flush tables with read lock;
查看Master数据库二进制日志写到的位置
mysql> show master status;
将Slave数据库的停止复制线程
mysql> stop slave;
在Slave数据库上重新同步
change master to master_host='192.168.18.42', master_port=3306, master_user='kyo', master_password='123', master_log_file='binlog.000004',master_log_pos=98;
将Slave数据库的启用复制线程
mysql> start slave;
在Slave数据库上查看同步状态
mysql> show slave status \G
====================
slave23#mysql -e 'show slave status \G;'
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
#!/bin/bash
mysql -e "show slave status\G" > /tmp/haha.sql
IO=`cat /tmp/haha.sql | head -12 | tail -1 | cut -d ":" -f 2`
RUN=`cat /tmp/haha.sql | head -13 | tail -1 | cut -d ":" -f 2`
if [ $IO = YES ] && [ $RUN = YES ];then
echo OK
else
echo not ok
fi
binlogM=`cat /var/lib/mysql/master.info | head -3 | tail -1`
binlogN=`cat /var/lib/mysql/relay-log.info | head -4 | tail -1`
if [ $[$binlogM-$binlogN] -ge 0 ];then
echo OKAY
else
echo bu OK
fi