Mysql 主从复制 (一)

一、主服务器 配置 my.ini 或 my.conf  作如下修改:

# Binary Logging. ===========================开始===========================================
log-bin=mysql-bin-1
binlog-format=Mixed

# Error Logging.
log-error="LAPTOP-LPN2JC8I.err"

# Server Id.
server-id=1

#当sync_binlog =N (N>0) ,MySQL 在每写 N次 二进制日志binary log时,会使用fdatasync()函数将它的写二进制日志binary log同步到磁盘中去。sync_binlog 的默认值是0,像操作系统刷其他文件的机制一样,MySQL不会同步到磁盘中去而是依赖操作系统来刷新binary log。
sync_binlog=1
 #同步数据库
binlog-do-db=db1
binlog-do-db=db2

#mysql复制模式,三种:SBR(基于sql语句复制),RBR(基于行的复制),MBR(混合模式复制)
#混合模式复制
binlog_format=MIXED
 #binlog过期清理时间
expire_logs_days=7
 #binlog每个日志文件大小
max_binlog_size=20M
 #===============================结束===========================================

二、从服务器: 配置 my.ini 或 my.conf  作如下修改

# Binary Logging. ===============================开始===========================================
log-bin=mysql-bin-1
binlog-format=Mixed

# Error Logging.
log-error="LAPTOP-LPN2JC8I.err"

# Server Id.
server-id=2

#当sync_binlog =N (N>0) ,MySQL 在每写 N次 二进制日志binary log时,会使用fdatasync()函数将它的写二进制日志binary log同步到磁盘中去。sync_binlog 的默认值是0,像操作系统刷其他文件的机制一样,MySQL不会同步到磁盘中去而是依赖操作系统来刷新binary log。
sync_binlog=1
 #同步数据库名
binlog-do-db=db1
binlog-do-db=db2

#mysql复制模式,三种:SBR(基于sql语句复制),RBR(基于行的复制),MBR(混合模式复制)
#混合模式复制
binlog_format=MIXED
 #binlog过期清理时间
expire_logs_days=7
 #binlog每个日志文件大小
max_binlog_size=20M
 #======================结束===========================================

 三、master 主服务器 操作:打开命令行:

grant replication slave on *.* to 'username'@'%' identified by 'password';   // % 可以替换成对应从服务器的ip (授权给对应ip 的从服务器的登录用户)

show master status;  // 查看 主服务器的状态

 四、slave 从服务器的 操作: 打开命令行界面:

change master to

master_host='master的ip',

master_port=3306,

master_user='username',

master_password='password',

master_log_file='mysql-bin-1.000001', // master 上的binlog 名称

 master_log_pos=154;// 对应master 上的pos

 start slave;

Query OK, 0 rows affected (0.04 sec)

 show slave status;

五、修改主服务器的db1,或 db2 中的数据就会同步 到 从服务器的数据库

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章