MySQL主从操作

主数据库master配置文件my.cnf修改
#修改master的日志文件存放地址和名称
log-bin = /data/data0/mysql/3306/binlog/binlog
#日志格式,建议mixed
binlog_format = mixed
#主数据库端ID号
server-id = 1
#不同步哪些数据库
binlog-ignore-db = mysql
binlog-ignore-db = test
binlog-ignore-db = information_schema
binlog-ignore-db = performance_schema
#只同步哪些数据库,除此之外其他不同步
binlog-do-db = oa
#日志保留时间
expire_logs_days = 30
#控制binlog文件的更新频率,每次执行n次事务
#保存一次这个参数性能消耗很大,但可减少mysql崩溃造成损失
sync_binlog = 1

#查看log位置
mysqlbinlog log-file | mysql -h


#创建用于同步的账户
#创建slave账户slave,密码123456
grant replication slave,select,reload,super on *.* to 'slave'@'%' identified by '123456';
#更新数据库权限
flush privileges;
#创建一个oa数据库
create database oa default character set utf8;

#重新启动mysql(使上面更改的参数生效)
/data/data0/mysql/3306/mysql stop
/data/data0/mysql/3306/mysql start

#启动后,主库执行锁表
FLUSH TABLES WITH READ LOCK;
SHOW MASTER STATUS \G
#记录下这个位置

#把主库的lunar数据库导出
#注:一定要打开另外一个窗口来操作
/usr/local/mysql/bin/mysqldump -uroot -proot --default-character-set=utf8 --databases oa > /root/oa2015-08-24-1442.sql

#解锁
UNLOCK TABLES;


#####################################################
#从slave修改
#修改配置文件my.cnf
server-id = 2
#下面几个数据库不复制
binlog-ignore-db = mysql
binlog-ignore-db = test
binlog-ignore-db = information_schema
binlog-ignore-db = performance_schema
#只接受复制oa数据库内容
binlog-do-db = oa
从服务器可能要升级成master,或者备份
log-bin = /data/data0/mysql/3306/binlog/binlog
#中继日志位置
relay-log-index = /data/data0/mysql/3306/relaylog/relaylog
relay-log-info-file = /data/data0/mysql/3306/relaylog/relaylog
relay-log = /data/data0/mysql/3306/relaylog/relaylog

#重启mysql服务器
/data/data0/mysql/3306/mysql stop
/data/data0/mysql/3306/mysql start

#导入oa.sql
#还原数据库
1、set names utf8
2、source /root/oa2015-08-24-1442.sql

#执行同步命令
change master to master_host='192.168.10.1' , master_user='slave', master_password='123456',master_log_file='binlog.000010', master_log_pos=107

#启动复制机制
start slave;
#查看状态
show slave status \G

你可能感兴趣的:(MySQL主从操作)