########主库Master:
vi /etc/my.cnf
[mysqld]
server-id=1
log-bin=mysql-bin ####日志文件
binlog_format=mixed ###日志格式化
binlog-do-db=test ###要求数据镜像的DB(选填可多行设定,默认同步全部DB)
binlog-ignore-db=mysql ###mysql库不用数据镜像(选填可多行设定,默认同步全部DB)
###########授权远程连接帐号start###############
/usr/local/mysql/bin/mysql -uroot -p
update mysql.user set host='%' where host='127.0.0.1'; ###远程连接DB
show master status; #######(选记)File:masterlog.000003 , Position:106
###########授权远程连接帐号end###############
#######从库Slave:
vi /etc/my.cnf
[mysqld]
server-id=2
master-host = 192.168.3.120
master-user = root
master-password = 1
master-port = 3306
或者直接在数据库操作:(推荐上面设置方式)
###数据镜像start#########
/usr/local/mysql/bin/mysql -uroot -p
change master to master_host='192.168.3.120,master_user='root', master_password='1',master_log_file='mysql-bin.000003',master_log_pos=106;
start slave
show slave status\G;
###数据镜像end#########
###注:主库或者从库重启,主库flush logs等不影响同步,除恶意删除文件和主库reset master后不能同步,其它操作不影响同步数据
若同步不成功试着删除从库所有同步有关数据,若再次同步成功会自动读取主库所有binlog日志进行数据更新,若主库中间reset master过则可能reset master之前的所有数据
/etc/init.d/mysql stop
rm -rf /usr/local/mysql/var/localhost-relay-bin.* master.info mysql-bin.* relay-log.info
/etc/init.d/mysql start