创建MySQL主实例
创建MySQL新实例,可以参考上篇博文: MySQL初始化与用户配置 www.cnblogs.com/qiusuo/p/9451717.html
bin-log配置
# log bin for master server-id = 1 # 必须配置server-id,并且与其他相关实例不同 binlog-do-db = dbname # 针对那些database有log-bin,对于多个可以分行写 binlog-ignore-db = mysql # 忽略那些database的log-bin,对于多个可以分行写 binlog-ignore-db = test log-bin = mysql-bin # log-bin的存储位置,可以是相对于datadir,也可以是绝对路径 log-bin-index = mysql-bin.index binlog-format = mixed # binlog的格式,mixed,statement,row,默认格式是statement expire-logs-days = 10 # binlog的保留天数,为0则永久保留 binlog-cache-size = 1M max-binlog-size = 1G
replication用户配置
为用户增加replication权限
grant replication slave on *.* to 'user'@'host' identified by 'password';
查看主实例状态
show master status;
show master status G;
创建MySQL从实例
拷贝主实例的数据
方法1:拷贝主实例的datadir,作为从实例的数据文件夹。
方法2:dump主实例的数据,导入到从实例中。
方法3:建立主从实例后,锁主库,导出主库,解锁主库。导入从库,开启slave同步。
保证从实例的数据状态和主实例未产生binlog前的状态一致。
replicate配置
# replicate from slave
server-id = 2
master-host = master-hostname
master-port = 3306
master-user = user
master-password = password
replicate-do-db = dbname
replicate-ignore-db = test
replicate-ignore-db = mysql
relay-log-purge = 1 # 自动删除无用的relay-log
relay-log-recovery = 1 # 因异常导致relay-log错误,是否删除并重新从master获取。需配合relay-log-purge=0
relay-log = relay-log
relay-log-index = relay-log.index
relay-log-info-file = relay-log.info
max-relay-log_size = 1G
log-slave-updates = 1 #启用从库日志,这样可以设置链式复制,否则从master中同步binlog的修改不会写入从库的bin-log
启用查看slave状态
slave start;
show slave status G;
如果Slave_IO_Running和Slave_SQL_Running都是Yes,表示成功了。
首次同步方式
对主实例进行锁表操作:FLUSH TABLES WITH READ LOCK;
导出主实例数据库备份:mysqldump --host xxxx --port xxx -u user -p password --databases dbname > db_dbname.dump
清除主实例binlog,重置master:reset master;
解锁主库:unlock tables;
将数据导入从实例:mysql --host xxxx --port xxx -u user -p password < db_dbname.dump
启动从实例slave:slave start;
发布地址:www.cnblogs.com/qiusuo/p/9643174.html