Mysql主从环境部署

Master数据库的My.Cnf文件

server-id=1
#数据库唯一ID,主从的标识号绝对不能重复
log-bin=mysql-bin
#开启bin-log,并指定文件目录和文件名前缀
log-bin-index=master-bin.index
#二进制日志索引文件
binlog-do-db=julia
#需要同步的数据库,去掉此行,表示同步所有库
binlog-ignore-db=mysql
#不同步mysql系统数据库
innodb_file_per_table=ON
#独立表空间
skip_name_resolve=ON
#禁止域名解析

sync_binlog = 1
#确保binlog日志写入后与硬盘同步
binlog_format = mixed
#bin-log日志文件格式,设置为MIXED可以防止主键重复
binlog_checksum = none
#跳过现有的采用checksum的事件,mysql5.6.5以后的版本中binlog_checksum=crc32,而低版本都是binlog_checksum=none

Master 主库备份思路:

锁表——>备份——>解锁

1,锁表

mysql> flush tables with read lock;
#数据库只读锁定命令,防止导出数据库的时候有数据写入
2,备份
# mysqldump -uroot -p123456 –database julia >/tmp/julia.sql
将导出的sql文件上传到slave机器上
查看主服务器master状态  (注意File与Position项,从服务器需要这两项参数)
mysql> show master status;
+——————+———-+————–+——————+——————-+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+——————+———-+————–+——————+——————-+
| mysql-bin.000007 |      120    |      julia         | mysql            |                   |
+——————+———-+————–+——————+——————-+
1 row in set (0.00 sec)
3,解锁(也可在操作完从库以后再开锁)

mysql> unlock tables;

设置数据同步权限
mysql> grant replication slave,replication client on *.* to repluser@’slave_ip’ identified by ‘password’;
刷新权限
mysql> flush privileges;
权限查看方式
mysql> show grants;

 

温馨提示:

在主服务器上最重要的二进制日志设置是sync_binlog,这使得mysql在每次提交事务的时候把二进制日志的内容同步到磁盘上,即使服务器崩溃也会把事件写入日志中。
sync_binlog这个参数是对于MySQL系统来说是至关重要的,他不仅影响到Binlog对MySQL所带来的性能损耗,而且还影响到MySQL中数据的完整性。对于”sync_binlog”

参数的各种设置的说明如下:

sync_binlog=0,当事务提交之后,MySQL不做fsync之类的磁盘同步指令刷新binlog_cache中的信息到磁盘,而让Filesystem自行决定什么时候来做同步,或者cache满了之后才同步到磁盘。
sync_binlog=n,当每进行n次事务提交之后,MySQL将进行一次fsync之类的磁盘同步指令来将binlog_cache中的数据强制写入磁盘。
在MySQL中系统默认的设置是sync_binlog=0,也就是不做任何强制性的磁盘刷新指令,这时候的性能是最好的,但是风险也是最大的。因为一旦系统Crash,在binlog_cache中的所有binlog信息都会被丢失。而当设置为“1”的时候,是最安全但是性能损耗最大的设置。因为当设置为1的时候,即使系统Crash,也最多丢失binlog_cache中未完成的一个事务,对实际数据没有任何实质性影响。
“sync_binlog”设置为0和设置为1的系统写入性能差距可能高达5倍甚至更多。

Slave数据库的My.Cnf文件

server-id=2
#设置从服务器id,必须于主服务器不同
relay_log=relay_log
relay-log-index=slave-relay-bin.index
log-bin=mysql-bin
#开启bin-log,并指定文件目录和文件名前缀
log_slave_updates = 1
#允许备库将其重放的事件也记录到自身的二进制日志
innodb_file_per_table=ON
replicate-do-db=julia
#需要同步的数据库名,就去掉这行,表示所有库的同步
replicate-ignore-db=mysql
#不同步mysql系统数据库
slave-skip-errors = all
#跳过所有的错误错误,继续执行复制操作
read_only=ON
#限制从服务器为只读 (super权限无效)
skip_slave_start=ON
#手动启动 slave 复制线程

sync_relay_log = 1
sync_relay_log_inf = 1
relay-log-info-repository = TABLE (mysql 5.6.2之后版本支持,之前为file)
sync_master_info=1  (增加IO)
master-info-repository  = TABLE (mysql 5.6.2之后版本支持,之前为file)

mysql> change  master to master_host=’master_ip’,
master_user=’repluser’,
master_password=’password’,
master_log_file=’mysql-bin.000007′,
master_log_pos=120;
mysql> start slave;
mysql> show slave status \G;
如上,当IO和SQL线程的状态均为Yes,则表示主从已实现同步了!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值